Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs @ 16983

Last change on this file since 16983 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

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