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

Last change on this file since 8720 was 8720, checked in by jkarder, 7 years ago

#1899: applied the changes suggested by ascheibe in comment:6:ticket:1899

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