Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ImprovingStringConvertibleMatrix/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs @ 9308

Last change on this file since 9308 was 9306, checked in by sforsten, 12 years ago

#2018:

  • renamed the structs and methods in IStringConvertibleMatrix
  • added MatrixValuesChangedEventArgs in IStringConvertibleMatrix
  • added methods SetValues(MatrixValues<T>) in ValueTypeMatrix
  • fixed bugs in StringConvertibleMatrixView: DataGridView has now at least one column and dataGridView_CellValidating does not set e.Cancel to true anymore.
File size: 20.7 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    #region Events
130    protected override void OnSolutionCreatorChanged() {
131      base.OnSolutionCreatorChanged();
132      ParameterizeSolutionCreator();
133      ParameterizeAnalyzers();
134      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
135      SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
136    }
137    protected override void OnEvaluatorChanged() {
138      base.OnEvaluatorChanged();
139      bool problemSizeChange = ProblemSize.Value < Evaluator.MinimumProblemSize
140        || ProblemSize.Value > Evaluator.MaximumProblemSize;
141      if (problemSizeChange) {
142        ProblemSize.Value = Math.Max(Evaluator.MinimumProblemSize, Math.Min(ProblemSize.Value, Evaluator.MaximumProblemSize));
143      } else {
144        ParameterizeEvaluator();
145      }
146      UpdateMoveEvaluators();
147      ParameterizeAnalyzers();
148      Maximization.Value = Evaluator.Maximization;
149      BoundsParameter.Value = Evaluator.Bounds;
150      BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
151      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
152      Evaluator_QualityParameter_ActualNameChanged(null, EventArgs.Empty);
153      OnReset();
154    }
155    private void ProblemSizeParameter_ValueChanged(object sender, EventArgs e) {
156      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
157      ProblemSize_ValueChanged(null, EventArgs.Empty);
158    }
159    private void ProblemSize_ValueChanged(object sender, EventArgs e) {
160      if (ProblemSize.Value < 1) ProblemSize.Value = 1;
161      ParameterizeSolutionCreator();
162      ParameterizeEvaluator();
163      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize.Value));
164      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize.Value)));
165      OnReset();
166    }
167    private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
168      ParameterizeEvaluator();
169      ParameterizeOperators();
170      ParameterizeAnalyzers();
171    }
172    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
173      ParameterizeOperators();
174    }
175    private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
176      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
177      Bounds_ToStringChanged(null, EventArgs.Empty);
178    }
179    private void Bounds_ToStringChanged(object sender, EventArgs e) {
180      if (Bounds.Columns != 2 || Bounds.Rows < 1)
181        Bounds = new DoubleMatrix(1, 2);
182      ParameterizeOperators();
183      UpdateStrategyVectorBounds();
184    }
185    private void Bounds_ItemChanged(object sender, MatrixValuesChangedEventArgs e) {
186      foreach (var pos in e.Value) {
187        if (pos.Column == 0 && Bounds[pos.Row, 1] <= Bounds[pos.Row, 0])
188          Bounds[pos.Row, 1] = Bounds[pos.Row, 0] + 0.1;
189        if (pos.Column == 1 && Bounds[pos.Row, 0] >= Bounds[pos.Row, 1])
190          Bounds[pos.Row, 0] = Bounds[pos.Row, 1] - 0.1;
191      }
192      ParameterizeOperators();
193      UpdateStrategyVectorBounds();
194    }
195    private void MoveGenerator_AdditiveMoveParameter_ActualNameChanged(object sender, EventArgs e) {
196      string name = ((ILookupParameter<AdditiveMove>)sender).ActualName;
197      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
198        op.AdditiveMoveParameter.ActualName = name;
199      }
200    }
201    private void SphereEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
202      SphereEvaluator eval = (Evaluator as SphereEvaluator);
203      if (eval != null) {
204        foreach (ISphereMoveEvaluator op in Operators.OfType<ISphereMoveEvaluator>()) {
205          op.C = eval.C;
206          op.Alpha = eval.Alpha;
207        }
208      }
209    }
210    private void RastriginEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
211      RastriginEvaluator eval = (Evaluator as RastriginEvaluator);
212      if (eval != null) {
213        foreach (IRastriginMoveEvaluator op in Operators.OfType<IRastriginMoveEvaluator>()) {
214          op.A = eval.A;
215        }
216      }
217    }
218    private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) {
219      strategyVectorManipulator.BoundsParameter.Value = (DoubleMatrix)strategyVectorCreator.BoundsParameter.Value.Clone();
220    }
221    private void strategyVectorCreator_StrategyParameterParameter_ActualNameChanged(object sender, EventArgs e) {
222      string name = strategyVectorCreator.StrategyParameterParameter.ActualName;
223      strategyVectorCrossover.ParentsParameter.ActualName = name;
224      strategyVectorCrossover.StrategyParameterParameter.ActualName = name;
225      strategyVectorManipulator.StrategyParameterParameter.ActualName = name;
226    }
227    #endregion
228
229    #region Helpers
230    [StorableHook(HookType.AfterDeserialization)]
231    private void AfterDeserialization() {
232      // BackwardsCompatibility3.3
233      #region Backwards compatible code (remove with 3.4)
234      if (Operators.Count == 0) InitializeOperators();
235      #endregion
236      RegisterEventHandlers();
237    }
238
239    private void RegisterEventHandlers() {
240      ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
241      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
242      BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
243      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
244      Bounds.ItemsChanged += new EventHandler<MatrixValuesChangedEventArgs>(Bounds_ItemChanged);
245      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
246      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
247      strategyVectorCreator.BoundsParameter.ValueChanged += new EventHandler(strategyVectorCreator_BoundsParameter_ValueChanged);
248      strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
249    }
250    private void ParameterizeAnalyzers() {
251      if (BestSingleObjectiveTestFunctionSolutionAnalyzer != null) {
252        BestSingleObjectiveTestFunctionSolutionAnalyzer.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
253        BestSingleObjectiveTestFunctionSolutionAnalyzer.ResultsParameter.ActualName = "Results";
254        BestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
255        BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
256        BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
257        BestSingleObjectiveTestFunctionSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
258        BestSingleObjectiveTestFunctionSolutionAnalyzer.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
259        BestSingleObjectiveTestFunctionSolutionAnalyzer.BoundsParameter.ActualName = BoundsParameter.Name;
260      }
261
262      if (SingleObjectivePopulationDiversityAnalyzer != null) {
263        SingleObjectivePopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
264        SingleObjectivePopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
265        SingleObjectivePopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
266        SingleObjectivePopulationDiversityAnalyzer.SimilarityCalculator = Operators.OfType<SingleObjectiveTestFunctionSimilarityCalculator>().SingleOrDefault();
267      }
268    }
269    private void InitializeOperators() {
270      Operators.Add(new SingleObjectiveTestFunctionImprovementOperator());
271      Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
272      Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
273
274      Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
275      Operators.Add(new SingleObjectivePopulationDiversityAnalyzer());
276      ParameterizeAnalyzers();
277      Operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
278      Operators.Add(strategyVectorCreator);
279      Operators.Add(strategyVectorCrossover);
280      Operators.Add(strategyVectorManipulator);
281      UpdateMoveEvaluators();
282      ParameterizeOperators();
283      InitializeMoveGenerators();
284    }
285    private void InitializeMoveGenerators() {
286      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
287        if (op is IMoveGenerator) {
288          op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
289        }
290      }
291    }
292    private void UpdateMoveEvaluators() {
293      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
294        Operators.Remove(op);
295      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
296        if (op.EvaluatorType == Evaluator.GetType()) {
297          Operators.Add(op);
298          #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
299          if (op is ISphereMoveEvaluator) {
300            SphereEvaluator e = (Evaluator as SphereEvaluator);
301            e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
302            e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
303            ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
304            em.C = e.C;
305            em.Alpha = e.Alpha;
306          } else if (op is IRastriginMoveEvaluator) {
307            RastriginEvaluator e = (Evaluator as RastriginEvaluator);
308            e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
309            IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
310            em.A = e.A;
311          }
312          #endregion
313        }
314      ParameterizeOperators();
315      OnOperatorsChanged();
316    }
317    private void ParameterizeSolutionCreator() {
318      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
319      SolutionCreator.LengthParameter.Hidden = true;
320      SolutionCreator.BoundsParameter.ActualName = BoundsParameter.Name;
321      SolutionCreator.BoundsParameter.Hidden = true;
322    }
323    private void ParameterizeEvaluator() {
324      Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
325      Evaluator.PointParameter.Hidden = true;
326      try {
327        BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize.Value);
328      }
329      catch (ArgumentException e) {
330        ErrorHandling.ShowErrorDialog(e);
331        ProblemSize.Value = Evaluator.MinimumProblemSize;
332      }
333    }
334    private void ParameterizeOperators() {
335      foreach (var op in Operators.OfType<IRealVectorCrossover>()) {
336        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
337        op.ParentsParameter.Hidden = true;
338        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
339        op.ChildParameter.Hidden = true;
340        op.BoundsParameter.ActualName = BoundsParameter.Name;
341        op.BoundsParameter.Hidden = true;
342      }
343      foreach (var op in Operators.OfType<IRealVectorManipulator>()) {
344        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
345        op.RealVectorParameter.Hidden = true;
346        op.BoundsParameter.ActualName = BoundsParameter.Name;
347        op.BoundsParameter.Hidden = true;
348      }
349      foreach (var op in Operators.OfType<IRealVectorMoveOperator>()) {
350        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
351        op.RealVectorParameter.Hidden = true;
352      }
353      foreach (var op in Operators.OfType<IRealVectorMoveGenerator>()) {
354        op.BoundsParameter.ActualName = BoundsParameter.Name;
355        op.BoundsParameter.Hidden = true;
356      }
357      foreach (var op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
358        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
359        op.QualityParameter.Hidden = true;
360        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
361        op.RealVectorParameter.Hidden = true;
362      }
363      foreach (var op in Operators.OfType<IRealVectorParticleCreator>()) {
364        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
365        op.RealVectorParameter.Hidden = true;
366        op.BoundsParameter.ActualName = BoundsParameter.Name;
367        op.BoundsParameter.Hidden = true;
368        op.ProblemSizeParameter.ActualName = ProblemSizeParameter.Name;
369        op.ProblemSizeParameter.Hidden = true;
370      }
371      foreach (var op in Operators.OfType<IRealVectorParticleUpdater>()) {
372        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
373        op.RealVectorParameter.Hidden = true;
374        op.BoundsParameter.ActualName = BoundsParameter.Name;
375        op.BoundsParameter.Hidden = true;
376      }
377      foreach (var op in Operators.OfType<IRealVectorSwarmUpdater>()) {
378        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
379        op.RealVectorParameter.Hidden = true;
380        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
381        op.MaximizationParameter.Hidden = true;
382      }
383      foreach (var op in Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()) {
384        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
385        op.RealVectorParameter.Hidden = true;
386      }
387      foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
388        op.SolutionParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
389        op.SolutionParameter.Hidden = true;
390      }
391      foreach (var op in Operators.OfType<ISingleObjectivePathRelinker>()) {
392        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
393        op.ParentsParameter.Hidden = true;
394      }
395      foreach (var op in Operators.OfType<SingleObjectiveTestFunctionSimilarityCalculator>()) {
396        op.SolutionVariableName = SolutionCreator.RealVectorParameter.ActualName;
397        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
398        op.Bounds = Bounds;
399      }
400    }
401    private void UpdateStrategyVectorBounds() {
402      var strategyBounds = (DoubleMatrix)Bounds.Clone();
403      for (int i = 0; i < strategyBounds.Rows; i++) {
404        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
405        strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
406      }
407      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
408    }
409    #endregion
410  }
411}
Note: See TracBrowser for help on using the repository browser.