Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2989_MovingPeaksBenchmark/HeuristicLab.Problems.MovingPeaksBenchmark/3.3/MovingPeaksBenchmarkProblem.cs @ 16606

Last change on this file since 16606 was 16606, checked in by swagner, 6 years ago

#2989: Started working on the Moving Peaks Benchmark

File size: 19.9 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.Collections.Generic;
24using System.Linq;
25//using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.RealVectorEncoding;
30using HeuristicLab.Optimization;
31//using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35//using HeuristicLab.Problems.Instances;
36
37namespace HeuristicLab.Problems.MovingPeaksBenchmark {
38  [Item("Moving Peaks Benchmark", "Moving peaks benchmark problem for dynamic optimization.")]
39  [StorableClass]
40  [Creatable(CreatableAttribute.Categories.Problems, Priority = 97)]
41  public sealed class MovingPeaksBenchmarkProblem : SingleObjectiveHeuristicOptimizationProblem<IMovingPeaksBenchmarkProblemEvaluator, IRealVectorCreator>, IStorableContent {
42    public string Filename { get; set; }
43
44    [Storable]
45    private StdDevStrategyVectorCreator strategyVectorCreator;
46    [Storable]
47    private StdDevStrategyVectorCrossover strategyVectorCrossover;
48    [Storable]
49    private StdDevStrategyVectorManipulator strategyVectorManipulator;
50
51    #region Parameter Properties
52    public ValueParameter<DoubleMatrix> BoundsParameter {
53      get { return (ValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
54    }
55    public ValueParameter<IntValue> ProblemSizeParameter {
56      get { return (ValueParameter<IntValue>)Parameters["ProblemSize"]; }
57    }
58    public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
59      get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
60    }
61    #endregion
62
63    #region Properties
64    public DoubleMatrix Bounds {
65      get { return BoundsParameter.Value; }
66      set { BoundsParameter.Value = value; }
67    }
68    public IntValue ProblemSize {
69      get { return ProblemSizeParameter.Value; }
70      set { ProblemSizeParameter.Value = value; }
71    }
72//    private BestSingleObjectiveTestFunctionSolutionAnalyzer BestSingleObjectiveTestFunctionSolutionAnalyzer {
73//      get { return Operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); }
74//    }
75    #endregion
76
77    [StorableConstructor]
78    private MovingPeaksBenchmarkProblem(bool deserializing) : base(deserializing) { }
79    private MovingPeaksBenchmarkProblem(MovingPeaksBenchmarkProblem original, Cloner cloner)
80      : base(original, cloner) {
81      strategyVectorCreator = cloner.Clone(original.strategyVectorCreator);
82      strategyVectorCrossover = cloner.Clone(original.strategyVectorCrossover);
83      strategyVectorManipulator = cloner.Clone(original.strategyVectorManipulator);
84      RegisterEventHandlers();
85    }
86    public MovingPeaksBenchmarkProblem()
87//      : base(new AckleyEvaluator(), new UniformRandomRealVectorCreator()) {
88      : base(null, new UniformRandomRealVectorCreator()) {
89      Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", Evaluator.Bounds));
90      Parameters.Add(new ValueParameter<IntValue>("ProblemSize", "The dimension of the problem.", new IntValue(2)));
91      Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
92
93      Maximization.Value = Evaluator.Maximization;
94      BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
95
96      strategyVectorCreator = new StdDevStrategyVectorCreator();
97      strategyVectorCreator.LengthParameter.ActualName = ProblemSizeParameter.Name;
98      strategyVectorCrossover = new StdDevStrategyVectorCrossover();
99      strategyVectorManipulator = new StdDevStrategyVectorManipulator();
100      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
101      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
102
103      SolutionCreator.RealVectorParameter.ActualName = "Point";
104      ParameterizeSolutionCreator();
105      ParameterizeEvaluator();
106
107      InitializeOperators();
108      RegisterEventHandlers();
109      UpdateStrategyVectorBounds();
110    }
111
112    public override IDeepCloneable Clone(Cloner cloner) {
113      return new MovingPeaksBenchmarkProblem(this, cloner);
114    }
115
116    #region Events
117    protected override void OnSolutionCreatorChanged() {
118      base.OnSolutionCreatorChanged();
119      ParameterizeSolutionCreator();
120      ParameterizeAnalyzers();
121      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
122      SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
123    }
124    protected override void OnEvaluatorChanged() {
125      base.OnEvaluatorChanged();
126      bool problemSizeChange = ProblemSize.Value < Evaluator.MinimumProblemSize
127        || ProblemSize.Value > Evaluator.MaximumProblemSize;
128      if (problemSizeChange) {
129        ProblemSize.Value = Math.Max(Evaluator.MinimumProblemSize, Math.Min(ProblemSize.Value, Evaluator.MaximumProblemSize));
130      } else {
131        ParameterizeEvaluator();
132      }
133      UpdateMoveEvaluators();
134      ParameterizeAnalyzers();
135      Maximization.Value = Evaluator.Maximization;
136      BoundsParameter.Value = Evaluator.Bounds;
137      BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
138      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
139      Evaluator_QualityParameter_ActualNameChanged(null, EventArgs.Empty);
140      OnReset();
141    }
142    private void ProblemSizeParameter_ValueChanged(object sender, EventArgs e) {
143      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
144      ProblemSize_ValueChanged(null, EventArgs.Empty);
145    }
146    private void ProblemSize_ValueChanged(object sender, EventArgs e) {
147      if (ProblemSize.Value < 1) ProblemSize.Value = 1;
148      ParameterizeSolutionCreator();
149      ParameterizeEvaluator();
150      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize.Value));
151      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize.Value)));
152      OnReset();
153    }
154    private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
155      ParameterizeEvaluator();
156      ParameterizeOperators();
157      ParameterizeAnalyzers();
158    }
159    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
160      ParameterizeOperators();
161    }
162    private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
163      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
164      Bounds_ToStringChanged(null, EventArgs.Empty);
165    }
166    private void Bounds_ToStringChanged(object sender, EventArgs e) {
167      if (Bounds.Columns != 2 || Bounds.Rows < 1)
168        Bounds = new DoubleMatrix(1, 2);
169      ParameterizeOperators();
170      UpdateStrategyVectorBounds();
171    }
172    private void Bounds_ItemChanged(object sender, EventArgs<int, int> e) {
173      if (e.Value2 == 0 && Bounds[e.Value, 1] <= Bounds[e.Value, 0])
174        Bounds[e.Value, 1] = Bounds[e.Value, 0] + 0.1;
175      if (e.Value2 == 1 && Bounds[e.Value, 0] >= Bounds[e.Value, 1])
176        Bounds[e.Value, 0] = Bounds[e.Value, 1] - 0.1;
177      ParameterizeOperators();
178      UpdateStrategyVectorBounds();
179    }
180    private void MoveGenerator_AdditiveMoveParameter_ActualNameChanged(object sender, EventArgs e) {
181      string name = ((ILookupParameter<AdditiveMove>)sender).ActualName;
182      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
183        op.AdditiveMoveParameter.ActualName = name;
184      }
185    }
186    //private void SphereEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
187    //  SphereEvaluator eval = (Evaluator as SphereEvaluator);
188    //  if (eval != null) {
189    //    foreach (ISphereMoveEvaluator op in Operators.OfType<ISphereMoveEvaluator>()) {
190    //      op.C = eval.C;
191    //      op.Alpha = eval.Alpha;
192    //    }
193    //  }
194    //}
195    //private void RastriginEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
196    //  RastriginEvaluator eval = (Evaluator as RastriginEvaluator);
197    //  if (eval != null) {
198    //    foreach (IRastriginMoveEvaluator op in Operators.OfType<IRastriginMoveEvaluator>()) {
199    //      op.A = eval.A;
200    //    }
201    //  }
202    //}
203    private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) {
204      strategyVectorManipulator.BoundsParameter.Value = (DoubleMatrix)strategyVectorCreator.BoundsParameter.Value.Clone();
205    }
206    private void strategyVectorCreator_StrategyParameterParameter_ActualNameChanged(object sender, EventArgs e) {
207      string name = strategyVectorCreator.StrategyParameterParameter.ActualName;
208      strategyVectorCrossover.ParentsParameter.ActualName = name;
209      strategyVectorCrossover.StrategyParameterParameter.ActualName = name;
210      strategyVectorManipulator.StrategyParameterParameter.ActualName = name;
211    }
212    #endregion
213
214    #region Helpers
215    [StorableHook(HookType.AfterDeserialization)]
216    private void AfterDeserialization() {
217      // BackwardsCompatibility3.3
218      #region Backwards compatible code (remove with 3.4)
219      if (Operators.Count == 0) InitializeOperators();
220      #endregion
221      RegisterEventHandlers();
222    }
223
224    private void RegisterEventHandlers() {
225      ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
226      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
227      BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
228      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
229      Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
230      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
231      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
232      strategyVectorCreator.BoundsParameter.ValueChanged += new EventHandler(strategyVectorCreator_BoundsParameter_ValueChanged);
233      strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
234    }
235    private void ParameterizeAnalyzers() {
236      //if (BestSingleObjectiveTestFunctionSolutionAnalyzer != null) {
237      //  BestSingleObjectiveTestFunctionSolutionAnalyzer.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
238      //  BestSingleObjectiveTestFunctionSolutionAnalyzer.ResultsParameter.ActualName = "Results";
239      //  BestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
240      //  BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
241      //  BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
242      //  BestSingleObjectiveTestFunctionSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
243      //  BestSingleObjectiveTestFunctionSolutionAnalyzer.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
244      //  BestSingleObjectiveTestFunctionSolutionAnalyzer.BoundsParameter.ActualName = BoundsParameter.Name;
245      //}
246    }
247    private void InitializeOperators() {
248      //Operators.Add(new SingleObjectiveTestFunctionImprovementOperator());
249      //Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
250      //Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
251      //Operators.Add(new HammingSimilarityCalculator());
252      //Operators.Add(new EuclideanSimilarityCalculator());
253      //Operators.Add(new QualitySimilarityCalculator());
254
255      //Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
256      //Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
257      //ParameterizeAnalyzers();
258      //Operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
259      //Operators.Add(strategyVectorCreator);
260      //Operators.Add(strategyVectorCrossover);
261      //Operators.Add(strategyVectorManipulator);
262      //UpdateMoveEvaluators();
263      //ParameterizeOperators();
264      //InitializeMoveGenerators();
265    }
266    private void InitializeMoveGenerators() {
267      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
268        if (op is IMoveGenerator) {
269          op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
270        }
271      }
272    }
273    private void UpdateMoveEvaluators() {
274      //foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
275      //  Operators.Remove(op);
276      //foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
277      //  if (op.EvaluatorType == Evaluator.GetType()) {
278      //    Operators.Add(op);
279      //    #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
280      //    if (op is ISphereMoveEvaluator) {
281      //      SphereEvaluator e = (Evaluator as SphereEvaluator);
282      //      e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
283      //      e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
284      //      ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
285      //      em.C = e.C;
286      //      em.Alpha = e.Alpha;
287      //    } else if (op is IRastriginMoveEvaluator) {
288      //      RastriginEvaluator e = (Evaluator as RastriginEvaluator);
289      //      e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
290      //      IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
291      //      em.A = e.A;
292      //    }
293      //    #endregion
294      //  }
295      //ParameterizeOperators();
296      //OnOperatorsChanged();
297    }
298    private void ParameterizeSolutionCreator() {
299      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
300      SolutionCreator.LengthParameter.Hidden = true;
301      SolutionCreator.BoundsParameter.ActualName = BoundsParameter.Name;
302      SolutionCreator.BoundsParameter.Hidden = true;
303    }
304    private void ParameterizeEvaluator() {
305      Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
306      Evaluator.PointParameter.Hidden = true;
307      try {
308        BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize.Value);
309      }
310      catch (ArgumentException e) {
311        ErrorHandling.ShowErrorDialog(e);
312        ProblemSize.Value = Evaluator.MinimumProblemSize;
313      }
314    }
315    private void ParameterizeOperators() {
316      foreach (var op in Operators.OfType<IRealVectorCrossover>()) {
317        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
318        op.ParentsParameter.Hidden = true;
319        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
320        op.ChildParameter.Hidden = true;
321        op.BoundsParameter.ActualName = BoundsParameter.Name;
322        op.BoundsParameter.Hidden = true;
323      }
324      foreach (var op in Operators.OfType<IRealVectorManipulator>()) {
325        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
326        op.RealVectorParameter.Hidden = true;
327        op.BoundsParameter.ActualName = BoundsParameter.Name;
328        op.BoundsParameter.Hidden = true;
329      }
330      foreach (var op in Operators.OfType<IRealVectorMoveOperator>()) {
331        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
332        op.RealVectorParameter.Hidden = true;
333      }
334      foreach (var op in Operators.OfType<IRealVectorMoveGenerator>()) {
335        op.BoundsParameter.ActualName = BoundsParameter.Name;
336        op.BoundsParameter.Hidden = true;
337      }
338      //foreach (var op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
339      //  op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
340      //  op.QualityParameter.Hidden = true;
341      //  op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
342      //  op.RealVectorParameter.Hidden = true;
343      //}
344      foreach (var op in Operators.OfType<IRealVectorParticleCreator>()) {
345        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
346        op.RealVectorParameter.Hidden = true;
347        op.BoundsParameter.ActualName = BoundsParameter.Name;
348        op.BoundsParameter.Hidden = true;
349      }
350      foreach (var op in Operators.OfType<IRealVectorParticleUpdater>()) {
351        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
352        op.RealVectorParameter.Hidden = true;
353        op.BoundsParameter.ActualName = BoundsParameter.Name;
354        op.BoundsParameter.Hidden = true;
355      }
356      foreach (var op in Operators.OfType<IRealVectorSwarmUpdater>()) {
357        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
358        op.RealVectorParameter.Hidden = true;
359        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
360        op.MaximizationParameter.Hidden = true;
361      }
362      foreach (var op in Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()) {
363        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
364        op.RealVectorParameter.Hidden = true;
365      }
366      foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
367        op.SolutionParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
368        op.SolutionParameter.Hidden = true;
369      }
370      foreach (var op in Operators.OfType<ISingleObjectivePathRelinker>()) {
371        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
372        op.ParentsParameter.Hidden = true;
373      }
374      foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) {
375        op.SolutionVariableName = SolutionCreator.RealVectorParameter.ActualName;
376        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
377        //var calc = op as SingleObjectiveTestFunctionSimilarityCalculator;
378        //if (calc != null) calc.Bounds = Bounds;
379      }
380    }
381    private void UpdateStrategyVectorBounds() {
382      var strategyBounds = (DoubleMatrix)Bounds.Clone();
383      for (int i = 0; i < strategyBounds.Rows; i++) {
384        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
385        strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
386      }
387      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
388    }
389    #endregion
390  }
391}
Note: See TracBrowser for help on using the repository browser.