Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RegressionBenchmarks/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionTreeLengthAnalyzer.cs @ 7255

Last change on this file since 7255 was 7255, checked in by sforsten, 12 years ago

#1708: merged r7209 from trunk

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