Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8389 was 8334, checked in by jkarder, 12 years ago

#1331: reintegrated branch

File size: 19.9 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 = new DoubleValue(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 SingleObjectiveTestFunctionImprovementOperator());
260      Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
261      Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
262
263      Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
264      ParameterizeAnalyzers();
265      Operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
266      Operators.Add(strategyVectorCreator);
267      Operators.Add(strategyVectorCrossover);
268      Operators.Add(strategyVectorManipulator);
269      UpdateMoveEvaluators();
270      ParameterizeOperators();
271      InitializeMoveGenerators();
272    }
273    private void InitializeMoveGenerators() {
274      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
275        if (op is IMoveGenerator) {
276          op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
277        }
278      }
279    }
280    private void UpdateMoveEvaluators() {
281      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
282        Operators.Remove(op);
283      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
284        if (op.EvaluatorType == Evaluator.GetType()) {
285          Operators.Add(op);
286          #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
287          if (op is ISphereMoveEvaluator) {
288            SphereEvaluator e = (Evaluator as SphereEvaluator);
289            e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
290            e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
291            ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
292            em.C = e.C;
293            em.Alpha = e.Alpha;
294          } else if (op is IRastriginMoveEvaluator) {
295            RastriginEvaluator e = (Evaluator as RastriginEvaluator);
296            e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
297            IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
298            em.A = e.A;
299          }
300          #endregion
301        }
302      ParameterizeOperators();
303      OnOperatorsChanged();
304    }
305    private void ParameterizeSolutionCreator() {
306      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
307      SolutionCreator.LengthParameter.Hidden = true;
308      SolutionCreator.BoundsParameter.ActualName = BoundsParameter.Name;
309      SolutionCreator.BoundsParameter.Hidden = true;
310    }
311    private void ParameterizeEvaluator() {
312      Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
313      Evaluator.PointParameter.Hidden = true;
314      BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize.Value);
315    }
316    private void ParameterizeOperators() {
317      foreach (IRealVectorCrossover op in Operators.OfType<IRealVectorCrossover>()) {
318        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
319        op.ParentsParameter.Hidden = true;
320        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
321        op.ChildParameter.Hidden = true;
322        op.BoundsParameter.ActualName = BoundsParameter.Name;
323        op.BoundsParameter.Hidden = true;
324      }
325      foreach (IRealVectorManipulator op in Operators.OfType<IRealVectorManipulator>()) {
326        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
327        op.RealVectorParameter.Hidden = true;
328        op.BoundsParameter.ActualName = BoundsParameter.Name;
329        op.BoundsParameter.Hidden = true;
330      }
331      foreach (IRealVectorMoveOperator op in Operators.OfType<IRealVectorMoveOperator>()) {
332        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
333        op.RealVectorParameter.Hidden = true;
334      }
335      foreach (IRealVectorMoveGenerator op in Operators.OfType<IRealVectorMoveGenerator>()) {
336        op.BoundsParameter.ActualName = BoundsParameter.Name;
337        op.BoundsParameter.Hidden = true;
338      }
339      foreach (ISingleObjectiveTestFunctionAdditiveMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
340        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
341        op.QualityParameter.Hidden = true;
342        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
343        op.RealVectorParameter.Hidden = true;
344      }
345      foreach (IRealVectorParticleCreator op in Operators.OfType<IRealVectorParticleCreator>()) {
346        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
347        op.RealVectorParameter.Hidden = true;
348        op.BoundsParameter.ActualName = BoundsParameter.Name;
349        op.BoundsParameter.Hidden = true;
350        op.ProblemSizeParameter.ActualName = ProblemSizeParameter.Name;
351        op.ProblemSizeParameter.Hidden = true;
352      }
353      foreach (IRealVectorParticleUpdater op in Operators.OfType<IRealVectorParticleUpdater>()) {
354        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
355        op.RealVectorParameter.Hidden = true;
356        op.BoundsParameter.ActualName = BoundsParameter.Name;
357        op.BoundsParameter.Hidden = true;
358      }
359      foreach (IRealVectorSwarmUpdater op in Operators.OfType<IRealVectorSwarmUpdater>()) {
360        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
361        op.RealVectorParameter.Hidden = true;
362        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
363        op.MaximizationParameter.Hidden = true;
364      }
365      foreach (IRealVectorMultiNeighborhoodShakingOperator op in Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()) {
366        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
367        op.RealVectorParameter.Hidden = true;
368      }
369      foreach (ISingleObjectiveImprovementOperator op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
370        op.SolutionParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
371        op.SolutionParameter.Hidden = true;
372      }
373      foreach (ISingleObjectivePathRelinker op in Operators.OfType<ISingleObjectivePathRelinker>()) {
374        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
375        op.ParentsParameter.Hidden = true;
376      }
377      foreach (SingleObjectiveTestFunctionSimilarityCalculator op in Operators.OfType<SingleObjectiveTestFunctionSimilarityCalculator>()) {
378        op.SolutionVariableName = SolutionCreator.RealVectorParameter.ActualName;
379        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
380        op.Bounds = Bounds;
381      }
382    }
383    private void UpdateStrategyVectorBounds() {
384      DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
385      for (int i = 0; i < strategyBounds.Rows; i++) {
386        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
387        strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
388      }
389      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
390    }
391    #endregion
392  }
393}
Note: See TracBrowser for help on using the repository browser.