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;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.EvolutionaryTracking;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
|
---|
31 | using TraceMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItemList<HeuristicLab.Core.IItem>>;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
34 | /// <summary>
|
---|
35 | /// A base class for operators that manipulate real-valued vectors.
|
---|
36 | /// </summary>
|
---|
37 | [Item("TracingSymbolicExpressionTreeManipulator", "A base class for operators that manipulate symbolic expression trees.")]
|
---|
38 | [StorableClass]
|
---|
39 | public abstract class TracingSymbolicExpressionTreeManipulator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeManipulator {
|
---|
40 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
41 | private const string GlobalTraceMapParameterName = "GlobalTraceMap";
|
---|
42 | private const string GlobalCloneMapParameterName = "GlobalCloneMap";
|
---|
43 | private const string GlobalFragmentMapParameterName = "GlobalFragmentMap";
|
---|
44 |
|
---|
45 | #region Parameter Properties
|
---|
46 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
47 | get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
48 | }
|
---|
49 | public LookupParameter<CloneMapType> GlobalCloneMapParameter {
|
---|
50 | get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; }
|
---|
51 | }
|
---|
52 | public LookupParameter<TraceMapType> GlobalTraceMapParameter {
|
---|
53 | get { return (LookupParameter<TraceMapType>)Parameters[GlobalTraceMapParameterName]; }
|
---|
54 | }
|
---|
55 | public LookupParameter<CloneMapType> GlobalFragmentMapParameter {
|
---|
56 | get { return (LookupParameter<CloneMapType>)Parameters[GlobalFragmentMapParameterName]; }
|
---|
57 | }
|
---|
58 | #endregion
|
---|
59 |
|
---|
60 | #region Properties
|
---|
61 | public ISymbolicExpressionTree SymbolicExpressionTree {
|
---|
62 | get { return SymbolicExpressionTreeParameter.ActualValue; }
|
---|
63 | }
|
---|
64 | public CloneMapType GlobalCloneMap {
|
---|
65 | get { return GlobalCloneMapParameter.ActualValue; }
|
---|
66 | }
|
---|
67 | public TraceMapType GlobalTraceMap {
|
---|
68 | get { return GlobalTraceMapParameter.ActualValue; }
|
---|
69 | }
|
---|
70 | public CloneMapType GlobalFragmentMap {
|
---|
71 | get { return GlobalFragmentMapParameter.ActualValue; }
|
---|
72 | }
|
---|
73 | #endregion
|
---|
74 |
|
---|
75 | [StorableConstructor]
|
---|
76 | protected TracingSymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { }
|
---|
77 | protected TracingSymbolicExpressionTreeManipulator(TracingSymbolicExpressionTreeManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
78 | public TracingSymbolicExpressionTreeManipulator()
|
---|
79 | : base() {
|
---|
80 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
|
---|
81 | Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
|
---|
82 | Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName, "A global cache containing tracing info."));
|
---|
83 | Parameters.Add(new LookupParameter<CloneMapType>(GlobalFragmentMapParameterName, "A global map keeping track of tree fragments received via crossover."));
|
---|
84 | }
|
---|
85 |
|
---|
86 | public sealed override IOperation Apply() {
|
---|
87 | ISymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
88 |
|
---|
89 | var gScope = ExecutionContext.Scope;
|
---|
90 | while (gScope.Parent != null)
|
---|
91 | gScope = gScope.Parent;
|
---|
92 |
|
---|
93 | if (GlobalTraceMap == null)
|
---|
94 | gScope.Variables.Add(new Variable(GlobalTraceMapParameterName, new TraceMapType()));
|
---|
95 | if (GlobalFragmentMap == null)
|
---|
96 | gScope.Variables.Add(new Variable(GlobalFragmentMapParameterName, new CloneMapType()));
|
---|
97 |
|
---|
98 | int i = 0;
|
---|
99 | if (GlobalTraceMap.ContainsKey(tree)) {
|
---|
100 | // tree was affected by crossover before mutation
|
---|
101 | // if the tree to be manipulated is already a product of crossover (in the current reproduction phase),
|
---|
102 | // then there exists no relationship with the original "parent".
|
---|
103 | // In order to preserve information, a tree clone is created before mutation and added to the trace map.
|
---|
104 | var clone = (IItem)tree.Clone();
|
---|
105 | GlobalTraceMap[clone] = GlobalTraceMap[tree]; // clone gets parents of tree
|
---|
106 | GlobalTraceMap[tree] = new ItemList<IItem> { clone }; // tree gets clone as parent
|
---|
107 | var nodes = tree.IterateNodesBreadth().ToList();
|
---|
108 | for (i = 0; i != nodes.Count; ++i) {
|
---|
109 | if ((GlobalFragmentMap[tree] as GenericWrapper<SymbolicExpressionTreeNode>).Content == nodes[i]) break;
|
---|
110 | }
|
---|
111 | var fragment = (SymbolicExpressionTreeNode)(((SymbolicExpressionTree)clone).IterateNodesBreadth().ElementAt(i));
|
---|
112 | GlobalFragmentMap[clone] = new GenericWrapper<SymbolicExpressionTreeNode>(fragment);
|
---|
113 | } else {
|
---|
114 | var original = GlobalCloneMap[tree];
|
---|
115 | GlobalTraceMap[tree] = new ItemList<IItem> { original };
|
---|
116 | }
|
---|
117 | var nodes0 = tree.IterateNodesBreadth().ToList();
|
---|
118 | Manipulate(RandomParameter.ActualValue, tree);
|
---|
119 | var nodes1 = tree.IterateNodesBreadth().ToList();
|
---|
120 | int min = Math.Min(nodes0.Count, nodes1.Count);
|
---|
121 | for (i = 0; i != min; ++i)
|
---|
122 | if (nodes0[i] != nodes1[i]) break;
|
---|
123 | GlobalFragmentMap[tree] = new GenericWrapper<SymbolicExpressionTreeNode>(i == min ? null : (SymbolicExpressionTreeNode)nodes1[i]);
|
---|
124 | return base.Apply();
|
---|
125 | }
|
---|
126 |
|
---|
127 | protected abstract void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree);
|
---|
128 | }
|
---|
129 | }
|
---|