- Timestamp:
- 07/29/09 18:28:45 (15 years ago)
- Location:
- branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/GPCrossoverBase.cs
r2202 r2210 31 31 using System.Diagnostics; 32 32 using HeuristicLab.Evolutionary; 33 using HeuristicLab.GP.Interfaces; 33 34 34 35 namespace HeuristicLab.GP { … … 36 37 public GPCrossoverBase() 37 38 : base() { 38 AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In)); 39 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New)); 40 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New)); 41 AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New)); 39 AddVariableInfo(new VariableInfo("FunctionLibrary", "The operator library containing all available operators", typeof(FunctionLibrary), VariableKind.In)); 40 AddVariableInfo(new VariableInfo("FunctionTree", "The tree to crossover", typeof(IGeneticProgrammingModel), VariableKind.In | VariableKind.New)); 42 41 } 43 42 … … 45 44 46 45 protected override void Cross(IScope scope, IRandom random) { 47 FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>(" OperatorLibrary", scope, true);46 FunctionLibrary opLibrary = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true); 48 47 TreeGardener gardener = new TreeGardener(random, opLibrary); 49 48 … … 51 50 throw new InvalidOperationException("Number of parents must be exactly two."); 52 51 53 I FunctionTree parent0 = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[0], false);54 I FunctionTree parent1 = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[1], false);52 IGeneticProgrammingModel parent0 = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope.SubScopes[0], false); 53 IGeneticProgrammingModel parent1 = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope.SubScopes[1], false); 55 54 56 55 // randomly swap parents to remove a possible bias from selection (e.g. when using gender-specific selection) 57 56 if (random.NextDouble() < 0.5) { 58 I FunctionTreetmp = parent0;57 IGeneticProgrammingModel tmp = parent0; 59 58 parent0 = parent1; 60 59 parent1 = tmp; 61 60 } 62 61 63 IFunctionTree child = Cross(scope, gardener, random, parent0 , parent1);62 IFunctionTree child = Cross(scope, gardener, random, parent0.FunctionTree, parent1.FunctionTree); 64 63 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))); 64 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), new GeneticProgrammingModel(child))); 68 65 } 69 66 } -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/LangdonHomologousCrossOver.cs
r835 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/OnePointCrossOver.cs
r1286 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { … … 51 52 internal override IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) { 52 53 List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>(); 53 GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0. Size, allowedCrossOverPoints);54 GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0.GetSize(), allowedCrossOverPoints); 54 55 if (allowedCrossOverPoints.Count > 0) { 55 56 CrossoverPoint crossOverPoint = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)]; … … 68 69 // if the current branch can be attached as a sub-tree to branch0 69 70 if (gardener.GetAllowedSubFunctions(branch0.Function, i).Contains(branch1.SubTrees[i].Function) && 70 branch1.SubTrees[i]. Size - branch0.SubTrees[i].Size<= maxNewNodes) {71 branch1.SubTrees[i].GetSize() - branch0.SubTrees[i].GetSize() <= maxNewNodes) { 71 72 CrossoverPoint p = new CrossoverPoint(); 72 73 p.childIndex = i; -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/SizeConstrictedGPCrossoverBase.cs
r1286 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { … … 58 59 newTree = Cross(gardener, random, (IFunctionTree)tree1.Clone(), (IFunctionTree)tree0.Clone(), maxTreeSize, maxTreeHeight); 59 60 } else newTree = tree0; 60 } while (newTree. Size > maxTreeSize || newTree.Height> maxTreeHeight);61 } while (newTree.GetSize() > maxTreeSize || newTree.GetHeight() > maxTreeHeight); 61 62 return newTree; 62 63 } 63 64 64 65 internal abstract IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight); 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 //}71 66 } 72 67 } -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/SizeFairCrossOver.cs
r2202 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { … … 69 70 private IFunctionTree GetReplacementBranch(MersenneTwister random, TreeGardener gardener, IFunctionTree intoTree, IFunctionTree parent, int replacedBranchIndex, IFunctionTree fromTree, int maxTreeSize, int maxTreeHeight) { 70 71 IList<IFunction> allowedFunctions = new List<IFunction>(gardener.GetAllowedSubFunctions(parent.Function, replacedBranchIndex)); 71 int removedBranchSize = parent.SubTrees[replacedBranchIndex]. Size;72 int maxBranchSize = maxTreeSize - (intoTree. Size- removedBranchSize);72 int removedBranchSize = parent.SubTrees[replacedBranchIndex].GetSize(); 73 int maxBranchSize = maxTreeSize - (intoTree.GetSize() - removedBranchSize); 73 74 int maxBranchHeight = maxTreeHeight - gardener.GetBranchLevel(intoTree, parent); // returns 1 if intoTree==parent and 2 if parent is a child of intoTree 74 75 List<int> replacedTrail = GetTrail(intoTree, parent); … … 84 85 double pEqualLength = equalLengthBranches.Count > 0 ? 1.0 / removedBranchSize : 0.0; 85 86 double pLonger; 86 if (parent. Size== maxTreeSize) {87 if (parent.GetSize() == maxTreeSize) { 87 88 pLonger = 0.0; 88 89 } else { … … 112 113 private void FindPossibleBranches(IFunctionTree tree, IList<IFunction> allowedFunctions, int maxBranchSize, int maxBranchHeight, int removedBranchSize, 113 114 List<CrossoverPoint> shorterBranches, List<CrossoverPoint> equalLengthBranches, List<CrossoverPoint> longerBranches, List<int> trail) { 114 int treeSize = tree. Size;115 if (allowedFunctions.Contains(tree.Function) && treeSize <= maxBranchSize && tree. Height<= maxBranchHeight) {115 int treeSize = tree.GetSize(); 116 if (allowedFunctions.Contains(tree.Function) && treeSize <= maxBranchSize && tree.GetHeight() <= maxBranchHeight) { 116 117 CrossoverPoint p = new CrossoverPoint(); 117 118 p.branchSize = treeSize; -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/StandardCrossOver.cs
r2202 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP { … … 57 58 58 59 // calculate the max size and height that the inserted branch can have 59 int maxInsertedBranchSize = maxTreeSize - (tree0. Size - parent0.SubTrees[replacedChildIndex].Size);60 int maxInsertedBranchSize = maxTreeSize - (tree0.GetSize() - parent0.SubTrees[replacedChildIndex].GetSize()); 60 61 int maxInsertedBranchHeight = maxTreeHeight - gardener.GetBranchLevel(tree0, parent0); // branchlevel is 1 if tree0==parent0 61 62 62 63 IList<IFunction> allowedFunctions = new List<IFunction>(gardener.GetAllowedSubFunctions(parent0.Function, replacedChildIndex)); 63 allowedCrossoverPoints = GetPossibleCrossoverPoints( gardener,tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions);64 allowedCrossoverPoints = GetPossibleCrossoverPoints(tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions); 64 65 } while (allowedCrossoverPoints.Count == 0 && tries++ < MaxRecombinationTries); 65 66 … … 74 75 } 75 76 76 private List<IFunctionTree> GetPossibleCrossoverPoints( TreeGardener gardener,IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) {77 private List<IFunctionTree> GetPossibleCrossoverPoints(IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) { 77 78 List<IFunctionTree> crossoverPoints = new List<IFunctionTree>(); 78 foreach (IFunctionTree possiblePoint in gardener.GetAllSubTrees(tree)) {79 if (allowedFunctions.Contains(possiblePoint.Function) && possiblePoint. Size <= maxInsertedBranchSize && possiblePoint.Height<= maxInsertedBranchHeight)79 foreach (IFunctionTree possiblePoint in TreeGardener.GetAllSubTrees(tree)) { 80 if (allowedFunctions.Contains(possiblePoint.Function) && possiblePoint.GetSize() <= maxInsertedBranchSize && possiblePoint.GetHeight() <= maxInsertedBranchHeight) 80 81 crossoverPoints.Add(possiblePoint); 81 82 } -
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Recombination/UniformCrossover.cs
r1286 r2210 30 30 using HeuristicLab.Constraints; 31 31 using System.Diagnostics; 32 using HeuristicLab.GP.Interfaces; 32 33 33 34 namespace HeuristicLab.GP {
Note: See TracChangeset
for help on using the changeset viewer.