Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12099 was 12087, checked in by jkarder, 10 years ago

#2332: removed unused similarity analyzer properties

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