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 |
|
---|
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, IStatefulItem {
|
---|
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 |
|
---|
74 | #region Properties
|
---|
75 | public bool EnabledByDefault {
|
---|
76 | get { return true; }
|
---|
77 | }
|
---|
78 | public IntValue UpdateInterval {
|
---|
79 | get { return UpdateIntervalParameter.Value; }
|
---|
80 | }
|
---|
81 | public IntValue UpdateCounter {
|
---|
82 | get { return UpdateCounterParameter.Value; }
|
---|
83 | }
|
---|
84 | public BoolValue StoreHistory {
|
---|
85 | get { return StoreHistoryParameter.Value; }
|
---|
86 | }
|
---|
87 | #endregion
|
---|
88 |
|
---|
89 | [StorableConstructor]
|
---|
90 | private SymbolicExpressionTreeLengthAnalyzer(bool deserializing) : base() { }
|
---|
91 | private SymbolicExpressionTreeLengthAnalyzer(SymbolicExpressionTreeLengthAnalyzer original, Cloner cloner)
|
---|
92 | : base(original, cloner) {
|
---|
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 | SymbolicExpressionTreeLengthsParameter.Hidden = true;
|
---|
109 | SymbolicExpressionTreeLengthsHistoryParameter.Hidden = true;
|
---|
110 | ResultsParameter.Hidden = true;
|
---|
111 | UpdateCounterParameter.Hidden = true;
|
---|
112 | }
|
---|
113 |
|
---|
114 | [StorableHook(HookType.AfterDeserialization)]
|
---|
115 | private void AfterDeserialization() {
|
---|
116 | // check if all the parameters are present and accounted for
|
---|
117 | if (!Parameters.ContainsKey(StoreHistoryParameterName)) {
|
---|
118 | Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false)));
|
---|
119 | }
|
---|
120 | if (!Parameters.ContainsKey(UpdateIntervalParameterName)) {
|
---|
121 | Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
|
---|
122 | }
|
---|
123 | if (!Parameters.ContainsKey(UpdateCounterParameterName)) {
|
---|
124 | Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
|
---|
125 | UpdateCounterParameter.Hidden = true;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | #region IStatefulItem members
|
---|
130 | public void InitializeState() {
|
---|
131 | UpdateCounter.Value = 0;
|
---|
132 | }
|
---|
133 | public void ClearState() {
|
---|
134 | UpdateCounter.Value = 0;
|
---|
135 | }
|
---|
136 | #endregion
|
---|
137 |
|
---|
138 | public override IOperation Apply() {
|
---|
139 | UpdateCounter.Value++;
|
---|
140 | // the analyzer runs periodically, every 'updateInterval' times
|
---|
141 | if (UpdateCounter.Value == UpdateInterval.Value) {
|
---|
142 | UpdateCounter.Value = 0; // reset counter
|
---|
143 |
|
---|
144 | // compute all tree lengths and store them in the lengthsTable
|
---|
145 | var solutions = SymbolicExpressionTreeParameter.ActualValue;
|
---|
146 |
|
---|
147 | var treeLengthsTable = SymbolicExpressionTreeLengthsParameter.ActualValue;
|
---|
148 | // if the table was not created yet, we create it here
|
---|
149 | if (treeLengthsTable == null) {
|
---|
150 | treeLengthsTable = new DataTable("Histogram");
|
---|
151 | SymbolicExpressionTreeLengthsParameter.ActualValue = treeLengthsTable;
|
---|
152 | }
|
---|
153 |
|
---|
154 | // data table which stores tree length values
|
---|
155 | DataRow treeLengthsTableRow;
|
---|
156 |
|
---|
157 | const string treeLengthsTableRowName = "Symbolic expression tree lengths";
|
---|
158 | const string treeLengthsTableRowDesc = "The distribution of symbolic expression tree lengths";
|
---|
159 | const string xAxisTitle = "Symbolic expression tree lengths";
|
---|
160 | const string yAxisTitle = "Frequency / Number of tree individuals";
|
---|
161 |
|
---|
162 | var treeLengths = solutions.Select(s => (int)s.Length).ToList();
|
---|
163 |
|
---|
164 | int maxLength = treeLengths.Max(t => t);
|
---|
165 | int minLength = treeLengths.Min(t => t);
|
---|
166 |
|
---|
167 | if (!treeLengthsTable.Rows.ContainsKey(treeLengthsTableRowName)) {
|
---|
168 | treeLengthsTableRow = new DataRow(treeLengthsTableRowName, treeLengthsTableRowDesc, treeLengths.Select(x => (double)x));
|
---|
169 | treeLengthsTable.Rows.Add(treeLengthsTableRow);
|
---|
170 | } else {
|
---|
171 | treeLengthsTableRow = treeLengthsTable.Rows[treeLengthsTableRowName];
|
---|
172 | treeLengthsTableRow.Values.Replace(treeLengths.Select(x => (double)x));
|
---|
173 | }
|
---|
174 |
|
---|
175 | double maximumAllowedTreeLength = ((LookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]).ActualValue.Value;
|
---|
176 |
|
---|
177 | treeLengthsTableRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
|
---|
178 | treeLengthsTableRow.VisualProperties.ExactBins = false;
|
---|
179 |
|
---|
180 | int range = maxLength - minLength;
|
---|
181 | if (range == 0) range = 1;
|
---|
182 | // the following trick should result in an integer intervalWidth of 1,2,4,...
|
---|
183 | treeLengthsTableRow.VisualProperties.Bins = range;
|
---|
184 |
|
---|
185 | if (maxLength <= 25) // [0,25]
|
---|
186 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0;
|
---|
187 | else if (maxLength <= 100) // [26,100]
|
---|
188 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 2.0;
|
---|
189 | else if (maxLength <= 250) // [101,250]
|
---|
190 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 5.0;
|
---|
191 | else if (maxLength <= 500) // [251,500]
|
---|
192 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 10.0;
|
---|
193 | else
|
---|
194 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 20.0; // [501,inf]
|
---|
195 |
|
---|
196 | treeLengthsTableRow.VisualProperties.IsVisibleInLegend = false;
|
---|
197 |
|
---|
198 | // visual properties for the X-axis
|
---|
199 | treeLengthsTable.VisualProperties.XAxisMinimumAuto = false;
|
---|
200 | treeLengthsTable.VisualProperties.XAxisMaximumAuto = false;
|
---|
201 | treeLengthsTable.VisualProperties.XAxisMinimumFixedValue = 0.0;
|
---|
202 | if (maxLength > maximumAllowedTreeLength + 1)
|
---|
203 | treeLengthsTable.VisualProperties.XAxisMaximumFixedValue = maxLength + 1; // +1 so the histogram column for the maximum length won't get trimmed
|
---|
204 | else
|
---|
205 | treeLengthsTable.VisualProperties.XAxisMaximumFixedValue = maximumAllowedTreeLength + 1;
|
---|
206 | treeLengthsTable.VisualProperties.XAxisTitle = xAxisTitle;
|
---|
207 | //visual properties for the Y-axis
|
---|
208 | treeLengthsTable.VisualProperties.YAxisMinimumAuto = false;
|
---|
209 | treeLengthsTable.VisualProperties.YAxisMaximumAuto = false;
|
---|
210 | treeLengthsTable.VisualProperties.YAxisMinimumFixedValue = 0.0;
|
---|
211 | int maxFreq = (int)Math.Round(solutions.GroupBy(s => s.Length).Max(g => g.Count()) / treeLengthsTableRow.VisualProperties.ScaleFactor);
|
---|
212 | if (maxFreq % 5 != 0)
|
---|
213 | maxFreq += (5 - maxFreq % 5);
|
---|
214 | double yAxisMaximumFixedValue = maxFreq;
|
---|
215 |
|
---|
216 | treeLengthsTable.VisualProperties.YAxisMaximumFixedValue = yAxisMaximumFixedValue;
|
---|
217 | treeLengthsTable.VisualProperties.YAxisTitle = yAxisTitle;
|
---|
218 |
|
---|
219 | var results = ResultsParameter.ActualValue;
|
---|
220 |
|
---|
221 | if (!results.ContainsKey(treeLengthsTableRowName)) {
|
---|
222 | results.Add(new Result(treeLengthsTableRowName, treeLengthsTable));
|
---|
223 | } else {
|
---|
224 | results[treeLengthsTableRowName].Value = treeLengthsTable;
|
---|
225 | }
|
---|
226 |
|
---|
227 | bool storeHistory = StoreHistoryParameter.Value.Value;
|
---|
228 | const string treeLengthHistoryTableName = "Tree lengths history";
|
---|
229 |
|
---|
230 | if (storeHistory) {
|
---|
231 | var treeLengthsHistory = SymbolicExpressionTreeLengthsHistoryParameter.ActualValue;
|
---|
232 | if (treeLengthsHistory == null) {
|
---|
233 | treeLengthsHistory = new DataTableHistory();
|
---|
234 | SymbolicExpressionTreeLengthsHistoryParameter.ActualValue = treeLengthsHistory;
|
---|
235 | }
|
---|
236 | treeLengthsHistory.Add((DataTable)treeLengthsTable.Clone());
|
---|
237 |
|
---|
238 | if (!results.ContainsKey(treeLengthHistoryTableName)) {
|
---|
239 | results.Add(new Result(treeLengthHistoryTableName, treeLengthsHistory));
|
---|
240 | } else {
|
---|
241 | results[treeLengthHistoryTableName].Value = treeLengthsHistory;
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 | return base.Apply();
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|