Changeset 2936 for trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/HeuristicCrossover.cs
- Timestamp:
- 03/05/10 13:14:26 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.