Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs @ 7624

Last change on this file since 7624 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 20.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Drawing;
25using System.Linq;
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 : ParameterizedNamedItem, ISingleObjectiveProblem, 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    public override Image ItemImage {
50      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
51    }
52
53    #region Parameter Properties
54    public ValueParameter<BoolValue> MaximizationParameter {
55      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
56    }
57    IParameter ISingleObjectiveProblem.MaximizationParameter {
58      get { return MaximizationParameter; }
59    }
60    public ValueParameter<DoubleMatrix> BoundsParameter {
61      get { return (ValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
62    }
63    public ValueParameter<IntValue> ProblemSizeParameter {
64      get { return (ValueParameter<IntValue>)Parameters["ProblemSize"]; }
65    }
66    public ValueParameter<IRealVectorCreator> SolutionCreatorParameter {
67      get { return (ValueParameter<IRealVectorCreator>)Parameters["SolutionCreator"]; }
68    }
69    IParameter IProblem.SolutionCreatorParameter {
70      get { return SolutionCreatorParameter; }
71    }
72    public ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
73      get { return (ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
74    }
75    IParameter IProblem.EvaluatorParameter {
76      get { return EvaluatorParameter; }
77    }
78    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
79      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
80    }
81    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
82      get { return BestKnownQualityParameter; }
83    }
84    public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
85      get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
86    }
87    #endregion
88
89    #region Properties
90    public BoolValue Maximization {
91      get { return MaximizationParameter.Value; }
92      set { MaximizationParameter.Value = value; }
93    }
94    public DoubleMatrix Bounds {
95      get { return BoundsParameter.Value; }
96      set { BoundsParameter.Value = value; }
97    }
98    public IntValue ProblemSize {
99      get { return ProblemSizeParameter.Value; }
100      set { ProblemSizeParameter.Value = value; }
101    }
102    public IRealVectorCreator SolutionCreator {
103      get { return SolutionCreatorParameter.Value; }
104      set { SolutionCreatorParameter.Value = value; }
105    }
106    ISolutionCreator IProblem.SolutionCreator {
107      get { return SolutionCreatorParameter.Value; }
108    }
109    public ISingleObjectiveTestFunctionProblemEvaluator Evaluator {
110      get { return EvaluatorParameter.Value; }
111      set { EvaluatorParameter.Value = value; }
112    }
113    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
114      get { return EvaluatorParameter.Value; }
115    }
116    IEvaluator IProblem.Evaluator {
117      get { return EvaluatorParameter.Value; }
118    }
119    public DoubleValue BestKnownQuality {
120      get { return BestKnownQualityParameter.Value; }
121      set { BestKnownQualityParameter.Value = value; }
122    }
123    public IEnumerable<IOperator> Operators {
124      get { return operators; }
125    }
126    private BestSingleObjectiveTestFunctionSolutionAnalyzer BestSingleObjectiveTestFunctionSolutionAnalyzer {
127      get { return operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); }
128    }
129    #endregion
130
131    [Storable]
132    private List<IOperator> operators;
133
134    [StorableConstructor]
135    private SingleObjectiveTestFunctionProblem(bool deserializing) : base(deserializing) { }
136    private SingleObjectiveTestFunctionProblem(SingleObjectiveTestFunctionProblem original, Cloner cloner)
137      : base(original, cloner) {
138      operators = original.operators.Where(x => original.IsNotFieldReferenced(x)).Select(x => cloner.Clone(x)).ToList();
139      strategyVectorCreator = cloner.Clone(original.strategyVectorCreator);
140      operators.Add(strategyVectorCreator);
141      strategyVectorCrossover = cloner.Clone(original.strategyVectorCrossover);
142      operators.Add(strategyVectorCrossover);
143      strategyVectorManipulator = cloner.Clone(original.strategyVectorManipulator);
144      operators.Add(strategyVectorManipulator);
145      AttachEventHandlers();
146    }
147    public SingleObjectiveTestFunctionProblem()
148      : base() {
149      UniformRandomRealVectorCreator creator = new UniformRandomRealVectorCreator();
150      AckleyEvaluator evaluator = new AckleyEvaluator();
151
152      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(evaluator.Maximization)));
153      Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", evaluator.Bounds));
154      Parameters.Add(new ValueParameter<IntValue>("ProblemSize", "The dimension of the problem.", new IntValue(2)));
155      Parameters.Add(new ValueParameter<IRealVectorCreator>("SolutionCreator", "The operator which should be used to create new test function solutions.", creator));
156      Parameters.Add(new ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The operator which should be used to evaluate test function solutions.", evaluator));
157      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this test function.", new DoubleValue(evaluator.BestKnownQuality)));
158      Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
159
160      strategyVectorCreator = new StdDevStrategyVectorCreator();
161      strategyVectorCreator.LengthParameter.ActualName = ProblemSizeParameter.Name;
162      strategyVectorCrossover = new StdDevStrategyVectorCrossover();
163      strategyVectorManipulator = new StdDevStrategyVectorManipulator();
164      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
165      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
166
167      creator.RealVectorParameter.ActualName = "Point";
168      ParameterizeSolutionCreator();
169      ParameterizeEvaluator();
170
171      InitializeOperators();
172      AttachEventHandlers();
173      UpdateStrategyVectorBounds();
174    }
175
176    public override IDeepCloneable Clone(Cloner cloner) {
177      return new SingleObjectiveTestFunctionProblem(this, cloner);
178    }
179
180    private bool IsNotFieldReferenced(IOperator x) {
181      return !(x == strategyVectorCreator
182        || x == strategyVectorCrossover
183        || x == strategyVectorManipulator);
184    }
185
186    #region Events
187    public event EventHandler SolutionCreatorChanged;
188    private void OnSolutionCreatorChanged() {
189      EventHandler handler = SolutionCreatorChanged;
190      if (handler != null) handler(this, EventArgs.Empty);
191    }
192    public event EventHandler EvaluatorChanged;
193    private void OnEvaluatorChanged() {
194      EventHandler handler = EvaluatorChanged;
195      if (handler != null) handler(this, EventArgs.Empty);
196    }
197    public event EventHandler OperatorsChanged;
198    private void OnOperatorsChanged() {
199      EventHandler handler = OperatorsChanged;
200      if (handler != null) handler(this, EventArgs.Empty);
201    }
202    public event EventHandler Reset;
203    private void OnReset() {
204      EventHandler handler = Reset;
205      if (handler != null) handler(this, EventArgs.Empty);
206    }
207
208    private void ProblemSizeParameter_ValueChanged(object sender, EventArgs e) {
209      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
210      ProblemSize_ValueChanged(null, EventArgs.Empty);
211    }
212    private void ProblemSize_ValueChanged(object sender, EventArgs e) {
213      if (ProblemSize.Value < 1) ProblemSize.Value = 1;
214      ParameterizeSolutionCreator();
215      ParameterizeEvaluator();
216      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize.Value));
217      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize.Value)));
218      OnReset();
219    }
220    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
221      ParameterizeSolutionCreator();
222      ParameterizeAnalyzers();
223      SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
224      OnSolutionCreatorChanged();
225    }
226    private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
227      ParameterizeEvaluator();
228      ParameterizeOperators();
229      ParameterizeAnalyzers();
230    }
231    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
232      ParameterizeEvaluator();
233      UpdateMoveEvaluators();
234      ParameterizeAnalyzers();
235      Maximization.Value = Evaluator.Maximization;
236      BoundsParameter.Value = Evaluator.Bounds;
237      if (ProblemSize.Value < Evaluator.MinimumProblemSize)
238        ProblemSize.Value = Evaluator.MinimumProblemSize;
239      else if (ProblemSize.Value > Evaluator.MaximumProblemSize)
240        ProblemSize.Value = Evaluator.MaximumProblemSize;
241      BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
242      Evaluator_QualityParameter_ActualNameChanged(null, EventArgs.Empty);
243      OnEvaluatorChanged();
244      OnReset();
245    }
246    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
247      ParameterizeOperators();
248    }
249    private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
250      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
251      Bounds_ToStringChanged(null, EventArgs.Empty);
252    }
253    private void Bounds_ToStringChanged(object sender, EventArgs e) {
254      if (Bounds.Columns != 2 || Bounds.Rows < 1)
255        Bounds = new DoubleMatrix(1, 2);
256      ParameterizeOperators();
257      UpdateStrategyVectorBounds();
258    }
259    private void Bounds_ItemChanged(object sender, EventArgs<int, int> e) {
260      if (e.Value2 == 0 && Bounds[e.Value, 1] <= Bounds[e.Value, 0])
261        Bounds[e.Value, 1] = Bounds[e.Value, 0] + 0.1;
262      if (e.Value2 == 1 && Bounds[e.Value, 0] >= Bounds[e.Value, 1])
263        Bounds[e.Value, 0] = Bounds[e.Value, 1] - 0.1;
264      ParameterizeOperators();
265      UpdateStrategyVectorBounds();
266    }
267    private void MoveGenerator_AdditiveMoveParameter_ActualNameChanged(object sender, EventArgs e) {
268      string name = ((ILookupParameter<AdditiveMove>)sender).ActualName;
269      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
270        op.AdditiveMoveParameter.ActualName = name;
271      }
272    }
273    private void SphereEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
274      SphereEvaluator eval = (Evaluator as SphereEvaluator);
275      if (eval != null) {
276        foreach (ISphereMoveEvaluator op in Operators.OfType<ISphereMoveEvaluator>()) {
277          op.C = eval.C;
278          op.Alpha = eval.Alpha;
279        }
280      }
281    }
282    private void RastriginEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
283      RastriginEvaluator eval = (Evaluator as RastriginEvaluator);
284      if (eval != null) {
285        foreach (IRastriginMoveEvaluator op in Operators.OfType<IRastriginMoveEvaluator>()) {
286          op.A = eval.A;
287        }
288      }
289    }
290    private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) {
291      strategyVectorManipulator.BoundsParameter.Value = (DoubleMatrix)strategyVectorCreator.BoundsParameter.Value.Clone();
292    }
293    private void strategyVectorCreator_StrategyParameterParameter_ActualNameChanged(object sender, EventArgs e) {
294      string name = strategyVectorCreator.StrategyParameterParameter.ActualName;
295      strategyVectorCrossover.ParentsParameter.ActualName = name;
296      strategyVectorCrossover.StrategyParameterParameter.ActualName = name;
297      strategyVectorManipulator.StrategyParameterParameter.ActualName = name;
298    }
299    #endregion
300
301    #region Helpers
302    [StorableHook(HookType.AfterDeserialization)]
303    private void AfterDeserialization() {
304      // BackwardsCompatibility3.3
305      #region Backwards compatible code (remove with 3.4)
306      if (operators == null) InitializeOperators();
307      #endregion
308      AttachEventHandlers();
309    }
310
311    private void AttachEventHandlers() {
312      ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
313      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
314      BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
315      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
316      Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
317      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
318      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
319      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
320      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
321      strategyVectorCreator.BoundsParameter.ValueChanged += new EventHandler(strategyVectorCreator_BoundsParameter_ValueChanged);
322      strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
323    }
324    private void ParameterizeAnalyzers() {
325      BestSingleObjectiveTestFunctionSolutionAnalyzer.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
326      BestSingleObjectiveTestFunctionSolutionAnalyzer.ResultsParameter.ActualName = "Results";
327      BestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
328      BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
329      BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
330      BestSingleObjectiveTestFunctionSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
331      BestSingleObjectiveTestFunctionSolutionAnalyzer.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
332      BestSingleObjectiveTestFunctionSolutionAnalyzer.BoundsParameter.ActualName = BoundsParameter.Name;
333    }
334    private void InitializeOperators() {
335      operators = new List<IOperator>();
336      operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
337      ParameterizeAnalyzers();
338      operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
339      operators.Add(strategyVectorCreator);
340      operators.Add(strategyVectorCrossover);
341      operators.Add(strategyVectorManipulator);
342      UpdateMoveEvaluators();
343      ParameterizeOperators();
344      InitializeMoveGenerators();
345    }
346    private void InitializeMoveGenerators() {
347      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
348        if (op is IMoveGenerator) {
349          op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
350        }
351      }
352    }
353    private void UpdateMoveEvaluators() {
354      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
355        operators.Remove(op);
356      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
357        if (op.EvaluatorType == Evaluator.GetType()) {
358          operators.Add(op);
359          #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
360          if (op is ISphereMoveEvaluator) {
361            SphereEvaluator e = (Evaluator as SphereEvaluator);
362            e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
363            e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
364            ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
365            em.C = e.C;
366            em.Alpha = e.Alpha;
367          } else if (op is IRastriginMoveEvaluator) {
368            RastriginEvaluator e = (Evaluator as RastriginEvaluator);
369            e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
370            IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
371            em.A = e.A;
372          }
373          #endregion
374        }
375      ParameterizeOperators();
376      OnOperatorsChanged();
377    }
378    private void ParameterizeSolutionCreator() {
379      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
380    }
381    private void ParameterizeEvaluator() {
382      Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
383      BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize.Value);
384    }
385    private void ParameterizeOperators() {
386      foreach (IRealVectorCrossover op in Operators.OfType<IRealVectorCrossover>()) {
387        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
388        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
389        op.BoundsParameter.ActualName = BoundsParameter.Name;
390      }
391      foreach (IRealVectorManipulator op in Operators.OfType<IRealVectorManipulator>()) {
392        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
393        op.BoundsParameter.ActualName = BoundsParameter.Name;
394      }
395      foreach (IRealVectorMoveOperator op in Operators.OfType<IRealVectorMoveOperator>()) {
396        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
397      }
398      foreach (IRealVectorMoveGenerator op in Operators.OfType<IRealVectorMoveGenerator>()) {
399        op.BoundsParameter.ActualName = BoundsParameter.Name;
400      }
401      foreach (ISingleObjectiveTestFunctionAdditiveMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
402        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
403        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
404      }
405      foreach (IRealVectorPSOEncoder op in Operators.OfType<IRealVectorPSOEncoder>()) {
406        ((ILookupParameter)op.OriginalRealVectorParameter).ActualName = SolutionCreator.RealVectorParameter.ActualName;
407        op.BoundsParameter.Value = (DoubleMatrix)BoundsParameter.Value.Clone();
408        op.BoundsParameter.ActualName = "ParticleBounds";
409      }
410    }
411    private void UpdateStrategyVectorBounds() {
412      DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
413      for (int i = 0; i < strategyBounds.Rows; i++)
414        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
415      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
416    }
417    #endregion
418  }
419}
Note: See TracBrowser for help on using the repository browser.