Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionTreeLengthAnalyzer.cs @ 12495

Last change on this file since 12495 was 12495, checked in by gkronber, 9 years ago

#2261: merged trunk changes to branch
r12494
#2403: added a null check in the MatlabParameterVectorEvaluator to prevent exceptions when clearstate is called


r12493
#2369: added support for squared errors and relative errors to error-characteristic-curve view


r12492
#2392: implemented PearsonsRCalculator to fix incorrect correlation values in the correlation matrix.


r12491
#2402 don't set task state to waiting when it fails


r12490
#2401 added missing Mono.Cecil plugin dependency


r12488
#2400 - Interfaces for Capaciated-, PickupAndDelivery- and TimeWindowed-ProblemInstances now specify an additional penalty parameter to set the current penalty factor for the constraint relaxation. - The setter of the penalty-property in ...


r12485
#2374 made RegressionSolution and ClassificationSolution non-abstract


r12482
#2320: Fixed warnings in unit test solutions introduced in r12420 by marking methods as obsolete.


r12481
#2320: Fixed AfterDeserialization of GEArtifialAntEvaluator.


r12480
#2320: Fixed error in symbolicexpressiontree crossover regarding the wiring of lookup parameters if persisted file is loaded.


r12479
#2397 moved GeoIP project into ExtLibs


r12478
#2329 fixed bug in simple code editor


r12476
#2331 removed outdated plugins


r12475
#2368 fixed compile warnings


r12474
#2399 worked on Mono project prepare script


r12473
#2329 added a simple code editor for Linux


r12472
#2399 - fixed MathJax.js file name - worked on Mono project prepare script


r12471
#2399 worked on Mono project prepare script


r12470
#2399 fixed pre-build events in project files


r12465
#2399 worked on mono project prepare script


r12464
#2399 added patch to project


r12463
#2399 fixed EPPlus so that it compiles on Linux


r12461
#2398: Skip root and start symbols when calculating impacts and replacement values in the pruning operators.


r12458
#2354 show label when no data is displayed and don't show the legend


r12457
#2353 removed duplicated call to Any() in Hive Status page


r12456
#2368 fixed modifier


r12455
#2368 added support in persistence for typecaches in streams


r12445
#2394: Changed Web.config compilation from debug to release to force script bundling. Changed history loading type from lazy to eager loading to increase performance. Fixed "getCoreStatus" typo in statusCtrl.js


r12443
#2394: Fixed UserTaskQuery and GetStatusHistory in the WebApp.Status plugin


r12442
#2394 added nuget folders to svn ignore list


r12435
#2394: Improved PluginManager and updated hive status monitor.


r12434
#2396 added symbolic expression tree formatter for C#


r12433
#2395: Minor change in DoubleValue.GetValue.


r12432
#2395 Use simple round-trip format for doubles because G17 prints some strange numbers (20.22 to 20.219999999999999999). Some accuracy can still be lost on 64bit machines, but should be very rare and minimal. double.MaxValue can still be pa...


r12431
#2395 Fixed parsing issues by using the G17 format.


r12430
#2394 added missing package config


r12429
#2394 added missing package config


r12428
#2394 added web app and status page to trunk


r12424
#2320: Adapted plugin file and updated project file of SymbolicExpressionTreeEncoding.


r12422
#2320: Merged the encoding class and all accompanying changes in the trunk.


r12401
#2387 Fixed a bug where the automatic selection of the first element behaved differently for the NewItemDialog.


r12400
#2387 Forgot to commit a file.


r12399
#2387 - Added context-menu for expanding and collapsing tree-nodes. - Improve response time when expanding/collapsing all nodes for TypeSelector and NewItemDialog.


r12398
#2387 - Added clearSearch-button in TypeSelector. - Adapted behavior of TypeSelector and NewItemDialog that a selected node stays selected as long as it matches the search criteria.


r12397
#2387 - Adapted behavior of the matching in the TypeSelector that it behave the same as the NewItemDialog. The search string is tokenized by space and matches if all tokens are contained, (eg. "Sym Reg" matches "SymbolicRegression...")...


r12393
#2025 - Removed Expand/CollapseAll buttons. - Removed cycling of items.


r12392
#2386: Updated GetHashCode method in the EnumerableBoolEqualityComparer.


