Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/08/10 15:30:26 (14 years ago)
Author:
abeham
Message:

Updated operators for real vector encoding #890

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

Legend:

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

    r2964 r2969  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    2523using HeuristicLab.Core;
    2624using HeuristicLab.Data;
     25using HeuristicLab.Parameters;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    28 using HeuristicLab.Parameters;
    2927
    3028namespace HeuristicLab.Encodings.RealVector {
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/LocalCrossover.cs

    r2964 r2969  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    2523using HeuristicLab.Core;
    2624using HeuristicLab.Data;
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/RandomConvexCrossover.cs

    r2964 r2969  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    2523using HeuristicLab.Core;
    2624using HeuristicLab.Data;
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/SelfAdaptiveDiscreteCrossover.cs

    r2900 r2969  
    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   /// Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is
    31   /// randomly selected from one parent. It will also use the same strategy to combine the endogenous
    32   /// strategy parameter vector.
     30  /// The self adaptive discrete crossover is similar to the <see cref="DiscreteCrossover"/>, but will also copy values from an exising strategy vector (such as in the sigma self adaptive evolution strategy)
     31  /// from the same parent to the offspring in each position. The strategy vector can be of different length to the parent vector. The idea is that if it is shorter, it will be cycled.
    3332  /// </summary>
    34   public class SelfAdaptiveDiscreteCrossover : RealVectorCrossoverBase {
    35     /// <inheritdoc select="summary"/>
    36     public override string Description {
    37       get {
    38         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. It will also use the same strategy to combine the endogenous strategy parameter vector.";
    39       }
     33  /// <remarks>
     34  /// This operator is not mentioned in the literature, as typically intermediate recombination (<see cref="AverageCrossover"/>) should be used to recombine the strategy vector (Beyer and Schwefel 2002).
     35  /// Since <see cref="AverageCrossover"/> is deterministic this can be done for solution vector and strategy vector separately.<br/>
     36  /// However if one wishes to use discrete (dominant) recombination on the strategy vectors the idea is probably that the same parent should donate strategy value and solution value in a position. In this case use this operator.
     37  /// Otherwise, you can also apply <see cref="DiscreteCrossover"/> independently on the solution vector and on the strategy vector.
     38  /// </remarks>
     39  public class SelfAdaptiveDiscreteCrossover : RealVectorCrossover {
     40    /// <summary>
     41    /// Parameter for the parents' strategy vectors.
     42    /// </summary>
     43    public SubScopesLookupParameter<DoubleArrayData> ParentsStrategyVectorParameter {
     44      get { return (SubScopesLookupParameter<DoubleArrayData>)Parameters["ParentsStrategyVector"]; }
     45    }
     46    /// <summary>
     47    /// Parameter for the child's strategy vector.
     48    /// </summary>
     49    public LookupParameter<DoubleArrayData> ChildStrategyVectorParameter {
     50      get { return (LookupParameter<DoubleArrayData>)Parameters["ChildStrategyVector"]; }
     51    }
     52    /// <summary>
     53    /// Initializes a new instance of <see cref="SelfAdaptiveDiscreteCrossover"/> with two parameters (<c>ParentsStrategyVector</c>, and <c>ChildStrategyVector</c>).
     54    /// </summary>
     55    public SelfAdaptiveDiscreteCrossover()
     56      : base() {
     57      Parameters.Add(new SubScopesLookupParameter<DoubleArrayData>("ParentsStrategyVector", "The vector containing the parents' endogenous strategy parameters."));
     58      ParentsStrategyVectorParameter.ActualName = "StrategyVector";
     59      Parameters.Add(new LookupParameter<DoubleArrayData>("ChildStrategyVector", "The vector containing the child's endogenous strategy parameters."));
     60      ChildStrategyVectorParameter.ActualName = "StrategyVector";
    4061    }
    4162
    4263    /// <summary>
    43     /// Initializes a new instance of <see cref="SelfAdaptiveDiscreteCrossover"/> with one additional
    44     /// variable infos (<c>StrategyVector</c>).
     64    /// Performs a discrete crossover operation for multiple given parent vectors in a way that the same parent
     65    /// is used to donate the value of the solution and of the strategy vector in each position.
    4566    /// </summary>
    46     public SelfAdaptiveDiscreteCrossover()
    47       : base() {
    48       AddVariableInfo(new VariableInfo("StrategyVector", "Endogenous strategy parameter vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
    49     }
    50 
    51     /// <summary>
    52     /// Performs a discrete crossover operation for multiple given parent real vectors that combines the strategy vectors in the same way.
    53     /// </summary>
     67    /// <remarks>
     68    /// The parent vectors can be of a different length to the strategy vectors, but among each group the length has to be the same.
     69    /// </remarks>
    5470    /// <param name="random">A random number generator.</param>
    5571    /// <param name="parents">An array containing the parents that should be crossed.</param>
     
    5773    /// <param name="child">The newly created real vector, resulting from the crossover operation (output parameter).</param>
    5874    /// <param name="strategy">The newly created strategy vector, resulting from the crossover operation (output parameter).</param>
    59     public static void Apply(IRandom random, double[][] parents, double[][] strategies, out double[] child, out double[] strategy) {
    60       child = new double[parents[0].Length];
    61       strategy = new double[strategies[0].Length];
    62       for (int i = 0; i < child.Length; i++) {
    63         int nextParent = random.Next(parents.Length);
    64         child[i] = parents[nextParent][i];
    65         strategy[i] = strategies[nextParent][i];
     75    public static void Apply(IRandom random, ItemArray<DoubleArrayData> parents, ItemArray<DoubleArrayData> strategies, out DoubleArrayData child, out DoubleArrayData strategy) {
     76      if (strategies.Length != parents.Length) throw new ArgumentException("SelfAdaptiveDiscreteCrossover: Number of parents is not equal to the number of strategy vectors", "parents");
     77      int length = parents[0].Length, strategyLength = strategies[0].Length, parentsCount = parents.Length;
     78      child = new DoubleArrayData(length);
     79      strategy = new DoubleArrayData(strategyLength);
     80      int loopEnd = Math.Max(length, strategyLength);
     81      try {
     82        for (int i = 0; i < loopEnd; i++) {
     83          int nextParent = random.Next(parentsCount);
     84          if (i < length) child[i] = parents[nextParent][i];
     85          if (i < strategyLength) strategy[i] = strategies[nextParent][i];
     86        }
     87      } catch (IndexOutOfRangeException) {
     88        throw new ArgumentException("SelfAdaptiveDiscreteCrossover: There are different lengths among either the parent vectors or the parents' strategy vectors.");
    6689      }
    6790    }
    6891
    6992    /// <summary>
    70     /// Performs a discrete crossover operation for multiple given parent real vectors that combines the strategy vectors in the same way.
     93    /// Checks that there are at least two parents, that the strategy vector is not null and forwards the call to <see cref="Apply(IRandom, ItemArray<DoubleArrayData>, ItemArray<DoubleArrayData>, out DoubleArrayData, out DoubleArrayData)"/>.
    7194    /// </summary>
    72     /// <exception cref="InvalidOperationException">Thrown if there are less than two parents or if the length of the strategy vectors is not the same as the length of the real vectors.</exception>
    73     /// <param name="scope">The current scope.</param>
    74     /// <param name="random">A random number generator.</param>
    75     /// <param name="parents">An array containing the real vectors that should be crossed.</param>
    76     /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    77     protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
    78       if (parents.Length < 2) throw new InvalidOperationException("ERROR in SelfAdaptiveDiscreteCrossover: The number of parents is less than 2");
    79       double[][] strategies = new double[scope.SubScopes.Count][];
    80       int length = parents[0].Length;
    81       for (int i = 0; i < scope.SubScopes.Count; i++) {
    82         strategies[i] = scope.SubScopes[i].GetVariableValue<DoubleArrayData>("StrategyVector", false).Data;
    83         if (strategies[i].Length != length) throw new InvalidOperationException("ERROR in SelfAdaptiveDiscreteCrossover: Strategy vectors do not have the same length as the real vectors");
    84       }
    85 
    86       double[] child, strategy;
    87       Apply(random, parents, strategies, out child, out strategy);
    88       scope.AddVariable(new Variable(scope.TranslateName("StrategyVector"), new DoubleArrayData(strategy)));
    89       return child;
     95    /// <param name="random">The random number generator.</param>
     96    /// <param name="parents">The collection of parents (at least of size 2).</param>
     97    /// <returns>The child vector that results from the crossover.</returns>
     98    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
     99      if (parents.Length < 2) throw new ArgumentException("SelfAdaptiveDiscreteCrossover: The number of parents is less than 2", "parents");
     100      if (ParentsStrategyVectorParameter.ActualValue == null) throw new InvalidOperationException("SelfAdaptiveDiscreteCrossover: Parameter " + ParentsStrategyVectorParameter.ActualName + " could not be found.");
     101      ItemArray<DoubleArrayData> strategies = ParentsStrategyVectorParameter.ActualValue;     
     102      DoubleArrayData result, resultStrategyVector;
     103      Apply(random, parents, strategies, out result, out resultStrategyVector);
     104      ChildStrategyVectorParameter.ActualValue = resultStrategyVector;
     105      return result;
    90106    }
    91107  }
Note: See TracChangeset for help on using the changeset viewer.