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

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

#2989: Worked on Moving Peaks Benchmark

File size: 4.1 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.RealVectorEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.MovingPeaksBenchmark {
31  [Item("MovingPeaksBenchmarkProblemEvaluator", "Evaluation operator for the Moving Peaks Benchmark.")]
32  [StorableClass]
33  public class MovingPeaksBenchmarkProblemEvaluator : InstrumentedOperator, IMovingPeaksBenchmarkProblemEvaluator {
34    public ILookupParameter<DoubleMatrix> PeakLocationsParameter {
35      get { return (ILookupParameter<DoubleMatrix>)Parameters["PeakLocations"]; }
36    }
37    public ILookupParameter<DoubleArray> PeakWidthsParameter {
38      get { return (ILookupParameter<DoubleArray>)Parameters["PeakWidths"]; }
39    }
40    public ILookupParameter<DoubleArray> PeakHeightsParameter {
41      get { return (ILookupParameter<DoubleArray>)Parameters["PeakHeights"]; }
42    }
43    public ILookupParameter<RealVector> PointParameter {
44      get { return (ILookupParameter<RealVector>)Parameters["Point"]; }
45    }
46    public ILookupParameter<DoubleValue> QualityParameter {
47      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
48    }
49
50    [StorableConstructor]
51    protected MovingPeaksBenchmarkProblemEvaluator(bool deserializing) : base(deserializing) { }
52    protected MovingPeaksBenchmarkProblemEvaluator(MovingPeaksBenchmarkProblemEvaluator original, Cloner cloner) : base(original, cloner) { }
53    public MovingPeaksBenchmarkProblemEvaluator() : base() {
54      Parameters.Add(new LookupParameter<DoubleMatrix>("PeakLocations", "Current position of the peaks."));
55      Parameters.Add(new LookupParameter<DoubleArray>("PeakWidths", "Current width of the peaks."));
56      Parameters.Add(new LookupParameter<DoubleArray>("PeakHeights", "Current height of the peaks."));
57      Parameters.Add(new LookupParameter<RealVector>("Point", "The point which should be evaluated."));
58      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "Quality value of the evaluated point."));
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new MovingPeaksBenchmarkProblemEvaluator(this, cloner);
63    }
64
65    public override IOperation InstrumentedApply() {
66      DoubleMatrix peaks = PeakLocationsParameter.ActualValue;
67      DoubleArray widths = PeakWidthsParameter.ActualValue;
68      DoubleArray heights = PeakHeightsParameter.ActualValue;
69      RealVector point = PointParameter.ActualValue;
70      double quality = Apply(peaks, widths, heights, point);
71      QualityParameter.ActualValue = new DoubleValue(quality);
72      return base.InstrumentedApply();
73    }
74
75    public double Apply(DoubleMatrix peaks, DoubleArray widths, DoubleArray heights, RealVector point) {
76      heights[0] *= 1.01;
77
78      double max = 0;
79      double val = 0;
80
81      for (int i = 0; i < widths.Length; i++) {
82        val = 0;
83        for (int j = 0; j < point.Length; j++) {
84          val += (point[j] - peaks[i, j]) * (point[j] - peaks[i, j]);
85        }
86        val = heights[i] / (1 + widths[i] * val);
87        if (val > max) max = val;
88      }
89      return max;
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.