Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs @ 12062

Last change on this file since 12062 was 8660, checked in by gkronber, 12 years ago

#1847 merged r8205:8635 from trunk into branch

File size: 20.1 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      bool problemSizeChange = ProblemSize.Value < Evaluator.MinimumProblemSize
142        || ProblemSize.Value > Evaluator.MaximumProblemSize;
143      if (problemSizeChange) {
144        ProblemSize.Value = Math.Max(Evaluator.MinimumProblemSize, Math.Min(ProblemSize.Value, Evaluator.MaximumProblemSize));
145      } else {
146        ParameterizeEvaluator();
147      }
148      UpdateMoveEvaluators();
149      ParameterizeAnalyzers();
150      Maximization.Value = Evaluator.Maximization;
151      BoundsParameter.Value = Evaluator.Bounds;
152      BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
153      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
154      Evaluator_QualityParameter_ActualNameChanged(null, EventArgs.Empty);
155      OnReset();
156    }
157    private void ProblemSizeParameter_ValueChanged(object sender, EventArgs e) {
158      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
159      ProblemSize_ValueChanged(null, EventArgs.Empty);
160    }
161    private void ProblemSize_ValueChanged(object sender, EventArgs e) {
162      if (ProblemSize.Value < 1) ProblemSize.Value = 1;
163      ParameterizeSolutionCreator();
164      ParameterizeEvaluator();
165      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize.Value));
166      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize.Value)));
167      OnReset();
168    }
169    private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
170      ParameterizeEvaluator();
171      ParameterizeOperators();
172      ParameterizeAnalyzers();
173    }
174    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
175      ParameterizeOperators();
176    }
177    private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
178      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
179      Bounds_ToStringChanged(null, EventArgs.Empty);
180    }
181    private void Bounds_ToStringChanged(object sender, EventArgs e) {
182      if (Bounds.Columns != 2 || Bounds.Rows < 1)
183        Bounds = new DoubleMatrix(1, 2);
184      ParameterizeOperators();
185      UpdateStrategyVectorBounds();
186    }
187    private void Bounds_ItemChanged(object sender, EventArgs<int, int> e) {
188      if (e.Value2 == 0 && Bounds[e.Value, 1] <= Bounds[e.Value, 0])
189        Bounds[e.Value, 1] = Bounds[e.Value, 0] + 0.1;
190      if (e.Value2 == 1 && Bounds[e.Value, 0] >= Bounds[e.Value, 1])
191        Bounds[e.Value, 0] = Bounds[e.Value, 1] - 0.1;
192      ParameterizeOperators();
193      UpdateStrategyVectorBounds();
194    }
195    private void MoveGenerator_AdditiveMoveParameter_ActualNameChanged(object sender, EventArgs e) {
196      string name = ((ILookupParameter<AdditiveMove>)sender).ActualName;
197      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
198        op.AdditiveMoveParameter.ActualName = name;
199      }
200    }
201    private void SphereEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
202      SphereEvaluator eval = (Evaluator as SphereEvaluator);
203      if (eval != null) {
204        foreach (ISphereMoveEvaluator op in Operators.OfType<ISphereMoveEvaluator>()) {
205          op.C = eval.C;
206          op.Alpha = eval.Alpha;
207        }
208      }
209    }
210    private void RastriginEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
211      RastriginEvaluator eval = (Evaluator as RastriginEvaluator);
212      if (eval != null) {
213        foreach (IRastriginMoveEvaluator op in Operators.OfType<IRastriginMoveEvaluator>()) {
214          op.A = eval.A;
215        }
216      }
217    }
218    private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) {
219      strategyVectorManipulator.BoundsParameter.Value = (DoubleMatrix)strategyVectorCreator.BoundsParameter.Value.Clone();
220    }
221    private void strategyVectorCreator_StrategyParameterParameter_ActualNameChanged(object sender, EventArgs e) {
222      string name = strategyVectorCreator.StrategyParameterParameter.ActualName;
223      strategyVectorCrossover.ParentsParameter.ActualName = name;
224      strategyVectorCrossover.StrategyParameterParameter.ActualName = name;
225      strategyVectorManipulator.StrategyParameterParameter.ActualName = name;
226    }
227    #endregion
228
229    #region Helpers
230    [StorableHook(HookType.AfterDeserialization)]
231    private void AfterDeserialization() {
232      // BackwardsCompatibility3.3
233      #region Backwards compatible code (remove with 3.4)
234      if (Operators.Count == 0) InitializeOperators();
235      #endregion
236      RegisterEventHandlers();
237    }
238
239    private void RegisterEventHandlers() {
240      ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
241      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
242      BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
243      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
244      Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
245      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
246      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
247      strategyVectorCreator.BoundsParameter.ValueChanged += new EventHandler(strategyVectorCreator_BoundsParameter_ValueChanged);
248      strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
249    }
250    private void ParameterizeAnalyzers() {
251      BestSingleObjectiveTestFunctionSolutionAnalyzer.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
252      BestSingleObjectiveTestFunctionSolutionAnalyzer.ResultsParameter.ActualName = "Results";
253      BestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
254      BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
255      BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
256      BestSingleObjectiveTestFunctionSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
257      BestSingleObjectiveTestFunctionSolutionAnalyzer.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
258      BestSingleObjectiveTestFunctionSolutionAnalyzer.BoundsParameter.ActualName = BoundsParameter.Name;
259    }
260    private void InitializeOperators() {
261      Operators.Add(new SingleObjectiveTestFunctionImprovementOperator());
262      Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
263      Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
264
265      Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
266      ParameterizeAnalyzers();
267      Operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
268      Operators.Add(strategyVectorCreator);
269      Operators.Add(strategyVectorCrossover);
270      Operators.Add(strategyVectorManipulator);
271      UpdateMoveEvaluators();
272      ParameterizeOperators();
273      InitializeMoveGenerators();
274    }
275    private void InitializeMoveGenerators() {
276      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
277        if (op is IMoveGenerator) {
278          op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
279        }
280      }
281    }
282    private void UpdateMoveEvaluators() {
283      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
284        Operators.Remove(op);
285      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
286        if (op.EvaluatorType == Evaluator.GetType()) {
287          Operators.Add(op);
288          #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
289          if (op is ISphereMoveEvaluator) {
290            SphereEvaluator e = (Evaluator as SphereEvaluator);
291            e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
292            e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
293            ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
294            em.C = e.C;
295            em.Alpha = e.Alpha;
296          } else if (op is IRastriginMoveEvaluator) {
297            RastriginEvaluator e = (Evaluator as RastriginEvaluator);
298            e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
299            IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
300            em.A = e.A;
301          }
302          #endregion
303        }
304      ParameterizeOperators();
305      OnOperatorsChanged();
306    }
307    private void ParameterizeSolutionCreator() {
308      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
309      SolutionCreator.LengthParameter.Hidden = true;
310      SolutionCreator.BoundsParameter.ActualName = BoundsParameter.Name;
311      SolutionCreator.BoundsParameter.Hidden = true;
312    }
313    private void ParameterizeEvaluator() {
314      Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
315      Evaluator.PointParameter.Hidden = true;
316      try {
317        BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize.Value);
318      } catch (ArgumentException e) {
319        ErrorHandling.ShowErrorDialog(e);
320        ProblemSize.Value = Evaluator.MinimumProblemSize;
321      }
322    }
323    private void ParameterizeOperators() {
324      foreach (IRealVectorCrossover op in Operators.OfType<IRealVectorCrossover>()) {
325        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
326        op.ParentsParameter.Hidden = true;
327        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
328        op.ChildParameter.Hidden = true;
329        op.BoundsParameter.ActualName = BoundsParameter.Name;
330        op.BoundsParameter.Hidden = true;
331      }
332      foreach (IRealVectorManipulator op in Operators.OfType<IRealVectorManipulator>()) {
333        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
334        op.RealVectorParameter.Hidden = true;
335        op.BoundsParameter.ActualName = BoundsParameter.Name;
336        op.BoundsParameter.Hidden = true;
337      }
338      foreach (IRealVectorMoveOperator op in Operators.OfType<IRealVectorMoveOperator>()) {
339        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
340        op.RealVectorParameter.Hidden = true;
341      }
342      foreach (IRealVectorMoveGenerator op in Operators.OfType<IRealVectorMoveGenerator>()) {
343        op.BoundsParameter.ActualName = BoundsParameter.Name;
344        op.BoundsParameter.Hidden = true;
345      }
346      foreach (ISingleObjectiveTestFunctionAdditiveMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
347        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
348        op.QualityParameter.Hidden = true;
349        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
350        op.RealVectorParameter.Hidden = true;
351      }
352      foreach (IRealVectorParticleCreator op in Operators.OfType<IRealVectorParticleCreator>()) {
353        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
354        op.RealVectorParameter.Hidden = true;
355        op.BoundsParameter.ActualName = BoundsParameter.Name;
356        op.BoundsParameter.Hidden = true;
357        op.ProblemSizeParameter.ActualName = ProblemSizeParameter.Name;
358        op.ProblemSizeParameter.Hidden = true;
359      }
360      foreach (IRealVectorParticleUpdater op in Operators.OfType<IRealVectorParticleUpdater>()) {
361        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
362        op.RealVectorParameter.Hidden = true;
363        op.BoundsParameter.ActualName = BoundsParameter.Name;
364        op.BoundsParameter.Hidden = true;
365      }
366      foreach (IRealVectorSwarmUpdater op in Operators.OfType<IRealVectorSwarmUpdater>()) {
367        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
368        op.RealVectorParameter.Hidden = true;
369        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
370        op.MaximizationParameter.Hidden = true;
371      }
372      foreach (IRealVectorMultiNeighborhoodShakingOperator op in Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()) {
373        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
374        op.RealVectorParameter.Hidden = true;
375      }
376      foreach (ISingleObjectiveImprovementOperator op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
377        op.SolutionParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
378        op.SolutionParameter.Hidden = true;
379      }
380      foreach (ISingleObjectivePathRelinker op in Operators.OfType<ISingleObjectivePathRelinker>()) {
381        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
382        op.ParentsParameter.Hidden = true;
383      }
384      foreach (SingleObjectiveTestFunctionSimilarityCalculator op in Operators.OfType<SingleObjectiveTestFunctionSimilarityCalculator>()) {
385        op.SolutionVariableName = SolutionCreator.RealVectorParameter.ActualName;
386        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
387        op.Bounds = Bounds;
388      }
389    }
390    private void UpdateStrategyVectorBounds() {
391      DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
392      for (int i = 0; i < strategyBounds.Rows; i++) {
393        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
394        strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
395      }
396      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
397    }
398    #endregion
399  }
400}
Note: See TracBrowser for help on using the repository browser.