Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3530 was 3450, checked in by abeham, 15 years ago

Changed handling of strategy parameters and renamed operators in realvector #890, #932, #934

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