Changeset 1286


Ignore:
Timestamp:
03/08/09 11:04:49 (10 years ago)
Author:
gkronber
Message:

Adapted GP crossover operators to match the new design for crossover operators. #512 (GP crossover operators should be changed to match the new design of crossover operators (see #475)).

Location:
trunk/sources/HeuristicLab.GP
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP/HeuristicLab.GP.csproj

    r852 r1286  
    123123      <Name>HeuristicLab.Data</Name>
    124124    </ProjectReference>
     125    <ProjectReference Include="..\HeuristicLab.Evolutionary\HeuristicLab.Evolutionary.csproj">
     126      <Project>{F5614C53-153C-4A37-A608-121E1C087F07}</Project>
     127      <Name>HeuristicLab.Evolutionary</Name>
     128    </ProjectReference>
    125129    <ProjectReference Include="..\HeuristicLab.Operators\HeuristicLab.Operators.csproj">
    126130      <Project>{A9983BA2-B3B2-475E-8E2C-62050B71D1C5}</Project>
  • trunk/sources/HeuristicLab.GP/Recombination/GPCrossoverBase.cs

    r835 r1286  
    3030using HeuristicLab.Constraints;
    3131using System.Diagnostics;
     32using HeuristicLab.Evolutionary;
    3233
    3334namespace HeuristicLab.GP {
    34   public abstract class GPCrossoverBase : OperatorBase {
     35  public abstract class GPCrossoverBase : CrossoverBase {
    3536    public GPCrossoverBase()
    3637      : base() {
    37       AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(MersenneTwister), VariableKind.In));
    3838      AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
    3939      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New));
     
    4242    }
    4343
    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) {
    4647      GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
    4748      TreeGardener gardener = new TreeGardener(random, opLibrary);
    4849
    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.");
    5152
    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);
    5655
    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;
    7161      }
    7262
    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)));
    8268    }
    8369  }
  • trunk/sources/HeuristicLab.GP/Recombination/OnePointCrossOver.cs

    r1197 r1286  
    4949    }
    5050
    51     internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwister random, 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) {
    5252      List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>();
    5353      GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0.Size, allowedCrossOverPoints);
  • trunk/sources/HeuristicLab.GP/Recombination/SizeConstrictedGPCrossoverBase.cs

    r1212 r1286  
    4242    }
    4343
    44     internal override IFunctionTree Cross(IScope scope, TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1) {
     44    internal override IFunctionTree Cross(IScope scope, TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1) {
    4545      int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
    4646      int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
     
    6262    }
    6363
    64     internal abstract IFunctionTree Cross(TreeGardener gardener, MersenneTwister random, 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);
    6565
    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    //}
    7171  }
    7272}
  • trunk/sources/HeuristicLab.GP/Recombination/SizeFairCrossOver.cs

    r1212 r1286  
    4747    }
    4848
    49     internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwister random, 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) {
    5050      int tries = 0;
    5151      IFunctionTree insertedBranch = null;
     
    5656        while (parent == null) parent = gardener.GetRandomParentNode(tree0);
    5757        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);
    5959      } while (insertedBranch == null && tries++ < MaxRecombinationTries);
    6060
  • trunk/sources/HeuristicLab.GP/Recombination/StandardCrossOver.cs

    r1197 r1286  
    4444    }
    4545
    46     internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwister random, 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) {
    4747      int tries = 0;
    4848      List<IFunctionTree> allowedCrossoverPoints = null;
  • trunk/sources/HeuristicLab.GP/Recombination/UniformCrossover.cs

    r1197 r1286  
    5252    }
    5353
    54     internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwister random, 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) {
    5555      List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>();
    5656      GetCrossOverPoints(gardener, tree0, tree1, allowedCrossOverPoints);
Note: See TracChangeset for help on using the changeset viewer.