Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/04/10 14:28:49 (15 years ago)
Author:
abeham
Message:

Ported DiscreteCrossover #890
fixed exception messages in SBX

Location:
trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers
Files:
2 edited

Legend:

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

    r2900 r2928  
    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;
     24using HeuristicLab.Data;
     25using HeuristicLab.Parameters;
     26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2627
    2728namespace HeuristicLab.Encodings.RealVector {
     
    3031  /// of one of the parents is randomly selected.
    3132  /// </summary>
    32   public class DiscreteCrossover : RealVectorCrossoverBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent."; }
    36     }
    37 
     33  /// <remarks>
     34  /// This operator is also called dominant recombination and unlike other crossovers works on more than two parents also.<br />
     35  /// It is implemented as described in Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.
     36  /// </remarks>
     37  [Item("DiscreteCrossover", "Discrete crossover for real vectors: Creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent. It is implemented as described in Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.")]
     38  [EmptyStorableClass]
     39  public class DiscreteCrossover : RealVectorCrossover {
    3840    /// <summary>
    39     /// Performs a discrete crossover operation on multiple given parents.
     41    /// Performs a discrete crossover operation on multiple parents.
    4042    /// </summary>
     43    /// <exception cref="ArgumentException">Thrown when the vectors of the parents are of different length.</exception>
    4144    /// <param name="random">A random number generator.</param>
    4245    /// <param name="parents">An array containing the parents that should be crossed.</param>
    4346    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    44     public static double[] Apply(IRandom random, double[][] parents) {
     47    public static DoubleArrayData Apply(IRandom random, ItemArray<DoubleArrayData> parents) {
    4548      int length = parents[0].Length;
    46       double[] result = new double[length];
    47       for (int i = 0; i < length; i++) {
    48         result[i] = parents[random.Next(parents.Length)][i];
     49      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");
    4956      }
    5057      return result;
     
    5259
    5360    /// <summary>
    54     /// Performs a discrete crossover operation for multiple given parent real vectors.
     61    /// Checks number of parents and forwards the call to <see cref="Apply(IRandom, ItemArray<DoubleArrayData>)"/>.
    5562    /// </summary>
    56     /// <exception cref="InvalidOperationException">Thrown if there are less than two parents.</exception>
    57     /// <param name="scope">The current scope.</param>
    58     /// <param name="random">A random number generator.</param>
    59     /// <param name="parents">An array containing the real vectors that should be crossed.</param>
    60     /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    61     protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
    62       if (parents.Length < 2) throw new InvalidOperationException("ERROR in DiscreteCrossover: The number of parents is less than 2");
     63    /// <exception cref="ArgumentException">Thrown when <paramref name="parents"/> contains less than 2 elements.</exception>
     64    /// <param name="random">The random number generator.</param>
     65    /// <param name="parents">The collection of parents (must be of size 2 or greater).</param>
     66    /// <returns>The real vector resulting from the crossover.</returns>
     67    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
     68      if (parents.Length < 2) throw new ArgumentException("DiscreteCrossover: The number of parents is less than 2.", "parents");
    6369      return Apply(random, parents);
    6470    }
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/SimulatedBinaryCrossover.cs

    r2927 r2928  
    6767    /// <returns>The vector resulting from the crossover.</returns>
    6868    public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2, DoubleData contiguity) {
    69       if (parent1.Length != parent2.Length) throw new ArgumentException("SinglePointCrossover: Parents are of unequal length");
     69      if (parent1.Length != parent2.Length) throw new ArgumentException("SimulatedBinaryCrossover: Parents are of unequal length");
    7070      if (contiguity.Value < 0) throw new ArgumentException("SimulatedBinaryCrossover: Contiguity value is smaller than 0", "contiguity");
    7171      int length = parent1.Length;
     
    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("SinglePointCrossover: The number of parents is not equal to 2");
     101      if (parents.Length != 2) throw new InvalidOperationException("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);
Note: See TracChangeset for help on using the changeset viewer.