Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TestFunctions/TestFunctionsImprovementOperator.cs @ 7786

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

#1331:

  • fixed bug in path relinking selection
  • fixed bug in ScatterSearch.Prepare()
  • added custom interface (IImprovementOperator) for Scatter Search specific improvement operators
  • switched from diversity calculation to similarity calculation
  • separated IPathRelinker from ICrossover
  • changed TestFunctionsImprovementOperator to use reflection for evaluating functions
  • changed SolutionPoolUpdateMethod to use similarity calculation for solution comparison
  • improved TravelingSalesmanImprovementOperator
  • improved operator graph
  • removed specific operators used to evaluate TestFunctions problems
  • removed custom crossover operator (NChildCrossover)
  • added parameters and adjusted types
  • adjusted event handling
  • changed access levels
  • minor code improvements
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.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.TestFunctions;
32
33namespace HeuristicLab.Algorithms.ScatterSearch.TestFunctions {
34  /// <summary>
35  /// An operator that improves test functions solutions.
36  /// </summary>
37  [Item("TestFunctionsImprovementOperator", "An operator that improves test functions solutions.")]
38  [StorableClass]
39  public sealed class TestFunctionsImprovementOperator : 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 TestFunctionsImprovementOperator(bool deserializing) : base(deserializing) { }
105    private TestFunctionsImprovementOperator(TestFunctionsImprovementOperator original, Cloner cloner) : base(original, cloner) { }
106    public TestFunctionsImprovementOperator()
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      TargetParameter.ActualName = "Point"; // temporary solution for the test functions problem
120    }
121
122    public override IDeepCloneable Clone(Cloner cloner) {
123      return new TestFunctionsImprovementOperator(this, cloner);
124    }
125
126    public override IOperation Apply() {
127      RealVector bestSol = CurrentScope.Variables[TargetParameter.ActualName].Value as RealVector;
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 * (Evaluator.Bounds[0, 1] - Evaluator.Bounds[0, 0]);
140        if (simplex[i][i] > Evaluator.Bounds[0, 1]) simplex[i][i] = Evaluator.Bounds[0, 1];
141        if (simplex[i][i] < Evaluator.Bounds[0, 0]) simplex[i][i] = Evaluator.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] > Evaluator.Bounds[0, 1]) simplex[0][i] = Evaluator.Bounds[0, 1];
199        if (simplex[0][i] < Evaluator.Bounds[0, 0]) simplex[0][i] = Evaluator.Bounds[0, 0];
200      }
201
202      CurrentScope.Variables[TargetParameter.ActualName].Value = simplex[0];
203
204      return base.Apply();
205    }
206  }
207}
Note: See TracBrowser for help on using the repository browser.