Free cookie consent management tool by TermsFeed Policy Generator

Changeset 73


Ignore:
Timestamp:
03/18/08 00:40:57 (17 years ago)
Author:
swagner
Message:

Worked on ticket #15

  • adapted interfaces of static Apply methods
  • added missing variable info Maximization in HeuristicCrossover
  • simplified code
Location:
trunk/sources/HeuristicLab.RealVector
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.RealVector/AverageCrossover.cs

    r70 r73  
    1111    }
    1212
    13     public static double[] Apply(IScope scope, IRandom random, double[] parent1, double[] parent2) {
     13    public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {
    1414      int length = parent1.Length;
    1515      double[] result = new double[length];
    16       double min = scope.GetVariableValue<DoubleData>("Minimum", true).Data;
    17       double max = scope.GetVariableValue<DoubleData>("Maximum", true).Data;
    1816
    1917      for (int i = 0; i < length; i++) {
     
    2321          result[i] = parent1[i];
    2422        }
    25 
    26         // check borders
    27         if (result[i] < min) { result[i] = min; }
    28         if (result[i] > max) { result[i] = max; }
    2923      }
    30 
    3124      return result;
    3225    }
    3326
    3427    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);
    3629    }
    3730  }
  • trunk/sources/HeuristicLab.RealVector/CompleteAverageCrossover.cs

    r70 r73  
    1111    }
    1212
    13     public static double[] Apply(IScope scope, IRandom random, double[] parent1, double[] parent2) {
     13    public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {
    1414      int length = parent1.Length;
    1515      double[] result = new double[length];
    16       double min = scope.GetVariableValue<DoubleData>("Minimum", true).Data;
    17       double max = scope.GetVariableValue<DoubleData>("Maximum", true).Data;
    1816
    19       for (int i = 0; i < length; i++) {
     17      for (int i = 0; i < length; i++)
    2018        result[i] = (parent1[i] + parent2[i]) / 2;
    21 
    22         // check borders
    23         if (result[i] < min) { result[i] = min; }
    24         if (result[i] > max) { result[i] = max; }
    25       }
    26 
    2719      return result;
    2820    }
    2921
    3022    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);
    3224    }
    3325  }
  • trunk/sources/HeuristicLab.RealVector/DiscreteCrossover.cs

    r70 r73  
    1515
    1616      for (int i = 0; i < length; i++) {
    17         if (random.NextDouble() < 0.5) {
     17        if (random.NextDouble() < 0.5)
    1818          result[i] = parent1[i];
    19         } else {
     19        else
    2020          result[i] = parent2[i];
    21         }
    2221      }
    23 
    2422      return result;
    2523    }
  • trunk/sources/HeuristicLab.RealVector/HeuristicCrossover.cs

    r70 r73  
    1010    public HeuristicCrossover()
    1111      : base() {
     12      AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
    1213      AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
    1314      AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
     
    1819    }
    1920
    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) {
    2222      int length = parent1.Length;
    2323      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();
    2925
    3026      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      }
    5232      return result;
    5333    }
    5434
    5535    protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
     36      bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
    5637      IVariableInfo realVectorInfo = GetVariableInfo("RealVector");
    5738      IVariableInfo qualityInfo = GetVariableInfo("Quality");
    5839      DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>(realVectorInfo.ActualName, false);
     40      DoubleData quality1 = parent1.GetVariableValue<DoubleData>(qualityInfo.ActualName, false);
    5941      DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>(realVectorInfo.ActualName, false);
    60       DoubleData quality1 = parent1.GetVariableValue<DoubleData>(qualityInfo.ActualName, false);
    6142      DoubleData quality2 = parent2.GetVariableValue<DoubleData>(qualityInfo.ActualName, false);
    6243
    6344      if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length.");
    6445
    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);
    6647      child.AddVariable(new Variable(realVectorInfo.ActualName, new DoubleArrayData(result)));
    6748    }
  • trunk/sources/HeuristicLab.RealVector/RandomConvexCrossover.cs

    r70 r73  
    1111    }
    1212
    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) {
    1514      int length = parent1.Length;
    1615      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();
    1917
    20       factor = random.NextDouble();
    21 
    22       for (int i = 0; i < length; i++) {
     18      for (int i = 0; i < length; i++)
    2319        result[i] = (factor * parent1[i]) + ((1 - factor) * parent2[i]);
    24 
    25         // check borders
    26         if (result[i] < min) { result[i] = min; }
    27         if (result[i] > max) { result[i] = max; }
    28       }
    29 
    3020      return result;
    3121    }
    3222
    3323    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);
    3525    }
    3626  }
  • trunk/sources/HeuristicLab.RealVector/RandomLocalCrossover.cs

    r70 r73  
    1111    }
    1212
    13     public static double[] Apply(IScope scope, IRandom random, double[] parent1, double[] parent2) {
     13    public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {
    1414      double factor;
    1515      int length = parent1.Length;
    1616      double[] result = new double[length];
    17       double min = scope.GetVariableValue<DoubleData>("Minimum", true).Data;
    18       double max = scope.GetVariableValue<DoubleData>("Maximum", true).Data;
    1917
    2018      for (int i = 0; i < length; i++) {
    2119        factor = random.NextDouble();
    2220        result[i] = (factor * parent1[i]) + ((1 - factor) * parent2[i]);
    23 
    24         // check borders
    25         if (result[i] < min) { result[i] = min; }
    26         if (result[i] > max) { result[i] = max; }
    2721      }
    28 
    2922      return result;
    3023    }
    3124
    3225    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);
    3427    }
    3528  }
Note: See TracChangeset for help on using the changeset viewer.