Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs @ 18066

Last change on this file since 18066 was 9363, checked in by spimming, 12 years ago

#1888:

  • Merged revisions from trunk
File size: 20.6 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 HeuristicLab.Analysis;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31using System;
32using System.Collections.Generic;
33using System.Linq;
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, EventArgs<int, int> e) {
186      if (e.Value2 == 0 && Bounds[e.Value, 1] <= Bounds[e.Value, 0])
187        Bounds[e.Value, 1] = Bounds[e.Value, 0] + 0.1;
188      if (e.Value2 == 1 && Bounds[e.Value, 0] >= Bounds[e.Value, 1])
189        Bounds[e.Value, 0] = Bounds[e.Value, 1] - 0.1;
190      ParameterizeOperators();
191      UpdateStrategyVectorBounds();
192    }
193    private void MoveGenerator_AdditiveMoveParameter_ActualNameChanged(object sender, EventArgs e) {
194      string name = ((ILookupParameter<AdditiveMove>)sender).ActualName;
195      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
196        op.AdditiveMoveParameter.ActualName = name;
197      }
198    }
199    private void SphereEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
200      SphereEvaluator eval = (Evaluator as SphereEvaluator);
201      if (eval != null) {
202        foreach (ISphereMoveEvaluator op in Operators.OfType<ISphereMoveEvaluator>()) {
203          op.C = eval.C;
204          op.Alpha = eval.Alpha;
205        }
206      }
207    }
208    private void RastriginEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
209      RastriginEvaluator eval = (Evaluator as RastriginEvaluator);
210      if (eval != null) {
211        foreach (IRastriginMoveEvaluator op in Operators.OfType<IRastriginMoveEvaluator>()) {
212          op.A = eval.A;
213        }
214      }
215    }
216    private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) {
217      strategyVectorManipulator.BoundsParameter.Value = (DoubleMatrix)strategyVectorCreator.BoundsParameter.Value.Clone();
218    }
219    private void strategyVectorCreator_StrategyParameterParameter_ActualNameChanged(object sender, EventArgs e) {
220      string name = strategyVectorCreator.StrategyParameterParameter.ActualName;
221      strategyVectorCrossover.ParentsParameter.ActualName = name;
222      strategyVectorCrossover.StrategyParameterParameter.ActualName = name;
223      strategyVectorManipulator.StrategyParameterParameter.ActualName = name;
224    }
225    #endregion
226
227    #region Helpers
228    [StorableHook(HookType.AfterDeserialization)]
229    private void AfterDeserialization() {
230      // BackwardsCompatibility3.3
231      #region Backwards compatible code (remove with 3.4)
232      if (Operators.Count == 0) InitializeOperators();
233      #endregion
234      RegisterEventHandlers();
235    }
236
237    private void RegisterEventHandlers() {
238      ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
239      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
240      BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
241      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
242      Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
243      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
244      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
245      strategyVectorCreator.BoundsParameter.ValueChanged += new EventHandler(strategyVectorCreator_BoundsParameter_ValueChanged);
246      strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
247    }
248    private void ParameterizeAnalyzers() {
249      if (BestSingleObjectiveTestFunctionSolutionAnalyzer != null) {
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
260      if (SingleObjectivePopulationDiversityAnalyzer != null) {
261        SingleObjectivePopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
262        SingleObjectivePopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
263        SingleObjectivePopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
264        SingleObjectivePopulationDiversityAnalyzer.SimilarityCalculator = Operators.OfType<SingleObjectiveTestFunctionSimilarityCalculator>().SingleOrDefault();
265      }
266    }
267    private void InitializeOperators() {
268      Operators.Add(new SingleObjectiveTestFunctionImprovementOperator());
269      Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
270      Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
271
272      Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
273      Operators.Add(new SingleObjectivePopulationDiversityAnalyzer());
274      ParameterizeAnalyzers();
275      Operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
276      Operators.Add(strategyVectorCreator);
277      Operators.Add(strategyVectorCrossover);
278      Operators.Add(strategyVectorManipulator);
279      UpdateMoveEvaluators();
280      ParameterizeOperators();
281      InitializeMoveGenerators();
282    }
283    private void InitializeMoveGenerators() {
284      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
285        if (op is IMoveGenerator) {
286          op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
287        }
288      }
289    }
290    private void UpdateMoveEvaluators() {
291      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
292        Operators.Remove(op);
293      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
294        if (op.EvaluatorType == Evaluator.GetType()) {
295          Operators.Add(op);
296          #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
297          if (op is ISphereMoveEvaluator) {
298            SphereEvaluator e = (Evaluator as SphereEvaluator);
299            e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
300            e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
301            ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
302            em.C = e.C;
303            em.Alpha = e.Alpha;
304          } else if (op is IRastriginMoveEvaluator) {
305            RastriginEvaluator e = (Evaluator as RastriginEvaluator);
306            e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
307            IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
308            em.A = e.A;
309          }
310          #endregion
311        }
312      ParameterizeOperators();
313      OnOperatorsChanged();
314    }
315    private void ParameterizeSolutionCreator() {
316      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
317      SolutionCreator.LengthParameter.Hidden = true;
318      SolutionCreator.BoundsParameter.ActualName = BoundsParameter.Name;
319      SolutionCreator.BoundsParameter.Hidden = true;
320    }
321    private void ParameterizeEvaluator() {
322      Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
323      Evaluator.PointParameter.Hidden = true;
324      try {
325        BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize.Value);
326      }
327      catch (ArgumentException e) {
328        ErrorHandling.ShowErrorDialog(e);
329        ProblemSize.Value = Evaluator.MinimumProblemSize;
330      }
331    }
332    private void ParameterizeOperators() {
333      foreach (var op in Operators.OfType<IRealVectorCrossover>()) {
334        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
335        op.ParentsParameter.Hidden = true;
336        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
337        op.ChildParameter.Hidden = true;
338        op.BoundsParameter.ActualName = BoundsParameter.Name;
339        op.BoundsParameter.Hidden = true;
340      }
341      foreach (var op in Operators.OfType<IRealVectorManipulator>()) {
342        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
343        op.RealVectorParameter.Hidden = true;
344        op.BoundsParameter.ActualName = BoundsParameter.Name;
345        op.BoundsParameter.Hidden = true;
346      }
347      foreach (var op in Operators.OfType<IRealVectorMoveOperator>()) {
348        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
349        op.RealVectorParameter.Hidden = true;
350      }
351      foreach (var op in Operators.OfType<IRealVectorMoveGenerator>()) {
352        op.BoundsParameter.ActualName = BoundsParameter.Name;
353        op.BoundsParameter.Hidden = true;
354      }
355      foreach (var op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
356        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
357        op.QualityParameter.Hidden = true;
358        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
359        op.RealVectorParameter.Hidden = true;
360      }
361      foreach (var op in Operators.OfType<IRealVectorParticleCreator>()) {
362        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
363        op.RealVectorParameter.Hidden = true;
364        op.BoundsParameter.ActualName = BoundsParameter.Name;
365        op.BoundsParameter.Hidden = true;
366        op.ProblemSizeParameter.ActualName = ProblemSizeParameter.Name;
367        op.ProblemSizeParameter.Hidden = true;
368      }
369      foreach (var op in Operators.OfType<IRealVectorParticleUpdater>()) {
370        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
371        op.RealVectorParameter.Hidden = true;
372        op.BoundsParameter.ActualName = BoundsParameter.Name;
373        op.BoundsParameter.Hidden = true;
374      }
375      foreach (var op in Operators.OfType<IRealVectorSwarmUpdater>()) {
376        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
377        op.RealVectorParameter.Hidden = true;
378        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
379        op.MaximizationParameter.Hidden = true;
380      }
381      foreach (var op in Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()) {
382        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
383        op.RealVectorParameter.Hidden = true;
384      }
385      foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
386        op.SolutionParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
387        op.SolutionParameter.Hidden = true;
388      }
389      foreach (var op in Operators.OfType<ISingleObjectivePathRelinker>()) {
390        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
391        op.ParentsParameter.Hidden = true;
392      }
393      foreach (var op in Operators.OfType<SingleObjectiveTestFunctionSimilarityCalculator>()) {
394        op.SolutionVariableName = SolutionCreator.RealVectorParameter.ActualName;
395        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
396        op.Bounds = Bounds;
397      }
398    }
399    private void UpdateStrategyVectorBounds() {
400      var strategyBounds = (DoubleMatrix)Bounds.Clone();
401      for (int i = 0; i < strategyBounds.Rows; i++) {
402        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
403        strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
404      }
405      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
406    }
407    #endregion
408  }
409}
Note: See TracBrowser for help on using the repository browser.