Changeset 1286 for trunk/sources
- Timestamp:
- 03/08/09 11:04:49 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.GP
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP/HeuristicLab.GP.csproj
r852 r1286 123 123 <Name>HeuristicLab.Data</Name> 124 124 </ProjectReference> 125 <ProjectReference Include="..\HeuristicLab.Evolutionary\HeuristicLab.Evolutionary.csproj"> 126 <Project>{F5614C53-153C-4A37-A608-121E1C087F07}</Project> 127 <Name>HeuristicLab.Evolutionary</Name> 128 </ProjectReference> 125 129 <ProjectReference Include="..\HeuristicLab.Operators\HeuristicLab.Operators.csproj"> 126 130 <Project>{A9983BA2-B3B2-475E-8E2C-62050B71D1C5}</Project> -
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 } -
trunk/sources/HeuristicLab.GP/Recombination/OnePointCrossOver.cs
r1197 r1286 49 49 } 50 50 51 internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwisterrandom, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) {51 internal override IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) { 52 52 List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>(); 53 53 GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0.Size, allowedCrossOverPoints); -
trunk/sources/HeuristicLab.GP/Recombination/SizeConstrictedGPCrossoverBase.cs
r1212 r1286 42 42 } 43 43 44 internal override IFunctionTree Cross(IScope scope, TreeGardener gardener, MersenneTwisterrandom, IFunctionTree tree0, IFunctionTree tree1) {44 internal override IFunctionTree Cross(IScope scope, TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1) { 45 45 int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data; 46 46 int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data; … … 62 62 } 63 63 64 internal abstract IFunctionTree Cross(TreeGardener gardener, MersenneTwisterrandom, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight);64 internal abstract IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight); 65 65 66 private IFunctionTree TakeNextParent(IScope scope) {67 IFunctionTree parent = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[0], false);68 scope.RemoveSubScope(scope.SubScopes[0]);69 return parent;70 }66 //private IFunctionTree TakeNextParent(IScope scope) { 67 // IFunctionTree parent = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[0], false); 68 // scope.RemoveSubScope(scope.SubScopes[0]); 69 // return parent; 70 //} 71 71 } 72 72 } -
trunk/sources/HeuristicLab.GP/Recombination/SizeFairCrossOver.cs
r1212 r1286 47 47 } 48 48 49 internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwisterrandom, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) {49 internal override IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) { 50 50 int tries = 0; 51 51 IFunctionTree insertedBranch = null; … … 56 56 while (parent == null) parent = gardener.GetRandomParentNode(tree0); 57 57 removedBranchIndex = random.Next(parent.SubTrees.Count); 58 insertedBranch = GetReplacementBranch( random, gardener, tree0, parent, removedBranchIndex, tree1, maxTreeSize, maxTreeHeight);58 insertedBranch = GetReplacementBranch((MersenneTwister)random, gardener, tree0, parent, removedBranchIndex, tree1, maxTreeSize, maxTreeHeight); 59 59 } while (insertedBranch == null && tries++ < MaxRecombinationTries); 60 60 -
trunk/sources/HeuristicLab.GP/Recombination/StandardCrossOver.cs
r1197 r1286 44 44 } 45 45 46 internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwisterrandom, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) {46 internal override IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) { 47 47 int tries = 0; 48 48 List<IFunctionTree> allowedCrossoverPoints = null; -
trunk/sources/HeuristicLab.GP/Recombination/UniformCrossover.cs
r1197 r1286 52 52 } 53 53 54 internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwisterrandom, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) {54 internal override IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) { 55 55 List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>(); 56 56 GetCrossOverPoints(gardener, tree0, tree1, allowedCrossOverPoints);
Note: See TracChangeset
for help on using the changeset viewer.