Changeset 807
- Timestamp:
- 11/23/08 19:29:26 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP/Recombination/UniformCrossover.cs
r803 r807 62 62 CompositeOperation initOperations = new CompositeOperation(); 63 63 64 int c hildren= scope.SubScopes.Count / 2;65 for (int i = 0; i < c hildren; i++) {64 int crossoverEvents = scope.SubScopes.Count / 2; 65 for (int i = 0; i < crossoverEvents; i++) { 66 66 IScope parent1 = scope.SubScopes[0]; 67 67 scope.RemoveSubScope(parent1); 68 68 IScope parent2 = scope.SubScopes[0]; 69 69 scope.RemoveSubScope(parent2); 70 IScope child = new Scope(i.ToString()); 71 IOperation childInitOperation = Cross(scope, random, gardener, parent1, parent2, child); 72 initOperations.AddOperation(childInitOperation); 73 scope.AddSubScope(child); 74 } 75 76 return initOperations; 77 } 78 79 private IOperation Cross(IScope scope, MersenneTwister random, TreeGardener gardener, IScope parent1, IScope parent2, IScope child) { 80 IFunctionTree newTree = Cross(random, gardener, parent1, parent2); 81 Debug.Assert(gardener.IsValidTree(newTree)); 82 int newTreeSize = newTree.Size; 83 int newTreeHeight = newTree.Height; 84 child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), newTree)); 85 child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(newTreeSize))); 86 child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(newTreeHeight))); 70 IScope child0 = new Scope((i*2).ToString()); 71 IScope child1 = new Scope((i*2+1).ToString()); 72 Cross(scope, random, gardener, parent1, parent2, child0, child1); 73 scope.AddSubScope(child0); 74 scope.AddSubScope(child1); 75 } 87 76 88 77 return null; 89 78 } 90 79 91 92 private IFunctionTree Cross(MersenneTwister random, TreeGardener gardener, IScope f, IScope g) { 80 private void Cross(IScope scope, MersenneTwister random, TreeGardener gardener, IScope parent1, IScope parent2, IScope child0, IScope child1) { 81 IFunctionTree childTree0; 82 IFunctionTree childTree1; 83 Cross(random, gardener, parent1, parent2, out childTree0, out childTree1); 84 Debug.Assert(gardener.IsValidTree(childTree0)); 85 Debug.Assert(gardener.IsValidTree(childTree1)); 86 child0.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), childTree0)); 87 child0.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(childTree0.Size))); 88 child0.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(childTree0.Height))); 89 90 child1.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), childTree1)); 91 child1.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(childTree1.Size))); 92 child1.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(childTree1.Height))); 93 } 94 95 96 private void Cross(MersenneTwister random, TreeGardener gardener, IScope f, IScope g, out IFunctionTree child0, out IFunctionTree child1) { 93 97 IFunctionTree tree0 = f.GetVariableValue<IFunctionTree>("FunctionTree", false); 94 98 int tree0Height = f.GetVariableValue<IntData>("TreeHeight", false).Data; … … 155 159 } 156 160 } 157 if (random.NextDouble() < 0.5) return tree0; else return tree1; 161 if (random.NextDouble() < 0.5) { 162 child0 = tree0; 163 child1 = tree1; 164 } else { 165 child0 = tree1; 166 child1 = tree0; 167 } 158 168 } 159 169
Note: See TracChangeset
for help on using the changeset viewer.