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

Last change on this file since 7789 was 7789, checked in by jkarder, 8 years ago

#1331:

  • added Scatter Search algorithm
  • added problem specific operators for improvement, path relinking and similarity calculation
  • adjusted event handling
File size: 9.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.Operators;
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 ScopeParameter CurrentScopeParameter {
48      get { return (ScopeParameter)Parameters["CurrentScope"]; }
49    }
50    public IValueParameter<DoubleValue> DeltaParameter {
51      get { return (IValueParameter<DoubleValue>)Parameters["Delta"]; }
52    }
53    public IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
54      get { return (IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
55    }
56    public IValueParameter<DoubleValue> GammaParameter {
57      get { return (IValueParameter<DoubleValue>)Parameters["Gamma"]; }
58    }
59    public IValueLookupParameter<IntValue> ImprovementAttemptsParameter {
60      get { return (IValueLookupParameter<IntValue>)Parameters["ImprovementAttempts"]; }
61    }
62    public IValueLookupParameter<IRandom> RandomParameter {
63      get { return (IValueLookupParameter<IRandom>)Parameters["Random"]; }
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    public IScope CurrentScope {
78      get { return CurrentScopeParameter.ActualValue; }
79    }
80    private DoubleValue Delta {
81      get { return DeltaParameter.Value; }
82    }
83    public ISingleObjectiveTestFunctionProblemEvaluator Evaluator {
84      get { return EvaluatorParameter.ActualValue; }
85      set { EvaluatorParameter.ActualValue = value; }
86    }
87    private DoubleValue Gamma {
88      get { return GammaParameter.Value; }
89    }
90    public IntValue ImprovementAttempts {
91      get { return ImprovementAttemptsParameter.ActualValue; }
92      set { ImprovementAttemptsParameter.ActualValue = value; }
93    }
94    public IRandom Random {
95      get { return RandomParameter.ActualValue; }
96      set { RandomParameter.ActualValue = value; }
97    }
98    private IItem Target {
99      get { return TargetParameter.ActualValue; }
100    }
101    #endregion
102
103    [StorableConstructor]
104    private SingleObjectiveTestFunctionImprovementOperator(bool deserializing) : base(deserializing) { }
105    private SingleObjectiveTestFunctionImprovementOperator(SingleObjectiveTestFunctionImprovementOperator original, Cloner cloner) : base(original, cloner) { }
106    public SingleObjectiveTestFunctionImprovementOperator()
107      : base() {
108      #region Create parameters
109      Parameters.Add(new ValueParameter<DoubleValue>("Alpha", new DoubleValue(1.0)));
110      Parameters.Add(new ValueParameter<DoubleValue>("Beta", new DoubleValue(2.0)));
111      Parameters.Add(new ScopeParameter("CurrentScope"));
112      Parameters.Add(new ValueParameter<DoubleValue>("Delta", new DoubleValue(0.5)));
113      Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator"));
114      Parameters.Add(new ValueParameter<DoubleValue>("Gamma", new DoubleValue(0.5)));
115      Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", new IntValue(100)));
116      Parameters.Add(new ValueLookupParameter<IItem>("Target"));
117      Parameters.Add(new ValueLookupParameter<IRandom>("Random"));
118      #endregion
119    }
120
121    public override IDeepCloneable Clone(Cloner cloner) {
122      return new SingleObjectiveTestFunctionImprovementOperator(this, cloner);
123    }
124
125    public override IOperation Apply() {
126      RealVector bestSol = CurrentScope.Variables[TargetParameter.ActualName].Value as RealVector;
127      MethodInfo evaluationMethod = Evaluator.GetType().GetMethod("Apply",
128                                                                  BindingFlags.Public | BindingFlags.Static,
129                                                                  null,
130                                                                  new Type[] { typeof(RealVector) }, null);
131      Func<RealVector, double> functionEvaluator = x => (double)evaluationMethod.Invoke(Evaluator, new object[] { x });
132      double bestSolQuality = functionEvaluator(bestSol);
133
134      // create perturbed solutions
135      RealVector[] simplex = new RealVector[bestSol.Length];
136      for (int i = 0; i < simplex.Length; i++) {
137        simplex[i] = bestSol.Clone() as RealVector;
138        simplex[i][i] += 0.1 * (Evaluator.Bounds[0, 1] - Evaluator.Bounds[0, 0]);
139        if (simplex[i][i] > Evaluator.Bounds[0, 1]) simplex[i][i] = Evaluator.Bounds[0, 1];
140        if (simplex[i][i] < Evaluator.Bounds[0, 0]) simplex[i][i] = Evaluator.Bounds[0, 0];
141      }
142
143      // improve solutions
144      for (int i = 0; i < ImprovementAttempts.Value; i++) {
145        // order according to their objective function value
146        Array.Sort(simplex, (x, y) => functionEvaluator(x).CompareTo(functionEvaluator(y)));
147
148        // calculate centroid
149        RealVector centroid = new RealVector(bestSol.Length);
150        foreach (var vector in simplex)
151          for (int j = 0; j < centroid.Length; j++)
152            centroid[j] += vector[j];
153        for (int j = 0; j < centroid.Length; j++)
154          centroid[j] /= simplex.Length;
155
156        // reflection
157        RealVector reflectionPoint = new RealVector(bestSol.Length);
158        for (int j = 0; j < reflectionPoint.Length; j++)
159          reflectionPoint[j] = centroid[j] + Alpha.Value * (centroid[j] - simplex[simplex.Length - 1][j]);
160        double reflectionPointQuality = functionEvaluator(reflectionPoint);
161        if (functionEvaluator(simplex[0]) <= reflectionPointQuality
162            && reflectionPointQuality < functionEvaluator(simplex[simplex.Length - 2]))
163          simplex[simplex.Length - 1] = reflectionPoint;
164
165        // expansion
166        if (reflectionPointQuality < functionEvaluator(simplex[0])) {
167          RealVector expansionPoint = new RealVector(bestSol.Length);
168          for (int j = 0; j < expansionPoint.Length; j++)
169            expansionPoint[j] = centroid[j] + Beta.Value * (reflectionPoint[j] - centroid[j]);
170          simplex[simplex.Length - 1] = functionEvaluator(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint;
171        }
172
173        // contraction
174        if (functionEvaluator(simplex[simplex.Length - 2]) <= reflectionPointQuality
175            && reflectionPointQuality < functionEvaluator(simplex[simplex.Length - 1])) {
176          RealVector outsideContractionPoint = new RealVector(bestSol.Length);
177          for (int j = 0; j < outsideContractionPoint.Length; j++)
178            outsideContractionPoint[j] = centroid[j] + Gamma.Value * (reflectionPoint[j] - centroid[j]);
179          if (functionEvaluator(outsideContractionPoint) <= reflectionPointQuality) {
180            simplex[simplex.Length - 1] = outsideContractionPoint;
181            if (functionEvaluator(reflectionPoint) >= functionEvaluator(simplex[simplex.Length - 1])) {
182              RealVector insideContractionPoint = new RealVector(bestSol.Length);
183              for (int j = 0; j < insideContractionPoint.Length; j++)
184                insideContractionPoint[j] = centroid[j] - Gamma.Value * (reflectionPoint[j] - centroid[j]);
185              if (functionEvaluator(insideContractionPoint) < functionEvaluator(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint;
186            }
187          }
188        }
189
190        // reduction
191        for (int j = 1; j < simplex.Length; j++)
192          for (int k = 0; k < simplex[j].Length; k++)
193            simplex[j][k] = simplex[0][k] + Delta.Value * (simplex[j][k] - simplex[0][k]);
194      }
195
196      for (int i = 0; i < simplex[0].Length; i++) {
197        if (simplex[0][i] > Evaluator.Bounds[0, 1]) simplex[0][i] = Evaluator.Bounds[0, 1];
198        if (simplex[0][i] < Evaluator.Bounds[0, 0]) simplex[0][i] = Evaluator.Bounds[0, 0];
199      }
200
201      CurrentScope.Variables[TargetParameter.ActualName].Value = simplex[0];
202
203      return base.Apply();
204    }
205  }
206}
Note: See TracBrowser for help on using the repository browser.