Changeset 7788 for branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Timestamp:
- 05/09/12 17:29:33 (13 years ago)
- Location:
- branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/TracingSymbolicExpressionTreeCrossover.cs
r7522 r7788 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data; 26 27 using HeuristicLab.Parameters; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 34 35 /// A base class for operators that perform a crossover of symbolic expression trees. 35 36 /// </summary> 36 [Item("SymbolicExpressionTreeCrossover", "A base class for operators that perform a crossover of symbolic expression trees.")] 37 [Item("SymbolicExpressionTreeCrossover", 38 "A base class for operators that perform a crossover of symbolic expression trees.")] 37 39 [StorableClass] 38 40 public abstract class TracingSymbolicExpressionTreeCrossover : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCrossover { … … 41 43 private const string GlobalTraceMapParameterName = "GlobalTraceMap"; 42 44 private const string GlobalCloneMapParameterName = "GlobalCloneMap"; 45 private const string GlobalFragmentMapParameterName = "GlobalFragmentMap"; 46 43 47 #region Parameter Properties 48 44 49 public ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter { 45 50 get { return (ScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[ParentsParameterName]; } 46 51 } 52 47 53 public ILookupParameter<ISymbolicExpressionTree> ChildParameter { 48 54 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[ChildParameterName]; } 49 55 } 56 50 57 public LookupParameter<CloneMapType> GlobalCloneMapParameter { 51 58 get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; } 52 59 } 60 53 61 public LookupParameter<TraceMapType> GlobalTraceMapParameter { 54 62 get { return (LookupParameter<TraceMapType>)Parameters[GlobalTraceMapParameterName]; } 55 63 } 64 65 public LookupParameter<CloneMapType> GlobalFragmentMapParameter { 66 get { return (LookupParameter<CloneMapType>)Parameters[GlobalFragmentMapParameterName]; } 67 } 68 56 69 #endregion 70 57 71 #region Properties 72 58 73 public ItemArray<ISymbolicExpressionTree> Parents { 59 74 get { return ParentsParameter.ActualValue; } 60 75 } 76 61 77 public ISymbolicExpressionTree Child { 62 78 get { return ChildParameter.ActualValue; } 63 79 set { ChildParameter.ActualValue = value; } 64 80 } 81 65 82 public CloneMapType GlobalCloneMap { 66 83 get { return GlobalCloneMapParameter.ActualValue; } 67 84 } 85 86 public CloneMapType GlobalFragmentMap { 87 get { return GlobalFragmentMapParameter.ActualValue; } 88 } 89 68 90 public TraceMapType GlobalTraceMap { 69 91 get { return GlobalTraceMapParameter.ActualValue; } 70 92 } 93 71 94 #endregion 95 72 96 [StorableConstructor] 73 protected TracingSymbolicExpressionTreeCrossover(bool deserializing) : base(deserializing) { } 74 protected TracingSymbolicExpressionTreeCrossover(TracingSymbolicExpressionTreeCrossover original, Cloner cloner) : base(original, cloner) { } 97 protected TracingSymbolicExpressionTreeCrossover(bool deserializing) 98 : base(deserializing) { 99 } 100 101 protected TracingSymbolicExpressionTreeCrossover(TracingSymbolicExpressionTreeCrossover original, Cloner cloner) 102 : base(original, cloner) { 103 } 104 75 105 protected TracingSymbolicExpressionTreeCrossover() 76 106 : base() { 77 Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed.")); 78 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover.")); 79 Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection).")); 80 Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName, "A global cache containing tracing info.")); 107 Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName, 108 "The parent symbolic expression trees which should be crossed.")); 109 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(ChildParameterName, 110 "The child symbolic expression tree resulting from the crossover.")); 111 Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, 112 "A global map keeping track of trees and their clones (made during selection).")); 113 Parameters.Add(new LookupParameter<CloneMapType>(GlobalFragmentMapParameterName, 114 "A global map keeping track of tree fragments received via crossover.")); 115 Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName, 116 "A global cache containing tracing info.")); 81 117 } 82 118 … … 85 121 throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators."); 86 122 // add global trace cache if not already present in global scope 87 if (GlobalTraceMap == null) {88 var gScope = ExecutionContext.Scope;89 while (gScope.Parent != null) gScope = gScope.Parent;123 var gScope = ExecutionContext.Scope; 124 while (gScope.Parent != null) gScope = gScope.Parent; 125 if (GlobalTraceMap == null) 90 126 gScope.Variables.Add(new Variable(GlobalTraceMapParameterName, new TraceMapType())); 91 } 127 if (GlobalFragmentMap == null) 128 gScope.Variables.Add(new Variable(GlobalFragmentMapParameterName, new CloneMapType())); 129 // get original parents 92 130 var originalParents = new ItemList<IItem>(Parents.Select(x => GlobalCloneMap[x])); 93 ISymbolicExpressionTree result = Crossover(Random, Parents[0], Parents[1]); 94 Child = result; 95 GlobalTraceMap.Add(Child, originalParents); 96 131 // save the nodes of parent0 in a list so we can track what modifications are made by crossover 132 var nodes0 = Parents[0].IterateNodes().ToList(); 133 //perform crossover 134 Child = Crossover(Random, Parents[0], Parents[1]); 135 // create another list of parent0's nodes, so we can compare it with the first list to see what changed 136 var nodes1 = Child.IterateNodes().ToList(); 137 // compare the two nodes lists and check the difference (comparing node references so we avoid false functional identity). 138 // if no difference is found, then it is assumed that the whole tree was swapped with itself (it can happen), so the index is 0 139 int i, min = Math.Max(nodes0.Count, nodes1.Count); 140 for (i = 0; i != min; ++i) 141 if (nodes0[i] != nodes1[i]) break; 142 if (i == min) i = 0; 143 GlobalTraceMap[Child] = originalParents; 144 GlobalFragmentMap[Child] = new IntValue(i); 97 145 return base.Apply(); 98 146 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj.user
r7785 r7788 7 7 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'"> 8 8 <StartAction>Program</StartAction> 9 <StartProgram>C:\Users\ Bogdan\Desktop\Trunk\sources\bin\HeuristicLab 3.3.exe</StartProgram>9 <StartProgram>C:\Users\bburlacu\Desktop\HL-Core\trunk\sources\bin\HeuristicLab 3.3.exe</StartProgram> 10 10 </PropertyGroup> 11 11 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'"> -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Plugin.cs
r7785 r7788 26 26 27 27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 28 [Plugin("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding","Provides operators and related classes for the symbolic expression tree encoding.", "3.4.2.77 79")]28 [Plugin("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding","Provides operators and related classes for the symbolic expression tree encoding.", "3.4.2.7785")] 29 29 [PluginFile("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.dll", PluginFileType.Assembly)] 30 30 [PluginDependency("HeuristicLab.Analysis", "3.3")] -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeMatching.cs
r7785 r7788 92 92 // convenience methods for less typing :) 93 93 private static IEnumerator<ISymbolicExpressionTreeNode> Enumerator(this ISymbolicExpressionTree tree) { 94 return tree.IterateNodes Postfix().GetEnumerator();94 return tree.IterateNodes().GetEnumerator(); 95 95 } 96 96 private static IEnumerator<ISymbolicExpressionTreeNode> Enumerator(this ISymbolicExpressionTreeNode tree) { 97 return tree.IterateNodes Postfix().GetEnumerator();97 return tree.IterateNodes().GetEnumerator(); 98 98 } 99 99 // method for breath-width iteration of nodes 100 p rivatestatic IEnumerable<ISymbolicExpressionTreeNode> IterateNodes(this ISymbolicExpressionTree tree) {100 public static IEnumerable<ISymbolicExpressionTreeNode> IterateNodes(this ISymbolicExpressionTree tree) { 101 101 return IterateNodes(tree.Root); 102 102 } … … 128 128 /// <returns>A symbolic expression tree node representing the difference fragment between parent and child</returns> 129 129 public static ISymbolicExpressionTreeNode GetFragmentDiff(ISymbolicExpressionTree parent, ISymbolicExpressionTree child) { 130 var e1 = parent.Enumerator(); 131 var e2 = child.Enumerator(); 130 var nodes1 = parent.IterateNodes().ToList(); 131 var nodes2 = child.IterateNodes().ToList(); 132 var e1 = nodes1.AsEnumerable().GetEnumerator(); 133 var e2 = nodes2.AsEnumerable().GetEnumerator(); 132 134 while (e1.MoveNext() && e2.MoveNext()) { 133 135 var comparer = new SymbolicExpressionTreeNodeComparer((int)SimilarityLevel.Exact); 134 if (!comparer.Equals(e1.Current, e2.Current)) { return e2.Current; } // return the fragment by which child differs from parent 136 if (!comparer.Equals(e1.Current, e2.Current)) { 137 return e2.Current; 138 } 139 //if (!e1.Current.IterateNodes().SequenceEqual(e2.Current.IterateNodes(), comparer)) { 140 // return e2.Current; 141 //} // return the fragment by which child differs from parent 135 142 } 136 143 return null; … … 145 152 var comparer = new SymbolicExpressionTreeNodeComparer(mode); 146 153 if (patlen == seqlen) return seq.SequenceEqual(pat, comparer) ? 0 : -1; 147 //int i = patlen;148 //while (i <= seqlen) {149 // var ch = seq[i - 1];150 // if (comparer.Equals(ch, pat.Last()))151 // if (seq.Skip(i - patlen).Take(patlen).SequenceEqual(pat, comparer))152 // return i - patlen;153 // ++i;154 //}155 154 for (int i = patlen; i <= seqlen; ++i) { 156 155 if (comparer.Equals(seq[i - 1], pat.Last())) // first check if last element is a match
Note: See TracChangeset
for help on using the changeset viewer.