source: branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs @ 7954

Last change on this file since 7954 was 7954, checked in by jkarder, 8 years ago

#1331:

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