Changeset 75 for trunk/sources/HeuristicLab.RealVector
- Timestamp:
- 03/18/08 12:15:06 (17 years ago)
- Location:
- trunk/sources/HeuristicLab.RealVector
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.RealVector/BoundsChecker.cs
r74 r75 4 4 using HeuristicLab.Core; 5 5 using HeuristicLab.Data; 6 using HeuristicLab.Evolutionary;7 6 8 7 namespace HeuristicLab.RealVector { 9 class HeuristicCrossover : CrossoverBase { 10 public HeuristicCrossover() 11 : base() { 12 AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In)); 13 AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In)); 14 AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New)); 8 class BoundsChecker : OperatorBase { 9 public override string Description { 10 get { return "Checks if all elements of a real vector are inside a given minimum and maximum value. If not, elements are corrected."; } 15 11 } 16 12 17 public override string Description { 18 get { return "Heuristic crossover for real vectors."; } 13 public BoundsChecker() 14 : base() { 15 AddVariableInfo(new VariableInfo("RealVector", "Real vector to check", typeof(DoubleArrayData), VariableKind.In | VariableKind.Out)); 16 AddVariableInfo(new VariableInfo("Minimum", "Minimum value of each vector element (included).", typeof(DoubleData), VariableKind.In)); 17 AddVariableInfo(new VariableInfo("Maximum", "Maximum value of each vector element (included).", typeof(DoubleData), VariableKind.In)); 19 18 } 20 19 21 public static double[] Apply(IRandom random, bool maximization, double[] parent1, double quality1, double[] parent2, double quality2) { 22 int length = parent1.Length; 23 double[] result = new double[length]; 24 double factor = random.NextDouble(); 20 public static double[] Apply(double min, double max, double[] vector) { 21 int length = vector.Length; 22 double[] result = (double[])vector.Clone(); 25 23 26 24 for (int i = 0; i < length; i++) { 27 if ((maximization && (quality1 > quality2)) || ((!maximization) && (quality1 < quality2))) 28 result[i] = parent1[i] + factor * (parent1[i] - parent2[i]); 29 else 30 result[i] = parent2[i] + factor * (parent2[i] - parent1[i]); 25 if (result[i] < min) result[i] = min; 26 if (result[i] > max) result[i] = max; 31 27 } 32 28 return result; 33 29 } 34 30 35 protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) { 36 bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data; 37 IVariableInfo realVectorInfo = GetVariableInfo("RealVector"); 38 IVariableInfo qualityInfo = GetVariableInfo("Quality"); 39 DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>(realVectorInfo.ActualName, false); 40 DoubleData quality1 = parent1.GetVariableValue<DoubleData>(qualityInfo.ActualName, false); 41 DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>(realVectorInfo.ActualName, false); 42 DoubleData quality2 = parent2.GetVariableValue<DoubleData>(qualityInfo.ActualName, false); 43 44 if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length."); 45 46 double[] result = Apply(random, maximization, vector1.Data, quality1.Data, vector2.Data, quality2.Data); 47 child.AddVariable(new Variable(realVectorInfo.ActualName, new DoubleArrayData(result))); 31 public override IOperation Apply(IScope scope) { 32 DoubleArrayData vector = GetVariableValue<DoubleArrayData>("RealVector", scope, false); 33 double min = GetVariableValue<DoubleData>("Minimum", scope, true).Data; 34 double max = GetVariableValue<DoubleData>("Maximum", scope, true).Data; 35 vector.Data = Apply(min, max, vector.Data); 36 return null; 48 37 } 49 38 } -
trunk/sources/HeuristicLab.RealVector/HeuristicLab.RealVector.csproj
r74 r75 42 42 <Compile Include="ContinuousCrossover.cs" /> 43 43 <Compile Include="CompleteContinuousCrossover.cs" /> 44 <Compile Include="BoundsChecker.cs" /> 44 45 <Compile Include="DiscreteCrossover.cs" /> 45 46 <Compile Include="HeuristicCrossover.cs" />
Note: See TracChangeset
for help on using the changeset viewer.