Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/02/10 16:48:24 (14 years ago)
Author:
abeham
Message:

checked and corrected (where necessary) BLX-a, BLX-a-b, as well the SinglePointCrossover
checked the UniformOnePositionManipulator
fixed a copy-paste oversight in RealVectorManipulator
#890

File:
1 edited

Legend:

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

    r2900 r2913  
    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.
     
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    2523using HeuristicLab.Core;
    2624using HeuristicLab.Data;
     25using HeuristicLab.Parameters;
     26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727
    2828namespace HeuristicLab.Encodings.RealVector {
    2929  /// <summary>
    30   /// Blend alpha crossover for real vectors. Creates a new offspring by selecting a random value
     30  /// Blend alpha crossover for real vectors (BLX-a). Creates a new offspring by selecting a random value
    3131  /// from the interval between the two alleles of the parent solutions. The interval is increased
    3232  /// in both directions by the factor alpha.
    3333  /// </summary>
    34   public class BlendAlphaCrossover : RealVectorCrossoverBase {
    35     /// <inheritdoc select="summary"/>
    36     public override string Description {
    37       get { return
    38 @"Blend alpha crossover for real vectors. Creates a new offspring by selecting a random value from the interval between the two alleles of the parent solutions. The interval is increased in both directions by the factor alpha.
    39 Please use the operator BoundsChecker if necessary.";
    40       }
     34  /// <remarks>
     35  /// It is implemented as described in Takahashi, M. and Kita, H. 2001. A crossover operator using independent component analysis for real-coded genetic algorithms Proceedings of the 2001 Congress on Evolutionary Computation, pp. 643-649.<br/>
     36  /// The default value for alpha is 0.5.
     37  /// </remarks>
     38  [Item("BlendAlphaCrossover", "The blend alpha crossover (BLX-a) for real vectors creates new offspring by sampling a new value in the range [min_i - d * alpha, max_i + d * alpha) at each position i. Here min_i and max_i are the smaller and larger value of the two parents at position i and d is max_i - min_i. It is implemented as described in Takahashi, M. and Kita, H. 2001. A crossover operator using independent component analysis for real-coded genetic algorithms Proceedings of the 2001 Congress on Evolutionary Computation, pp. 643-649.")]
     39  [EmptyStorableClass]
     40  public class BlendAlphaCrossover : RealVectorCrossover {
     41    public ValueLookupParameter<DoubleData> AlphaParameter {
     42      get { return (ValueLookupParameter<DoubleData>)Parameters["Alpha"]; }
     43    }
     44    /// <summary>
     45    /// Initializes a new instance of <see cref="BlendAlphaCrossover"/> with one parameter (<c>Alpha</c>).
     46    /// </summary>
     47    public BlendAlphaCrossover()
     48      : base() {
     49      Parameters.Add(new ValueLookupParameter<DoubleData>("Alpha", "Value for alpha", new DoubleData(0.5)));
    4150    }
    4251
    4352    /// <summary>
    44     /// Initializes a new instance of <see cref="BlendAlphaCrossover"/> with one variable info (<c>Alpha</c>).
     53    /// Performs the blend alpha crossover (BLX-a) of two real vectors.<br/>
     54    /// It creates new offspring by sampling a new value in the range [min_i - d * alpha, max_i + d * alpha) at each position i.
     55    /// Here min_i and max_i are the smaller and larger value of the two parents at position i and d is max_i - min_i.
    4556    /// </summary>
    46     public BlendAlphaCrossover()
    47       : base() {
    48       VariableInfo alphaVarInfo = new VariableInfo("Alpha", "Value for alpha", typeof(DoubleData), VariableKind.In);
    49       alphaVarInfo.Local = true;
    50       AddVariableInfo(alphaVarInfo);
    51       AddVariable(new Variable("Alpha", new DoubleData(0.5)));
    52     }
    53 
    54     /// <summary>
    55     /// Performs a blend alpha crossover of two real vectors.
    56     /// </summary>
     57    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are of different length.</exception>
    5758    /// <param name="random">The random number generator.</param>
    5859    /// <param name="parent1">The first parent for the crossover operation.</param>
     
    6061    /// <param name="alpha">The alpha value for the crossover.</param>
    6162    /// <returns>The newly created real vector resulting from the crossover operation.</returns>
    62     public static double[] Apply(IRandom random, double[] parent1, double[] parent2, double alpha) {
     63    public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2, DoubleData alpha) {
     64      if (parent1.Length != parent2.Length) throw new ArgumentException("BlendAlphaCrossover: The parents' vectors are of different length.", "parent1");
    6365      int length = parent1.Length;
    64       double[] result = new double[length];
    65       double cMax = 0, cMin = 0, interval = 0, resMin = 0, resMax = 0;
     66      DoubleArrayData result = new DoubleArrayData(length);
     67      double max = 0, min = 0, d = 0, resMin = 0, resMax = 0;
    6668
    6769      for (int i = 0; i < length; i++) {
    68         cMax = Math.Max(parent1[i], parent2[i]);
    69         cMin = Math.Min(parent1[i], parent2[i]);
    70         interval = Math.Abs(cMax - cMin);
    71         resMin = cMin - interval * alpha;
    72         resMax = cMax + interval * alpha;
     70        max = Math.Max(parent1[i], parent2[i]);
     71        min = Math.Min(parent1[i], parent2[i]);
     72        d = Math.Abs(max - min);
     73        resMin = min - d * alpha.Value;
     74        resMax = max + d * alpha.Value;
    7375
    7476        result[i] = resMin + random.NextDouble() * Math.Abs(resMax - resMin);
     
    7880
    7981    /// <summary>
    80     /// Performs a blend alpha crossover operation for two given parent real vectors.
     82    /// Checks that the number of parents is equal to 2 and forwards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleArrayData, DoubleData)"/>.
    8183    /// </summary>
    82     /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    83     /// <param name="scope">The current scope.</param>
    84     /// <param name="random">A random number generator.</param>
    85     /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
    86     /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    87     protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
    88       if (parents.Length != 2) throw new InvalidOperationException("ERROR in BlendAlphaCrossover: The number of parents is not equal to 2");
    89       double alpha = GetVariableValue<DoubleData>("Alpha", scope, true).Data;
    90       return Apply(random, parents[0], parents[1], alpha);
     84    /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
     85    /// <exception cref="InvalidOperationException">Thrown when the parameter alpha could not be found.</exception>
     86    /// <param name="random">The random number generator to use.</param>
     87    /// <param name="parents">The collection of parents (must be of size 2).</param>
     88    /// <returns>The real vector resulting from the crossover operation.</returns>
     89    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
     90      if (parents.Length != 2) throw new ArgumentException("BlendAlphaCrossover: The number of parents is not equal to 2", "parents");
     91      if (AlphaParameter.ActualValue == null) throw new InvalidOperationException("BlendAlphaCrossover: Parameter " + AlphaParameter.ActualName + " could not be found.");
     92      return Apply(random, parents[0], parents[1], AlphaParameter.ActualValue);
    9193    }
    9294  }
Note: See TracChangeset for help on using the changeset viewer.