Free cookie consent management tool by TermsFeed Policy Generator

Changeset 111


Ignore:
Timestamp:
03/28/08 14:28:10 (16 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)

Location:
trunk/sources/HeuristicLab.RealVector
Files:
2 added
1 edited
4 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  }
  • trunk/sources/HeuristicLab.RealVector/HeuristicLab.RealVector.csproj

    r105 r111  
    4747    <Compile Include="BoundsChecker.cs" />
    4848    <Compile Include="DiscreteCrossover.cs" />
     49    <Compile Include="DiscreteMultiCrossover.cs" />
    4950    <Compile Include="HeuristicCrossover.cs" />
    50     <Compile Include="IntermediateRecombination.cs" />
     51    <Compile Include="IntermediateMultiCrossover.cs" />
    5152    <Compile Include="MichalewiczNonUniformAllPositionsManipulator.cs" />
    5253    <Compile Include="MichalewiczNonUniformOnePositionManipulator.cs" />
    5354    <Compile Include="RandomConvexCrossover.cs" />
    5455    <Compile Include="LocalCrossover.cs" />
    55     <Compile Include="DiscreteRecombination.cs" />
    56     <Compile Include="SelfAdaptiveDiscreteRecombination.cs" />
    57     <Compile Include="SelfAdaptiveIntermediateRecombination.cs" />
     56    <Compile Include="RealVectorMultiCrossoverBase.cs" />
     57    <Compile Include="RealVectorSelfAdaptiveMultiCrossoverBase.cs" />
     58    <Compile Include="SelfAdaptiveDiscreteMultiCrossover.cs" />
     59    <Compile Include="SelfAdaptiveIntermediateMultiCrossover.cs" />
    5860    <Compile Include="SelfAdaptiveNormalAllPositionsManipulator.cs" />
    5961    <Compile Include="UniformAllPositionsManipulator.cs" />
  • trunk/sources/HeuristicLab.RealVector/IntermediateMultiCrossover.cs

    r110 r111  
    2727
    2828namespace HeuristicLab.RealVector {
    29   public class IntermediateRecombination : OperatorBase {
     29  public class IntermediateMultiCrossover : RealVectorMultiCrossoverBase {
    3030    public override string Description {
    3131      get {
    32         return @"Intermediate recombination creates a new offspring by computing the centroid of a number of parents";
     32        return @"This creates a new offspring by computing the centroid of a number of parents";
    3333      }
    3434    }
    3535
    36     public IntermediateRecombination()
    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(IList<double[]> parents) {
     37      int length = parents[0].Length;
     38      double[] result = new double[length];
     39      for (int i = 0; i < length; i++) {
     40        double sum = 0.0;
     41        for (int j = 0; j < parents.Count; j++)
     42          sum += parents[j][i];
     43        result[i] = sum / parents.Count;
     44      }
     45      return result;
    4146    }
    4247
    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 intermediate recombination
    64         for (int x = 0; x < childGene.Length; x++) {
    65           double sum = 0.0;
    66           for (int y = 0; y < rho; y++) {
    67             sum += parents[y][x];
    68           }
    69           childGene[x] = sum / rho;
    70         }
    71         childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("RealVector"), new DoubleArrayData(childGene)));
    72         scope.AddSubScope(childScope);
    73       }
    74       return null;
     48    protected override double[] Cross(IScope scope, IRandom random, IList<double[]> parents) {
     49      return Apply(parents);
    7550    }
    7651  }
  • trunk/sources/HeuristicLab.RealVector/SelfAdaptiveDiscreteMultiCrossover.cs

    r110 r111  
    2727
    2828namespace HeuristicLab.RealVector {
    29   public class SelfAdaptiveDiscreteRecombination : DiscreteRecombination {
     29  public class SelfAdaptiveDiscreteMultiCrossover : RealVectorSelfAdaptiveMultiCrossoverBase {
    3030    public override string Description {
    3131      get {
    32         return @"Self adaptive Discrete/dominant recombination creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent. It will also use the same strategy to combine the endogenous strategy parameter vector.";
     32        return @"This creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent. It will also use the same strategy to combine the endogenous strategy parameter vector.";
    3333      }
    3434    }
    3535
    36     public SelfAdaptiveDiscreteRecombination()
    37       : base() {
    38       AddVariableInfo(new VariableInfo("StrategyVector", "Vector containing the endogenous strategy parameters", typeof(DoubleArrayData), VariableKind.In));
     36    public static void Apply(IRandom random, IList<double[]> parents, IList<double[]> strategyParametersList, out double[] childIndividual, out double[] strategyParameters) {
     37      childIndividual = new double[parents[0].Length];
     38      strategyParameters = new double[strategyParametersList[0].Length];
     39      try {
     40        for (int i = 0; i < childIndividual.Length; i++) {
     41          int nextParent = random.Next(parents.Count);
     42          childIndividual[i] = parents[nextParent][i];
     43          strategyParameters[i] = strategyParametersList[nextParent][i];
     44        }
     45      } catch (IndexOutOfRangeException) {
     46        throw new InvalidOperationException("Cannot apply self adaptive multicrossover to real vectors of different length.");
     47      }
    3948    }
    4049
    41     public override IOperation Apply(IScope scope) {
    42       int rho = GetVariableValue<IntData>("Rho", scope, true).Data;
    43       // with just 1 parent no recombination is necessary/possible
    44       if (rho == 1) return null;
    45       IRandom random = GetVariableValue<IRandom>("Random", scope, true);
    46 
    47       if (scope.SubScopes.Count % rho != 0)
    48         throw new InvalidOperationException("Number of parents is not a multiple of rho");
    49       int lambda = scope.SubScopes.Count / rho;
    50       IList<double[]> parents = new List<double[]>(rho);
    51       IList<double[]> parentsStrategy = new List<double[]>(rho);
    52 
    53       for (int i = 0; i < lambda; i++) {
    54         IScope childScope = new Scope(i.ToString());
    55         double[] childGene = (double[])scope.SubScopes[0].GetVariableValue<DoubleArrayData>("RealVector", false).Data.Clone();
    56         double[] strategyParams = (double[])scope.SubScopes[0].GetVariableValue<DoubleArrayData>("StrategyVector", 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           parentsStrategy.Add(parent.GetVariableValue<DoubleArrayData>("StrategyVector", false).Data);
    62           scope.RemoveSubScope(parent);
    63         }
    64         // actual discrete recombination
    65         if (childGene.Length != strategyParams.Length)
    66           throw new InvalidOperationException("ERROR: strategy vector must be as long as there are dimensions");
    67 
    68         for (int x = 0; x < childGene.Length; x++) {
    69           int nextParent = random.Next(rho);
    70           childGene[x] = parents[nextParent][x];
    71           strategyParams[x] = parentsStrategy[nextParent][x];
    72         }
    73         childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("RealVector"), new DoubleArrayData(childGene)));
    74         childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("StrategyVector"), new DoubleArrayData(strategyParams)));
    75         scope.AddSubScope(childScope);
    76       }
    77       return null;
     50    protected override void Cross(IScope scope, IRandom random, IList<double[]> parents, IList<double[]> strategyParametersList, out double[] childIndividual, out double[] strategyParameters) {
     51      Apply(random, parents, strategyParametersList, out childIndividual, out strategyParameters);
    7852    }
    7953  }
  • trunk/sources/HeuristicLab.RealVector/SelfAdaptiveIntermediateMultiCrossover.cs

    r110 r111  
    2727
    2828namespace HeuristicLab.RealVector {
    29   public class SelfAdaptiveIntermediateRecombination : DiscreteRecombination {
     29  public class SelfAdaptiveIntermediateMultiCrossover : RealVectorSelfAdaptiveMultiCrossoverBase {
    3030    public override string Description {
    3131      get {
    32         return @"Self adaptive intermediate recombination creates a new offspring by computing the centroid of the parents. It will also use the same strategy to combine the endogenous strategy parameter vector.";
     32        return @"This creates a new offspring by computing the centroid of the parents. It will also use the same strategy to combine the endogenous strategy parameter vector.";
    3333      }
    3434    }
    3535
    36     public SelfAdaptiveIntermediateRecombination()
    37       : base() {
    38       AddVariableInfo(new VariableInfo("StrategyVector", "Vector containing the endogenous strategy parameters", typeof(DoubleArrayData), VariableKind.In));
     36    public static void Apply(IList<double[]> parents, IList<double[]> strategyParametersList, out double[] childIndividual, out double[] strategyParameters) {
     37      childIndividual = IntermediateMultiCrossover.Apply(parents);
     38      strategyParameters = IntermediateMultiCrossover.Apply(strategyParametersList);
    3939    }
    4040
    41     public override IOperation Apply(IScope scope) {
    42       int rho = GetVariableValue<IntData>("Rho", scope, true).Data;
    43       // with just 1 parent no recombination is necessary/possible
    44       if (rho == 1) return null;
    45       IRandom random = GetVariableValue<IRandom>("Random", scope, true);
    46 
    47       if (scope.SubScopes.Count % rho != 0)
    48         throw new InvalidOperationException("Number of parents is not a multiple of rho");
    49       int lambda = scope.SubScopes.Count / rho;
    50       IList<double[]> parents = new List<double[]>(rho);
    51       IList<double[]> parentsStrategy = new List<double[]>(rho);
    52 
    53       for (int i = 0; i < lambda; i++) {
    54         IScope childScope = new Scope(i.ToString());
    55         double[] childGene = (double[])scope.SubScopes[0].GetVariableValue<DoubleArrayData>("RealVector", false).Data.Clone();
    56         double[] strategyParams = (double[])scope.SubScopes[0].GetVariableValue<DoubleArrayData>("StrategyVector", 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           parentsStrategy.Add(parent.GetVariableValue<DoubleArrayData>("StrategyVector", false).Data);
    62           scope.RemoveSubScope(parent);
    63         }
    64         // actual discrete recombination
    65         if (childGene.Length != strategyParams.Length)
    66           throw new InvalidOperationException("ERROR: strategy vector must be as long as there are dimensions");
    67 
    68         for (int x = 0; x < childGene.Length; x++) {
    69           double sum = 0.0, sumStrategy = 0.0;
    70           for (int y = 0; y < rho; y++) {
    71             sum += parents[y][x];
    72             sumStrategy += parentsStrategy[y][x];
    73           }
    74           childGene[x] = sum / rho;
    75           strategyParams[x] = sumStrategy / rho;
    76         }
    77         childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("RealVector"), new DoubleArrayData(childGene)));
    78         childScope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("StrategyVector"), new DoubleArrayData(strategyParams)));
    79         scope.AddSubScope(childScope);
    80       }
    81       return null;
     41    protected override void Cross(IScope scope, IRandom random, IList<double[]> parents, IList<double[]> strategyParametersList, out double[] childIndividual, out double[] strategyParameters) {
     42      Apply(parents, strategyParametersList, out childIndividual, out strategyParameters);
    8243    }
    8344  }
Note: See TracChangeset for help on using the changeset viewer.