Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3322 was 3318, checked in by abeham, 15 years ago

Updated test functions, added reference for Zakharov
Did not find a reference for SumSquares, just described it
Added wiring for rastrigin and sphere
#934

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