- Timestamp:
- 02/16/09 01:21:53 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.RealVector/HeuristicCrossover.cs
r1184 r1218 32 32 /// of the two parents times a randomly chosen factor. 33 33 /// </summary> 34 public class HeuristicCrossover : CrossoverBase {34 public class HeuristicCrossover : RealVectorCrossoverBase { 35 35 /// <summary> 36 /// Initializes a new instance of <see cref="HeuristicCrossover"/> with t hreevariable infos37 /// (<c>Maximization</c> , <c>Quality</c> and <c>RealVector</c>).36 /// Initializes a new instance of <see cref="HeuristicCrossover"/> with two variable infos 37 /// (<c>Maximization</c> and <c>Quality</c>). 38 38 /// </summary> 39 39 public HeuristicCrossover() … … 41 41 AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In)); 42 42 AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In)); 43 AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));44 43 } 45 44 … … 74 73 75 74 /// <summary> 76 /// Perfo mrs a heuristic crossover on the two given parents.75 /// Performs a heuristic crossover operation for two given parent real vectors. 77 76 /// </summary> 78 /// <exception cref="InvalidOperationException">Thrown when the parent vectors have different lengths.</exception>77 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 79 78 /// <param name="scope">The current scope.</param> 80 /// <param name="random"> Therandom number generator.</param>81 /// <param name="parent 1">The first parent for the crossover operation.</param>82 /// < param name="parent2">The second parent for the crossover operation.</param>83 /// <param name="child">The newly created real vector, resulting from the heuristic crossover.</param>84 protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {79 /// <param name="random">A random number generator.</param> 80 /// <param name="parents">An array containing the two real vectors that should be crossed.</param> 81 /// <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"); 85 84 bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data; 86 DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>("RealVector", false); 87 DoubleData quality1 = parent1.GetVariableValue<DoubleData>("Quality", false); 88 DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>("RealVector", false); 89 DoubleData quality2 = parent2.GetVariableValue<DoubleData>("Quality", false); 85 double quality1 = scope.SubScopes[0].GetVariableValue<DoubleData>("Quality", false).Data; 86 double quality2 = scope.SubScopes[1].GetVariableValue<DoubleData>("Quality", false).Data; 90 87 91 if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length."); 92 93 double[] result = Apply(random, maximization, vector1.Data, quality1.Data, vector2.Data, quality2.Data); 94 child.AddVariable(new Variable(child.TranslateName("RealVector"), new DoubleArrayData(result))); 88 return Apply(random, maximization, parents[0], quality1, parents[1], quality2); 95 89 } 96 90 }
Note: See TracChangeset
for help on using the changeset viewer.