Changeset 9083 for branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/TracingSymbolicExpressionTreeManipulator.cs
- Timestamp:
- 12/20/12 16:24:54 (11 years ago)
- Location:
- branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Property svn:mergeinfo changed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/TracingSymbolicExpressionTreeManipulator.cs
r8557 r9083 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.EvolutionaryTracking;28 27 using HeuristicLab.Parameters; 29 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 37 36 public abstract class TracingSymbolicExpressionTreeManipulator : TracingSymbolicExpressionTreeOperator, ISymbolicExpressionTreeManipulator { 38 37 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 38 [Storable] 39 private readonly Dictionary<Type, byte> manipulationTypes = new Dictionary<Type, byte>(); 39 40 40 41 #region Parameter Properties … … 52 53 [StorableConstructor] 53 54 protected TracingSymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { } 54 protected TracingSymbolicExpressionTreeManipulator(TracingSymbolicExpressionTreeManipulator original, Cloner cloner) : base(original, cloner) { } 55 protected TracingSymbolicExpressionTreeManipulator(TracingSymbolicExpressionTreeManipulator original, Cloner cloner) 56 : base(original, cloner) { 57 this.manipulationTypes = new Dictionary<Type, byte>(original.manipulationTypes); 58 } 55 59 public TracingSymbolicExpressionTreeManipulator() 56 60 : base() { 57 61 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied.")); 62 63 manipulationTypes.Add(typeof(FullTreeShaker), ManipulationType.FullTreeShaker); 64 manipulationTypes.Add(typeof(OnePointShaker), ManipulationType.OnePointShaker); 65 manipulationTypes.Add(typeof(ChangeNodeTypeManipulation), ManipulationType.ChangeNodeTypeManipulation); 66 manipulationTypes.Add(typeof(ReplaceBranchManipulation), ManipulationType.ReplaceBranchManipulation); 58 67 } 59 68 … … 72 81 * - After tree is mutated, we set GlobalTraceMap[tree] = clone and GlobalFragmentMap[tree] = modified node from tree 73 82 */ 74 ISymbolicExpressionTreeNode fragment;83 Fragment fragment = null; 75 84 if (GlobalTraceMap.ContainsKey(tree)) { 76 var clone = (IItem)tree.Clone(); // create clone of tree 85 var clone = (IItem)tree.Clone(); 86 GlobalCloneMap[clone] = GlobalCloneMap[tree]; 87 GlobalCloneMap[tree] = clone; 77 88 GlobalTraceMap[clone] = GlobalTraceMap[tree]; // clone gets parents of tree 78 89 GlobalTraceMap[tree] = new ItemList<IItem> { clone }; // tree gets clone as parent 79 fragment = ( (GenericWrapper<ISymbolicExpressionTreeNode>)GlobalFragmentMap[tree]).Content;80 int index = tree.IterateNodesBreadth().ToList().IndexOf(fragment ); // returns -1 if fragment is not present in tree81 GlobalFragmentMap[clone] = new GenericWrapper<ISymbolicExpressionTreeNode>(index == -1 ? null : ((ISymbolicExpressionTree)clone).IterateNodesBreadth().ElementAt(index));90 fragment = (Fragment)GlobalFragmentMap[tree]; 91 int index = tree.IterateNodesBreadth().ToList().IndexOf(fragment.Root); // returns -1 if fragment is not present in tree (can happen if fragment is null) 92 GlobalFragmentMap[clone] = new Fragment(index == -1 ? null : ((ISymbolicExpressionTree)clone).IterateNodesBreadth().ElementAt(index)); 82 93 } else { 83 94 GlobalTraceMap[tree] = new ItemList<IItem> { GlobalCloneMap[tree] }; 84 95 } 85 96 86 var original = GlobalCloneMap[tree]; 87 var nodes0 = ((ISymbolicExpressionTree)original).IterateNodesBreadth().ToList(); 97 var concreteType = this.GetType(); 88 98 89 // perform mutation 99 if (!manipulationTypes.ContainsKey(concreteType)) 100 throw new Exception(this.Name + ": Unknown manipulation type (key not found in the dictionary)."); 101 102 var nodes0 = tree.IterateNodesBreadth().ToList(); // list of nodes before manipulation 90 103 Manipulate(RandomParameter.ActualValue, tree); 104 var nodes1 = tree.IterateNodesBreadth().ToList(); // list of nodes after manipulation 105 int min = Math.Min(nodes0.Count, nodes1.Count); 91 106 92 var nodes1 = tree.IterateNodesBreadth().ToList(); 107 switch (manipulationTypes[concreteType]) { 108 case ManipulationType.FullTreeShaker: { 109 // the FullTreeShaker changes the local parameters of all the nodes in the tree 110 // therefore, the whole tree is considered to be the fragment 111 fragment = new Fragment(tree.Root.GetSubtree(0).GetSubtree(0)); 112 break; 113 } 114 case ManipulationType.OnePointShaker: { 115 var original = (ISymbolicExpressionTree)GlobalCloneMap[tree]; 116 var nodesOriginal = original.IterateNodesBreadth().ToList(); 117 int i = 0; 118 var comparer = SymbolicExpressionTreeNodeComparer; 119 while (i != min && comparer.Equals(nodesOriginal[i], nodes1[i])) ++i; 120 fragment = new Fragment(i == min ? null : nodes1[i], 1); 121 break; 122 } 123 case ManipulationType.ChangeNodeTypeManipulation: { 124 int i = 0; 125 while (i != min && ReferenceEquals(nodes0[i], nodes1[i])) ++i; 126 fragment = new Fragment(i == min ? null : nodes1[i], 1); 127 break; 128 } 129 case ManipulationType.ReplaceBranchManipulation: { 130 int i = 0; 131 while (i != min && ReferenceEquals(nodes0[i], nodes1[i])) ++i; 132 fragment = new Fragment(i == min ? null : nodes1[i]); 133 break; 134 } 135 default: 136 throw new Exception(Name + ": Unknown manipulaton type."); 137 } 93 138 94 int min = Math.Min(nodes0.Count, nodes1.Count); 95 // some tree manipulators only change local parameters while the actual node stays the same 96 // consequently, we need a stronger comparison than ReferenceEquals 97 var comp = SymbolicExpressionTreeNodeComparer; 98 if (comp == null) 99 throw new Exception(Name + ": Could not get SymbolicExpressionTreeNodeComparerParameter!"); 100 var mismatches = new List<int>(); 101 for (int i = 0; i != min; ++i) { 102 if (!comp.Equals(nodes0[i], nodes1[i])) { 103 mismatches.Add(i); 104 } 105 } 106 if (mismatches.Count == 0) // should never happen 107 fragment = null; 108 else 109 fragment = mismatches.Count > 1 ? nodes1[mismatches[0]].Parent : nodes1[mismatches[0]]; 110 GlobalFragmentMap[tree] = new GenericWrapper<ISymbolicExpressionTreeNode>(fragment); 139 GlobalFragmentMap[tree] = fragment; 111 140 return base.Apply(); 112 141 } 113 142 114 143 protected abstract void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree); 144 145 private struct ManipulationType { 146 public const byte FullTreeShaker = 1; 147 public const byte OnePointShaker = 2; 148 public const byte ChangeNodeTypeManipulation = 3; 149 public const byte ReplaceBranchManipulation = 4; 150 } 115 151 } 116 152 }
Note: See TracChangeset
for help on using the changeset viewer.