Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/28/08 14:28:10 (17 years ago)
Author:
abeham
Message:

Renamed Recombination operators to MultCrossover operators and added the necessary base classes (ticket #89)
Created a static Apply method that encapsulates the functionality of the operators (ticket #88)

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.RealVector/DiscreteMultiCrossover.cs

    r110 r111  
    2727
    2828namespace HeuristicLab.RealVector {
    29   public class DiscreteRecombination : OperatorBase {
     29  public class DiscreteMultiCrossover : RealVectorMultiCrossoverBase {
    3030    public override string Description {
    3131      get {
    32         return @"Discrete/dominant recombination creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent";
     32        return @"This creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent";
    3333      }
    3434    }
    3535
    36     public DiscreteRecombination()
    37       : base() {
    38       AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
    39       AddVariableInfo(new VariableInfo("Rho", "Amount of parents to recombine", typeof(IntData), VariableKind.In));
    40       AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
     36    public static double[] Apply(IRandom random, IList<double[]> parents) {
     37      int length = parents[0].Length;
     38      double[] result = new double[length];
     39      try {
     40        for (int i = 0; i < length; i++) {
     41          result[i] = parents[random.Next(parents.Count)][i];
     42        }
     43      } catch (IndexOutOfRangeException) {
     44        throw new InvalidOperationException("Cannot apply multicrossover to real vectors of different length.");
     45      }
     46      return result;
    4147    }
    4248
    43     public override IOperation Apply(IScope scope) {
    44       int rho = GetVariableValue<IntData>("Rho", scope, true).Data;
    45       // with just 1 parent no recombination is necessary/possible
    46       if (rho == 1) return null;
    47       IRandom random = GetVariableValue<IRandom>("Random", scope, true);
    48 
    49       if (scope.SubScopes.Count % rho != 0)
    50         throw new InvalidOperationException("Number of parents is not a multiple of rho");
    51       int lambda = scope.SubScopes.Count / rho;
    52       IList<double[]> parents = new List<double[]>(rho);
    53 
    54       for (int i = 0; i < lambda; i++) {
    55         IScope childScope = new Scope(i.ToString());
    56         double[] childGene = (double[])scope.SubScopes[0].GetVariableValue<DoubleArrayData>("RealVector", false).Data.Clone();
    57         parents.Clear();
    58         for (int j = 0; j < rho; j++) {
    59           IScope parent = scope.SubScopes[0];
    60           parents.Add(parent.GetVariableValue<DoubleArrayData>("RealVector", false).Data);
    61           scope.RemoveSubScope(parent);
    62         }
    63         // actual discrete recombination
    64         for (int x = 0; x < childGene.Length; x++) {
    65           childGene[x] = parents[random.Next(rho)][x];
    66         }
    67         childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("RealVector"), new DoubleArrayData(childGene)));
    68         scope.AddSubScope(childScope);
    69       }
    70       return null;
     49    protected override double[] Cross(IScope scope, IRandom random, IList<double[]> parents) {
     50      return Apply(random, parents);
    7151    }
    7252  }
Note: See TracChangeset for help on using the changeset viewer.