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

Last change on this file since 16609 was 16609, checked in by swagner, 3 years ago

#2989: Worked on Moving Peaks Benchmark

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