Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12075 was 12069, checked in by jkarder, 10 years ago

#2332: refactored operators and analyzers

  • removed quality and maximization parameters in SingleObjectivePopulationDiversityAnalyzer
  • renamed SingleObjectivePopulationDiversityAnalyzer to PopulationSimilarityAnalyzer
  • added ConstrainedValueParameter for similarity calculators of analyzer
  • added ValueLookupParameter for similarity calculator of the following operators:
    • DuplicatesSelector, ProgressiveOffspringPreserver, ReferenceSetUpdateMethod, SolutionPoolUpdateMethod
  • removed some wiring code in specific problems
File size: 20.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.RealVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Problems.Instances;
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 : SingleObjectiveHeuristicOptimizationProblem<ISingleObjectiveTestFunctionProblemEvaluator, IRealVectorCreator>, IStorableContent, IProblemInstanceConsumer<SOTFData> {
41    public string Filename { get; set; }
42
43    [Storable]
44    private StdDevStrategyVectorCreator strategyVectorCreator;
45    [Storable]
46    private StdDevStrategyVectorCrossover strategyVectorCrossover;
47    [Storable]
48    private StdDevStrategyVectorManipulator strategyVectorManipulator;
49
50    #region Parameter Properties
51    public ValueParameter<DoubleMatrix> BoundsParameter {
52      get { return (ValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
53    }
54    public ValueParameter<IntValue> ProblemSizeParameter {
55      get { return (ValueParameter<IntValue>)Parameters["ProblemSize"]; }
56    }
57    public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
58      get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
59    }
60    #endregion
61
62    #region Properties
63    public DoubleMatrix Bounds {
64      get { return BoundsParameter.Value; }
65      set { BoundsParameter.Value = value; }
66    }
67    public IntValue ProblemSize {
68      get { return ProblemSizeParameter.Value; }
69      set { ProblemSizeParameter.Value = value; }
70    }
71    private BestSingleObjectiveTestFunctionSolutionAnalyzer BestSingleObjectiveTestFunctionSolutionAnalyzer {
72      get { return Operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); }
73    }
74    private PopulationSimilarityAnalyzer PopulationSimilarityAnalyzer {
75      get { return Operators.OfType<PopulationSimilarityAnalyzer>().FirstOrDefault(); }
76    }
77    #endregion
78
79    // BackwardsCompatibility3.3
80    #region Backwards compatible code, remove with 3.4
81    [Obsolete]
82    [Storable(Name = "operators")]
83    private IEnumerable<IOperator> oldOperators {
84      get { return null; }
85      set {
86        if (value != null && value.Any())
87          Operators.AddRange(value);
88      }
89    }
90    #endregion
91
92    [StorableConstructor]
93    private SingleObjectiveTestFunctionProblem(bool deserializing) : base(deserializing) { }
94    private SingleObjectiveTestFunctionProblem(SingleObjectiveTestFunctionProblem original, Cloner cloner)
95      : base(original, cloner) {
96      strategyVectorCreator = cloner.Clone(original.strategyVectorCreator);
97      strategyVectorCrossover = cloner.Clone(original.strategyVectorCrossover);
98      strategyVectorManipulator = cloner.Clone(original.strategyVectorManipulator);
99      RegisterEventHandlers();
100    }
101    public SingleObjectiveTestFunctionProblem()
102      : base(new AckleyEvaluator(), new UniformRandomRealVectorCreator()) {
103      Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", Evaluator.Bounds));
104      Parameters.Add(new ValueParameter<IntValue>("ProblemSize", "The dimension of the problem.", new IntValue(2)));
105      Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
106
107      Maximization.Value = Evaluator.Maximization;
108      BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
109
110      strategyVectorCreator = new StdDevStrategyVectorCreator();
111      strategyVectorCreator.LengthParameter.ActualName = ProblemSizeParameter.Name;
112      strategyVectorCrossover = new StdDevStrategyVectorCrossover();
113      strategyVectorManipulator = new StdDevStrategyVectorManipulator();
114      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
115      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
116
117      SolutionCreator.RealVectorParameter.ActualName = "Point";
118      ParameterizeSolutionCreator();
119      ParameterizeEvaluator();
120
121      InitializeOperators();
122      RegisterEventHandlers();
123      UpdateStrategyVectorBounds();
124    }
125
126    public override IDeepCloneable Clone(Cloner cloner) {
127      return new SingleObjectiveTestFunctionProblem(this, cloner);
128    }
129
130    #region Events
131    protected override void OnSolutionCreatorChanged() {
132      base.OnSolutionCreatorChanged();
133      ParameterizeSolutionCreator();
134      ParameterizeAnalyzers();
135      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
136      SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
137    }
138    protected override void OnEvaluatorChanged() {
139      base.OnEvaluatorChanged();
140      bool problemSizeChange = ProblemSize.Value < Evaluator.MinimumProblemSize
141        || ProblemSize.Value > Evaluator.MaximumProblemSize;
142      if (problemSizeChange) {
143        ProblemSize.Value = Math.Max(Evaluator.MinimumProblemSize, Math.Min(ProblemSize.Value, Evaluator.MaximumProblemSize));
144      } else {
145        ParameterizeEvaluator();
146      }
147      UpdateMoveEvaluators();
148      ParameterizeAnalyzers();
149      Maximization.Value = Evaluator.Maximization;
150      BoundsParameter.Value = Evaluator.Bounds;
151      BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
152      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
153      Evaluator_QualityParameter_ActualNameChanged(null, EventArgs.Empty);
154      OnReset();
155    }
156    private void ProblemSizeParameter_ValueChanged(object sender, EventArgs e) {
157      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
158      ProblemSize_ValueChanged(null, EventArgs.Empty);
159    }
160    private void ProblemSize_ValueChanged(object sender, EventArgs e) {
161      if (ProblemSize.Value < 1) ProblemSize.Value = 1;
162      ParameterizeSolutionCreator();
163      ParameterizeEvaluator();
164      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize.Value));
165      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize.Value)));
166      OnReset();
167    }
168    private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
169      ParameterizeEvaluator();
170      ParameterizeOperators();
171      ParameterizeAnalyzers();
172    }
173    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
174      ParameterizeOperators();
175    }
176    private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
177      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
178      Bounds_ToStringChanged(null, EventArgs.Empty);
179    }
180    private void Bounds_ToStringChanged(object sender, EventArgs e) {
181      if (Bounds.Columns != 2 || Bounds.Rows < 1)
182        Bounds = new DoubleMatrix(1, 2);
183      ParameterizeOperators();
184      UpdateStrategyVectorBounds();
185    }
186    private void Bounds_ItemChanged(object sender, EventArgs<int, int> e) {
187      if (e.Value2 == 0 && Bounds[e.Value, 1] <= Bounds[e.Value, 0])
188        Bounds[e.Value, 1] = Bounds[e.Value, 0] + 0.1;
189      if (e.Value2 == 1 && Bounds[e.Value, 0] >= Bounds[e.Value, 1])
190        Bounds[e.Value, 0] = Bounds[e.Value, 1] - 0.1;
191      ParameterizeOperators();
192      UpdateStrategyVectorBounds();
193    }
194    private void MoveGenerator_AdditiveMoveParameter_ActualNameChanged(object sender, EventArgs e) {
195      string name = ((ILookupParameter<AdditiveMove>)sender).ActualName;
196      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
197        op.AdditiveMoveParameter.ActualName = name;
198      }
199    }
200    private void SphereEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
201      SphereEvaluator eval = (Evaluator as SphereEvaluator);
202      if (eval != null) {
203        foreach (ISphereMoveEvaluator op in Operators.OfType<ISphereMoveEvaluator>()) {
204          op.C = eval.C;
205          op.Alpha = eval.Alpha;
206        }
207      }
208    }
209    private void RastriginEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
210      RastriginEvaluator eval = (Evaluator as RastriginEvaluator);
211      if (eval != null) {
212        foreach (IRastriginMoveEvaluator op in Operators.OfType<IRastriginMoveEvaluator>()) {
213          op.A = eval.A;
214        }
215      }
216    }
217    private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) {
218      strategyVectorManipulator.BoundsParameter.Value = (DoubleMatrix)strategyVectorCreator.BoundsParameter.Value.Clone();
219    }
220    private void strategyVectorCreator_StrategyParameterParameter_ActualNameChanged(object sender, EventArgs e) {
221      string name = strategyVectorCreator.StrategyParameterParameter.ActualName;
222      strategyVectorCrossover.ParentsParameter.ActualName = name;
223      strategyVectorCrossover.StrategyParameterParameter.ActualName = name;
224      strategyVectorManipulator.StrategyParameterParameter.ActualName = name;
225    }
226    #endregion
227
228    #region Helpers
229    [StorableHook(HookType.AfterDeserialization)]
230    private void AfterDeserialization() {
231      // BackwardsCompatibility3.3
232      #region Backwards compatible code (remove with 3.4)
233      if (Operators.Count == 0) InitializeOperators();
234      #endregion
235      RegisterEventHandlers();
236    }
237
238    private void RegisterEventHandlers() {
239      ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
240      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
241      BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
242      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
243      Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
244      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
245      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
246      strategyVectorCreator.BoundsParameter.ValueChanged += new EventHandler(strategyVectorCreator_BoundsParameter_ValueChanged);
247      strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
248    }
249    private void ParameterizeAnalyzers() {
250      if (BestSingleObjectiveTestFunctionSolutionAnalyzer != null) {
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    }
261    private void InitializeOperators() {
262      Operators.Add(new SingleObjectiveTestFunctionImprovementOperator());
263      Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
264      Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
265
266      Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
267      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
268      ParameterizeAnalyzers();
269      Operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
270      Operators.Add(strategyVectorCreator);
271      Operators.Add(strategyVectorCrossover);
272      Operators.Add(strategyVectorManipulator);
273      UpdateMoveEvaluators();
274      ParameterizeOperators();
275      InitializeMoveGenerators();
276    }
277    private void InitializeMoveGenerators() {
278      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
279        if (op is IMoveGenerator) {
280          op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
281        }
282      }
283    }
284    private void UpdateMoveEvaluators() {
285      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
286        Operators.Remove(op);
287      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
288        if (op.EvaluatorType == Evaluator.GetType()) {
289          Operators.Add(op);
290          #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
291          if (op is ISphereMoveEvaluator) {
292            SphereEvaluator e = (Evaluator as SphereEvaluator);
293            e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
294            e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
295            ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
296            em.C = e.C;
297            em.Alpha = e.Alpha;
298          } else if (op is IRastriginMoveEvaluator) {
299            RastriginEvaluator e = (Evaluator as RastriginEvaluator);
300            e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
301            IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
302            em.A = e.A;
303          }
304          #endregion
305        }
306      ParameterizeOperators();
307      OnOperatorsChanged();
308    }
309    private void ParameterizeSolutionCreator() {
310      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
311      SolutionCreator.LengthParameter.Hidden = true;
312      SolutionCreator.BoundsParameter.ActualName = BoundsParameter.Name;
313      SolutionCreator.BoundsParameter.Hidden = true;
314    }
315    private void ParameterizeEvaluator() {
316      Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
317      Evaluator.PointParameter.Hidden = true;
318      try {
319        BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize.Value);
320      } catch (ArgumentException e) {
321        ErrorHandling.ShowErrorDialog(e);
322        ProblemSize.Value = Evaluator.MinimumProblemSize;
323      }
324    }
325    private void ParameterizeOperators() {
326      foreach (var op in Operators.OfType<IRealVectorCrossover>()) {
327        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
328        op.ParentsParameter.Hidden = true;
329        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
330        op.ChildParameter.Hidden = true;
331        op.BoundsParameter.ActualName = BoundsParameter.Name;
332        op.BoundsParameter.Hidden = true;
333      }
334      foreach (var op in Operators.OfType<IRealVectorManipulator>()) {
335        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
336        op.RealVectorParameter.Hidden = true;
337        op.BoundsParameter.ActualName = BoundsParameter.Name;
338        op.BoundsParameter.Hidden = true;
339      }
340      foreach (var op in Operators.OfType<IRealVectorMoveOperator>()) {
341        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
342        op.RealVectorParameter.Hidden = true;
343      }
344      foreach (var op in Operators.OfType<IRealVectorMoveGenerator>()) {
345        op.BoundsParameter.ActualName = BoundsParameter.Name;
346        op.BoundsParameter.Hidden = true;
347      }
348      foreach (var op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
349        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
350        op.QualityParameter.Hidden = true;
351        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
352        op.RealVectorParameter.Hidden = true;
353      }
354      foreach (var op in Operators.OfType<IRealVectorParticleCreator>()) {
355        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
356        op.RealVectorParameter.Hidden = true;
357        op.BoundsParameter.ActualName = BoundsParameter.Name;
358        op.BoundsParameter.Hidden = true;
359        op.ProblemSizeParameter.ActualName = ProblemSizeParameter.Name;
360        op.ProblemSizeParameter.Hidden = true;
361      }
362      foreach (var op in Operators.OfType<IRealVectorParticleUpdater>()) {
363        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
364        op.RealVectorParameter.Hidden = true;
365        op.BoundsParameter.ActualName = BoundsParameter.Name;
366        op.BoundsParameter.Hidden = true;
367      }
368      foreach (var op in Operators.OfType<IRealVectorSwarmUpdater>()) {
369        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
370        op.RealVectorParameter.Hidden = true;
371        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
372        op.MaximizationParameter.Hidden = true;
373      }
374      foreach (var op in Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()) {
375        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
376        op.RealVectorParameter.Hidden = true;
377      }
378      foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
379        op.SolutionParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
380        op.SolutionParameter.Hidden = true;
381      }
382      foreach (var op in Operators.OfType<ISingleObjectivePathRelinker>()) {
383        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
384        op.ParentsParameter.Hidden = true;
385      }
386      foreach (var op in Operators.OfType<SingleObjectiveTestFunctionSimilarityCalculator>()) {
387        op.SolutionVariableName = SolutionCreator.RealVectorParameter.ActualName;
388        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
389        op.Bounds = Bounds;
390      }
391    }
392    private void UpdateStrategyVectorBounds() {
393      var strategyBounds = (DoubleMatrix)Bounds.Clone();
394      for (int i = 0; i < strategyBounds.Rows; i++) {
395        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
396        strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
397      }
398      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
399    }
400    #endregion
401
402    public void Load(SOTFData data) {
403      Name = data.Name;
404      Description = data.Description;
405      Evaluator = data.Evaluator;
406    }
407  }
408}
Note: See TracBrowser for help on using the repository browser.