Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TestFunctions/3.3/Improvers/SingleObjectiveTestFunctionImprovementOperator.cs @ 8086

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

#1331:

  • synced branch with trunk
  • added custom interface (ISimilarityBasedOperator) to mark operators that conduct similarity calculation
  • similarity calculators are now parameterized by the algorithm
  • deleted SolutionPool2TierUpdateMethod
  • deleted KnapsackMultipleGuidesPathRelinker
  • moved IImprovementOperator, IPathRelinker and ISimilarityCalculator to HeuristicLab.Optimization
  • added parameter descriptions
  • fixed plugin references
  • fixed count of EvaluatedSolutions
  • fixed check for duplicate solutions
  • minor code improvements
File size: 9.9 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  [Item("SingleObjectiveTestFunctionImprovementOperator", "An operator that improves test functions solutions.")]
38  [StorableClass]
39  public sealed class SingleObjectiveTestFunctionImprovementOperator : SingleSuccessorOperator, IImprovementOperator {
40    #region Parameter properties
41    public IValueParameter<DoubleValue> AlphaParameter {
42      get { return (IValueParameter<DoubleValue>)Parameters["Alpha"]; }
43    }
44    public IValueParameter<DoubleValue> BetaParameter {
45      get { return (IValueParameter<DoubleValue>)Parameters["Beta"]; }
46    }
47    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
48      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
49    }
50    public ScopeParameter CurrentScopeParameter {
51      get { return (ScopeParameter)Parameters["CurrentScope"]; }
52    }
53    public IValueParameter<DoubleValue> DeltaParameter {
54      get { return (IValueParameter<DoubleValue>)Parameters["Delta"]; }
55    }
56    public IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
57      get { return (IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
58    }
59    public IValueParameter<DoubleValue> GammaParameter {
60      get { return (IValueParameter<DoubleValue>)Parameters["Gamma"]; }
61    }
62    public IValueLookupParameter<IntValue> ImprovementAttemptsParameter {
63      get { return (IValueLookupParameter<IntValue>)Parameters["ImprovementAttempts"]; }
64    }
65    public IValueLookupParameter<IItem> TargetParameter {
66      get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
67    }
68    #endregion
69
70    #region Properties
71    private DoubleValue Alpha {
72      get { return AlphaParameter.Value; }
73    }
74    private DoubleValue Beta {
75      get { return BetaParameter.Value; }
76    }
77    private DoubleMatrix Bounds {
78      get { return BoundsParameter.ActualValue; }
79    }
80    public IScope CurrentScope {
81      get { return CurrentScopeParameter.ActualValue; }
82    }
83    private DoubleValue Delta {
84      get { return DeltaParameter.Value; }
85    }
86    public ISingleObjectiveTestFunctionProblemEvaluator Evaluator {
87      get { return EvaluatorParameter.ActualValue; }
88    }
89    private DoubleValue Gamma {
90      get { return GammaParameter.Value; }
91    }
92    public IntValue ImprovementAttempts {
93      get { return ImprovementAttemptsParameter.ActualValue; }
94    }
95    #endregion
96
97    [StorableConstructor]
98    private SingleObjectiveTestFunctionImprovementOperator(bool deserializing) : base(deserializing) { }
99    private SingleObjectiveTestFunctionImprovementOperator(SingleObjectiveTestFunctionImprovementOperator original, Cloner cloner) : base(original, cloner) { }
100    public SingleObjectiveTestFunctionImprovementOperator()
101      : base() {
102      #region Create parameters
103      Parameters.Add(new ValueParameter<DoubleValue>("Alpha", new DoubleValue(1.0)));
104      Parameters.Add(new ValueParameter<DoubleValue>("Beta", new DoubleValue(2.0)));
105      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension."));
106      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solution to be improved."));
107      Parameters.Add(new ValueParameter<DoubleValue>("Delta", new DoubleValue(0.5)));
108      Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The operator used to evaluate solutions."));
109      Parameters.Add(new ValueParameter<DoubleValue>("Gamma", new DoubleValue(0.5)));
110      Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
111      Parameters.Add(new ValueLookupParameter<IItem>("Target", "This parameter is used for name translation only."));
112      #endregion
113    }
114
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new SingleObjectiveTestFunctionImprovementOperator(this, cloner);
117    }
118
119    public override IOperation Apply() {
120      RealVector bestSol = CurrentScope.Variables[TargetParameter.ActualName].Value as RealVector;
121      if (bestSol == null)
122        throw new ArgumentException("Cannot improve solution because it has the wrong type.");
123
124      MethodInfo evaluationMethod = Evaluator.GetType().GetMethod("Apply",
125                                                                  BindingFlags.Public | BindingFlags.Static,
126                                                                  null,
127                                                                  new Type[] { typeof(RealVector) }, null);
128      Func<RealVector, double> functionEvaluator = x => (double)evaluationMethod.Invoke(Evaluator, new object[] { x });
129      double bestSolQuality = functionEvaluator(bestSol);
130
131      // create perturbed solutions
132      RealVector[] simplex = new RealVector[bestSol.Length];
133      for (int i = 0; i < simplex.Length; i++) {
134        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];
138      }
139
140      // improve solutions
141      for (int i = 0; i < ImprovementAttempts.Value; i++) {
142        // order according to their objective function value
143        Array.Sort(simplex, (x, y) => functionEvaluator(x).CompareTo(functionEvaluator(y)));
144
145        // calculate centroid
146        RealVector centroid = new RealVector(bestSol.Length);
147        foreach (var vector in simplex)
148          for (int j = 0; j < centroid.Length; j++)
149            centroid[j] += vector[j];
150        for (int j = 0; j < centroid.Length; j++)
151          centroid[j] /= simplex.Length;
152
153        // reflection
154        RealVector reflectionPoint = new RealVector(bestSol.Length);
155        for (int j = 0; j < reflectionPoint.Length; j++)
156          reflectionPoint[j] = centroid[j] + Alpha.Value * (centroid[j] - simplex[simplex.Length - 1][j]);
157        double reflectionPointQuality = functionEvaluator(reflectionPoint);
158        if (functionEvaluator(simplex[0]) <= reflectionPointQuality
159            && reflectionPointQuality < functionEvaluator(simplex[simplex.Length - 2]))
160          simplex[simplex.Length - 1] = reflectionPoint;
161
162        // expansion
163        if (reflectionPointQuality < functionEvaluator(simplex[0])) {
164          RealVector expansionPoint = new RealVector(bestSol.Length);
165          for (int j = 0; j < expansionPoint.Length; j++)
166            expansionPoint[j] = centroid[j] + Beta.Value * (reflectionPoint[j] - centroid[j]);
167          simplex[simplex.Length - 1] = functionEvaluator(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint;
168        }
169
170        // contraction
171        if (functionEvaluator(simplex[simplex.Length - 2]) <= reflectionPointQuality
172            && reflectionPointQuality < functionEvaluator(simplex[simplex.Length - 1])) {
173          RealVector outsideContractionPoint = new RealVector(bestSol.Length);
174          for (int j = 0; j < outsideContractionPoint.Length; j++)
175            outsideContractionPoint[j] = centroid[j] + Gamma.Value * (reflectionPoint[j] - centroid[j]);
176          if (functionEvaluator(outsideContractionPoint) <= reflectionPointQuality) {
177            simplex[simplex.Length - 1] = outsideContractionPoint;
178            if (functionEvaluator(reflectionPoint) >= functionEvaluator(simplex[simplex.Length - 1])) {
179              RealVector insideContractionPoint = new RealVector(bestSol.Length);
180              for (int j = 0; j < insideContractionPoint.Length; j++)
181                insideContractionPoint[j] = centroid[j] - Gamma.Value * (reflectionPoint[j] - centroid[j]);
182              if (functionEvaluator(insideContractionPoint) < functionEvaluator(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint;
183            }
184          }
185        }
186
187        // reduction
188        for (int j = 1; j < simplex.Length; j++)
189          for (int k = 0; k < simplex[j].Length; k++)
190            simplex[j][k] = simplex[0][k] + Delta.Value * (simplex[j][k] - simplex[0][k]);
191      }
192
193      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];
196      }
197
198      CurrentScope.Variables[TargetParameter.ActualName].Value = simplex[0];
199      CurrentScope.Variables.Add(new Variable("LocalEvaluatedSolutions", ImprovementAttempts));
200
201      return base.Apply();
202    }
203  }
204}
Note: See TracBrowser for help on using the repository browser.