Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs @ 14561

Last change on this file since 14561 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 20.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [StorableClass]
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(bool deserializing) : base(deserializing) { }
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 QualitySimilarityCalculator());
264      Operators.Add(new NoSimilarityCalculator());
265
266      Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
267      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
268      ParameterizeAnalyzers();
269      Operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
270      Operators.Add(strategyVectorCreator);
271      Operators.Add(strategyVectorCrossover);
272      Operators.Add(strategyVectorManipulator);
273      UpdateMoveEvaluators();
274      ParameterizeOperators();
275      InitializeMoveGenerators();
276    }
277    private void InitializeMoveGenerators() {
278      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
279        if (op is IMoveGenerator) {
280          op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
281        }
282      }
283    }
284    private void UpdateMoveEvaluators() {
285      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
286        Operators.Remove(op);
287      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
288        if (op.EvaluatorType == Evaluator.GetType()) {
289          Operators.Add(op);
290          #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
291          if (op is ISphereMoveEvaluator) {
292            SphereEvaluator e = (Evaluator as SphereEvaluator);
293            e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
294            e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
295            ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
296            em.C = e.C;
297            em.Alpha = e.Alpha;
298          } else if (op is IRastriginMoveEvaluator) {
299            RastriginEvaluator e = (Evaluator as RastriginEvaluator);
300            e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
301            IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
302            em.A = e.A;
303          }
304          #endregion
305        }
306      ParameterizeOperators();
307      OnOperatorsChanged();
308    }
309    private void ParameterizeSolutionCreator() {
310      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
311      SolutionCreator.LengthParameter.Hidden = true;
312      SolutionCreator.BoundsParameter.ActualName = BoundsParameter.Name;
313      SolutionCreator.BoundsParameter.Hidden = true;
314    }
315    private void ParameterizeEvaluator() {
316      Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
317      Evaluator.PointParameter.Hidden = true;
318      try {
319        BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize.Value);
320      }
321      catch (ArgumentException e) {
322        ErrorHandling.ShowErrorDialog(e);
323        ProblemSize.Value = Evaluator.MinimumProblemSize;
324      }
325    }
326    private void ParameterizeOperators() {
327      foreach (var op in Operators.OfType<IRealVectorCrossover>()) {
328        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
329        op.ParentsParameter.Hidden = true;
330        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
331        op.ChildParameter.Hidden = true;
332        op.BoundsParameter.ActualName = BoundsParameter.Name;
333        op.BoundsParameter.Hidden = true;
334      }
335      foreach (var op in Operators.OfType<IRealVectorManipulator>()) {
336        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
337        op.RealVectorParameter.Hidden = true;
338        op.BoundsParameter.ActualName = BoundsParameter.Name;
339        op.BoundsParameter.Hidden = true;
340      }
341      foreach (var op in Operators.OfType<IRealVectorMoveOperator>()) {
342        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
343        op.RealVectorParameter.Hidden = true;
344      }
345      foreach (var op in Operators.OfType<IRealVectorMoveGenerator>()) {
346        op.BoundsParameter.ActualName = BoundsParameter.Name;
347        op.BoundsParameter.Hidden = true;
348      }
349      foreach (var op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
350        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
351        op.QualityParameter.Hidden = true;
352        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
353        op.RealVectorParameter.Hidden = true;
354      }
355      foreach (var op in Operators.OfType<IRealVectorParticleCreator>()) {
356        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
357        op.RealVectorParameter.Hidden = true;
358        op.BoundsParameter.ActualName = BoundsParameter.Name;
359        op.BoundsParameter.Hidden = true;
360        op.ProblemSizeParameter.ActualName = ProblemSizeParameter.Name;
361        op.ProblemSizeParameter.Hidden = true;
362      }
363      foreach (var op in Operators.OfType<IRealVectorParticleUpdater>()) {
364        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
365        op.RealVectorParameter.Hidden = true;
366        op.BoundsParameter.ActualName = BoundsParameter.Name;
367        op.BoundsParameter.Hidden = true;
368      }
369      foreach (var op in Operators.OfType<IRealVectorSwarmUpdater>()) {
370        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
371        op.RealVectorParameter.Hidden = true;
372        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
373        op.MaximizationParameter.Hidden = true;
374      }
375      foreach (var op in Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()) {
376        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
377        op.RealVectorParameter.Hidden = true;
378      }
379      foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
380        op.SolutionParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
381        op.SolutionParameter.Hidden = true;
382      }
383      foreach (var op in Operators.OfType<ISingleObjectivePathRelinker>()) {
384        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
385        op.ParentsParameter.Hidden = true;
386      }
387      foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) {
388        op.SolutionVariableName = SolutionCreator.RealVectorParameter.ActualName;
389        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
390        var calc = op as SingleObjectiveTestFunctionSimilarityCalculator;
391        if (calc != null) calc.Bounds = Bounds;
392      }
393    }
394    private void UpdateStrategyVectorBounds() {
395      var strategyBounds = (DoubleMatrix)Bounds.Clone();
396      for (int i = 0; i < strategyBounds.Rows; i++) {
397        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
398        strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
399      }
400      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
401    }
402    #endregion
403
404    public void Load(SOTFData data) {
405      Name = data.Name;
406      Description = data.Description;
407      Evaluator = data.Evaluator;
408    }
409  }
410}
Note: See TracBrowser for help on using the repository browser.