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 SimilarityValuesParmeterName = "Similarity";
|
---|
52 |
|
---|
53 | private const string DistanceCalculatorParameterName = "DistanceCalculator";
|
---|
54 |
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | #region Parameters
|
---|
58 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
59 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
60 | }
|
---|
61 | public ValueParameter<IntValue> UpdateIntervalParameter {
|
---|
62 | get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
|
---|
63 | }
|
---|
64 | public ValueParameter<IntValue> UpdateCounterParameter {
|
---|
65 | get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
|
---|
66 | }
|
---|
67 | public LookupParameter<ResultCollection> ResultsParameter {
|
---|
68 | get { return (LookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
69 | }
|
---|
70 | public LookupParameter<IntValue> GenerationsParameter {
|
---|
71 | get { return (LookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
72 | }
|
---|
73 | public ValueParameter<BoolValue> StoreHistoryParameter {
|
---|
74 | get { return (ValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
|
---|
75 | }
|
---|
76 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
77 | get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
78 | }
|
---|
79 | public ILookupParameter<DoubleValue> SimilarityParameter {
|
---|
80 | get { return (ILookupParameter<DoubleValue>)Parameters[SimilarityValuesParmeterName]; }
|
---|
81 | }
|
---|
82 | public IValueParameter<ISymbolicExpressionTreeDistanceCalculator> DistanceCalculatorParameter {
|
---|
83 | get { return (IValueParameter<ISymbolicExpressionTreeDistanceCalculator>)Parameters[DistanceCalculatorParameterName]; }
|
---|
84 | }
|
---|
85 | #endregion
|
---|
86 |
|
---|
87 | #region Parameter properties
|
---|
88 | public IntValue MaximumSymbolicExpressionTreeDepth { get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } }
|
---|
89 | public IntValue UpdateInterval { get { return UpdateIntervalParameter.Value; } }
|
---|
90 | public IntValue UpdateCounter { get { return UpdateCounterParameter.Value; } }
|
---|
91 | public ResultCollection Results { get { return ResultsParameter.ActualValue; } }
|
---|
92 | public IntValue Generations { get { return GenerationsParameter.ActualValue; } }
|
---|
93 | public BoolValue StoreHistory { get { return StoreHistoryParameter.Value; } }
|
---|
94 | #endregion
|
---|
95 |
|
---|
96 | [StorableConstructor]
|
---|
97 | private SymbolicDataAnalysisPopulationDiversityAnalyzer(bool deserializing)
|
---|
98 | : base(deserializing) {
|
---|
99 | }
|
---|
100 |
|
---|
101 | private SymbolicDataAnalysisPopulationDiversityAnalyzer(
|
---|
102 | SymbolicDataAnalysisPopulationDiversityAnalyzer original, Cloner cloner)
|
---|
103 | : base(original, cloner) {
|
---|
104 | }
|
---|
105 |
|
---|
106 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
107 | return new SymbolicDataAnalysisPopulationDiversityAnalyzer(this, cloner);
|
---|
108 | }
|
---|
109 |
|
---|
110 | public SymbolicDataAnalysisPopulationDiversityAnalyzer() {
|
---|
111 | // add parameters
|
---|
112 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
|
---|
113 | Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
|
---|
114 | Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
|
---|
115 | Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
116 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far."));
|
---|
117 | Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false)));
|
---|
118 | Parameters.Add(new LookupParameter<DoubleValue>(SimilarityValuesParmeterName, ""));
|
---|
119 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
120 | Parameters.Add(new ValueParameter<ISymbolicExpressionTreeDistanceCalculator>(DistanceCalculatorParameterName, "The distance calculator between trees.", new BottomUpTreeDistanceCalculator()));
|
---|
121 | UpdateCounterParameter.Hidden = true;
|
---|
122 | UpdateIntervalParameter.Hidden = true;
|
---|
123 | }
|
---|
124 |
|
---|
125 | [StorableHook(HookType.AfterDeserialization)]
|
---|
126 | private void AfterDeserialization() {
|
---|
127 | if (!Parameters.ContainsKey(MaximumSymbolicExpressionTreeDepthParameterName)) {
|
---|
128 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | #region IStatefulItem members
|
---|
133 | public override void InitializeState() {
|
---|
134 | UpdateCounter.Value = 0;
|
---|
135 | base.InitializeState();
|
---|
136 | }
|
---|
137 | #endregion
|
---|
138 |
|
---|
139 | public bool EnabledByDefault {
|
---|
140 | get { return true; }
|
---|
141 | }
|
---|
142 |
|
---|
143 | public override IOperation Apply() {
|
---|
144 | if (SimilarityParameter.ActualValue == null || SimilarityParameter.ActualValue.Value.IsAlmost(-1.0)) {
|
---|
145 | UpdateCounter.Value++;
|
---|
146 | if (UpdateCounter.Value != UpdateInterval.Value) return base.Apply();
|
---|
147 | UpdateCounter.Value = 0;
|
---|
148 | var trees = SymbolicExpressionTreeParameter.ActualValue.ToList();
|
---|
149 |
|
---|
150 | SimilarityParameter.ActualValue = new DoubleValue();
|
---|
151 |
|
---|
152 | var operations = new OperationCollection { Parallel = true };
|
---|
153 | foreach (var tree in trees) {
|
---|
154 | var op = new SymbolicDataAnalysisExpressionTreeSimilarityCalculator(DistanceCalculatorParameter.Value) {
|
---|
155 | CurrentSymbolicExpressionTree = tree,
|
---|
156 | MaximumTreeDepth = MaximumSymbolicExpressionTreeDepth.Value
|
---|
157 | };
|
---|
158 | var operation = ExecutionContext.CreateChildOperation(op, ExecutionContext.Scope);
|
---|
159 | operations.Add(operation);
|
---|
160 | }
|
---|
161 | return new OperationCollection { operations, ExecutionContext.CreateOperation(this) };
|
---|
162 | }
|
---|
163 |
|
---|
164 | var results = ResultsParameter.ActualValue;
|
---|
165 | // population diversity
|
---|
166 | DataTable populationDiversityTable;
|
---|
167 | if (!results.ContainsKey("PopulationDiversity")) {
|
---|
168 | populationDiversityTable = new DataTable("PopulationDiversity") { VisualProperties = { YAxisTitle = "Diversity" } };
|
---|
169 | results.Add(new Result("PopulationDiversity", populationDiversityTable));
|
---|
170 | }
|
---|
171 | populationDiversityTable = (DataTable)results["PopulationDiversity"].Value;
|
---|
172 | if (!populationDiversityTable.Rows.ContainsKey("Diversity"))
|
---|
173 | populationDiversityTable.Rows.Add(new DataRow("Diversity") { VisualProperties = { StartIndexZero = true } });
|
---|
174 |
|
---|
175 | int length = SymbolicExpressionTreeParameter.ActualValue.Length;
|
---|
176 | var similarity = SimilarityParameter.ActualValue.Value / (length * (length - 1) / 2.0);
|
---|
177 | var diversity = 1 - similarity;
|
---|
178 | SimilarityParameter.ActualValue.Value = -1.0;
|
---|
179 |
|
---|
180 | populationDiversityTable.Rows["Diversity"].Values.Add(diversity);
|
---|
181 |
|
---|
182 | return base.Apply();
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|