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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
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 | using HeuristicLab.Problems.DataAnalysis;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
34 | // type definitions for convenience
|
---|
35 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
36 | using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
|
---|
37 | using TraceMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItemList<HeuristicLab.Core.IItem>>;
|
---|
38 |
|
---|
39 | namespace HeuristicLab.EvolutionaryTracking {
|
---|
40 | /// <summary>
|
---|
41 | /// An operator that gathers information on tree fragments that get passed from one generation to the next via genetic operators
|
---|
42 | /// (Tracking of genetic variance and heredity)
|
---|
43 | /// </summary>
|
---|
44 | [Item("SymbolicExpressionTreeFragmentsAnalyzer", "An operator that provides statistics about crossover fragments")]
|
---|
45 | [StorableClass]
|
---|
46 | public abstract class SymbolicExpressionTreeFragmentsAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
47 | #region Parameter names
|
---|
48 | protected const string UpdateIntervalParameterName = "UpdateInterval";
|
---|
49 | protected const string UpdateCounterParameterName = "UpdateCounter";
|
---|
50 | protected const string ResultsParameterName = "Results";
|
---|
51 | protected const string SecondaryTraceMapParameterName = "SecondaryTraceMap";
|
---|
52 | protected const string SecondaryFragmentMapParameterName = "SecondaryFragmentMap";
|
---|
53 | protected const string SecondaryCloneMapParameterName = "SecondaryCloneMap";
|
---|
54 | protected const string GlobalTraceMapParameterName = "GlobalTraceMap";
|
---|
55 | protected const string GlobalTraceMapParameterDescription = "Global map of trees and their parents.";
|
---|
56 | protected const string GlobalCloneMapParameterName = "GlobalCloneMap";
|
---|
57 | protected const string GlobalCloneMapParameterDescription = "Global map of trees and their clones";
|
---|
58 | protected const string GlobalFragmentMapParameterName = "GlobalFragmentMap";
|
---|
59 | protected const string GlobalFragmentMapParameterDescription = "Global map of trees and their respective fragments.";
|
---|
60 | protected const string GenerationsParameterName = "Generations";
|
---|
61 | protected const string SymbolicExpressionInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
62 | protected const string SymbolicRegressionProblemDataParameterName = "ProblemData";
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 | // impact values calculator
|
---|
66 | private readonly SymbolicRegressionSolutionImpactValuesCalculator calculator = new SymbolicRegressionSolutionImpactValuesCalculator();
|
---|
67 |
|
---|
68 | #region Parameter properties
|
---|
69 | public ValueParameter<IntValue> UpdateIntervalParameter {
|
---|
70 | get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
|
---|
71 | }
|
---|
72 | public ValueParameter<IntValue> UpdateCounterParameter {
|
---|
73 | get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
|
---|
74 | }
|
---|
75 | public LookupParameter<ResultCollection> ResultsParameter {
|
---|
76 | get { return (LookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
77 | }
|
---|
78 | public LookupParameter<IntValue> GenerationsParameter {
|
---|
79 | get { return (LookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
80 | }
|
---|
81 | // tracking structures
|
---|
82 | public LookupParameter<TraceMapType> GlobalTraceMapParameter {
|
---|
83 | get { return (LookupParameter<TraceMapType>)Parameters[GlobalTraceMapParameterName]; }
|
---|
84 | }
|
---|
85 | public LookupParameter<CloneMapType> GlobalCloneMapParameter {
|
---|
86 | get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; }
|
---|
87 | }
|
---|
88 | public LookupParameter<CloneMapType> GlobalFragmentMapParameter {
|
---|
89 | get { return (LookupParameter<CloneMapType>)Parameters[GlobalFragmentMapParameterName]; }
|
---|
90 | }
|
---|
91 | // auxiliary structures for tracking fragments and individuals from the previous generation
|
---|
92 | // (this is needed because tracking is done in connection to the current generation, i.e., for
|
---|
93 | // counting "successful" offspring and fragments
|
---|
94 | public LookupParameter<TraceMapType> SecondaryTraceMapParameter {
|
---|
95 | get { return (LookupParameter<TraceMapType>)Parameters[SecondaryTraceMapParameterName]; }
|
---|
96 | }
|
---|
97 | public LookupParameter<CloneMapType> SecondaryFragmentMapParameter {
|
---|
98 | get { return (LookupParameter<CloneMapType>)Parameters[SecondaryFragmentMapParameterName]; }
|
---|
99 | }
|
---|
100 | public LookupParameter<CloneMapType> SecondaryCloneMapParameter {
|
---|
101 | get { return (LookupParameter<CloneMapType>)Parameters[SecondaryCloneMapParameterName]; }
|
---|
102 | }
|
---|
103 | // problem data, interpreter and evaluator
|
---|
104 | public LookupParameter<SymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionInterpreterParameter {
|
---|
105 | get { return (LookupParameter<SymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionInterpreterParameterName]; }
|
---|
106 | }
|
---|
107 | public LookupParameter<RegressionProblemData> SymbolicRegressionProblemDataParameter {
|
---|
108 | get { return (LookupParameter<RegressionProblemData>)Parameters[SymbolicRegressionProblemDataParameterName]; }
|
---|
109 | }
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | #region Parameters
|
---|
113 | public bool EnabledByDefault {
|
---|
114 | get { return true; }
|
---|
115 | }
|
---|
116 | public IntValue UpdateInterval {
|
---|
117 | get { return UpdateIntervalParameter.Value; }
|
---|
118 | }
|
---|
119 | public IntValue UpdateCounter {
|
---|
120 | get { return UpdateCounterParameter.Value; }
|
---|
121 | }
|
---|
122 | public ResultCollection Results {
|
---|
123 | get { return ResultsParameter.ActualValue; }
|
---|
124 | }
|
---|
125 | public CloneMapType GlobalCloneMap {
|
---|
126 | get { return GlobalCloneMapParameter.ActualValue; }
|
---|
127 | }
|
---|
128 | public TraceMapType GlobalTraceMap {
|
---|
129 | get { return GlobalTraceMapParameter.ActualValue; }
|
---|
130 | }
|
---|
131 | public TraceMapType SecondaryTraceMap {
|
---|
132 | get { return SecondaryTraceMapParameter.ActualValue; }
|
---|
133 | set { SecondaryTraceMapParameter.ActualValue = value; }
|
---|
134 | }
|
---|
135 | public CloneMapType SecondaryFragmentMap {
|
---|
136 | get { return SecondaryFragmentMapParameter.ActualValue; }
|
---|
137 | set { SecondaryFragmentMapParameter.ActualValue = value; }
|
---|
138 | }
|
---|
139 | public CloneMapType SecondaryCloneMap {
|
---|
140 | get { return SecondaryCloneMapParameter.ActualValue; }
|
---|
141 | set { SecondaryCloneMapParameter.ActualValue = value; }
|
---|
142 | }
|
---|
143 | public CloneMapType GlobalFragmentMap {
|
---|
144 | get { return GlobalFragmentMapParameter.ActualValue; }
|
---|
145 | }
|
---|
146 | public IntValue Generations {
|
---|
147 | get { return GenerationsParameter.ActualValue; }
|
---|
148 | }
|
---|
149 | public SymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionInterpreter {
|
---|
150 | get { return SymbolicExpressionInterpreterParameter.ActualValue; }
|
---|
151 | }
|
---|
152 | public RegressionProblemData SymbolicRegressionProblemData {
|
---|
153 | get { return SymbolicRegressionProblemDataParameter.ActualValue; }
|
---|
154 | }
|
---|
155 | #endregion
|
---|
156 |
|
---|
157 | [StorableConstructor]
|
---|
158 | protected SymbolicExpressionTreeFragmentsAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
159 |
|
---|
160 | protected SymbolicExpressionTreeFragmentsAnalyzer(SymbolicExpressionTreeFragmentsAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
161 |
|
---|
162 | protected SymbolicExpressionTreeFragmentsAnalyzer()
|
---|
163 | : base() {
|
---|
164 | #region Add parameters
|
---|
165 | // analyzer update counter and update interval
|
---|
166 | Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
|
---|
167 | Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
|
---|
168 | // tracking
|
---|
169 | Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName, GlobalTraceMapParameterDescription));
|
---|
170 | Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, GlobalCloneMapParameterDescription));
|
---|
171 | Parameters.Add(new LookupParameter<CloneMapType>(GlobalFragmentMapParameterName, GlobalFragmentMapParameterDescription));
|
---|
172 | // secondary tracking
|
---|
173 | Parameters.Add(new LookupParameter<TraceMapType>(SecondaryTraceMapParameterName, GlobalTraceMapParameterDescription));
|
---|
174 | Parameters.Add(new LookupParameter<CloneMapType>(SecondaryCloneMapParameterName, GlobalCloneMapParameterDescription));
|
---|
175 | Parameters.Add(new LookupParameter<CloneMapType>(SecondaryFragmentMapParameterName, GlobalFragmentMapParameterDescription));
|
---|
176 | // impact calculation
|
---|
177 | Parameters.Add(new LookupParameter<SymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionInterpreterParameterName, "Interpreter for symbolic expression trees"));
|
---|
178 | Parameters.Add(new LookupParameter<RegressionProblemData>(SymbolicRegressionProblemDataParameterName, "The symbolic data analysis problem."));
|
---|
179 | // fragment statistics parameters
|
---|
180 | Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
181 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far."));
|
---|
182 | #endregion
|
---|
183 |
|
---|
184 | UpdateCounterParameter.Hidden = true;
|
---|
185 | UpdateIntervalParameter.Hidden = true;
|
---|
186 |
|
---|
187 | }
|
---|
188 | #region After deserialization code
|
---|
189 | [StorableHook(HookType.AfterDeserialization)]
|
---|
190 | private void AfterDeserialization() {
|
---|
191 | // check if all the parameters are present and accounted for
|
---|
192 | if (!Parameters.ContainsKey(UpdateIntervalParameterName)) {
|
---|
193 | Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
|
---|
194 | }
|
---|
195 | if (!Parameters.ContainsKey(UpdateCounterParameterName)) {
|
---|
196 | Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
|
---|
197 | UpdateCounterParameter.Hidden = true;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | #endregion
|
---|
201 | #region IStatefulItem members
|
---|
202 | public override void InitializeState() {
|
---|
203 | base.InitializeState();
|
---|
204 | UpdateCounter.Value = 0;
|
---|
205 | }
|
---|
206 |
|
---|
207 | public override void ClearState() {
|
---|
208 | base.ClearState();
|
---|
209 | UpdateCounter.Value = 0;
|
---|
210 | }
|
---|
211 | #endregion
|
---|
212 |
|
---|
213 | /* A small description of the way this analyzer works
|
---|
214 | *
|
---|
215 | * We keep 3 maps in the global scope: the GlobalTraceMap, the GlobalFragmentMap and the GlobalCloneMap that are updated on each step: selection, crossover, mutation
|
---|
216 | * These maps provide information about the children, parents and transferred fragments as follows:
|
---|
217 | *
|
---|
218 | * 1) GlobalCloneMap
|
---|
219 | * This data structure provides the basis for tracking. In the selection step, every selected individual gets cloned and transferred into the recombination pool.
|
---|
220 | * The GlobalCloneMap keeps track of clones by mapping them to the original individuals. This is an essential step for building the genealogy graph. Using this
|
---|
221 | * structure we can find out how many times each individual was selected.
|
---|
222 | * The GlobalCloneMap consists of Key-Value pairs Clone->Original
|
---|
223 | *
|
---|
224 | * 2) GlobalTraceMap
|
---|
225 | * Keeps information in the form of key-value pairs Child->{Parents}, where {Parents} is a set consisting of
|
---|
226 | * - one parent individual (in the case of mutation), by language abuse we say that the individual that mutation acted on is the parent,
|
---|
227 | * and the resulting individual is the child
|
---|
228 | * - two parent individuals (in the case of crossover), named Parent0 and Parent1. Parent0 is the individual that crossover acts on by replacing
|
---|
229 | * one of its subtrees with a compatible subtree, randomly selected from Parent1
|
---|
230 | *
|
---|
231 | * CROSSOVER
|
---|
232 | * Parent0 Parent1 {Parents}
|
---|
233 | * \ / ^
|
---|
234 | * \ fragment (inserted from Parent1 into Parent0 to create the Child) ^ [Mapping provided by the GlobalTraceMap]
|
---|
235 | * \ / ^
|
---|
236 | * \ / ^
|
---|
237 | * Child Child
|
---|
238 | *
|
---|
239 | * MUTATION
|
---|
240 | * Parent0
|
---|
241 | * |
|
---|
242 | * | [mutation acts on a random node/subtree in the 'parent' individual
|
---|
243 | * |
|
---|
244 | * Child
|
---|
245 | *
|
---|
246 | * Since crossover and mutation act on individuals in the recombination pool (which in fact are clones of individuals from the previous generation), the GlobalTraceMap
|
---|
247 | * is populated with values given by the mappings in the GlobalCloneMap, so that the original parent for each child is known.
|
---|
248 | *
|
---|
249 | * 3) GlobalFragmentMap
|
---|
250 | * Keeps information in the form of Key-Value pairs about an individual and its respective fragment
|
---|
251 | * (ie., the subtree received via crossover or created/altered by mutation)
|
---|
252 | * */
|
---|
253 |
|
---|
254 | public override IOperation Apply() {
|
---|
255 | // save the current generation so it can be compared to the following one, next time the analyzer is applied
|
---|
256 | SecondaryTraceMap = SecondaryTraceMap ?? new TraceMapType();
|
---|
257 | SecondaryCloneMap = SecondaryCloneMap ?? new CloneMapType();
|
---|
258 | SecondaryFragmentMap = SecondaryFragmentMap ?? new CloneMapType();
|
---|
259 |
|
---|
260 | SecondaryTraceMap.Clear();
|
---|
261 | if (GlobalTraceMap != null)
|
---|
262 | foreach (var m in GlobalTraceMap)
|
---|
263 | SecondaryTraceMap.Add(m.Key, m.Value);
|
---|
264 |
|
---|
265 | SecondaryCloneMap.Clear();
|
---|
266 | if (GlobalCloneMap != null)
|
---|
267 | foreach (var m in GlobalCloneMap)
|
---|
268 | SecondaryCloneMap.Add(m.Key, m.Value);
|
---|
269 |
|
---|
270 | SecondaryFragmentMap.Clear();
|
---|
271 | if (GlobalFragmentMap != null)
|
---|
272 | foreach (var m in GlobalFragmentMap)
|
---|
273 | SecondaryFragmentMap.Add(m.Key, m.Value);
|
---|
274 | return base.Apply();
|
---|
275 | }
|
---|
276 |
|
---|
277 | protected virtual void InitializeParameters() {
|
---|
278 | #region Init parameters
|
---|
279 |
|
---|
280 | #endregion
|
---|
281 | }
|
---|
282 |
|
---|
283 | private static int VisitationLength(ISymbolicExpressionTree tree) {
|
---|
284 | return VisitationLength(tree.Root);
|
---|
285 | }
|
---|
286 |
|
---|
287 | private static int VisitationLength(ISymbolicExpressionTreeNode node) {
|
---|
288 | return node.IterateNodesBreadth().Sum(n => n.GetLength());
|
---|
289 | }
|
---|
290 |
|
---|
291 | #region Similarity computations
|
---|
292 | /// <summary>
|
---|
293 | /// Provide a measure of how similar the fragments that get passed by crossover from one generation to the next are.
|
---|
294 | /// </summary>
|
---|
295 | /// <param name="fragments">The symbolic expression tree fragments</param>
|
---|
296 | /// <param name="mode">The similarity mode (0 - exact, 1 - high, 2 - relaxed)</param>
|
---|
297 | /// <returns>The average number of similar fragments</returns>
|
---|
298 | private static double CalculateSimilarity(IList<IFragment> fragments, SymbolicExpressionTreeNodeSimilarityComparer comparer) {
|
---|
299 | var visited = new bool[fragments.Count];
|
---|
300 | int groups = 0;
|
---|
301 | for (int i = 0; i != fragments.Count - 1; ++i) {
|
---|
302 | if (visited[i]) continue;
|
---|
303 | for (int j = i + 1; j != fragments.Count; ++j) {
|
---|
304 | if (visited[j]) continue;
|
---|
305 | if (fragments[i].Root.IsSimilarTo(fragments[j].Root, comparer))
|
---|
306 | visited[j] = true;
|
---|
307 | }
|
---|
308 | ++groups;
|
---|
309 | }
|
---|
310 | return (double)fragments.Count / groups;
|
---|
311 | }
|
---|
312 | #endregion
|
---|
313 | } //SymbolicExpressionTreeFragmentsAnalyzer
|
---|
314 | }
|
---|