Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1331:

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