File size: 13.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
34  /// <summary>
35  /// An operator that tracks tree lengths of Symbolic Expression Trees
36  /// </summary>
37  [Item("SymbolicExpressionTreeLengthAnalyzer", "An operator that tracks tree lengths of Symbolic Expression Trees")]
38  [StorableClass]
39  public sealed class SymbolicExpressionTreeLengthAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer {
40    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
41    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
42    private const string SymbolicExpressionTreeLengthsParameterName = "SymbolicExpressionTreeLengthsTable";
43    private const string SymbolicExpressionTreeLengthsHistoryParameterName = "SymbolicExpressionTreeLengthsHistoryTable";
44    private const string ResultsParameterName = "Results";
45    private const string StoreHistoryParameterName = "StoreHistory";
46    private const string UpdateIntervalParameterName = "UpdateInterval";
47    private const string UpdateCounterParameterName = "UpdateCounter";
48
49    #region Parameter properties
50    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
51      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
52    }
53
54    public ILookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
55      get { return (ILookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
56    }
57    public ValueLookupParameter<DataTable> SymbolicExpressionTreeLengthsParameter {
58      get { return (ValueLookupParameter<DataTable>)Parameters[SymbolicExpressionTreeLengthsParameterName]; }
59    }
60    public ValueLookupParameter<DataTableHistory> SymbolicExpressionTreeLengthsHistoryParameter {
61      get { return (ValueLookupParameter<DataTableHistory>)Parameters[SymbolicExpressionTreeLengthsHistoryParameterName]; }
62    }
63    public ValueLookupParameter<ResultCollection> ResultsParameter {
64      get { return (ValueLookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
65    }
66    // history
67    public ValueParameter<BoolValue> StoreHistoryParameter {
68      get { return (ValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
69    }
70    public ValueParameter<IntValue> UpdateIntervalParameter {
71      get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
72    }
73    public ValueParameter<IntValue> UpdateCounterParameter {
74      get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
75    }
76    #endregion
77
78    #region Properties
79    public bool EnabledByDefault {
80      get { return true; }
81    }
82    public IntValue UpdateInterval {
83      get { return UpdateIntervalParameter.Value; }
84    }
85    public IntValue UpdateCounter {
86      get { return UpdateCounterParameter.Value; }
87    }
88    public BoolValue StoreHistory {
89      get { return StoreHistoryParameter.Value; }
90    }
91    #endregion
92
93    [StorableConstructor]
94    private SymbolicExpressionTreeLengthAnalyzer(bool deserializing) : base(deserializing) { }
95    private SymbolicExpressionTreeLengthAnalyzer(SymbolicExpressionTreeLengthAnalyzer original, Cloner cloner)
96      : base(original, cloner) {
97    }
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new SymbolicExpressionTreeLengthAnalyzer(this, cloner);
100    }
101    public SymbolicExpressionTreeLengthAnalyzer()
102      : base() {
103      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree whose length should be calculated."));
104      Parameters.Add(new LookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximum allowed symbolic expression tree length"));
105      Parameters.Add(new ValueLookupParameter<DataTable>(SymbolicExpressionTreeLengthsParameterName, "The data table to store the symbolic expression tree lengths."));
106      Parameters.Add(new ValueLookupParameter<DataTableHistory>(SymbolicExpressionTreeLengthsHistoryParameterName, "The data table to store the symbolic expression tree lengths history."));
107      Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
108      Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false)));
109      Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
110      Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
111
112      SymbolicExpressionTreeLengthsParameter.Hidden = true;
113      SymbolicExpressionTreeLengthsHistoryParameter.Hidden = true;
114      ResultsParameter.Hidden = true;
115      UpdateCounterParameter.Hidden = true;
116    }
117
118    [StorableHook(HookType.AfterDeserialization)]
119    private void AfterDeserialization() {
120      // check if all the parameters are present and accounted for
121      if (!Parameters.ContainsKey(StoreHistoryParameterName)) {
122        Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false)));
123      }
124      if (!Parameters.ContainsKey(UpdateIntervalParameterName)) {
125        Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
126      }
127      //necessary code to correct UpdateCounterParameter - type was changed from LookupParameter to ValueParameter
128      if (Parameters.ContainsKey(UpdateCounterParameterName) && (Parameters[UpdateCounterParameterName] is LookupParameter<IntValue>))
129        Parameters.Remove(UpdateCounterParameterName);
130      if (!Parameters.ContainsKey(UpdateCounterParameterName)) {
131        Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
132        UpdateCounterParameter.Hidden = true;
133      }
134    }
135
136    #region IStatefulItem members
137    public override void InitializeState() {
138      base.InitializeState();
139      UpdateCounter.Value = 0;
140    }
141    public override void ClearState() {
142      base.ClearState();
143      UpdateCounter.Value = 0;
144    }
145    #endregion
146
147    public override IOperation Apply() {
148      UpdateCounter.Value++;
149      // the analyzer runs periodically, every 'updateInterval' times
150      if (UpdateCounter.Value == UpdateInterval.Value) {
151        UpdateCounter.Value = 0; // reset counter
152
153        // compute all tree lengths and store them in the lengthsTable
154        var solutions = SymbolicExpressionTreeParameter.ActualValue;
155
156        var treeLengthsTable = SymbolicExpressionTreeLengthsParameter.ActualValue;
157        // if the table was not created yet, we create it here
158        if (treeLengthsTable == null) {
159          treeLengthsTable = new DataTable("Tree Length Histogram");
160          SymbolicExpressionTreeLengthsParameter.ActualValue = treeLengthsTable;
161        }
162
163        // data table which stores tree length values
164        DataRow treeLengthsTableRow;
165
166        const string treeLengthsTableRowName = "Symbolic expression tree lengths";
167        const string treeLengthsTableRowDesc = "The distribution of symbolic expression tree lengths";
168        const string xAxisTitle = "Symbolic expression tree lengths";
169        const string yAxisTitle = "Frequency / Number of tree individuals";
170
171        var treeLengths = solutions.Select(s => (int)s.Length).ToList();
172
173        int maxLength = treeLengths.Max(t => t);
174        int minLength = treeLengths.Min(t => t);
175
176        if (!treeLengthsTable.Rows.ContainsKey(treeLengthsTableRowName)) {
177          treeLengthsTableRow = new DataRow(treeLengthsTableRowName, treeLengthsTableRowDesc, treeLengths.Select(x => (double)x));
178          treeLengthsTable.Rows.Add(treeLengthsTableRow);
179        } else {
180          treeLengthsTableRow = treeLengthsTable.Rows[treeLengthsTableRowName];
181          treeLengthsTableRow.Values.Replace(treeLengths.Select(x => (double)x));
182        }
183
184        double maximumAllowedTreeLength = MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value;
185
186        treeLengthsTableRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
187        treeLengthsTableRow.VisualProperties.ExactBins = false;
188
189        int range = maxLength - minLength;
190        if (range == 0) range = 1;
191        // the following trick should result in an integer intervalWidth of 1,2,4,...
192        treeLengthsTableRow.VisualProperties.Bins = range;
193
194        if (maxLength <= 25) // [0,25]
195          treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0;
196        else if (maxLength <= 100) // [26,100]
197          treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 2.0;
198        else if (maxLength <= 250) // [101,250]
199          treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 5.0;
200        else if (maxLength <= 500) // [251,500]
201          treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 10.0;
202        else
203          treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 20.0; // [501,inf]
204
205        treeLengthsTableRow.VisualProperties.IsVisibleInLegend = false;
206
207        // visual properties for the X-axis
208        treeLengthsTable.VisualProperties.XAxisMinimumAuto = false;
209        treeLengthsTable.VisualProperties.XAxisMaximumAuto = false;
210        treeLengthsTable.VisualProperties.XAxisMinimumFixedValue = 0.0;
211        if (maxLength > maximumAllowedTreeLength + 1)
212          treeLengthsTable.VisualProperties.XAxisMaximumFixedValue = maxLength + 1; // +1 so the histogram column for the maximum length won't get trimmed
213        else
214          treeLengthsTable.VisualProperties.XAxisMaximumFixedValue = maximumAllowedTreeLength + 1;
215        treeLengthsTable.VisualProperties.XAxisTitle = xAxisTitle;
216        //visual properties for the Y-axis
217        treeLengthsTable.VisualProperties.YAxisMinimumAuto = false;
218        treeLengthsTable.VisualProperties.YAxisMaximumAuto = false;
219        treeLengthsTable.VisualProperties.YAxisMinimumFixedValue = 0.0;
220        int maxFreq = (int)Math.Round(solutions.GroupBy(s => s.Length).Max(g => g.Count()) / treeLengthsTableRow.VisualProperties.ScaleFactor);
221        if (maxFreq % 5 != 0)
222          maxFreq += (5 - maxFreq % 5);
223        double yAxisMaximumFixedValue = maxFreq;
224
225        treeLengthsTable.VisualProperties.YAxisMaximumFixedValue = yAxisMaximumFixedValue;
226        treeLengthsTable.VisualProperties.YAxisTitle = yAxisTitle;
227
228        var results = ResultsParameter.ActualValue;
229
230        if (!results.ContainsKey(treeLengthsTableRowName)) {
231          results.Add(new Result(treeLengthsTableRowName, treeLengthsTable));
232        } else {
233          results[treeLengthsTableRowName].Value = treeLengthsTable;
234        }
235
236        bool storeHistory = StoreHistoryParameter.Value.Value;
237        const string treeLengthHistoryTableName = "Tree lengths history";
238
239        if (storeHistory) {
240          var treeLengthsHistory = SymbolicExpressionTreeLengthsHistoryParameter.ActualValue;
241          if (treeLengthsHistory == null) {
242            treeLengthsHistory = new DataTableHistory();
243            SymbolicExpressionTreeLengthsHistoryParameter.ActualValue = treeLengthsHistory;
244          }
245          treeLengthsHistory.Add((DataTable)treeLengthsTable.Clone());
246
247          if (!results.ContainsKey(treeLengthHistoryTableName)) {
248            results.Add(new Result(treeLengthHistoryTableName, treeLengthsHistory));
249          } else {
250            results[treeLengthHistoryTableName].Value = treeLengthsHistory;
251          }
252        }
253      }
254      return base.Apply();
255    }
256  }
257}
Note: See TracBrowser for help on using the repository browser.