- Timestamp:
- 03/05/10 13:14:26 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/DiscreteCrossover.cs
r2928 r2936 47 47 public static DoubleArrayData Apply(IRandom random, ItemArray<DoubleArrayData> parents) { 48 48 int length = parents[0].Length; 49 50 for (int i = 0; i < parents.Length; i++) { 51 if(parents[i].Length != length) 52 throw new ArgumentException("DiscreteCrossover: The parents' vectors are of different length.", "parents"); 53 } 54 49 55 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"); 56 } 56 for (int i = 0; i < length; i++) { 57 result[i] = parents[random.Next(parents.Length)][i]; 58 } 59 57 60 return result; 58 61 } -
trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/HeuristicCrossover.cs
r2900 r2936 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. … … 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Evolutionary; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Parameters; 28 29 29 30 namespace HeuristicLab.Encodings.RealVector { … … 32 33 /// of the two parents times a randomly chosen factor. 33 34 /// </summary> 34 public class HeuristicCrossover : RealVectorCrossoverBase { 35 /// <remarks> 36 /// It is implemented as described in Wright, A.H. (1994), Genetic algorithms for real parameter optimization, Foundations of Genetic Algorithms, G.J.E. Rawlins (Ed.), Morgan Kaufmann, San Mateo, CA, 205-218. 37 /// </remarks> 38 [Item("HeuristicCrossover", "Heuristic crossover for real vectors: Takes for each position the better parent and adds the difference. It is implemented as described in Wright, A.H. (1994), Genetic algorithms for real parameter optimization, Foundations of Genetic Algorithms, G.J.E. Rawlins (Ed.), Morgan Kaufmann, San Mateo, CA, 205-218.")] 39 [EmptyStorableClass] 40 public class HeuristicCrossover : RealVectorCrossover { 41 /// <summary> 42 /// Whether the problem is a maximization or minimization problem. 43 /// </summary> 44 public ValueLookupParameter<BoolData> MaximizationParameter { 45 get { return (ValueLookupParameter<BoolData>)Parameters["Maximization"]; } 46 } 47 /// <summary> 48 /// The quality of the parents. 49 /// </summary> 50 public SubScopesLookupParameter<DoubleData> QualityParameter { 51 get { return (SubScopesLookupParameter<DoubleData>)Parameters["Quality"]; } 52 } 53 35 54 /// <summary> 36 55 /// Initializes a new instance of <see cref="HeuristicCrossover"/> with two variable infos … … 39 58 public HeuristicCrossover() 40 59 : base() { 41 AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In)); 42 AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In)); 43 } 44 45 /// <inheritdoc select="summary"/> 46 public override string Description { 47 get { return "Heuristic crossover for real vectors."; } 60 Parameters.Add(new ValueLookupParameter<BoolData>("Maximization", "Whether the problem is a maximization problem or not.")); 61 Parameters.Add(new SubScopesLookupParameter<DoubleData>("Quality", "The quality values of the parents.")); 48 62 } 49 63 … … 51 65 /// Perfomrs a heuristic crossover on the two given parents. 52 66 /// </summary> 67 /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception> 53 68 /// <param name="random">The random number generator.</param> 54 /// <param name="maximization">Boolean flag whether it is a maximization problem.</param> 55 /// <param name="parent1">The first parent for the crossover operation.</param> 56 /// <param name="quality1">The quality of the first parent.</param> 57 /// <param name="parent2">The second parent for the crossover operation.</param> 58 /// <param name="quality2">The quality of the second parent.</param> 69 /// <param name="betterParent">The first parent for the crossover operation.</param> 70 /// <param name="worseParent">The second parent for the crossover operation.</param> 59 71 /// <returns>The newly created real vector, resulting from the heuristic crossover.</returns> 60 public static double[] Apply(IRandom random, bool maximization, double[] parent1, double quality1, double[] parent2, double quality2) { 61 int length = parent1.Length; 72 public static DoubleArrayData Apply(IRandom random, DoubleArrayData betterParent, DoubleArrayData worseParent) { 73 if (betterParent.Length != worseParent.Length) 74 throw new ArgumentException("ERROR in HeuristicCrossover: the two parents are not of the same length"); 75 76 int length = betterParent.Length; 62 77 double[] result = new double[length]; 63 78 double factor = random.NextDouble(); 64 79 65 80 for (int i = 0; i < length; i++) { 66 if ((maximization && (quality1 > quality2)) || ((!maximization) && (quality1 < quality2))) 67 result[i] = parent1[i] + factor * (parent1[i] - parent2[i]); 68 else 69 result[i] = parent2[i] + factor * (parent2[i] - parent1[i]); 81 result[i] = betterParent[i] + factor * (betterParent[i] - worseParent[i]); 70 82 } 71 return result;83 return new DoubleArrayData(result); 72 84 } 73 85 … … 75 87 /// Performs a heuristic crossover operation for two given parent real vectors. 76 88 /// </summary> 77 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 78 /// <param name="scope">The current scope.</param> 89 /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception> 90 /// <exception cref="InvalidOperationException"> 91 /// Thrown when either:<br/> 92 /// <list type="bullet"> 93 /// <item><description>Maximization parameter could not be found.</description></item> 94 /// <item><description>Quality parameter could not be found or the number of quality values is not equal to the number of parents.</description></item> 95 /// </list> 96 /// </exception> 79 97 /// <param name="random">A random number generator.</param> 80 98 /// <param name="parents">An array containing the two real vectors that should be crossed.</param> 81 99 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 82 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 83 if (parents.Length != 2) throw new InvalidOperationException("ERROR in HeuristicCrossover: The number of parents is not equal to 2"); 84 bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data; 85 double quality1 = scope.SubScopes[0].GetVariableValue<DoubleData>("Quality", false).Data; 86 double quality2 = scope.SubScopes[1].GetVariableValue<DoubleData>("Quality", false).Data; 100 protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) { 101 if (parents.Length != 2) throw new ArgumentException("ERROR in HeuristicCrossover: The number of parents is not equal to 2"); 87 102 88 return Apply(random, maximization, parents[0], quality1, parents[1], quality2); 103 if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("HeuristicCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found."); 104 if (QualityParameter.ActualValue == null || QualityParameter.ActualValue.Length != parents.Length) throw new InvalidOperationException("HeuristicCrossover: Parameter " + QualityParameter.ActualName + " could not be found, or not in the same quantity as there are parents."); 105 106 ItemArray<DoubleData> qualities = QualityParameter.ActualValue; 107 bool maximization = MaximizationParameter.ActualValue.Value; 108 109 if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value) 110 return Apply(random, parents[0], parents[1]); 111 else 112 return Apply(random, parents[1], parents[0]); 89 113 } 90 114 } -
trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/LocalCrossover.cs
r2900 r2936 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. … … 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 28 29 namespace HeuristicLab.Encodings.RealVector { … … 31 32 /// always newly created randomly chosen factor and adds the allele of the second parent times (1 - the randomly chosen factor). 32 33 /// </summary> 33 public class LocalCrossover : RealVectorCrossoverBase { 34 /// <inheritdoc select="summary"/> 35 public override string Description { 36 get { return "Local crossover for real vectors."; } 37 } 38 34 /// <remarks> 35 /// It is implemented as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL, p. 194. 36 /// </remarks> 37 [Item("LocalCrossover", "Local crossover for real vectors: Takes for each element the allele of the first parent times a always newly created randomly chosen factor and adds the allele of the second parent times (1 - the randomly chosen factor. " + 38 "It is implemented as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL., p. 194.")] 39 [EmptyStorableClass] 40 public class LocalCrossover : RealVectorCrossover { 39 41 /// <summary> 40 42 /// Performs a local crossover on the two given parent vectors. 41 43 /// </summary> 44 /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception> 42 45 /// <param name="random">The random number generator.</param> 43 46 /// <param name="parent1">The first parent for the crossover operation.</param> 44 47 /// <param name="parent2">The second parent for the crossover operation.</param> 45 48 /// <returns>The newly created real vector, resulting from the local crossover.</returns> 46 public static double[] Apply(IRandom random, double[] parent1, double[] parent2) { 49 public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2) { 50 if (parent1.Length != parent2.Length) 51 throw new ArgumentException("ERROR in LocalCrossover: the two parents are not of the same length"); 52 47 53 double factor; 48 54 int length = parent1.Length; … … 53 59 result[i] = (factor * parent1[i]) + ((1 - factor) * parent2[i]); 54 60 } 55 return result;61 return new DoubleArrayData(result); 56 62 } 57 63 … … 59 65 /// Performs a local crossover operation for two given parent real vectors. 60 66 /// </summary> 61 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 62 /// <param name="scope">The current scope.</param> 67 /// <exception cref="ArgumentException">Thrown if there are not exactly two parents.</exception> 63 68 /// <param name="random">A random number generator.</param> 64 69 /// <param name="parents">An array containing the two real vectors that should be crossed.</param> 65 70 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 66 protected override double[] Cross(IScope scope, IRandom random, double[][]parents) {67 if (parents.Length != 2) throw new InvalidOperationException("ERROR in LocalCrossover: The number of parents is not equal to 2");71 protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) { 72 if (parents.Length != 2) throw new ArgumentException("ERROR in LocalCrossover: The number of parents is not equal to 2"); 68 73 return Apply(random, parents[0], parents[1]); 69 74 } -
trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/RandomConvexCrossover.cs
r2900 r2936 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. … … 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 28 29 namespace HeuristicLab.Encodings.RealVector { … … 32 33 /// (1 - the randomly chosen factor). 33 34 /// </summary> 34 public class RandomConvexCrossover : RealVectorCrossoverBase { 35 /// <inheritdoc select="summary"/> 36 public override string Description { 37 get { return "Random convex crossover for real vectors."; } 38 } 39 35 /// <remarks> 36 /// It is implemented as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL, pp. 193 - 194. 37 /// </remarks> 38 [Item("RandomConvexCrossover", "Random convex crossover for real vectors: Takes for each element the allele of the first parent times a once created randomly chosen factor and adds the allele of the second parent times (1 - the randomly chosen factor). " + 39 "It is implementes as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL, pp. 193 - 194.")] 40 [EmptyStorableClass] 41 public class RandomConvexCrossover : RealVectorCrossover { 40 42 /// <summary> 41 43 /// Performs a random convex crossover on the two given parents. 42 44 /// </summary> 45 /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception> 43 46 /// <param name="random">The random number generator.</param> 44 47 /// <param name="parent1">The first parent vector for the crossover.</param> 45 48 /// <param name="parent2">The second parent vector for the crossover.</param> 46 49 /// <returns>The newly created real vector, resulting from the random convex crossover.</returns> 47 public static double[] Apply(IRandom random, double[] parent1, double[] parent2) { 50 public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2) { 51 if (parent1.Length != parent2.Length) 52 throw new ArgumentException("ERROR in RandomConvexCrossover: the two parents are not of the same length"); 53 48 54 int length = parent1.Length; 49 55 double[] result = new double[length]; … … 52 58 for (int i = 0; i < length; i++) 53 59 result[i] = (factor * parent1[i]) + ((1 - factor) * parent2[i]); 54 return result;60 return new DoubleArrayData(result); 55 61 } 56 62 … … 58 64 /// Performs a random convex crossover operation for two given parent real vectors. 59 65 /// </summary> 60 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 61 /// <param name="scope">The current scope.</param> 66 /// <exception cref="ArgumentException">Thrown if there are not exactly two parents.</exception> 62 67 /// <param name="random">A random number generator.</param> 63 68 /// <param name="parents">An array containing the two real vectors that should be crossed.</param> 64 69 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 65 protected override double[] Cross(IScope scope, IRandom random, double[][]parents) {66 if (parents.Length != 2) throw new InvalidOperationException("ERROR in RandomConvexCrossover: The number of parents is not equal to 2");70 protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) { 71 if (parents.Length != 2) throw new ArgumentException("ERROR in RandomConvexCrossover: The number of parents is not equal to 2"); 67 72 return Apply(random, parents[0], parents[1]); 68 73 } -
trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/SimulatedBinaryCrossover.cs
r2928 r2936 94 94 /// Checks number of parents, availability of the parameters and forwards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleArrayData, DoubleData)"/>. 95 95 /// </summary> 96 /// <exception cref=" InvalidOperationException">Thrown when there are not exactly 2 parents or when the contiguity parameter could not be found.</exception>96 /// <exception cref="ArgumentException">Thrown when there are not exactly 2 parents or when the contiguity parameter could not be found.</exception> 97 97 /// <param name="random">The random number generator.</param> 98 98 /// <param name="parents">The collection of parents (must be of size 2).</param> 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("SimulatedBinaryCrossover: The number of parents is not equal to 2");101 if (parents.Length != 2) throw new ArgumentException("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/Crossovers/SinglePointCrossover.cs
r2913 r2936 62 62 /// <summary> 63 63 /// Checks number of parents and forwards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleArrayData)"/>. 64 /// </summary> 64 /// </summary> 65 /// <exception cref="ArgumentException">Thrown when the parents' vectors are of unequal length or when <paramref name="contiguity"/> is smaller than 0.</exception> 65 66 /// <param name="random">The pseudo random number generator to use.</param> 66 67 /// <param name="parents">The list of parents.</param> 67 68 /// <returns>A new real vector.</returns> 68 69 protected override HeuristicLab.Data.DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) { 69 if (parents.Length != 2) throw new InvalidOperationException("SinglePointCrossover: The number of parents is not equal to 2");70 if (parents.Length != 2) throw new ArgumentException("SinglePointCrossover: The number of parents is not equal to 2"); 70 71 return Apply(random, parents[0], parents[1]); 71 72 }
Note: See TracChangeset
for help on using the changeset viewer.