Changeset 2928
- Timestamp:
- 03/04/10 14:28:49 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.RealVector/3.3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/DiscreteCrossover.cs
r2900 r2928 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-20 08Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Text;25 23 using HeuristicLab.Core; 24 using HeuristicLab.Data; 25 using HeuristicLab.Parameters; 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 27 27 28 namespace HeuristicLab.Encodings.RealVector { … … 30 31 /// of one of the parents is randomly selected. 31 32 /// </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 { 38 40 /// <summary> 39 /// Performs a discrete crossover operation on multiple givenparents.41 /// Performs a discrete crossover operation on multiple parents. 40 42 /// </summary> 43 /// <exception cref="ArgumentException">Thrown when the vectors of the parents are of different length.</exception> 41 44 /// <param name="random">A random number generator.</param> 42 45 /// <param name="parents">An array containing the parents that should be crossed.</param> 43 46 /// <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) { 45 48 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"); 49 56 } 50 57 return result; … … 52 59 53 60 /// <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>)"/>. 55 62 /// </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"); 63 69 return Apply(random, parents); 64 70 } -
trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/SimulatedBinaryCrossover.cs
r2927 r2928 67 67 /// <returns>The vector resulting from the crossover.</returns> 68 68 public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2, DoubleData contiguity) { 69 if (parent1.Length != parent2.Length) throw new ArgumentException("Si nglePointCrossover: Parents are of unequal length");69 if (parent1.Length != parent2.Length) throw new ArgumentException("SimulatedBinaryCrossover: Parents are of unequal length"); 70 70 if (contiguity.Value < 0) throw new ArgumentException("SimulatedBinaryCrossover: Contiguity value is smaller than 0", "contiguity"); 71 71 int length = parent1.Length; … … 99 99 /// <returns>The real vector resulting from the crossover.</returns> 100 100 protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) { 101 if (parents.Length != 2) throw new InvalidOperationException("Si nglePointCrossover: 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"); 102 102 if (ContiguityParameter.ActualValue == null) throw new InvalidOperationException("SimulatedBinaryCrossover: Parameter " + ContiguityParameter.ActualName + " could not be found."); 103 103 return Apply(random, parents[0], parents[1], ContiguityParameter.ActualValue); -
trunk/sources/HeuristicLab.Encodings.RealVector/3.3/HeuristicLab.Encodings.RealVector-3.3.csproj
r2920 r2928 92 92 <ItemGroup> 93 93 <Compile Include="Crossovers\BlendAlphaCrossover.cs" /> 94 <Compile Include="Crossovers\DiscreteCrossover.cs" /> 94 95 <Compile Include="Crossovers\SimulatedBinaryCrossover.cs" /> 95 96 <Compile Include="Crossovers\SinglePointCrossover.cs">
Note: See TracChangeset
for help on using the changeset viewer.