Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/08/09 11:04:49 (15 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)).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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  }
Note: See TracChangeset for help on using the changeset viewer.