Changeset 73
- Timestamp:
- 03/18/08 00:40:57 (17 years ago)
- Location:
- trunk/sources/HeuristicLab.RealVector
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.RealVector/AverageCrossover.cs
r70 r73 11 11 } 12 12 13 public static double[] Apply(I Scope scope, IRandom random, double[] parent1, double[] parent2) {13 public static double[] Apply(IRandom random, double[] parent1, double[] parent2) { 14 14 int length = parent1.Length; 15 15 double[] result = new double[length]; 16 double min = scope.GetVariableValue<DoubleData>("Minimum", true).Data;17 double max = scope.GetVariableValue<DoubleData>("Maximum", true).Data;18 16 19 17 for (int i = 0; i < length; i++) { … … 23 21 result[i] = parent1[i]; 24 22 } 25 26 // check borders27 if (result[i] < min) { result[i] = min; }28 if (result[i] > max) { result[i] = max; }29 23 } 30 31 24 return result; 32 25 } 33 26 34 27 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) { 35 return Apply( scope,random, parent1, parent2);28 return Apply(random, parent1, parent2); 36 29 } 37 30 } -
trunk/sources/HeuristicLab.RealVector/CompleteAverageCrossover.cs
r70 r73 11 11 } 12 12 13 public static double[] Apply(I Scope scope, IRandom random, double[] parent1, double[] parent2) {13 public static double[] Apply(IRandom random, double[] parent1, double[] parent2) { 14 14 int length = parent1.Length; 15 15 double[] result = new double[length]; 16 double min = scope.GetVariableValue<DoubleData>("Minimum", true).Data;17 double max = scope.GetVariableValue<DoubleData>("Maximum", true).Data;18 16 19 for (int i = 0; i < length; i++) {17 for (int i = 0; i < length; i++) 20 18 result[i] = (parent1[i] + parent2[i]) / 2; 21 22 // check borders23 if (result[i] < min) { result[i] = min; }24 if (result[i] > max) { result[i] = max; }25 }26 27 19 return result; 28 20 } 29 21 30 22 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) { 31 return Apply( scope,random, parent1, parent2);23 return Apply(random, parent1, parent2); 32 24 } 33 25 } -
trunk/sources/HeuristicLab.RealVector/DiscreteCrossover.cs
r70 r73 15 15 16 16 for (int i = 0; i < length; i++) { 17 if (random.NextDouble() < 0.5) {17 if (random.NextDouble() < 0.5) 18 18 result[i] = parent1[i]; 19 } else {19 else 20 20 result[i] = parent2[i]; 21 }22 21 } 23 24 22 return result; 25 23 } -
trunk/sources/HeuristicLab.RealVector/HeuristicCrossover.cs
r70 r73 10 10 public HeuristicCrossover() 11 11 : base() { 12 AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In)); 12 13 AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In)); 13 14 AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New)); … … 18 19 } 19 20 20 public static double[] Apply(IScope scope, IRandom random, double[] parent1, double[] parent2, double quality1, double quality2) { 21 double factor; 21 public static double[] Apply(IRandom random, bool maximization, double[] parent1, double quality1, double[] parent2, double quality2) { 22 22 int length = parent1.Length; 23 23 double[] result = new double[length]; 24 bool isMaximization = scope.GetVariableValue<BoolData>("Maximization", true).Data; 25 double min = scope.GetVariableValue<DoubleData>("Minimum", true).Data; 26 double max = scope.GetVariableValue<DoubleData>("Maximum", true).Data; 27 28 factor = random.NextDouble(); 24 double factor = random.NextDouble(); 29 25 30 26 for (int i = 0; i < length; i++) { 31 if (isMaximization) { 32 // maximization problem 33 if (quality1 > quality2) { 34 result[i] = parent1[i] + factor * (parent1[i] - parent2[i]); 35 } else { 36 result[i] = parent2[i] + factor * (parent2[i] - parent1[i]); 37 } // if 38 } else { 39 // minimization problem 40 if (quality1 < quality2) { 41 result[i] = parent1[i] + factor * (parent1[i] - parent2[i]); 42 } else { 43 result[i] = parent2[i] + factor * (parent2[i] - parent1[i]); 44 } // if 45 } // if 46 47 // check borders 48 if (result[i] < min) { result[i] = min; } 49 if (result[i] > max) { result[i] = max; } 50 } // for 51 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]); 31 } 52 32 return result; 53 33 } 54 34 55 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; 56 37 IVariableInfo realVectorInfo = GetVariableInfo("RealVector"); 57 38 IVariableInfo qualityInfo = GetVariableInfo("Quality"); 58 39 DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>(realVectorInfo.ActualName, false); 40 DoubleData quality1 = parent1.GetVariableValue<DoubleData>(qualityInfo.ActualName, false); 59 41 DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>(realVectorInfo.ActualName, false); 60 DoubleData quality1 = parent1.GetVariableValue<DoubleData>(qualityInfo.ActualName, false);61 42 DoubleData quality2 = parent2.GetVariableValue<DoubleData>(qualityInfo.ActualName, false); 62 43 63 44 if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length."); 64 45 65 double[] result = Apply( scope, random, vector1.Data, vector2.Data, quality1.Data, quality2.Data);46 double[] result = Apply(random, maximization, vector1.Data, quality1.Data, vector2.Data, quality2.Data); 66 47 child.AddVariable(new Variable(realVectorInfo.ActualName, new DoubleArrayData(result))); 67 48 } -
trunk/sources/HeuristicLab.RealVector/RandomConvexCrossover.cs
r70 r73 11 11 } 12 12 13 public static double[] Apply(IScope scope, IRandom random, double[] parent1, double[] parent2) { 14 double factor; 13 public static double[] Apply(IRandom random, double[] parent1, double[] parent2) { 15 14 int length = parent1.Length; 16 15 double[] result = new double[length]; 17 double min = scope.GetVariableValue<DoubleData>("Minimum", true).Data; 18 double max = scope.GetVariableValue<DoubleData>("Maximum", true).Data; 16 double factor = random.NextDouble(); 19 17 20 factor = random.NextDouble(); 21 22 for (int i = 0; i < length; i++) { 18 for (int i = 0; i < length; i++) 23 19 result[i] = (factor * parent1[i]) + ((1 - factor) * parent2[i]); 24 25 // check borders26 if (result[i] < min) { result[i] = min; }27 if (result[i] > max) { result[i] = max; }28 }29 30 20 return result; 31 21 } 32 22 33 23 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) { 34 return Apply( scope,random, parent1, parent2);24 return Apply(random, parent1, parent2); 35 25 } 36 26 } -
trunk/sources/HeuristicLab.RealVector/RandomLocalCrossover.cs
r70 r73 11 11 } 12 12 13 public static double[] Apply(I Scope scope, IRandom random, double[] parent1, double[] parent2) {13 public static double[] Apply(IRandom random, double[] parent1, double[] parent2) { 14 14 double factor; 15 15 int length = parent1.Length; 16 16 double[] result = new double[length]; 17 double min = scope.GetVariableValue<DoubleData>("Minimum", true).Data;18 double max = scope.GetVariableValue<DoubleData>("Maximum", true).Data;19 17 20 18 for (int i = 0; i < length; i++) { 21 19 factor = random.NextDouble(); 22 20 result[i] = (factor * parent1[i]) + ((1 - factor) * parent2[i]); 23 24 // check borders25 if (result[i] < min) { result[i] = min; }26 if (result[i] > max) { result[i] = max; }27 21 } 28 29 22 return result; 30 23 } 31 24 32 25 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) { 33 return Apply( scope,random, parent1, parent2);26 return Apply(random, parent1, parent2); 34 27 } 35 28 }
Note: See TracChangeset
for help on using the changeset viewer.