Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.TestFunctions/3.3/Improvers/SingleObjectiveTestFunctionImprovementOperator.cs @ 12448

Last change on this file since 12448 was 8327, checked in by jkarder, 12 years ago

#1331: applied the rest of the changes suggested by ascheibe in comment:32:ticket:1331

File size: 10.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Reflection;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.TestFunctions {
34  /// <summary>
35  /// An operator that improves test functions solutions.
36  /// </summary>
37  /// <remarks>
38  /// 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.<br />
39  /// The operator uses an implementation of the Nelder-Mead method with adaptive parameters as described in Gao, F. and Han, L. (2010). Implementing the Nelder-Mead simplex algorithm with adaptive parameters. Computational Optimization and Applications, Vol. 51. Springer. and conducts relection, expansion, contraction and reduction on the test functions solution.
40  /// </remarks>
41  [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.")]
42  [StorableClass]
43  public sealed class SingleObjectiveTestFunctionImprovementOperator : SingleSuccessorOperator, ISingleObjectiveImprovementOperator {
44    #region Parameter properties
45    public IValueParameter<DoubleValue> AlphaParameter {
46      get { return (IValueParameter<DoubleValue>)Parameters["Alpha"]; }
47    }
48    public IValueParameter<DoubleValue> BetaParameter {
49      get { return (IValueParameter<DoubleValue>)Parameters["Beta"]; }
50    }
51    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
52      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
53    }
54    public ScopeParameter CurrentScopeParameter {
55      get { return (ScopeParameter)Parameters["CurrentScope"]; }
56    }
57    public IValueParameter<DoubleValue> DeltaParameter {
58      get { return (IValueParameter<DoubleValue>)Parameters["Delta"]; }
59    }
60    public IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
61      get { return (IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
62    }
63    public IValueParameter<DoubleValue> GammaParameter {
64      get { return (IValueParameter<DoubleValue>)Parameters["Gamma"]; }
65    }
66    public IValueLookupParameter<IntValue> ImprovementAttemptsParameter {
67      get { return (IValueLookupParameter<IntValue>)Parameters["ImprovementAttempts"]; }
68    }
69    public IValueLookupParameter<IItem> SolutionParameter {
70      get { return (IValueLookupParameter<IItem>)Parameters["Solution"]; }
71    }
72    #endregion
73
74    #region Properties
75    private DoubleValue Alpha {
76      get { return AlphaParameter.Value; }
77    }
78    private DoubleValue Beta {
79      get { return BetaParameter.Value; }
80    }
81    private DoubleMatrix Bounds {
82      get { return BoundsParameter.ActualValue; }
83    }
84    public IScope CurrentScope {
85      get { return CurrentScopeParameter.ActualValue; }
86    }
87    private DoubleValue Delta {
88      get { return DeltaParameter.Value; }
89    }
90    public ISingleObjectiveTestFunctionProblemEvaluator Evaluator {
91      get { return EvaluatorParameter.ActualValue; }
92    }
93    private DoubleValue Gamma {
94      get { return GammaParameter.Value; }
95    }
96    public IntValue ImprovementAttempts {
97      get { return ImprovementAttemptsParameter.ActualValue; }
98    }
99    #endregion
100
101    [StorableConstructor]
102    private SingleObjectiveTestFunctionImprovementOperator(bool deserializing) : base(deserializing) { }
103    private SingleObjectiveTestFunctionImprovementOperator(SingleObjectiveTestFunctionImprovementOperator original, Cloner cloner) : base(original, cloner) { }
104    public SingleObjectiveTestFunctionImprovementOperator()
105      : base() {
106      #region Create parameters
107      Parameters.Add(new ValueParameter<DoubleValue>("Alpha", new DoubleValue(1.0)));
108      Parameters.Add(new ValueParameter<DoubleValue>("Beta", new DoubleValue(2.0)));
109      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension."));
110      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solution to be improved."));
111      Parameters.Add(new ValueParameter<DoubleValue>("Delta", new DoubleValue(0.5)));
112      Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The operator used to evaluate solutions."));
113      Parameters.Add(new ValueParameter<DoubleValue>("Gamma", new DoubleValue(0.5)));
114      Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
115      Parameters.Add(new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only."));
116      #endregion
117    }
118
119    public override IDeepCloneable Clone(Cloner cloner) {
120      return new SingleObjectiveTestFunctionImprovementOperator(this, cloner);
121    }
122
123    public override IOperation Apply() {
124      RealVector bestSol = CurrentScope.Variables[SolutionParameter.ActualName].Value as RealVector;
125      if (bestSol == null)
126        throw new ArgumentException("Cannot improve solution because it has the wrong type.");
127
128      MethodInfo evaluationMethod = Evaluator.GetType().GetMethod("Apply",
129                                                                  BindingFlags.Public | BindingFlags.Static,
130                                                                  null,
131                                                                  new Type[] { typeof(RealVector) }, null);
132      Func<RealVector, double> functionEvaluator = x => (double)evaluationMethod.Invoke(Evaluator, new object[] { x });
133      double bestSolQuality = functionEvaluator(bestSol);
134
135      // create perturbed solutions
136      RealVector[] simplex = new RealVector[bestSol.Length];
137      for (int i = 0; i < simplex.Length; i++) {
138        simplex[i] = bestSol.Clone() as RealVector;
139        simplex[i][i] += 0.1 * (Bounds[0, 1] - Bounds[0, 0]);
140        if (simplex[i][i] > Bounds[0, 1]) simplex[i][i] = Bounds[0, 1];
141        if (simplex[i][i] < Bounds[0, 0]) simplex[i][i] = Bounds[0, 0];
142      }
143
144      // improve solutions
145      for (int i = 0; i < ImprovementAttempts.Value; i++) {
146        // order according to their objective function value
147        Array.Sort(simplex, (x, y) => functionEvaluator(x).CompareTo(functionEvaluator(y)));
148
149        // calculate centroid
150        RealVector centroid = new RealVector(bestSol.Length);
151        foreach (var vector in simplex)
152          for (int j = 0; j < centroid.Length; j++)
153            centroid[j] += vector[j];
154        for (int j = 0; j < centroid.Length; j++)
155          centroid[j] /= simplex.Length;
156
157        // reflection
158        RealVector reflectionPoint = new RealVector(bestSol.Length);
159        for (int j = 0; j < reflectionPoint.Length; j++)
160          reflectionPoint[j] = centroid[j] + Alpha.Value * (centroid[j] - simplex[simplex.Length - 1][j]);
161        double reflectionPointQuality = functionEvaluator(reflectionPoint);
162        if (functionEvaluator(simplex[0]) <= reflectionPointQuality
163            && reflectionPointQuality < functionEvaluator(simplex[simplex.Length - 2]))
164          simplex[simplex.Length - 1] = reflectionPoint;
165
166        // expansion
167        if (reflectionPointQuality < functionEvaluator(simplex[0])) {
168          RealVector expansionPoint = new RealVector(bestSol.Length);
169          for (int j = 0; j < expansionPoint.Length; j++)
170            expansionPoint[j] = centroid[j] + Beta.Value * (reflectionPoint[j] - centroid[j]);
171          simplex[simplex.Length - 1] = functionEvaluator(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint;
172        }
173
174        // contraction
175        if (functionEvaluator(simplex[simplex.Length - 2]) <= reflectionPointQuality
176            && reflectionPointQuality < functionEvaluator(simplex[simplex.Length - 1])) {
177          RealVector outsideContractionPoint = new RealVector(bestSol.Length);
178          for (int j = 0; j < outsideContractionPoint.Length; j++)
179            outsideContractionPoint[j] = centroid[j] + Gamma.Value * (reflectionPoint[j] - centroid[j]);
180          if (functionEvaluator(outsideContractionPoint) <= reflectionPointQuality) {
181            simplex[simplex.Length - 1] = outsideContractionPoint;
182            if (functionEvaluator(reflectionPoint) >= functionEvaluator(simplex[simplex.Length - 1])) {
183              RealVector insideContractionPoint = new RealVector(bestSol.Length);
184              for (int j = 0; j < insideContractionPoint.Length; j++)
185                insideContractionPoint[j] = centroid[j] - Gamma.Value * (reflectionPoint[j] - centroid[j]);
186              if (functionEvaluator(insideContractionPoint) < functionEvaluator(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint;
187            }
188          }
189        }
190
191        // reduction
192        for (int j = 1; j < simplex.Length; j++)
193          for (int k = 0; k < simplex[j].Length; k++)
194            simplex[j][k] = simplex[0][k] + Delta.Value * (simplex[j][k] - simplex[0][k]);
195      }
196
197      for (int i = 0; i < simplex[0].Length; i++) {
198        if (simplex[0][i] > Bounds[0, 1]) simplex[0][i] = Bounds[0, 1];
199        if (simplex[0][i] < Bounds[0, 0]) simplex[0][i] = Bounds[0, 0];
200      }
201
202      CurrentScope.Variables[SolutionParameter.ActualName].Value = simplex[0];
203      CurrentScope.Variables.Add(new Variable("LocalEvaluatedSolutions", ImprovementAttempts));
204
205      return base.Apply();
206    }
207  }
208}
Note: See TracBrowser for help on using the repository browser.