Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ImprovingStringConvertibleMatrix/HeuristicLab.Problems.TestFunctions/3.3/Improvers/SingleObjectiveTestFunctionImprovementOperator.cs @ 9420

Last change on this file since 9420 was 8987, checked in by jkarder, 12 years ago

#1331: fixed lookup of the method used to evaluate TestFunctions in the SingleObjectiveTestFunctionImprovementOperator

File size: 10.8 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("EvaluateFunction",
129                                                                  BindingFlags.Instance | BindingFlags.NonPublic,
130                                                                  null,
131                                                                  new[] { typeof(RealVector) },
132                                                                  null);
133      Func<RealVector, double> functionEvaluator = x => (double)evaluationMethod.Invoke(Evaluator, new object[] { x });
134      double bestSolQuality = functionEvaluator(bestSol);
135
136      // create perturbed solutions
137      RealVector[] simplex = new RealVector[bestSol.Length];
138      for (int i = 0; i < simplex.Length; i++) {
139        simplex[i] = bestSol.Clone() as RealVector;
140        simplex[i][i] += 0.1 * (Bounds[0, 1] - Bounds[0, 0]);
141        if (simplex[i][i] > Bounds[0, 1]) simplex[i][i] = Bounds[0, 1];
142        if (simplex[i][i] < Bounds[0, 0]) simplex[i][i] = Bounds[0, 0];
143      }
144
145      // improve solutions
146      for (int i = 0; i < ImprovementAttempts.Value; i++) {
147        // order according to their objective function value
148        Array.Sort(simplex, (x, y) => functionEvaluator(x).CompareTo(functionEvaluator(y)));
149
150        // calculate centroid
151        RealVector centroid = new RealVector(bestSol.Length);
152        foreach (var vector in simplex)
153          for (int j = 0; j < centroid.Length; j++)
154            centroid[j] += vector[j];
155        for (int j = 0; j < centroid.Length; j++)
156          centroid[j] /= simplex.Length;
157
158        // reflection
159        RealVector reflectionPoint = new RealVector(bestSol.Length);
160        for (int j = 0; j < reflectionPoint.Length; j++)
161          reflectionPoint[j] = centroid[j] + Alpha.Value * (centroid[j] - simplex[simplex.Length - 1][j]);
162        double reflectionPointQuality = functionEvaluator(reflectionPoint);
163        if (functionEvaluator(simplex[0]) <= reflectionPointQuality
164            && reflectionPointQuality < functionEvaluator(simplex[simplex.Length - 2]))
165          simplex[simplex.Length - 1] = reflectionPoint;
166
167        // expansion
168        if (reflectionPointQuality < functionEvaluator(simplex[0])) {
169          RealVector expansionPoint = new RealVector(bestSol.Length);
170          for (int j = 0; j < expansionPoint.Length; j++)
171            expansionPoint[j] = centroid[j] + Beta.Value * (reflectionPoint[j] - centroid[j]);
172          simplex[simplex.Length - 1] = functionEvaluator(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint;
173        }
174
175        // contraction
176        if (functionEvaluator(simplex[simplex.Length - 2]) <= reflectionPointQuality
177            && reflectionPointQuality < functionEvaluator(simplex[simplex.Length - 1])) {
178          RealVector outsideContractionPoint = new RealVector(bestSol.Length);
179          for (int j = 0; j < outsideContractionPoint.Length; j++)
180            outsideContractionPoint[j] = centroid[j] + Gamma.Value * (reflectionPoint[j] - centroid[j]);
181          if (functionEvaluator(outsideContractionPoint) <= reflectionPointQuality) {
182            simplex[simplex.Length - 1] = outsideContractionPoint;
183            if (functionEvaluator(reflectionPoint) >= functionEvaluator(simplex[simplex.Length - 1])) {
184              RealVector insideContractionPoint = new RealVector(bestSol.Length);
185              for (int j = 0; j < insideContractionPoint.Length; j++)
186                insideContractionPoint[j] = centroid[j] - Gamma.Value * (reflectionPoint[j] - centroid[j]);
187              if (functionEvaluator(insideContractionPoint) < functionEvaluator(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint;
188            }
189          }
190        }
191
192        // reduction
193        for (int j = 1; j < simplex.Length; j++)
194          for (int k = 0; k < simplex[j].Length; k++)
195            simplex[j][k] = simplex[0][k] + Delta.Value * (simplex[j][k] - simplex[0][k]);
196      }
197
198      for (int i = 0; i < simplex[0].Length; i++) {
199        if (simplex[0][i] > Bounds[0, 1]) simplex[0][i] = Bounds[0, 1];
200        if (simplex[0][i] < Bounds[0, 0]) simplex[0][i] = Bounds[0, 0];
201      }
202
203      CurrentScope.Variables[SolutionParameter.ActualName].Value = simplex[0];
204      CurrentScope.Variables.Add(new Variable("LocalEvaluatedSolutions", ImprovementAttempts));
205
206      return base.Apply();
207    }
208  }
209}
Note: See TracBrowser for help on using the repository browser.