Changeset 707 for trunk/sources/HeuristicLab.GP/Recombination
- Timestamp:
- 11/02/08 22:28:35 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP/Recombination/OnePointCrossOver.cs
r666 r707 45 45 : base() { 46 46 AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(MersenneTwister), VariableKind.In)); 47 AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In)); 47 48 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New)); 48 49 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.New)); … … 52 53 public override IOperation Apply(IScope scope) { 53 54 MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true); 55 GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true); 56 TreeGardener gardener = new TreeGardener(random, opLibrary); 54 57 55 58 if((scope.SubScopes.Count % 2) != 0) … … 65 68 scope.RemoveSubScope(parent2); 66 69 IScope child = new Scope(i.ToString()); 67 IOperation childInitOperation = Cross(scope, random, parent1, parent2, child);70 IOperation childInitOperation = Cross(scope, random, gardener, parent1, parent2, child); 68 71 initOperations.AddOperation(childInitOperation); 69 72 scope.AddSubScope(child); … … 73 76 } 74 77 75 private IOperation Cross(IScope scope, MersenneTwister random, IScope parent1, IScope parent2, IScope child) {76 IFunctionTree newTree = Cross(random, parent1, parent2);78 private IOperation Cross(IScope scope, MersenneTwister random, TreeGardener gardener, IScope parent1, IScope parent2, IScope child) { 79 IFunctionTree newTree = Cross(random, gardener, parent1, parent2); 77 80 78 81 int newTreeSize = newTree.Size; … … 86 89 87 90 88 private IFunctionTree Cross(MersenneTwister random, IScope f, IScope g) {91 private IFunctionTree Cross(MersenneTwister random, TreeGardener gardener, IScope f, IScope g) { 89 92 IFunctionTree tree0 = f.GetVariableValue<IFunctionTree>("FunctionTree", false); 90 93 int tree0Height = f.GetVariableValue<IntData>("TreeHeight", false).Data; … … 95 98 int tree1Size = g.GetVariableValue<IntData>("TreeSize", false).Data; 96 99 97 List<IFunctionTree[]> allowedCrossOverPoints = GetCrossOverPoints( null, tree0, tree1);100 List<IFunctionTree[]> allowedCrossOverPoints = GetCrossOverPoints(gardener, null, tree0, tree1); 98 101 if(allowedCrossOverPoints.Count == 0) { 99 102 if(random.NextDouble() < 0.5) return tree0; else return tree1; … … 113 116 } 114 117 115 private List<IFunctionTree[]> GetCrossOverPoints( IFunctionTree parent, IFunctionTree tree0, IFunctionTree tree1) {118 private List<IFunctionTree[]> GetCrossOverPoints(TreeGardener gardener, IFunctionTree parent, IFunctionTree tree0, IFunctionTree tree1) { 116 119 List<IFunctionTree[]> results = new List<IFunctionTree[]>(); 117 120 if(tree0.SubTrees.Count != tree1.SubTrees.Count) return results; 118 121 // invariant arity - number of subtrees is equal in both trees 119 120 results.Add(new IFunctionTree[] { parent, tree0, tree1 });121 122 for(int i = 0; i < tree0.SubTrees.Count; i++) { 122 results.AddRange(GetCrossOverPoints(tree0, tree0.SubTrees[i], tree1.SubTrees[i])); 123 if(gardener.GetAllowedSubFunctions(tree0.Function, i).Contains(tree1.SubTrees[i].Function)) { 124 results.Add(new IFunctionTree[] { tree0, tree0.SubTrees[i], tree1.SubTrees[i]}); 125 } 126 results.AddRange(GetCrossOverPoints(gardener, tree0, tree0.SubTrees[i], tree1.SubTrees[i])); 123 127 } 124 128 return results;
Note: See TracChangeset
for help on using the changeset viewer.