Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/25/15 23:24:01 (8 years ago)
Author:
abeham
Message:

#2521:

  • Adapted single-objective test function problem to new problem infrastructure
  • Added additional interfaces to RealVectorEncoding
  • Fixed IParticleUpdater interface (must implement IStochasticOperator if it contains a Random parameter)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/Improvers/SingleObjectiveTestFunctionImprovementOperator.cs

    r12012 r13403  
    2626using HeuristicLab.Encodings.RealVectorEncoding;
    2727using HeuristicLab.Operators;
    28 using HeuristicLab.Optimization;
    2928using HeuristicLab.Parameters;
    3029using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    4039  [Item("SingleObjectiveTestFunctionImprovementOperator", "An operator that improves test functions solutions. It is implemented as described in Laguna, M. and Martí, R. (2003). Scatter Search: Methodology and Implementations in C. Operations Research/Computer Science Interfaces Series, Vol. 24. Springer.")]
    4140  [StorableClass]
    42   public sealed class SingleObjectiveTestFunctionImprovementOperator : SingleSuccessorOperator, ISingleObjectiveImprovementOperator {
     41  public sealed class SingleObjectiveTestFunctionImprovementOperator : SingleSuccessorOperator, ISingleObjectiveTestFunctionImprovementOperator {
    4342    #region Parameter properties
    4443    public IValueParameter<DoubleValue> AlphaParameter {
     
    5150      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
    5251    }
    53     public ScopeParameter CurrentScopeParameter {
    54       get { return (ScopeParameter)Parameters["CurrentScope"]; }
    55     }
    5652    public IValueParameter<DoubleValue> DeltaParameter {
    5753      get { return (IValueParameter<DoubleValue>)Parameters["Delta"]; }
    5854    }
    59     public IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
    60       get { return (IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
     55    public IValueLookupParameter<ISingleObjectiveTestFunction> TestFunctionParameter {
     56      get { return (IValueLookupParameter<ISingleObjectiveTestFunction>)Parameters["TestFunction"]; }
    6157    }
    6258    public IValueParameter<DoubleValue> GammaParameter {
     
    7874      get { return BetaParameter.Value; }
    7975    }
    80     private DoubleMatrix Bounds {
    81       get { return BoundsParameter.ActualValue; }
    82     }
    83     public IScope CurrentScope {
    84       get { return CurrentScopeParameter.ActualValue; }
    85     }
    8676    private DoubleValue Delta {
    8777      get { return DeltaParameter.Value; }
    8878    }
    89     public ISingleObjectiveTestFunctionProblemEvaluator Evaluator {
    90       get { return EvaluatorParameter.ActualValue; }
    91     }
    9279    private DoubleValue Gamma {
    9380      get { return GammaParameter.Value; }
    94     }
    95     public IntValue ImprovementAttempts {
    96       get { return ImprovementAttemptsParameter.ActualValue; }
    9781    }
    9882    #endregion
     
    10690      Parameters.Add(new ValueParameter<DoubleValue>("Alpha", new DoubleValue(1.0)));
    10791      Parameters.Add(new ValueParameter<DoubleValue>("Beta", new DoubleValue(2.0)));
     92      Parameters.Add(new ValueParameter<DoubleValue>("Delta", new DoubleValue(0.5)));
     93      Parameters.Add(new ValueParameter<DoubleValue>("Gamma", new DoubleValue(0.5)));
     94      Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunction>("TestFunction", "The operator used to evaluate solutions."));
    10895      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension."));
    109       Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solution to be improved."));
    110       Parameters.Add(new ValueParameter<DoubleValue>("Delta", new DoubleValue(0.5)));
    111       Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The operator used to evaluate solutions."));
    112       Parameters.Add(new ValueParameter<DoubleValue>("Gamma", new DoubleValue(0.5)));
    11396      Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
    114       Parameters.Add(new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only."));
     97      Parameters.Add(new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only.")); // TODO: Problematic, this cannot be wired! IImprovementOperators need to be generic
    11598      #endregion
    11699    }
     
    121104
    122105    public override IOperation Apply() {
    123       RealVector bestSol = CurrentScope.Variables[SolutionParameter.ActualName].Value as RealVector;
     106      RealVector bestSol = ExecutionContext.Scope.Variables[SolutionParameter.ActualName].Value as RealVector;
    124107      if (bestSol == null)
    125108        throw new ArgumentException("Cannot improve solution because it has the wrong type.");
    126109
    127       var evaluator = Evaluator;
     110      var bounds = BoundsParameter.ActualValue;
     111      var function = TestFunctionParameter.ActualValue;
     112      var maxIterations = ImprovementAttemptsParameter.ActualValue.Value;
    128113
    129       double bestSolQuality = evaluator.Evaluate(bestSol);
     114      double bestSolQuality = function.Evaluate(bestSol);
    130115
    131116      // create perturbed solutions
     
    133118      for (int i = 0; i < simplex.Length; i++) {
    134119        simplex[i] = bestSol.Clone() as RealVector;
    135         simplex[i][i] += 0.1 * (Bounds[0, 1] - Bounds[0, 0]);
    136         if (simplex[i][i] > Bounds[0, 1]) simplex[i][i] = Bounds[0, 1];
    137         if (simplex[i][i] < Bounds[0, 0]) simplex[i][i] = Bounds[0, 0];
     120        simplex[i][i] += 0.1 * (bounds[0, 1] - bounds[0, 0]);
     121        if (simplex[i][i] > bounds[0, 1]) simplex[i][i] = bounds[0, 1];
     122        if (simplex[i][i] < bounds[0, 0]) simplex[i][i] = bounds[0, 0];
    138123      }
    139124
    140125      // improve solutions
    141       for (int i = 0; i < ImprovementAttempts.Value; i++) {
     126      for (int i = 0; i < maxIterations; i++) {
    142127        // order according to their objective function value
    143         Array.Sort(simplex, (x, y) => evaluator.Evaluate(x).CompareTo(evaluator.Evaluate(y)));
     128        Array.Sort(simplex, (x, y) => function.Evaluate(x).CompareTo(function.Evaluate(y)));
    144129
    145130        // calculate centroid
     
    155140        for (int j = 0; j < reflectionPoint.Length; j++)
    156141          reflectionPoint[j] = centroid[j] + Alpha.Value * (centroid[j] - simplex[simplex.Length - 1][j]);
    157         double reflectionPointQuality = evaluator.Evaluate(reflectionPoint);
    158         if (evaluator.Evaluate(simplex[0]) <= reflectionPointQuality
    159             && reflectionPointQuality < evaluator.Evaluate(simplex[simplex.Length - 2]))
     142        double reflectionPointQuality = function.Evaluate(reflectionPoint);
     143        if (function.Evaluate(simplex[0]) <= reflectionPointQuality
     144            && reflectionPointQuality < function.Evaluate(simplex[simplex.Length - 2]))
    160145          simplex[simplex.Length - 1] = reflectionPoint;
    161146
    162147        // expansion
    163         if (reflectionPointQuality < evaluator.Evaluate(simplex[0])) {
     148        if (reflectionPointQuality < function.Evaluate(simplex[0])) {
    164149          RealVector expansionPoint = new RealVector(bestSol.Length);
    165150          for (int j = 0; j < expansionPoint.Length; j++)
    166151            expansionPoint[j] = centroid[j] + Beta.Value * (reflectionPoint[j] - centroid[j]);
    167           simplex[simplex.Length - 1] = evaluator.Evaluate(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint;
     152          simplex[simplex.Length - 1] = function.Evaluate(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint;
    168153        }
    169154
    170155        // contraction
    171         if (evaluator.Evaluate(simplex[simplex.Length - 2]) <= reflectionPointQuality
    172             && reflectionPointQuality < evaluator.Evaluate(simplex[simplex.Length - 1])) {
     156        if (function.Evaluate(simplex[simplex.Length - 2]) <= reflectionPointQuality
     157            && reflectionPointQuality < function.Evaluate(simplex[simplex.Length - 1])) {
    173158          RealVector outsideContractionPoint = new RealVector(bestSol.Length);
    174159          for (int j = 0; j < outsideContractionPoint.Length; j++)
    175160            outsideContractionPoint[j] = centroid[j] + Gamma.Value * (reflectionPoint[j] - centroid[j]);
    176           if (evaluator.Evaluate(outsideContractionPoint) <= reflectionPointQuality) {
     161          if (function.Evaluate(outsideContractionPoint) <= reflectionPointQuality) {
    177162            simplex[simplex.Length - 1] = outsideContractionPoint;
    178             if (evaluator.Evaluate(reflectionPoint) >= evaluator.Evaluate(simplex[simplex.Length - 1])) {
     163            if (function.Evaluate(reflectionPoint) >= function.Evaluate(simplex[simplex.Length - 1])) {
    179164              RealVector insideContractionPoint = new RealVector(bestSol.Length);
    180165              for (int j = 0; j < insideContractionPoint.Length; j++)
    181166                insideContractionPoint[j] = centroid[j] - Gamma.Value * (reflectionPoint[j] - centroid[j]);
    182               if (evaluator.Evaluate(insideContractionPoint) < evaluator.Evaluate(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint;
     167              if (function.Evaluate(insideContractionPoint) < function.Evaluate(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint;
    183168            }
    184169          }
     
    192177
    193178      for (int i = 0; i < simplex[0].Length; i++) {
    194         if (simplex[0][i] > Bounds[0, 1]) simplex[0][i] = Bounds[0, 1];
    195         if (simplex[0][i] < Bounds[0, 0]) simplex[0][i] = Bounds[0, 0];
     179        if (simplex[0][i] > bounds[0, 1]) simplex[0][i] = bounds[0, 1];
     180        if (simplex[0][i] < bounds[0, 0]) simplex[0][i] = bounds[0, 0];
    196181      }
    197182
    198       CurrentScope.Variables[SolutionParameter.ActualName].Value = simplex[0];
    199       CurrentScope.Variables.Add(new Variable("LocalEvaluatedSolutions", ImprovementAttempts));
     183      ExecutionContext.Scope.Variables[SolutionParameter.ActualName].Value = simplex[0];
     184      ExecutionContext.Scope.Variables.Add(new Variable("LocalEvaluatedSolutions", new IntValue(maxIterations)));
    200185
    201186      return base.Apply();
Note: See TracChangeset for help on using the changeset viewer.