[6978] | 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 {
|
---|
| 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";
|
---|
[7143] | 48 | private const string GenerationCounterParameterName = "GenerationCounter";
|
---|
[6978] | 49 |
|
---|
| 50 | #region Parameter properties
|
---|
| 51 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 52 | get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
| 53 | }
|
---|
| 54 | public ValueLookupParameter<DataTable> SymbolicExpressionTreeLengthsParameter {
|
---|
[7124] | 55 | get { return (ValueLookupParameter<DataTable>)Parameters[SymbolicExpressionTreeLengthsParameterName]; }
|
---|
[6978] | 56 | }
|
---|
| 57 | public ValueLookupParameter<DataTableHistory> SymbolicExpressionTreeLengthsHistoryParameter {
|
---|
[7124] | 58 | get { return (ValueLookupParameter<DataTableHistory>)Parameters[SymbolicExpressionTreeLengthsHistoryParameterName]; }
|
---|
[6978] | 59 | }
|
---|
| 60 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 61 | get { return (ValueLookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
| 62 | }
|
---|
| 63 | // history
|
---|
| 64 | public ValueParameter<BoolValue> StoreHistoryParameter {
|
---|
| 65 | get { return (ValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
|
---|
| 66 | }
|
---|
| 67 | public ValueParameter<IntValue> UpdateIntervalParameter {
|
---|
| 68 | get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
|
---|
| 69 | }
|
---|
[7143] | 70 | public ValueParameter<IntValue> UpdateCounterParameter {
|
---|
| 71 | get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
|
---|
[6978] | 72 | }
|
---|
[7143] | 73 | public ValueParameter<IntValue> GenerationCounterParameter {
|
---|
| 74 | get { return (ValueParameter<IntValue>)Parameters[GenerationCounterParameterName]; }
|
---|
| 75 | }
|
---|
[6978] | 76 | #endregion
|
---|
[7124] | 77 |
|
---|
[7143] | 78 | #region Properties
|
---|
| 79 | public IntValue UpdateInterval {
|
---|
| 80 | get { return UpdateIntervalParameter.Value; }
|
---|
| 81 | }
|
---|
| 82 | public IntValue UpdateCounter {
|
---|
| 83 | get { return UpdateCounterParameter.Value; }
|
---|
| 84 | }
|
---|
| 85 | public BoolValue StoreHistory {
|
---|
| 86 | get { return StoreHistoryParameter.Value; }
|
---|
| 87 | }
|
---|
| 88 | public IntValue GenerationCounter {
|
---|
| 89 | get { return GenerationCounterParameter.Value; }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | #endregion
|
---|
| 93 |
|
---|
[6978] | 94 | [StorableConstructor]
|
---|
| 95 | private SymbolicExpressionTreeLengthAnalyzer(bool deserializing) : base() { }
|
---|
| 96 | private SymbolicExpressionTreeLengthAnalyzer(SymbolicExpressionTreeLengthAnalyzer original, Cloner cloner)
|
---|
| 97 | : base(original, cloner) {
|
---|
| 98 | AfterDeserialization();
|
---|
| 99 | }
|
---|
| 100 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 101 | return new SymbolicExpressionTreeLengthAnalyzer(this, cloner);
|
---|
| 102 | }
|
---|
| 103 | public SymbolicExpressionTreeLengthAnalyzer()
|
---|
| 104 | : base() {
|
---|
| 105 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree whose length should be calculated."));
|
---|
| 106 | Parameters.Add(new LookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximum allowed symbolic expression tree length"));
|
---|
| 107 | Parameters.Add(new ValueLookupParameter<DataTable>(SymbolicExpressionTreeLengthsParameterName, "The data table to store the symbolic expression tree lengths."));
|
---|
| 108 | Parameters.Add(new ValueLookupParameter<DataTableHistory>(SymbolicExpressionTreeLengthsHistoryParameterName, "The data table to store the symbolic expression tree lengths history."));
|
---|
| 109 | Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
| 110 | Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false)));
|
---|
| 111 | Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
|
---|
[7143] | 112 | Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
|
---|
| 113 | Parameters.Add(new ValueParameter<IntValue>(GenerationCounterParameterName, "The value of the total number of generations this operator has been applied.", new IntValue(0)));
|
---|
[6978] | 114 |
|
---|
| 115 | UpdateCounterParameter.Hidden = true;
|
---|
| 116 |
|
---|
| 117 | AfterDeserialization();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 121 | private void AfterDeserialization() {
|
---|
| 122 | // check if all the parameters are present and accounted for
|
---|
| 123 | if (!Parameters.ContainsKey(StoreHistoryParameterName)) {
|
---|
| 124 | Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false)));
|
---|
| 125 | }
|
---|
| 126 | if (!Parameters.ContainsKey(UpdateIntervalParameterName)) {
|
---|
| 127 | Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
|
---|
| 128 | }
|
---|
[7147] | 129 | if (Parameters.ContainsKey(UpdateCounterParameterName))
|
---|
| 130 | Parameters.Remove(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)));
|
---|
[7143] | 132 | if (!Parameters.ContainsKey(GenerationCounterParameterName)) {
|
---|
| 133 | Parameters.Add(new ValueParameter<IntValue>(GenerationCounterParameterName, "The value of the total number of generations this operator has been applied.", new IntValue(1)));
|
---|
| 134 | }
|
---|
[6978] | 135 | }
|
---|
| 136 |
|
---|
| 137 | public override IOperation Apply() {
|
---|
[7143] | 138 | //IntValue updateCounter = UpdateCounterParameter.Value;
|
---|
[6978] | 139 | // if the counter doesn't exist yet, we initialize it here with the current update interval
|
---|
[7143] | 140 | GenerationCounter.Value++;
|
---|
| 141 | UpdateCounter.Value++;
|
---|
[6978] | 142 |
|
---|
| 143 | // the analyzer runs periodically, every 'updateInterval' times
|
---|
[7143] | 144 | if (UpdateCounter.Value == UpdateInterval.Value) {
|
---|
| 145 | UpdateCounter.Value = 0; // reset counter
|
---|
[6978] | 146 |
|
---|
| 147 | // compute all tree lengths and store them in the lengthsTable
|
---|
| 148 | var solutions = SymbolicExpressionTreeParameter.ActualValue;
|
---|
| 149 |
|
---|
| 150 | var treeLengthsTable = SymbolicExpressionTreeLengthsParameter.ActualValue;
|
---|
| 151 | // if the table was not created yet, we create it here
|
---|
| 152 | if (treeLengthsTable == null) {
|
---|
[7124] | 153 | treeLengthsTable = new DataTable("Histogram");
|
---|
[6978] | 154 | SymbolicExpressionTreeLengthsParameter.ActualValue = treeLengthsTable;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | // data table which stores tree length values
|
---|
[7124] | 158 | DataRow treeLengthsTableRow;
|
---|
[6978] | 159 |
|
---|
[7124] | 160 | const string treeLengthsTableRowName = "Symbolic expression tree lengths";
|
---|
| 161 | const string treeLengthsTableRowDesc = "The distribution of symbolic expression tree lengths";
|
---|
| 162 | const string xAxisTitle = "Symbolic expression tree lengths";
|
---|
| 163 | const string yAxisTitle = "Frequency / Number of tree individuals";
|
---|
| 164 |
|
---|
[7147] | 165 | var treeLengths = solutions.Select(s => (int)s.Length).ToList();
|
---|
[6978] | 166 |
|
---|
[7147] | 167 | int maxLength = treeLengths.Max(t => t);
|
---|
| 168 | int minLength = treeLengths.Min(t => t);
|
---|
| 169 |
|
---|
[7124] | 170 | if (!treeLengthsTable.Rows.ContainsKey(treeLengthsTableRowName)) {
|
---|
[7147] | 171 | treeLengthsTableRow = new DataRow(treeLengthsTableRowName, treeLengthsTableRowDesc, treeLengths.Select(x => (double)x));
|
---|
[7124] | 172 | treeLengthsTable.Rows.Add(treeLengthsTableRow);
|
---|
[6978] | 173 | } else {
|
---|
[7124] | 174 | treeLengthsTableRow = treeLengthsTable.Rows[treeLengthsTableRowName];
|
---|
[7147] | 175 | treeLengthsTableRow.Values.Replace(treeLengths.Select(x => (double)x));
|
---|
[6978] | 176 | }
|
---|
| 177 |
|
---|
| 178 | double maximumAllowedTreeLength = ((LookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]).ActualValue.Value;
|
---|
| 179 |
|
---|
[7124] | 180 | treeLengthsTableRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
|
---|
| 181 | treeLengthsTableRow.VisualProperties.ExactBins = false;
|
---|
[6978] | 182 |
|
---|
[7147] | 183 | int range = maxLength - minLength;
|
---|
| 184 | if (range == 0) range = 1;
|
---|
[7124] | 185 | // the following trick should result in an integer intervalWidth of 1,2,4,...
|
---|
[7147] | 186 | treeLengthsTableRow.VisualProperties.Bins = range;
|
---|
[6978] | 187 |
|
---|
[7124] | 188 | if (maxLength <= 25) // [0,25]
|
---|
| 189 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0;
|
---|
[7147] | 190 | else if (maxLength <= 100) // [26,100]
|
---|
[7124] | 191 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 2.0;
|
---|
[7147] | 192 | else if (maxLength <= 250) // [101,250]
|
---|
[7124] | 193 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 5.0;
|
---|
[6978] | 194 | else if (maxLength <= 500) // [251,500]
|
---|
[7124] | 195 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 10.0;
|
---|
[6978] | 196 | else
|
---|
[7124] | 197 | treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 20.0; // [501,inf]
|
---|
[6978] | 198 |
|
---|
[7124] | 199 | treeLengthsTableRow.VisualProperties.IsVisibleInLegend = false;
|
---|
| 200 |
|
---|
[6978] | 201 | // visual properties for the X-axis
|
---|
| 202 | treeLengthsTable.VisualProperties.XAxisMinimumAuto = false;
|
---|
| 203 | treeLengthsTable.VisualProperties.XAxisMaximumAuto = false;
|
---|
| 204 | treeLengthsTable.VisualProperties.XAxisMinimumFixedValue = 0.0;
|
---|
| 205 | if (maxLength > maximumAllowedTreeLength + 1)
|
---|
[7124] | 206 | treeLengthsTable.VisualProperties.XAxisMaximumFixedValue = maxLength + 1; // +1 so the histogram column for the maximum length won't get trimmed
|
---|
[6978] | 207 | else
|
---|
| 208 | treeLengthsTable.VisualProperties.XAxisMaximumFixedValue = maximumAllowedTreeLength + 1;
|
---|
[7124] | 209 | treeLengthsTable.VisualProperties.XAxisTitle = xAxisTitle;
|
---|
[7147] | 210 | //visual properties for the Y-axis
|
---|
[6978] | 211 | treeLengthsTable.VisualProperties.YAxisMinimumAuto = false;
|
---|
| 212 | treeLengthsTable.VisualProperties.YAxisMaximumAuto = false;
|
---|
| 213 | treeLengthsTable.VisualProperties.YAxisMinimumFixedValue = 0.0;
|
---|
[7147] | 214 | int maxFreq = solutions.GroupBy(s => s.Length).Max(g => g.Count());
|
---|
| 215 | double yAxisMaximumFixedValue = Math.Ceiling(solutions.Length / 2.0) > maxFreq ? Math.Ceiling(solutions.Length / 2.0) : maxFreq;
|
---|
| 216 | // round up yAxisMaximumFixedValue to the nearest multiple of 5, so it would look nice in the chart
|
---|
| 217 | yAxisMaximumFixedValue = yAxisMaximumFixedValue + 5 - ((int)yAxisMaximumFixedValue % 5);
|
---|
| 218 |
|
---|
| 219 | treeLengthsTable.VisualProperties.YAxisMaximumFixedValue = yAxisMaximumFixedValue;
|
---|
[7124] | 220 | treeLengthsTable.VisualProperties.YAxisTitle = yAxisTitle;
|
---|
[6978] | 221 |
|
---|
| 222 | var results = ResultsParameter.ActualValue;
|
---|
| 223 |
|
---|
[7124] | 224 | if (!results.ContainsKey(treeLengthsTableRowName)) {
|
---|
| 225 | results.Add(new Result(treeLengthsTableRowName, treeLengthsTable));
|
---|
[6978] | 226 | } else {
|
---|
[7124] | 227 | results[treeLengthsTableRowName].Value = treeLengthsTable;
|
---|
[6978] | 228 | }
|
---|
| 229 |
|
---|
| 230 | bool storeHistory = StoreHistoryParameter.Value.Value;
|
---|
[7124] | 231 | const string treeLengthHistoryTableName = "Tree lengths history";
|
---|
| 232 | const string treeLengthHistoryRowPrefix = "Tree lengths ";
|
---|
[6978] | 233 |
|
---|
| 234 | if (storeHistory) {
|
---|
| 235 | // store tree lengths for each generation
|
---|
[7143] | 236 | var historyDataRow = new DataRow(treeLengthHistoryRowPrefix + GenerationCounter.Value, "Symbolic expression tree lengths at generation " + GenerationCounter.Value, treeLengthsTableRow.Values);
|
---|
[6978] | 237 | historyDataRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
|
---|
| 238 | historyDataRow.VisualProperties.ExactBins = false;
|
---|
[7147] | 239 | historyDataRow.VisualProperties.Bins = range;
|
---|
[7124] | 240 | historyDataRow.VisualProperties.ScaleFactor = treeLengthsTableRow.VisualProperties.ScaleFactor;
|
---|
| 241 | var historyTable = new DataTable();
|
---|
[6978] | 242 | historyTable.Rows.Add(historyDataRow);
|
---|
| 243 | // visual properties for the X-axis
|
---|
| 244 | historyTable.VisualProperties.XAxisMinimumAuto = false;
|
---|
| 245 | historyTable.VisualProperties.XAxisMaximumAuto = false;
|
---|
| 246 | historyTable.VisualProperties.XAxisMinimumFixedValue = 0.0;
|
---|
[7124] | 247 | if (maxLength > maximumAllowedTreeLength + 1)
|
---|
[6978] | 248 | historyTable.VisualProperties.XAxisMaximumFixedValue = maxLength + 1; // +1 so the histogram column for the maximum length won't get trimmed
|
---|
| 249 | else
|
---|
| 250 | historyTable.VisualProperties.XAxisMaximumFixedValue = maximumAllowedTreeLength + 1;
|
---|
[7124] | 251 | historyTable.VisualProperties.XAxisTitle = xAxisTitle;
|
---|
[6978] | 252 | // visual properties for the Y-axis
|
---|
| 253 | historyTable.VisualProperties.YAxisMinimumAuto = false;
|
---|
| 254 | historyTable.VisualProperties.YAxisMaximumAuto = false;
|
---|
| 255 | historyTable.VisualProperties.YAxisMinimumFixedValue = 0.0;
|
---|
[7147] | 256 | historyTable.VisualProperties.YAxisMaximumFixedValue = yAxisMaximumFixedValue;
|
---|
[7124] | 257 | historyTable.VisualProperties.YAxisTitle = yAxisTitle;
|
---|
[6978] | 258 |
|
---|
| 259 | var treeLengthsHistory = SymbolicExpressionTreeLengthsHistoryParameter.ActualValue;
|
---|
| 260 | if (treeLengthsHistory == null) {
|
---|
| 261 | treeLengthsHistory = new DataTableHistory();
|
---|
| 262 | SymbolicExpressionTreeLengthsHistoryParameter.ActualValue = treeLengthsHistory;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | treeLengthsHistory.Add(historyTable);
|
---|
| 266 |
|
---|
[7124] | 267 | if (!results.ContainsKey(treeLengthHistoryTableName)) {
|
---|
| 268 | results.Add(new Result(treeLengthHistoryTableName, treeLengthsHistory));
|
---|
[6978] | 269 | } else {
|
---|
[7124] | 270 | results[treeLengthHistoryTableName].Value = treeLengthsHistory;
|
---|
[6978] | 271 | }
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 | return base.Apply();
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 | }
|
---|