Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2989_MovingPeaksBenchmark/HeuristicLab.Problems.MovingPeaksBenchmark/3.3/MovingPeaksBenchmarkProblemEvaluator.cs @ 16611

Last change on this file since 16611 was 16611, checked in by swagner, 5 years ago

#2989: Moving Peaks Benchmark

  • added calculation of offline error
File size: 10.1 KB
RevLine 
[16607]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
[16609]22using System;
23using System.Linq;
[16607]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Operators;
[16609]29using HeuristicLab.Optimization;
[16607]30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[16609]32using HeuristicLab.Random;
[16607]33
34namespace HeuristicLab.Problems.MovingPeaksBenchmark {
35  [Item("MovingPeaksBenchmarkProblemEvaluator", "Evaluation operator for the Moving Peaks Benchmark.")]
36  [StorableClass]
37  public class MovingPeaksBenchmarkProblemEvaluator : InstrumentedOperator, IMovingPeaksBenchmarkProblemEvaluator {
[16609]38    [Storable]
39    IRandom uniformRandom;
40    [Storable]
41    long executions;
[16611]42    [Storable]
43    double currentBest;
44    [Storable]
45    double offlineErrorSum;
[16609]46
47    public IValueLookupParameter<DoubleMatrix> PeakLocationsParameter {
48      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["PeakLocations"]; }
[16607]49    }
[16609]50    public IValueLookupParameter<DoubleArray> PeakWidthsParameter {
51      get { return (IValueLookupParameter<DoubleArray>)Parameters["PeakWidths"]; }
[16607]52    }
[16609]53    public IValueLookupParameter<DoubleArray> PeakHeightsParameter {
54      get { return (IValueLookupParameter<DoubleArray>)Parameters["PeakHeights"]; }
[16607]55    }
[16609]56    public ILookupParameter<IntValue> MovingPeaksRandomSeedParameter {
57      get { return (ILookupParameter<IntValue>)Parameters["MovingPeaksRandomSeed"]; }
58    }
59    public ILookupParameter<IntValue> PeakMovementIntervalParameter {
60      get { return (ILookupParameter<IntValue>)Parameters["PeakMovementInterval"]; }
61    }
62    public ILookupParameter<DoubleValue> PeakMovementStrengthParameter {
63      get { return (ILookupParameter<DoubleValue>)Parameters["PeakMovementStrength"]; }
64    }
[16607]65    public ILookupParameter<RealVector> PointParameter {
66      get { return (ILookupParameter<RealVector>)Parameters["Point"]; }
67    }
68    public ILookupParameter<DoubleValue> QualityParameter {
69      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
70    }
[16609]71    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
72      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
73    }
74    public ILookupParameter<RealVector> BestKnownSolutionParameter {
75      get { return (ILookupParameter<RealVector>)Parameters["BestKnownSolution"]; }
76    }
77    public IValueLookupParameter<ResultCollection> ResultsParameter {
78      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
79    }
[16607]80
81    [StorableConstructor]
82    protected MovingPeaksBenchmarkProblemEvaluator(bool deserializing) : base(deserializing) { }
83    protected MovingPeaksBenchmarkProblemEvaluator(MovingPeaksBenchmarkProblemEvaluator original, Cloner cloner) : base(original, cloner) { }
84    public MovingPeaksBenchmarkProblemEvaluator() : base() {
[16609]85      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("PeakLocations", "Current position of the peaks."));
86      PeakLocationsParameter.ActualName = "InitialPeakLocations";
87      Parameters.Add(new ValueLookupParameter<DoubleArray>("PeakWidths", "Current width of the peaks."));
88      PeakWidthsParameter.ActualName = "InitialPeakWidths";
89      Parameters.Add(new ValueLookupParameter<DoubleArray>("PeakHeights", "Current height of the peaks."));
90      PeakHeightsParameter.ActualName = "InitialPeakHeights";
91      Parameters.Add(new LookupParameter<IntValue>("MovingPeaksRandomSeed", "The random seed for initializing the PRNG for changing the peaks."));
92      Parameters.Add(new LookupParameter<IntValue>("PeakMovementInterval", "The interval in evaluated solutions in which peaks are moved."));
93      Parameters.Add(new LookupParameter<DoubleValue>("PeakMovementStrength", "The length of the random vector used for changing peak locations."));
[16607]94      Parameters.Add(new LookupParameter<RealVector>("Point", "The point which should be evaluated."));
95      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "Quality value of the evaluated point."));
[16609]96      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "Quality value of the highest peak."));
97      Parameters.Add(new LookupParameter<RealVector>("BestKnownSolution", "The location of the highest peak."));
98      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection for storing result values."));
99
100      PeakLocationsParameter.Hidden = true;
101      PeakWidthsParameter.Hidden = true;
102      PeakHeightsParameter.Hidden = true;
103      BestKnownQualityParameter.Hidden = true;
104      BestKnownSolutionParameter.Hidden = true;
105      ResultsParameter.Hidden = true;
[16607]106    }
107
108    public override IDeepCloneable Clone(Cloner cloner) {
109      return new MovingPeaksBenchmarkProblemEvaluator(this, cloner);
110    }
111
112    public override IOperation InstrumentedApply() {
113      DoubleMatrix peaks = PeakLocationsParameter.ActualValue;
114      DoubleArray widths = PeakWidthsParameter.ActualValue;
115      DoubleArray heights = PeakHeightsParameter.ActualValue;
[16609]116
117      if (PeakLocationsParameter.Value == null) {
118        peaks = peaks.Clone() as DoubleMatrix;
119        widths = widths.Clone() as DoubleArray;
120        heights = heights.Clone() as DoubleArray;
121        PeakLocationsParameter.Value = peaks;
122        PeakWidthsParameter.Value = widths;
123        PeakHeightsParameter.Value = heights;
124
125        ResultCollection results = ResultsParameter.ActualValue;
126        results.Add(new Result("Current Peak Locations", peaks));
127        results.Add(new Result("Current Peak Widths", widths));
128        results.Add(new Result("Current Peak Heights", heights));
129      }
130      if (uniformRandom == null) {
131        uniformRandom = new MersenneTwister();
132        uniformRandom.Reset(MovingPeaksRandomSeedParameter.ActualValue.Value);
133      }
134
135      // move peaks if peaks movement interval is reached
136      lock (this) {
137        if ((executions % PeakMovementIntervalParameter.ActualValue.Value) == 0) {
138          MovePeaks(uniformRandom, peaks, widths, heights, PeakMovementStrengthParameter.ActualValue.Value);
139
140          // update best known solution & quality according to highest peak
141          double maxHeight = heights.Max();
142          int peakIndex = Array.IndexOf(heights.CloneAsArray(), maxHeight);
143          double[] peak = new double[peaks.Columns];
144          for (int i = 0; i < peak.Length; i++) {
145            peak[i] = peaks[peakIndex, i];
146          }
147          BestKnownSolutionParameter.ActualValue = new RealVector(peak);
148          BestKnownQualityParameter.ActualValue.Value = heights.Max();
[16611]149          currentBest = -1;
[16609]150        }
151        executions++;
152      }
153
[16607]154      RealVector point = PointParameter.ActualValue;
155      double quality = Apply(peaks, widths, heights, point);
156      QualityParameter.ActualValue = new DoubleValue(quality);
[16611]157
158      lock (this) {
159        if (quality > currentBest) {
160          currentBest = quality;
161        }
162        offlineErrorSum += heights.Max() - currentBest;
163        ResultCollection results = ResultsParameter.ActualValue;
164        IResult offlineError;
165        if (results.TryGetValue("Offline Error", out offlineError)) {
166          (offlineError.Value as DoubleValue).Value = offlineErrorSum / executions;
167        } else {
168          results.Add(new Result("Offline Error", new DoubleValue(offlineErrorSum / executions)));
169        }
170      }
[16607]171      return base.InstrumentedApply();
172    }
173
[16609]174    public override void InitializeState() {
175      base.InitializeState();
176      executions = 0;
[16611]177      currentBest = -1;
178      offlineErrorSum = 0;
[16609]179    }
180
181    public override void ClearState() {
182      base.ClearState();
183      uniformRandom = null;
184      PeakLocationsParameter.Value = null;
185      PeakWidthsParameter.Value = null;
186      PeakHeightsParameter.Value = null;
187    }
188
[16607]189    public double Apply(DoubleMatrix peaks, DoubleArray widths, DoubleArray heights, RealVector point) {
[16608]190      double max = 0;
191      double val = 0;
192
193      for (int i = 0; i < widths.Length; i++) {
194        val = 0;
195        for (int j = 0; j < point.Length; j++) {
196          val += (point[j] - peaks[i, j]) * (point[j] - peaks[i, j]);
197        }
198        val = heights[i] / (1 + widths[i] * val);
199        if (val > max) max = val;
200      }
201      return max;
[16607]202    }
[16609]203
204    private void MovePeaks(IRandom uniformRandom, DoubleMatrix peaks, DoubleArray widths, DoubleArray heights, double strength) {
205      IRandom normalRandom = new NormalDistributedRandom(uniformRandom, 0, 1);
206      for (int i = 0; i < peaks.Rows; i++) {
207        double[] v = RandomVector(uniformRandom, peaks.Columns, strength);
208        for (int j = 0; j < v.Length; j++) {
209          peaks[i, j] += v[j];
210        }
211        widths[i] = widths[i] + 0.01 * normalRandom.NextDouble();
212        heights[i] = heights[i] + 7 * normalRandom.NextDouble();
213      }
214    }
215
216    private double[] RandomVector(IRandom uniformRandom, int dimensions, double length) {
217      double[] vector = new double[dimensions];
218
219      for (int i = 0; i < vector.Length; i++) {
220        vector[i] = uniformRandom.NextDouble() - 0.5;
221      }
222      double factor = length / Math.Sqrt(vector.Select(x => x * x).Sum());
223      for (int i = 0; i < vector.Length; i++) {
224        vector[i] *= factor;
225      }
226      return vector;
227    }
[16607]228  }
229}
Note: See TracBrowser for help on using the repository browser.