Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2936


Ignore:
Timestamp:
03/05/10 13:14:26 (15 years ago)
Author:
svonolfe
Message:

Added heuristic, local, random convex crossover and added some initial unit tests for all RealVector operators (#890)

Location:
trunk/sources/HeuristicLab.Encodings.RealVector/3.3
Files:
12 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/DiscreteCrossover.cs

    r2928 r2936  
    4747    public static DoubleArrayData Apply(IRandom random, ItemArray<DoubleArrayData> parents) {
    4848      int length = parents[0].Length;
     49     
     50      for (int i = 0; i < parents.Length; i++) {
     51        if(parents[i].Length != length)
     52          throw new ArgumentException("DiscreteCrossover: The parents' vectors are of different length.", "parents");
     53      }
     54     
    4955      DoubleArrayData result = new DoubleArrayData(length);
    50       try {
    51         for (int i = 0; i < length; i++) {
    52           result[i] = parents[random.Next(parents.Length)][i];
    53         }
    54       } catch (IndexOutOfRangeException) {
    55         throw new ArgumentException("DiscreteCrossover: The parents' vectors are of different length.", "parents");
    56       }
     56      for (int i = 0; i < length; i++) {
     57        result[i] = parents[random.Next(parents.Length)][i];
     58      }       
     59     
    5760      return result;
    5861    }
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/HeuristicCrossover.cs

    r2900 r2936  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
    27 using HeuristicLab.Evolutionary;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Parameters;
    2829
    2930namespace HeuristicLab.Encodings.RealVector {
     
    3233  /// of the two parents times a randomly chosen factor.
    3334  /// </summary>
    34   public class HeuristicCrossover : RealVectorCrossoverBase {
     35  /// <remarks>
     36  /// It is implemented as described in Wright, A.H. (1994), Genetic algorithms for real parameter optimization, Foundations of Genetic Algorithms, G.J.E. Rawlins (Ed.), Morgan Kaufmann, San Mateo, CA, 205-218.
     37  /// </remarks>
     38  [Item("HeuristicCrossover", "Heuristic crossover for real vectors: Takes for each position the better parent and adds the difference. It is implemented as described in Wright, A.H. (1994), Genetic algorithms for real parameter optimization, Foundations of Genetic Algorithms, G.J.E. Rawlins (Ed.), Morgan Kaufmann, San Mateo, CA, 205-218.")]
     39  [EmptyStorableClass]
     40  public class HeuristicCrossover : RealVectorCrossover {
     41    /// <summary>
     42    /// Whether the problem is a maximization or minimization problem.
     43    /// </summary>
     44    public ValueLookupParameter<BoolData> MaximizationParameter {
     45      get { return (ValueLookupParameter<BoolData>)Parameters["Maximization"]; }
     46    }
     47    /// <summary>
     48    /// The quality of the parents.
     49    /// </summary>
     50    public SubScopesLookupParameter<DoubleData> QualityParameter {
     51      get { return (SubScopesLookupParameter<DoubleData>)Parameters["Quality"]; }
     52    }
     53
    3554    /// <summary>
    3655    /// Initializes a new instance of <see cref="HeuristicCrossover"/> with two variable infos
     
    3958    public HeuristicCrossover()
    4059      : base() {
    41       AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
    42       AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
    43     }
    44 
    45     /// <inheritdoc select="summary"/>
    46     public override string Description {
    47       get { return "Heuristic crossover for real vectors."; }
     60      Parameters.Add(new ValueLookupParameter<BoolData>("Maximization", "Whether the problem is a maximization problem or not."));
     61      Parameters.Add(new SubScopesLookupParameter<DoubleData>("Quality", "The quality values of the parents."));
    4862    }
    4963
     
    5165    /// Perfomrs a heuristic crossover on the two given parents.
    5266    /// </summary>
     67    /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception>
    5368    /// <param name="random">The random number generator.</param>
    54     /// <param name="maximization">Boolean flag whether it is a maximization problem.</param>
    55     /// <param name="parent1">The first parent for the crossover operation.</param>
    56     /// <param name="quality1">The quality of the first parent.</param>
    57     /// <param name="parent2">The second parent for the crossover operation.</param>
    58     /// <param name="quality2">The quality of the second parent.</param>
     69    /// <param name="betterParent">The first parent for the crossover operation.</param>
     70    /// <param name="worseParent">The second parent for the crossover operation.</param>
    5971    /// <returns>The newly created real vector, resulting from the heuristic crossover.</returns>
    60     public static double[] Apply(IRandom random, bool maximization, double[] parent1, double quality1, double[] parent2, double quality2) {
    61       int length = parent1.Length;
     72    public static DoubleArrayData Apply(IRandom random, DoubleArrayData betterParent, DoubleArrayData worseParent) {
     73      if (betterParent.Length != worseParent.Length)
     74        throw new ArgumentException("ERROR in HeuristicCrossover: the two parents are not of the same length");
     75     
     76      int length = betterParent.Length;
    6277      double[] result = new double[length];
    6378      double factor = random.NextDouble();
    6479
    6580      for (int i = 0; i < length; i++) {
    66         if ((maximization && (quality1 > quality2)) || ((!maximization) && (quality1 < quality2)))
    67           result[i] = parent1[i] + factor * (parent1[i] - parent2[i]);
    68         else
    69           result[i] = parent2[i] + factor * (parent2[i] - parent1[i]);
     81        result[i] = betterParent[i] + factor * (betterParent[i] - worseParent[i]);
    7082      }
    71       return result;
     83      return new DoubleArrayData(result);
    7284    }
    7385
     
    7587    /// Performs a heuristic crossover operation for two given parent real vectors.
    7688    /// </summary>
    77     /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    78     /// <param name="scope">The current scope.</param>
     89    /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
     90    /// <exception cref="InvalidOperationException">
     91    /// Thrown when either:<br/>
     92    /// <list type="bullet">
     93    /// <item><description>Maximization parameter could not be found.</description></item>
     94    /// <item><description>Quality parameter could not be found or the number of quality values is not equal to the number of parents.</description></item>
     95    /// </list>
     96    /// </exception>
    7997    /// <param name="random">A random number generator.</param>
    8098    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
    8199    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    82     protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
    83       if (parents.Length != 2) throw new InvalidOperationException("ERROR in HeuristicCrossover: The number of parents is not equal to 2");
    84       bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
    85       double quality1 = scope.SubScopes[0].GetVariableValue<DoubleData>("Quality", false).Data;
    86       double quality2 = scope.SubScopes[1].GetVariableValue<DoubleData>("Quality", false).Data;
     100    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
     101      if (parents.Length != 2) throw new ArgumentException("ERROR in HeuristicCrossover: The number of parents is not equal to 2");
    87102
    88       return Apply(random, maximization, parents[0], quality1, parents[1], quality2);
     103      if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("HeuristicCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
     104      if (QualityParameter.ActualValue == null || QualityParameter.ActualValue.Length != parents.Length) throw new InvalidOperationException("HeuristicCrossover: Parameter " + QualityParameter.ActualName + " could not be found, or not in the same quantity as there are parents.");
     105
     106      ItemArray<DoubleData> qualities = QualityParameter.ActualValue;
     107      bool maximization = MaximizationParameter.ActualValue.Value;
     108
     109      if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value)
     110        return Apply(random, parents[0], parents[1]);
     111      else
     112        return Apply(random, parents[1], parents[0]);
    89113    }
    90114  }
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/LocalCrossover.cs

    r2900 r2936  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
    2829namespace HeuristicLab.Encodings.RealVector {
     
    3132  /// always newly created randomly chosen factor and adds the allele of the second parent times (1 - the randomly chosen factor).
    3233  /// </summary>
    33   public class LocalCrossover : RealVectorCrossoverBase {
    34     /// <inheritdoc select="summary"/>
    35     public override string Description {
    36       get { return "Local crossover for real vectors."; }
    37     }
    38 
     34  /// <remarks>
     35  /// It is implemented as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL, p. 194.
     36  /// </remarks>
     37  [Item("LocalCrossover", "Local crossover for real vectors: Takes for each element the allele of the first parent times a always newly created randomly chosen factor and adds the allele of the second parent times (1 - the randomly chosen factor. " +
     38    "It is implemented as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL., p. 194.")]
     39  [EmptyStorableClass]
     40  public class LocalCrossover : RealVectorCrossover {
    3941    /// <summary>
    4042    /// Performs a local crossover on the two given parent vectors.
    4143    /// </summary>
     44    /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception>
    4245    /// <param name="random">The random number generator.</param>
    4346    /// <param name="parent1">The first parent for the crossover operation.</param>
    4447    /// <param name="parent2">The second parent for the crossover operation.</param>
    4548    /// <returns>The newly created real vector, resulting from the local crossover.</returns>
    46     public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {
     49    public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2) {
     50      if (parent1.Length != parent2.Length)
     51        throw new ArgumentException("ERROR in LocalCrossover: the two parents are not of the same length");
     52     
    4753      double factor;
    4854      int length = parent1.Length;
     
    5359        result[i] = (factor * parent1[i]) + ((1 - factor) * parent2[i]);
    5460      }
    55       return result;
     61      return new DoubleArrayData(result);
    5662    }
    5763
     
    5965    /// Performs a local crossover operation for two given parent real vectors.
    6066    /// </summary>
    61     /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    62     /// <param name="scope">The current scope.</param>
     67    /// <exception cref="ArgumentException">Thrown if there are not exactly two parents.</exception>
    6368    /// <param name="random">A random number generator.</param>
    6469    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
    6570    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    66     protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
    67       if (parents.Length != 2) throw new InvalidOperationException("ERROR in LocalCrossover: The number of parents is not equal to 2");
     71    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
     72      if (parents.Length != 2) throw new ArgumentException("ERROR in LocalCrossover: The number of parents is not equal to 2");
    6873      return Apply(random, parents[0], parents[1]);
    6974    }
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/RandomConvexCrossover.cs

    r2900 r2936  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
    2829namespace HeuristicLab.Encodings.RealVector {
     
    3233  /// (1 - the randomly chosen factor).
    3334  /// </summary>
    34   public class RandomConvexCrossover : RealVectorCrossoverBase {
    35     /// <inheritdoc select="summary"/>
    36     public override string Description {
    37       get { return "Random convex crossover for real vectors."; }
    38     }
    39 
     35  /// <remarks>
     36  /// It is implemented as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL, pp. 193 - 194.
     37  /// </remarks>
     38  [Item("RandomConvexCrossover", "Random convex crossover for real vectors: Takes for each element the allele of the first parent times a once created randomly chosen factor and adds the allele of the second parent times (1 - the randomly chosen factor). " +
     39    "It is implementes as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL, pp. 193 - 194.")]
     40  [EmptyStorableClass]
     41  public class RandomConvexCrossover : RealVectorCrossover {
    4042    /// <summary>
    4143    /// Performs a random convex crossover on the two given parents.
    4244    /// </summary>
     45    /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception>
    4346    /// <param name="random">The random number generator.</param>
    4447    /// <param name="parent1">The first parent vector for the crossover.</param>
    4548    /// <param name="parent2">The second parent vector for the crossover.</param>
    4649    /// <returns>The newly created real vector, resulting from the random convex crossover.</returns>
    47     public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {
     50    public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2) {
     51      if (parent1.Length != parent2.Length)
     52        throw new ArgumentException("ERROR in RandomConvexCrossover: the two parents are not of the same length");
     53     
    4854      int length = parent1.Length;
    4955      double[] result = new double[length];
     
    5258      for (int i = 0; i < length; i++)
    5359        result[i] = (factor * parent1[i]) + ((1 - factor) * parent2[i]);
    54       return result;
     60      return new DoubleArrayData(result);
    5561    }
    5662
     
    5864    /// Performs a random convex crossover operation for two given parent real vectors.
    5965    /// </summary>
    60     /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    61     /// <param name="scope">The current scope.</param>
     66    /// <exception cref="ArgumentException">Thrown if there are not exactly two parents.</exception>
    6267    /// <param name="random">A random number generator.</param>
    6368    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
    6469    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    65     protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
    66       if (parents.Length != 2) throw new InvalidOperationException("ERROR in RandomConvexCrossover: The number of parents is not equal to 2");
     70    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
     71      if (parents.Length != 2) throw new ArgumentException("ERROR in RandomConvexCrossover: The number of parents is not equal to 2");
    6772      return Apply(random, parents[0], parents[1]);
    6873    }
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/SimulatedBinaryCrossover.cs

    r2928 r2936  
    9494    /// Checks number of parents, availability of the parameters and forwards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleArrayData, DoubleData)"/>.
    9595    /// </summary>
    96     /// <exception cref="InvalidOperationException">Thrown when there are not exactly 2 parents or when the contiguity parameter could not be found.</exception>
     96    /// <exception cref="ArgumentException">Thrown when there are not exactly 2 parents or when the contiguity parameter could not be found.</exception>
    9797    /// <param name="random">The random number generator.</param>
    9898    /// <param name="parents">The collection of parents (must be of size 2).</param>
    9999    /// <returns>The real vector resulting from the crossover.</returns>
    100100    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
    101       if (parents.Length != 2) throw new InvalidOperationException("SimulatedBinaryCrossover: The number of parents is not equal to 2");
     101      if (parents.Length != 2) throw new ArgumentException("SimulatedBinaryCrossover: The number of parents is not equal to 2");
    102102      if (ContiguityParameter.ActualValue == null) throw new InvalidOperationException("SimulatedBinaryCrossover: Parameter " + ContiguityParameter.ActualName + " could not be found.");
    103103      return Apply(random, parents[0], parents[1], ContiguityParameter.ActualValue);
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/SinglePointCrossover.cs

    r2913 r2936  
    6262    /// <summary>
    6363    /// Checks number of parents and forwards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleArrayData)"/>.
    64     /// </summary>
     64    /// </summary>
     65    /// <exception cref="ArgumentException">Thrown when the parents' vectors are of unequal length or when <paramref name="contiguity"/> is smaller than 0.</exception>
    6566    /// <param name="random">The pseudo random number generator to use.</param>
    6667    /// <param name="parents">The list of parents.</param>
    6768    /// <returns>A new real vector.</returns>
    6869    protected override HeuristicLab.Data.DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
    69       if (parents.Length != 2) throw new InvalidOperationException("SinglePointCrossover: The number of parents is not equal to 2");
     70      if (parents.Length != 2) throw new ArgumentException("SinglePointCrossover: The number of parents is not equal to 2");
    7071      return Apply(random, parents[0], parents[1]);
    7172    }
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/HeuristicLab.Encodings.RealVector-3.3.csproj

    r2928 r2936  
    9393    <Compile Include="Crossovers\BlendAlphaCrossover.cs" />
    9494    <Compile Include="Crossovers\DiscreteCrossover.cs" />
     95    <Compile Include="Crossovers\HeuristicCrossover.cs" />
     96    <Compile Include="Crossovers\LocalCrossover.cs" />
     97    <Compile Include="Crossovers\RandomConvexCrossover.cs" />
    9598    <Compile Include="Crossovers\SimulatedBinaryCrossover.cs" />
    9699    <Compile Include="Crossovers\SinglePointCrossover.cs">
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Manipulators/PolynomialAllPositionManipulator.cs

    r2920 r2936  
    2626using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727
    28 namespace HeuristicLab.Encodings.RealVector.Manipulators {
     28namespace HeuristicLab.Encodings.RealVector {
    2929  /// <summary>
    3030  /// Performs the polynomial manipulation on all positions in the real vector.
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Manipulators/PolynomialOnePositionManipulator.cs

    r2920 r2936  
    2626using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727
    28 namespace HeuristicLab.Encodings.RealVector.Manipulators {
     28namespace HeuristicLab.Encodings.RealVector {
    2929  /// <summary>
    3030  /// Performs the polynomial manipulation on a randomly chosen single position in the real vector.
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Tests/HeuristicLab.Encodings.RealVector-3.3.Tests.csproj

    r2929 r2936  
    7676    <Compile Include="Auxiliary.cs" />
    7777    <Compile Include="BlendAlphaCrossoverTest.cs" />
     78    <Compile Include="BlendAlphaBetaCrossoverTest.cs" />
     79    <Compile Include="PolynomialOnePositionManipulatorTest.cs" />
     80    <Compile Include="UniformOnePositionManipulatorTest.cs" />
     81    <Compile Include="PolynomialAllPositionManipulatorTest.cs" />
     82    <Compile Include="MichalewiczNonUniformOnePositionManipulatorTest.cs" />
     83    <Compile Include="SinglePointCrossoverTest.cs" />
     84    <Compile Include="SimulatedBinaryCrossoverTest.cs" />
     85    <Compile Include="RandomConvexCrossoverTest.cs" />
     86    <Compile Include="LocalCrossoverTest.cs" />
     87    <Compile Include="HeuristicCrossoverTest.cs" />
     88    <Compile Include="DiscreteCrossoverTest.cs" />
    7889    <Compile Include="Properties\AssemblyInfo.cs" />
     90    <Compile Include="MichalewiczNonUniformAllPositionsManipulatorTest.cs" />
    7991    <Compile Include="TestRandom.cs" />
    8092  </ItemGroup>
Note: See TracChangeset for help on using the changeset viewer.