1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace 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 | public ValueLookupParameter<DataTable> SymbolicExpressionTreeLengthsParameter {
|
---|
54 | get { return (ValueLookupParameter<DataTable>)Parameters[SymbolicExpressionTreeLengthsParameterName]; }
|
---|
55 | }
|
---|
56 | public ValueLookupParameter<DataTableHistory> SymbolicExpressionTreeLengthsHistoryParameter {
|
---|
57 | get { return (ValueLookupParameter<DataTableHistory>)Parameters[SymbolicExpressionTreeLengthsHistoryParameterName]; }
|
---|
58 | }
|
---|
59 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
60 | get { return (ValueLookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
61 | }
|
---|
62 | // history
|
---|
63 | public ValueParameter<BoolValue> StoreHistoryParameter {
|
---|
64 | get { return (ValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
|
---|
65 | }
|
---|
66 | public ValueParameter<IntValue> UpdateIntervalParameter {
|
---|
67 | get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
|
---|
68 | }
|
---|
69 | public ValueParameter<IntValue> UpdateCounterParameter {
|
---|
70 | get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
|
---|
71 | }
|
---|
72 | #endregion
|
---|
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(deserializing) { }
|
---|
90 | private SymbolicExpressionTreeLengthAnalyzer(SymbolicExpressionTreeLengthAnalyzer original, Cloner cloner)
|
---|
91 | : base(original, cloner) {
|
---|
92 | }
|
---|
93 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
94 | return new SymbolicExpressionTreeLengthAnalyzer(this, cloner);
|
---|
95 | }
|
---|
96 | public SymbolicExpressionTreeLengthAnalyzer()
|
---|
97 | : base() {
|
---|
98 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree whose length should be calculated."));
|
---|
99 | Parameters.Add(new LookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximum allowed symbolic expression tree length"));
|
---|
100 | Parameters.Add(new ValueLookupParameter<DataTable>(SymbolicExpressionTreeLengthsParameterName, "The data table to store the symbolic expression tree lengths."));
|
---|
101 | Parameters.Add(new ValueLookupParameter<DataTableHistory>(SymbolicExpressionTreeLengthsHistoryParameterName, "The data table to store the symbolic expression tree lengths history."));
|
---|
102 | Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
103 | Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false)));
|
---|
104 | Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
|
---|
105 | Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
|
---|
106 |
|
---|
107 | SymbolicExpressionTreeLengthsParameter.Hidden = true;
|
---|
108 | SymbolicExpressionTreeLengthsHistoryParameter.Hidden = true;
|
---|
109 | ResultsParameter.Hidden = true;
|
---|
110 | UpdateCounterParameter.Hidden = true;
|
---|
111 | }
|
---|
112 |
|
---|
113 | [StorableHook(HookType.AfterDeserialization)]
|
---|
114 | private void AfterDeserialization() {
|
---|
115 | // check if all the parameters are present and accounted for
|
---|
116 | if (!Parameters.ContainsKey(StoreHistoryParameterName)) {
|
---|
117 | Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false)));
|
---|
118 | }
|
---|
119 | if (!Parameters.ContainsKey(UpdateIntervalParameterName)) {
|
---|
120 | Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
|
---|
121 | }
|
---|
122 | //necessary code to correct UpdateCounterParameter - type was changed from LookupParameter to ValueParameter
|
---|
123 | if (Parameters.ContainsKey(UpdateCounterParameterName) && (Parameters[UpdateCounterParameterName] is LookupParameter<IntValue>))
|
---|
124 | Parameters.Remove(UpdateCounterParameterName);
|
---|
125 | if (!Parameters.ContainsKey(UpdateCounterParameterName)) {
|
---|
126 | Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
|
---|
127 | UpdateCounterParameter.Hidden = true;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | #region IStatefulItem members
|
---|
132 | public override void InitializeState() {
|
---|
133 | base.InitializeState();
|
---|
134 | UpdateCounter.Value = 0;
|
---|
135 | }
|
---|
136 | public override void ClearState() {
|
---|
137 | base.ClearState();
|
---|
138 | UpdateCounter.Value = 0;
|
---|
139 | }
|
---|
140 | #endregion
|
---|
141 |
|
---|
142 | public override IOperation Apply() {
|
---|
143 | UpdateCounter.Value++;
|
---|
144 | // the analyzer runs periodically, every 'updateInterval' times
|
---|
145 | if (UpdateCounter.Value == UpdateInterval.Value) {
|
---|
146 | UpdateCounter.Value = 0; // reset counter
|
---|
147 |
|
---|
148 | // compute all tree lengths and store them in the lengthsTable
|
---|
149 | var solutions = SymbolicExpressionTreeParameter.ActualValue;
|
---|
150 |
|
---|
151 | var treeLengthsTable = SymbolicExpressionTreeLengthsParameter.ActualValue;
|
---|
152 | // if the table was not created yet, we create it here
|
---|
153 | if (treeLengthsTable == null) {
|
---|
154 | treeLengthsTable = new DataTable("Tree Length Histogram");
|
---|
155 | SymbolicExpressionTreeLengthsParameter.ActualValue = treeLengthsTable;
|
---|
156 | }
|
---|
157 |
|
---|
158 | // data table which stores tree length values
|
---|
159 | DataRow treeLengthsTableRow;
|
---|
160 |
|
---|
161 | const string treeLengthsTableRowName = "Symbolic expression tree lengths";
|
---|
162 | const string treeLengthsTableRowDesc = "The distribution of symbolic expression tree lengths";
|
---|
163 | const string xAxisTitle = "Symbolic expression tree lengths";
|
---|
164 | const string yAxisTitle = "Frequency / Number of tree individuals";
|
---|
165 |
|
---|
166 | var treeLengths = solutions.Select(s => (int)s.Length).ToList();
|
---|
167 |
|
---|
168 | int maxLength = treeLengths.Max(t => t);
|
---|
169 | int minLength = treeLengths.Min(t => t);
|
---|
170 |
|
---|
171 | if (!treeLengthsTable.Rows.ContainsKey(treeLengthsTableRowName)) {
|
---|
172 | treeLengthsTableRow = new DataRow(treeLengthsTableRowName, treeLengthsTableRowDesc, treeLengths.Select(x => (double)x));
|
---|
173 | treeLengthsTable.Rows.Add(treeLengthsTableRow);
|
---|
174 | } else {
|
---|
175 | treeLengthsTableRow = treeLengthsTable.Rows[treeLengthsTableRowName];
|
---|
176 | treeLengthsTableRow.Values.Replace(treeLengths.Select(x => (double)x));
|
---|
177 | }
|
---|
178 |
|
---|
179 | double maximumAllowedTreeLength = ((LookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]).ActualValue.Value;
|
---|
180 |
|
---|
181 | treeLengthsTableRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
|
---|
182 | treeLengthsTableRow.VisualProperties.ExactBins = false;
|
---|
183 |
|
---|
184 | int range = maxLength - minLength;
|
---|
185 | if (range == 0) range = 1;
|
---|
186 | // the following trick should result in an integer intervalWidth of 1,2,4,...
|
---|
187 | treeLengthsTableRow.VisualProperties.Bins = range;
|
---|
188 |
|
---|
189 | if (maxLength <= 25) // [0,25]
|
---|
190 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0;
|
---|
191 | else if (maxLength <= 100) // [26,100]
|
---|
192 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 2.0;
|
---|
193 | else if (maxLength <= 250) // [101,250]
|
---|
194 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 5.0;
|
---|
195 | else if (maxLength <= 500) // [251,500]
|
---|
196 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 10.0;
|
---|
197 | else
|
---|
198 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 20.0; // [501,inf]
|
---|
199 |
|
---|
200 | treeLengthsTableRow.VisualProperties.IsVisibleInLegend = false;
|
---|
201 |
|
---|
202 | // visual properties for the X-axis
|
---|
203 | treeLengthsTable.VisualProperties.XAxisMinimumAuto = false;
|
---|
204 | treeLengthsTable.VisualProperties.XAxisMaximumAuto = false;
|
---|
205 | treeLengthsTable.VisualProperties.XAxisMinimumFixedValue = 0.0;
|
---|
206 | if (maxLength > maximumAllowedTreeLength + 1)
|
---|
207 | treeLengthsTable.VisualProperties.XAxisMaximumFixedValue = maxLength + 1; // +1 so the histogram column for the maximum length won't get trimmed
|
---|
208 | else
|
---|
209 | treeLengthsTable.VisualProperties.XAxisMaximumFixedValue = maximumAllowedTreeLength + 1;
|
---|
210 | treeLengthsTable.VisualProperties.XAxisTitle = xAxisTitle;
|
---|
211 | //visual properties for the Y-axis
|
---|
212 | treeLengthsTable.VisualProperties.YAxisMinimumAuto = false;
|
---|
213 | treeLengthsTable.VisualProperties.YAxisMaximumAuto = false;
|
---|
214 | treeLengthsTable.VisualProperties.YAxisMinimumFixedValue = 0.0;
|
---|
215 | int maxFreq = (int)Math.Round(solutions.GroupBy(s => s.Length).Max(g => g.Count()) / treeLengthsTableRow.VisualProperties.ScaleFactor);
|
---|
216 | if (maxFreq % 5 != 0)
|
---|
217 | maxFreq += (5 - maxFreq % 5);
|
---|
218 | double yAxisMaximumFixedValue = maxFreq;
|
---|
219 |
|
---|
220 | treeLengthsTable.VisualProperties.YAxisMaximumFixedValue = yAxisMaximumFixedValue;
|
---|
221 | treeLengthsTable.VisualProperties.YAxisTitle = yAxisTitle;
|
---|
222 |
|
---|
223 | var results = ResultsParameter.ActualValue;
|
---|
224 |
|
---|
225 | if (!results.ContainsKey(treeLengthsTableRowName)) {
|
---|
226 | results.Add(new Result(treeLengthsTableRowName, treeLengthsTable));
|
---|
227 | } else {
|
---|
228 | results[treeLengthsTableRowName].Value = treeLengthsTable;
|
---|
229 | }
|
---|
230 |
|
---|
231 | bool storeHistory = StoreHistoryParameter.Value.Value;
|
---|
232 | const string treeLengthHistoryTableName = "Tree lengths history";
|
---|
233 |
|
---|
234 | if (storeHistory) {
|
---|
235 | var treeLengthsHistory = SymbolicExpressionTreeLengthsHistoryParameter.ActualValue;
|
---|
236 | if (treeLengthsHistory == null) {
|
---|
237 | treeLengthsHistory = new DataTableHistory();
|
---|
238 | SymbolicExpressionTreeLengthsHistoryParameter.ActualValue = treeLengthsHistory;
|
---|
239 | }
|
---|
240 | treeLengthsHistory.Add((DataTable)treeLengthsTable.Clone());
|
---|
241 |
|
---|
242 | if (!results.ContainsKey(treeLengthHistoryTableName)) {
|
---|
243 | results.Add(new Result(treeLengthHistoryTableName, treeLengthsHistory));
|
---|
244 | } else {
|
---|
245 | results[treeLengthHistoryTableName].Value = treeLengthsHistory;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 | return base.Apply();
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|