- Timestamp:
- 02/16/09 01:21:53 (16 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.RealVector/SelfAdaptiveDiscreteCrossover.cs
r1217 r1218 28 28 namespace HeuristicLab.RealVector { 29 29 /// <summary> 30 /// Creates a new offspring by combining the alleles in the parents such that each allele is30 /// Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is 31 31 /// randomly selected from one parent. It will also use the same strategy to combine the endogenous 32 /// strategy parameter vector. 32 /// strategy parameter vector. 33 33 /// </summary> 34 public class SelfAdaptiveDiscrete MultiCrossover : RealVectorSelfAdaptiveMultiCrossoverBase {34 public class SelfAdaptiveDiscreteCrossover : RealVectorCrossoverBase { 35 35 /// <inheritdoc select="summary"/> 36 36 public override string Description { 37 37 get { 38 return @" Thiscreates 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.";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 39 } 40 40 } 41 41 42 42 /// <summary> 43 /// Performs a self adaptive discrete multiple crossover on the given list of <paramref name="parents"/>. 43 /// Initializes a new instance of <see cref="SelfAdaptiveDiscreteCrossover"/> with one additional 44 /// variable infos (<c>StrategyVector</c>). 44 45 /// </summary> 45 /// <exception cref="InvalidOperationException">Thrown when the parent vectors have different lengths.</exception> 46 /// <param name="random">The random number generator.</param> 47 /// <param name="parents">The list of parents to crossover.</param> 48 /// <param name="strategyParametersList">The strategy parameter list.</param> 49 /// <param name="childIndividual">Output parameter; the created child.</param> 50 /// <param name="strategyParameters">Output parameter; endogenous strategy parameters.</param> 51 public static void Apply(IRandom random, IList<double[]> parents, IList<double[]> strategyParametersList, out double[] childIndividual, out double[] strategyParameters) { 52 childIndividual = new double[parents[0].Length]; 53 strategyParameters = new double[strategyParametersList[0].Length]; 54 try { 55 for (int i = 0; i < childIndividual.Length; i++) { 56 int nextParent = random.Next(parents.Count); 57 childIndividual[i] = parents[nextParent][i]; 58 strategyParameters[i] = strategyParametersList[nextParent][i]; 59 } 60 } catch (IndexOutOfRangeException) { 61 throw new InvalidOperationException("Cannot apply self adaptive multicrossover to real vectors of different length."); 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> 54 /// <param name="random">A random number generator.</param> 55 /// <param name="parents">An array containing the parents that should be crossed.</param> 56 /// <param name="strategies">An array containing the strategy vectors of the parents.</param> 57 /// <param name="child">The newly created real vector, resulting from the crossover operation (output parameter).</param> 58 /// <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]; 62 66 } 63 67 } 64 68 65 69 /// <summary> 66 /// Performs a self adaptive discrete multiple crossover on the given list of <paramref name="parents"/>.70 /// Performs a discrete crossover operation for multiple given parent real vectors that combines the strategy vectors in the same way. 67 71 /// </summary> 68 /// < remarks>Calls <see cref="Apply"/>.</remarks>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> 69 73 /// <param name="scope">The current scope.</param> 70 /// <param name="random">The random number generator.</param> 71 /// <param name="parents">The list of parents to crossover.</param> 72 /// <param name="strategyParametersList">The strategy parameter list.</param> 73 /// <param name="childIndividual">Output parameter; the created child.</param> 74 /// <param name="strategyParameters">Output parameter; endogenous strategy parameters.</param> 75 protected override void Cross(IScope scope, IRandom random, IList<double[]> parents, IList<double[]> strategyParametersList, out double[] childIndividual, out double[] strategyParameters) { 76 Apply(random, parents, strategyParametersList, out childIndividual, out strategyParameters); 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; 77 90 } 78 91 }
Note: See TracChangeset
for help on using the changeset viewer.