1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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.Linq;
|
---|
23 | using HeuristicLab.Analysis;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | // type definitions for ease of use
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
36 | /// <summary>
|
---|
37 | /// Population diversity analyzer
|
---|
38 | /// </summary>
|
---|
39 | [Item("SymbolicDataAnalysisPopulationDiversityAnalyzer", "An operator that tracks population diversity")]
|
---|
40 | [StorableClass]
|
---|
41 | public sealed class SymbolicDataAnalysisPopulationDiversityAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
42 | #region Parameter names
|
---|
43 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
44 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
45 | private const string UpdateIntervalParameterName = "UpdateInterval";
|
---|
46 | private const string UpdateCounterParameterName = "UpdateCounter";
|
---|
47 | private const string ResultsParameterName = "Results";
|
---|
48 | private const string GenerationsParameterName = "Generations";
|
---|
49 | private const string StoreHistoryParameterName = "StoreHistory";
|
---|
50 | // comparer parameters
|
---|
51 | private const string MatchVariablesParameterName = "MatchVariableNames";
|
---|
52 | private const string MatchVariableWeightsParameterName = "MatchVariableWeights";
|
---|
53 | private const string MatchConstantValuesParameterName = "MatchConstantValues";
|
---|
54 | private const string SimilarityValuesParmeterName = "Similarity";
|
---|
55 |
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region Parameters
|
---|
59 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
60 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
61 | }
|
---|
62 | public ValueParameter<IntValue> UpdateIntervalParameter {
|
---|
63 | get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
|
---|
64 | }
|
---|
65 | public ValueParameter<IntValue> UpdateCounterParameter {
|
---|
66 | get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
|
---|
67 | }
|
---|
68 | public LookupParameter<ResultCollection> ResultsParameter {
|
---|
69 | get { return (LookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
70 | }
|
---|
71 | public LookupParameter<IntValue> GenerationsParameter {
|
---|
72 | get { return (LookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
73 | }
|
---|
74 | public ValueParameter<BoolValue> StoreHistoryParameter {
|
---|
75 | get { return (ValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
|
---|
76 | }
|
---|
77 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
78 | get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
79 | }
|
---|
80 | public ValueParameter<BoolValue> MatchVariableNamesParameter {
|
---|
81 | get { return (ValueParameter<BoolValue>)Parameters[MatchVariablesParameterName]; }
|
---|
82 | }
|
---|
83 | public ValueParameter<BoolValue> MatchVariableWeightsParameter {
|
---|
84 | get { return (ValueParameter<BoolValue>)Parameters[MatchVariableWeightsParameterName]; }
|
---|
85 | }
|
---|
86 | public ValueParameter<BoolValue> MatchConstantValuesParameter {
|
---|
87 | get { return (ValueParameter<BoolValue>)Parameters[MatchConstantValuesParameterName]; }
|
---|
88 | }
|
---|
89 | public ILookupParameter<DoubleValue> SimilarityParameter {
|
---|
90 | get { return (ILookupParameter<DoubleValue>)Parameters[SimilarityValuesParmeterName]; }
|
---|
91 | }
|
---|
92 | #endregion
|
---|
93 |
|
---|
94 | #region Parameter properties
|
---|
95 | public IntValue MaximumSymbolicExpressionTreeDepth { get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } }
|
---|
96 | public IntValue UpdateInterval { get { return UpdateIntervalParameter.Value; } }
|
---|
97 | public IntValue UpdateCounter { get { return UpdateCounterParameter.Value; } }
|
---|
98 | public ResultCollection Results { get { return ResultsParameter.ActualValue; } }
|
---|
99 | public IntValue Generations { get { return GenerationsParameter.ActualValue; } }
|
---|
100 | public BoolValue StoreHistory { get { return StoreHistoryParameter.Value; } }
|
---|
101 | #endregion
|
---|
102 |
|
---|
103 | [StorableConstructor]
|
---|
104 | private SymbolicDataAnalysisPopulationDiversityAnalyzer(bool deserializing)
|
---|
105 | : base(deserializing) {
|
---|
106 | }
|
---|
107 |
|
---|
108 | private SymbolicDataAnalysisPopulationDiversityAnalyzer(
|
---|
109 | SymbolicDataAnalysisPopulationDiversityAnalyzer original, Cloner cloner)
|
---|
110 | : base(original, cloner) {
|
---|
111 | }
|
---|
112 |
|
---|
113 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
114 | return new SymbolicDataAnalysisPopulationDiversityAnalyzer(this, cloner);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public SymbolicDataAnalysisPopulationDiversityAnalyzer() {
|
---|
118 | // add parameters
|
---|
119 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
|
---|
120 | Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
|
---|
121 | Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
|
---|
122 | Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
123 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far."));
|
---|
124 | Parameters.Add(new ValueParameter<BoolValue>(MatchVariablesParameterName, "Specify if the symbolic expression tree comparer should match variable names.", new BoolValue(true)));
|
---|
125 | Parameters.Add(new ValueParameter<BoolValue>(MatchVariableWeightsParameterName, "Specify if the symbolic expression tree comparer should match variable weights.", new BoolValue(true)));
|
---|
126 | Parameters.Add(new ValueParameter<BoolValue>(MatchConstantValuesParameterName, "Specify if the symbolic expression tree comparer should match constant values.", new BoolValue(true)));
|
---|
127 | Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false)));
|
---|
128 | Parameters.Add(new LookupParameter<DoubleValue>(SimilarityValuesParmeterName, ""));
|
---|
129 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
130 | UpdateCounterParameter.Hidden = true;
|
---|
131 | UpdateIntervalParameter.Hidden = true;
|
---|
132 | }
|
---|
133 |
|
---|
134 | [StorableHook(HookType.AfterDeserialization)]
|
---|
135 | private void AfterDeserialization() {
|
---|
136 | if (!Parameters.ContainsKey(MaximumSymbolicExpressionTreeDepthParameterName)) {
|
---|
137 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | #region IStatefulItem members
|
---|
142 | public override void InitializeState() {
|
---|
143 | UpdateCounter.Value = 0;
|
---|
144 | base.InitializeState();
|
---|
145 | }
|
---|
146 | #endregion
|
---|
147 |
|
---|
148 | public bool EnabledByDefault {
|
---|
149 | get { return true; }
|
---|
150 | }
|
---|
151 |
|
---|
152 | public override IOperation Apply() {
|
---|
153 | if (SimilarityParameter.ActualValue == null || SimilarityParameter.ActualValue.Value.IsAlmost(-1.0)) {
|
---|
154 | UpdateCounter.Value++;
|
---|
155 | if (UpdateCounter.Value != UpdateInterval.Value) return base.Apply();
|
---|
156 | UpdateCounter.Value = 0;
|
---|
157 | var trees = SymbolicExpressionTreeParameter.ActualValue.ToList();
|
---|
158 |
|
---|
159 | SimilarityParameter.ActualValue = new DoubleValue();
|
---|
160 |
|
---|
161 | var comparer = new SymbolicExpressionTreeNodeSimilarityComparer {
|
---|
162 | MatchConstantValues = MatchConstantValuesParameter.Value.Value,
|
---|
163 | MatchVariableNames = MatchVariableNamesParameter.Value.Value,
|
---|
164 | MatchVariableWeights = MatchVariableWeightsParameter.Value.Value
|
---|
165 | };
|
---|
166 |
|
---|
167 | var operations = new OperationCollection { Parallel = true };
|
---|
168 | foreach (var tree in trees) {
|
---|
169 | var op = new SymbolicDataAnalysisExpressionTreeSimilarityCalculator {
|
---|
170 | CurrentSymbolicExpressionTree = tree,
|
---|
171 | SimilarityComparer = comparer,
|
---|
172 | MaximumTreeDepth = MaximumSymbolicExpressionTreeDepth.Value
|
---|
173 | };
|
---|
174 | var operation = ExecutionContext.CreateChildOperation(op, ExecutionContext.Scope);
|
---|
175 | operations.Add(operation);
|
---|
176 | }
|
---|
177 | return new OperationCollection { operations, ExecutionContext.CreateOperation(this) };
|
---|
178 | }
|
---|
179 |
|
---|
180 | var results = ResultsParameter.ActualValue;
|
---|
181 | // population diversity
|
---|
182 | DataTable populationDiversityTable;
|
---|
183 | if (!results.ContainsKey("PopulationDiversity")) {
|
---|
184 | populationDiversityTable = new DataTable("PopulationDiversity") { VisualProperties = { YAxisTitle = "Diversity" } };
|
---|
185 | results.Add(new Result("PopulationDiversity", populationDiversityTable));
|
---|
186 | }
|
---|
187 | populationDiversityTable = (DataTable)results["PopulationDiversity"].Value;
|
---|
188 | if (!populationDiversityTable.Rows.ContainsKey("Diversity"))
|
---|
189 | populationDiversityTable.Rows.Add(new DataRow("Diversity") { VisualProperties = { StartIndexZero = true } });
|
---|
190 |
|
---|
191 | int length = SymbolicExpressionTreeParameter.ActualValue.Length;
|
---|
192 | var similarity = SimilarityParameter.ActualValue.Value / (length * (length - 1) / 2.0);
|
---|
193 | var diversity = 1 - similarity;
|
---|
194 | SimilarityParameter.ActualValue.Value = -1.0;
|
---|
195 |
|
---|
196 | populationDiversityTable.Rows["Diversity"].Values.Add(diversity);
|
---|
197 |
|
---|
198 | return base.Apply();
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|