- Timestamp:
- 03/08/09 11:04:49 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP/Recombination/GPCrossoverBase.cs
r835 r1286 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.Evolutionary; 32 33 33 34 namespace HeuristicLab.GP { 34 public abstract class GPCrossoverBase : OperatorBase {35 public abstract class GPCrossoverBase : CrossoverBase { 35 36 public GPCrossoverBase() 36 37 : base() { 37 AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(MersenneTwister), VariableKind.In));38 38 AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In)); 39 39 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New)); … … 42 42 } 43 43 44 public override IOperation Apply(IScope scope) { 45 MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true); 44 internal abstract IFunctionTree Cross(IScope scope, TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1); 45 46 protected override void Cross(IScope scope, IRandom random) { 46 47 GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true); 47 48 TreeGardener gardener = new TreeGardener(random, opLibrary); 48 49 49 if ( (scope.SubScopes.Count % 2) != 0)50 throw new InvalidOperationException("Number of parents is not even");50 if (scope.SubScopes.Count != 2) 51 throw new InvalidOperationException("Number of parents must be exactly two."); 51 52 52 int children = scope.SubScopes.Count / 2; 53 for (int i = 0; i < children; i++) { 54 IFunctionTree parent0 = TakeNextParent(scope); 55 IFunctionTree parent1 = TakeNextParent(scope); 53 IFunctionTree parent0 = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[0], false); 54 IFunctionTree parent1 = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[1], false); 56 55 57 // randomly swap parents to remove a possible bias from selection (e.g. when using gender-specific selection) 58 if (random.NextDouble() < 0.5) { 59 IFunctionTree tmp = parent0; 60 parent0 = parent1; 61 parent1 = tmp; 62 } 63 64 IFunctionTree child = Cross(scope, gardener, random, parent0, parent1); 65 Debug.Assert(gardener.IsValidTree(child)); 66 IScope childScope = new Scope(i.ToString()); 67 childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), child)); 68 childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(child.Size))); 69 childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(child.Height))); 70 scope.AddSubScope(childScope); 56 // randomly swap parents to remove a possible bias from selection (e.g. when using gender-specific selection) 57 if (random.NextDouble() < 0.5) { 58 IFunctionTree tmp = parent0; 59 parent0 = parent1; 60 parent1 = tmp; 71 61 } 72 62 73 return null; 74 } 75 76 internal abstract IFunctionTree Cross(IScope scope, TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1); 77 78 private IFunctionTree TakeNextParent(IScope scope) { 79 IFunctionTree parent = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[0], false); 80 scope.RemoveSubScope(scope.SubScopes[0]); 81 return parent; 63 IFunctionTree child = Cross(scope, gardener, random, parent0, parent1); 64 Debug.Assert(gardener.IsValidTree(child)); 65 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), child)); 66 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(child.Size))); 67 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(child.Height))); 82 68 } 83 69 }
Note: See TracChangeset
for help on using the changeset viewer.