Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7351 was 7351, checked in by abeham, 12 years ago

#1722

  • fixed some problems that were identified with the first existing FxCop rules (duplicate storable hook in ExternalEvaluationProblem, multiple wrong names)
  • generally renamed AttachEventHandlers to RegisterEventHandlers to be consistent
  • fixed some backwards compatible regions to use the format from the snippet and the comment
File size: 18.8 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.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Problems.TestFunctions {
35  [Item("Single Objective Test Function", "Test function with real valued inputs and a single objective.")]
36  [StorableClass]
37  [Creatable("Problems")]
38  public sealed class SingleObjectiveTestFunctionProblem : SingleObjectiveHeuristicOptimizationProblem<ISingleObjectiveTestFunctionProblemEvaluator, IRealVectorCreator>, IStorableContent {
39    public string Filename { get; set; }
40
41    [Storable]
42    private StdDevStrategyVectorCreator strategyVectorCreator;
43    [Storable]
44    private StdDevStrategyVectorCrossover strategyVectorCrossover;
45    [Storable]
46    private StdDevStrategyVectorManipulator strategyVectorManipulator;
47
48    #region Parameter Properties
49    public ValueParameter<DoubleMatrix> BoundsParameter {
50      get { return (ValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
51    }
52    public ValueParameter<IntValue> ProblemSizeParameter {
53      get { return (ValueParameter<IntValue>)Parameters["ProblemSize"]; }
54    }
55    public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
56      get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
57    }
58    #endregion
59
60    #region Properties
61    public DoubleMatrix Bounds {
62      get { return BoundsParameter.Value; }
63      set { BoundsParameter.Value = value; }
64    }
65    public IntValue ProblemSize {
66      get { return ProblemSizeParameter.Value; }
67      set { ProblemSizeParameter.Value = value; }
68    }
69    private BestSingleObjectiveTestFunctionSolutionAnalyzer BestSingleObjectiveTestFunctionSolutionAnalyzer {
70      get { return Operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); }
71    }
72    #endregion
73
74    // BackwardsCompatibility3.3
75    #region Backwards compatible code, remove with 3.4
76    [Obsolete]
77    [Storable(Name = "operators")]
78    private IEnumerable<IOperator> oldOperators {
79      get { return null; }
80      set {
81        if (value != null && value.Any())
82          Operators.AddRange(value);
83      }
84    }
85    #endregion
86
87    [StorableConstructor]
88    private SingleObjectiveTestFunctionProblem(bool deserializing) : base(deserializing) { }
89    private SingleObjectiveTestFunctionProblem(SingleObjectiveTestFunctionProblem original, Cloner cloner)
90      : base(original, cloner) {
91      strategyVectorCreator = cloner.Clone(original.strategyVectorCreator);
92      strategyVectorCrossover = cloner.Clone(original.strategyVectorCrossover);
93      strategyVectorManipulator = cloner.Clone(original.strategyVectorManipulator);
94      RegisterEventHandlers();
95    }
96    public SingleObjectiveTestFunctionProblem()
97      : base(new AckleyEvaluator(), new UniformRandomRealVectorCreator()) {
98      Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", Evaluator.Bounds));
99      Parameters.Add(new ValueParameter<IntValue>("ProblemSize", "The dimension of the problem.", new IntValue(2)));
100      Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
101
102      Maximization.Value = Evaluator.Maximization;
103      BestKnownQuality.Value = Evaluator.BestKnownQuality;
104
105      strategyVectorCreator = new StdDevStrategyVectorCreator();
106      strategyVectorCreator.LengthParameter.ActualName = ProblemSizeParameter.Name;
107      strategyVectorCrossover = new StdDevStrategyVectorCrossover();
108      strategyVectorManipulator = new StdDevStrategyVectorManipulator();
109      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
110      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
111
112      SolutionCreator.RealVectorParameter.ActualName = "Point";
113      ParameterizeSolutionCreator();
114      ParameterizeEvaluator();
115
116      InitializeOperators();
117      RegisterEventHandlers();
118      UpdateStrategyVectorBounds();
119    }
120
121    public override IDeepCloneable Clone(Cloner cloner) {
122      return new SingleObjectiveTestFunctionProblem(this, cloner);
123    }
124
125    private bool IsNotFieldReferenced(IOperator x) {
126      return !(x == strategyVectorCreator
127        || x == strategyVectorCrossover
128        || x == strategyVectorManipulator);
129    }
130
131    #region Events
132    protected override void OnSolutionCreatorChanged() {
133      base.OnSolutionCreatorChanged();
134      ParameterizeSolutionCreator();
135      ParameterizeAnalyzers();
136      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
137      SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
138    }
139    protected override void OnEvaluatorChanged() {
140      base.OnEvaluatorChanged();
141      ParameterizeEvaluator();
142      UpdateMoveEvaluators();
143      ParameterizeAnalyzers();
144      Maximization.Value = Evaluator.Maximization;
145      BoundsParameter.Value = Evaluator.Bounds;
146      if (ProblemSize.Value < Evaluator.MinimumProblemSize)
147        ProblemSize.Value = Evaluator.MinimumProblemSize;
148      else if (ProblemSize.Value > Evaluator.MaximumProblemSize)
149        ProblemSize.Value = Evaluator.MaximumProblemSize;
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      BestSingleObjectiveTestFunctionSolutionAnalyzer.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
250      BestSingleObjectiveTestFunctionSolutionAnalyzer.ResultsParameter.ActualName = "Results";
251      BestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
252      BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
253      BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
254      BestSingleObjectiveTestFunctionSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
255      BestSingleObjectiveTestFunctionSolutionAnalyzer.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
256      BestSingleObjectiveTestFunctionSolutionAnalyzer.BoundsParameter.ActualName = BoundsParameter.Name;
257    }
258    private void InitializeOperators() {
259      Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
260      ParameterizeAnalyzers();
261      Operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
262      Operators.Add(strategyVectorCreator);
263      Operators.Add(strategyVectorCrossover);
264      Operators.Add(strategyVectorManipulator);
265      UpdateMoveEvaluators();
266      ParameterizeOperators();
267      InitializeMoveGenerators();
268    }
269    private void InitializeMoveGenerators() {
270      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
271        if (op is IMoveGenerator) {
272          op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
273        }
274      }
275    }
276    private void UpdateMoveEvaluators() {
277      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
278        Operators.Remove(op);
279      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
280        if (op.EvaluatorType == Evaluator.GetType()) {
281          Operators.Add(op);
282          #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
283          if (op is ISphereMoveEvaluator) {
284            SphereEvaluator e = (Evaluator as SphereEvaluator);
285            e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
286            e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
287            ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
288            em.C = e.C;
289            em.Alpha = e.Alpha;
290          } else if (op is IRastriginMoveEvaluator) {
291            RastriginEvaluator e = (Evaluator as RastriginEvaluator);
292            e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
293            IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
294            em.A = e.A;
295          }
296          #endregion
297        }
298      ParameterizeOperators();
299      OnOperatorsChanged();
300    }
301    private void ParameterizeSolutionCreator() {
302      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
303      SolutionCreator.LengthParameter.Hidden = true;
304      SolutionCreator.BoundsParameter.ActualName = BoundsParameter.Name;
305      SolutionCreator.BoundsParameter.Hidden = true;
306    }
307    private void ParameterizeEvaluator() {
308      Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
309      Evaluator.PointParameter.Hidden = true;
310      BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize.Value);
311    }
312    private void ParameterizeOperators() {
313      foreach (IRealVectorCrossover op in Operators.OfType<IRealVectorCrossover>()) {
314        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
315        op.ParentsParameter.Hidden = true;
316        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
317        op.ChildParameter.Hidden = true;
318        op.BoundsParameter.ActualName = BoundsParameter.Name;
319        op.BoundsParameter.Hidden = true;
320      }
321      foreach (IRealVectorManipulator op in Operators.OfType<IRealVectorManipulator>()) {
322        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
323        op.RealVectorParameter.Hidden = true;
324        op.BoundsParameter.ActualName = BoundsParameter.Name;
325        op.BoundsParameter.Hidden = true;
326      }
327      foreach (IRealVectorMoveOperator op in Operators.OfType<IRealVectorMoveOperator>()) {
328        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
329        op.RealVectorParameter.Hidden = true;
330      }
331      foreach (IRealVectorMoveGenerator op in Operators.OfType<IRealVectorMoveGenerator>()) {
332        op.BoundsParameter.ActualName = BoundsParameter.Name;
333        op.BoundsParameter.Hidden = true;
334      }
335      foreach (ISingleObjectiveTestFunctionAdditiveMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
336        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
337        op.QualityParameter.Hidden = true;
338        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
339        op.RealVectorParameter.Hidden = true;
340      }
341      foreach (IRealVectorParticleCreator op in Operators.OfType<IRealVectorParticleCreator>()) {
342        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
343        op.RealVectorParameter.Hidden = true;
344        op.BoundsParameter.ActualName = BoundsParameter.Name;
345        op.BoundsParameter.Hidden = true;
346        op.ProblemSizeParameter.ActualName = ProblemSizeParameter.Name;
347        op.ProblemSizeParameter.Hidden = true;
348      }
349      foreach (IRealVectorParticleUpdater op in Operators.OfType<IRealVectorParticleUpdater>()) {
350        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
351        op.RealVectorParameter.Hidden = true;
352        op.BoundsParameter.ActualName = BoundsParameter.Name;
353        op.BoundsParameter.Hidden = true;
354      }
355      foreach (IRealVectorSwarmUpdater op in Operators.OfType<IRealVectorSwarmUpdater>()) {
356        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
357        op.RealVectorParameter.Hidden = true;
358        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
359        op.MaximizationParameter.Hidden = true;
360      }
361      foreach (var op in Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()) {
362        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
363        op.RealVectorParameter.Hidden = true;
364      }
365    }
366    private void UpdateStrategyVectorBounds() {
367      DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
368      for (int i = 0; i < strategyBounds.Rows; i++) {
369        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
370        strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
371      }
372      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
373    }
374    #endregion
375  }
376}
Note: See TracBrowser for help on using the repository browser.