Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4040 was 3894, checked in by abeham, 14 years ago

#1029

  • Added Bounds to SingleObjectiveTestFunctionSolution
  • This would break persistence, so I added a CompatibilityMethod (which can be removed again once moving to 3.4)
File size: 20.0 KB
RevLine 
[3150]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
[3170]36namespace HeuristicLab.Problems.TestFunctions {
[3221]37  [Item("Single Objective Test Function", "Test function with real valued inputs and a single objective.")]
[3150]38  [StorableClass]
39  [Creatable("Problems")]
[3170]40  public sealed class SingleObjectiveTestFunctionProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
[3336]41    [Storable]
[3450]42    private StdDevStrategyVectorCreator strategyVectorCreator;
[3336]43    [Storable]
[3450]44    private StdDevStrategyVectorCrossover strategyVectorCrossover;
[3336]45    [Storable]
[3450]46    private StdDevStrategyVectorManipulator strategyVectorManipulator;
[3336]47
[3150]48    public override Image ItemImage {
49      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
50    }
51
52    #region Parameter Properties
53    public ValueParameter<BoolValue> MaximizationParameter {
54      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
55    }
56    IParameter ISingleObjectiveProblem.MaximizationParameter {
57      get { return MaximizationParameter; }
58    }
59    public ValueParameter<DoubleMatrix> BoundsParameter {
60      get { return (ValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
61    }
62    public ValueParameter<IntValue> ProblemSizeParameter {
63      get { return (ValueParameter<IntValue>)Parameters["ProblemSize"]; }
64    }
65    public ValueParameter<IRealVectorCreator> SolutionCreatorParameter {
66      get { return (ValueParameter<IRealVectorCreator>)Parameters["SolutionCreator"]; }
67    }
68    IParameter IProblem.SolutionCreatorParameter {
69      get { return SolutionCreatorParameter; }
70    }
[3170]71    public ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
72      get { return (ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
[3150]73    }
74    IParameter IProblem.EvaluatorParameter {
75      get { return EvaluatorParameter; }
76    }
77    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
78      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
79    }
80    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
81      get { return BestKnownQualityParameter; }
82    }
[3781]83    public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
84      get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
85    }
[3150]86    #endregion
87
88    #region Properties
[3154]89    public BoolValue Maximization {
90      get { return MaximizationParameter.Value; }
91      set { MaximizationParameter.Value = value; }
92    }
[3150]93    public DoubleMatrix Bounds {
94      get { return BoundsParameter.Value; }
95      set { BoundsParameter.Value = value; }
96    }
97    public IntValue ProblemSize {
98      get { return ProblemSizeParameter.Value; }
99      set { ProblemSizeParameter.Value = value; }
100    }
101    public IRealVectorCreator SolutionCreator {
102      get { return SolutionCreatorParameter.Value; }
103      set { SolutionCreatorParameter.Value = value; }
104    }
105    ISolutionCreator IProblem.SolutionCreator {
106      get { return SolutionCreatorParameter.Value; }
107    }
[3170]108    public ISingleObjectiveTestFunctionProblemEvaluator Evaluator {
[3150]109      get { return EvaluatorParameter.Value; }
110      set { EvaluatorParameter.Value = value; }
111    }
112    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
113      get { return EvaluatorParameter.Value; }
114    }
115    IEvaluator IProblem.Evaluator {
116      get { return EvaluatorParameter.Value; }
117    }
118    public DoubleValue BestKnownQuality {
119      get { return BestKnownQualityParameter.Value; }
120      set { BestKnownQualityParameter.Value = value; }
121    }
[3187]122    private List<IOperator> operators;
[3150]123    public IEnumerable<IOperator> Operators {
[3187]124      get { return operators; }
[3150]125    }
[3789]126    private BestSingleObjectiveTestFunctionSolutionAnalyzer BestSingleObjectiveTestFunctionSolutionAnalyzer {
127      get { return operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); }
[3647]128    }
[3150]129    #endregion
130
131    [StorableConstructor]
[3170]132    private SingleObjectiveTestFunctionProblem(bool deserializing) : base() { }
133    public SingleObjectiveTestFunctionProblem()
[3150]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)));
[3685]141      Parameters.Add(new ValueParameter<IRealVectorCreator>("SolutionCreator", "The operator which should be used to create new test function solutions.", creator));
142      Parameters.Add(new ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The operator which should be used to evaluate test function solutions.", evaluator));
143      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this test function.", new DoubleValue(evaluator.BestKnownQuality)));
[3781]144      Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
[3150]145
[3450]146      strategyVectorCreator = new StdDevStrategyVectorCreator();
[3336]147      strategyVectorCreator.LengthParameter.ActualName = ProblemSizeParameter.Name;
[3450]148      strategyVectorCrossover = new StdDevStrategyVectorCrossover();
149      strategyVectorManipulator = new StdDevStrategyVectorManipulator();
150      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
151      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
[3336]152
[3150]153      creator.RealVectorParameter.ActualName = "Point";
154      ParameterizeSolutionCreator();
155      ParameterizeEvaluator();
156
157      Initialize();
[3783]158      UpdateStrategyVectorBounds();
[3150]159    }
160
161    public override IDeepCloneable Clone(Cloner cloner) {
[3170]162      SingleObjectiveTestFunctionProblem clone = (SingleObjectiveTestFunctionProblem)base.Clone(cloner);
[3751]163      clone.strategyVectorCreator = (StdDevStrategyVectorCreator)cloner.Clone(strategyVectorCreator);
164      clone.strategyVectorCrossover = (StdDevStrategyVectorCrossover)cloner.Clone(strategyVectorCrossover);
165      clone.strategyVectorManipulator = (StdDevStrategyVectorManipulator)cloner.Clone(strategyVectorManipulator);
[3150]166      clone.Initialize();
167      return clone;
168    }
169
170    #region Events
171    public event EventHandler SolutionCreatorChanged;
172    private void OnSolutionCreatorChanged() {
[3739]173      EventHandler handler = SolutionCreatorChanged;
174      if (handler != null) handler(this, EventArgs.Empty);
[3150]175    }
176    public event EventHandler EvaluatorChanged;
177    private void OnEvaluatorChanged() {
[3739]178      EventHandler handler = EvaluatorChanged;
179      if (handler != null) handler(this, EventArgs.Empty);
[3150]180    }
181    public event EventHandler OperatorsChanged;
182    private void OnOperatorsChanged() {
[3739]183      EventHandler handler = OperatorsChanged;
184      if (handler != null) handler(this, EventArgs.Empty);
[3150]185    }
[3739]186    public event EventHandler Reset;
187    private void OnReset() {
188      EventHandler handler = Reset;
189      if (handler != null) handler(this, EventArgs.Empty);
190    }
191
[3150]192    private void ProblemSizeParameter_ValueChanged(object sender, EventArgs e) {
193      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
194      ProblemSize_ValueChanged(null, EventArgs.Empty);
195    }
196    private void ProblemSize_ValueChanged(object sender, EventArgs e) {
[3336]197      if (ProblemSize.Value < 1) ProblemSize.Value = 1;
[3150]198      ParameterizeSolutionCreator();
[3781]199      ParameterizeEvaluator();
[3336]200      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize.Value));
201      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize.Value)));
[3781]202      OnReset();
[3150]203    }
204    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
205      ParameterizeSolutionCreator();
[3647]206      ParameterizeAnalyzers();
[3150]207      SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
[3865]208      OnSolutionCreatorChanged();
[3150]209    }
210    private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
211      ParameterizeEvaluator();
212      ParameterizeOperators();
[3647]213      ParameterizeAnalyzers();
[3150]214    }
215    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
216      ParameterizeEvaluator();
[3187]217      UpdateMoveEvaluators();
[3647]218      ParameterizeAnalyzers();
[3154]219      Maximization.Value = Evaluator.Maximization;
220      BoundsParameter.Value = Evaluator.Bounds;
221      if (ProblemSize.Value < Evaluator.MinimumProblemSize)
222        ProblemSize.Value = Evaluator.MinimumProblemSize;
223      else if (ProblemSize.Value > Evaluator.MaximumProblemSize)
224        ProblemSize.Value = Evaluator.MaximumProblemSize;
225      BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
[3150]226      Evaluator_QualityParameter_ActualNameChanged(null, EventArgs.Empty);
[3865]227      OnEvaluatorChanged();
[3781]228      OnReset();
[3150]229    }
230    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
231      ParameterizeOperators();
232    }
[3154]233    private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
234      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
235      Bounds_ToStringChanged(null, EventArgs.Empty);
236    }
237    private void Bounds_ToStringChanged(object sender, EventArgs e) {
238      if (Bounds.Columns != 2 || Bounds.Rows < 1)
239        Bounds = new DoubleMatrix(1, 2);
[3781]240      ParameterizeOperators();
241      UpdateStrategyVectorBounds();
[3154]242    }
243    private void Bounds_ItemChanged(object sender, EventArgs<int, int> e) {
244      if (e.Value2 == 0 && Bounds[e.Value, 1] <= Bounds[e.Value, 0])
245        Bounds[e.Value, 1] = Bounds[e.Value, 0] + 0.1;
246      if (e.Value2 == 1 && Bounds[e.Value, 0] >= Bounds[e.Value, 1])
247        Bounds[e.Value, 0] = Bounds[e.Value, 1] - 0.1;
[3781]248      ParameterizeOperators();
249      UpdateStrategyVectorBounds();
[3154]250    }
[3187]251    private void MoveGenerator_AdditiveMoveParameter_ActualNameChanged(object sender, EventArgs e) {
252      string name = ((ILookupParameter<AdditiveMove>)sender).ActualName;
253      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
254        op.AdditiveMoveParameter.ActualName = name;
255      }
[3182]256    }
[3318]257    private void SphereEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
258      SphereEvaluator eval = (Evaluator as SphereEvaluator);
259      if (eval != null) {
260        foreach (ISphereMoveEvaluator op in Operators.OfType<ISphereMoveEvaluator>()) {
261          op.C = eval.C;
262          op.Alpha = eval.Alpha;
263        }
264      }
265    }
266    private void RastriginEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
267      RastriginEvaluator eval = (Evaluator as RastriginEvaluator);
268      if (eval != null) {
269        foreach (IRastriginMoveEvaluator op in Operators.OfType<IRastriginMoveEvaluator>()) {
270          op.A = eval.A;
271        }
272      }
273    }
[3336]274    private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) {
[3781]275      strategyVectorManipulator.BoundsParameter.Value = (DoubleMatrix)strategyVectorCreator.BoundsParameter.Value.Clone();
[3336]276    }
277    private void strategyVectorCreator_StrategyParameterParameter_ActualNameChanged(object sender, EventArgs e) {
278      string name = strategyVectorCreator.StrategyParameterParameter.ActualName;
279      strategyVectorCrossover.ParentsParameter.ActualName = name;
280      strategyVectorCrossover.StrategyParameterParameter.ActualName = name;
281      strategyVectorManipulator.StrategyParameterParameter.ActualName = name;
282    }
[3150]283    #endregion
284
285    #region Helpers
286    [StorableHook(HookType.AfterDeserialization)]
287    private void Initialize() {
288      InitializeOperators();
289      ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
290      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
[3154]291      BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
292      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
293      Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
[3150]294      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
295      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
296      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
297      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[3336]298      strategyVectorCreator.BoundsParameter.ValueChanged += new EventHandler(strategyVectorCreator_BoundsParameter_ValueChanged);
299      strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
[3150]300    }
[3647]301    private void ParameterizeAnalyzers() {
[3789]302      BestSingleObjectiveTestFunctionSolutionAnalyzer.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
303      BestSingleObjectiveTestFunctionSolutionAnalyzer.ResultsParameter.ActualName = "Results";
304      BestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
305      BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
306      BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
307      BestSingleObjectiveTestFunctionSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
308      BestSingleObjectiveTestFunctionSolutionAnalyzer.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
[3894]309      BestSingleObjectiveTestFunctionSolutionAnalyzer.BoundsParameter.ActualName = BoundsParameter.Name;
[3647]310    }
[3150]311    private void InitializeOperators() {
[3187]312      operators = new List<IOperator>();
[3647]313      operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
314      ParameterizeAnalyzers();
[3303]315      operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
[3336]316      operators.Add(strategyVectorCreator);
317      operators.Add(strategyVectorCrossover);
318      operators.Add(strategyVectorManipulator);
[3209]319      UpdateMoveEvaluators();
320      ParameterizeOperators();
[3187]321      InitializeMoveGenerators();
[3150]322    }
[3187]323    private void InitializeMoveGenerators() {
324      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
[3150]325        if (op is IMoveGenerator) {
[3187]326          op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
[3150]327        }
328      }
[3187]329    }
330    private void UpdateMoveEvaluators() {
[3303]331      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
332        operators.Remove(op);
333      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
334        if (op.EvaluatorType == Evaluator.GetType()) {
335          operators.Add(op);
[3318]336          #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
337          if (op is ISphereMoveEvaluator) {
338            SphereEvaluator e = (Evaluator as SphereEvaluator);
339            e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
340            e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
341            ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
342            em.C = e.C;
343            em.Alpha = e.Alpha;
344          } else if (op is IRastriginMoveEvaluator) {
345            RastriginEvaluator e = (Evaluator as RastriginEvaluator);
346            e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
347            IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
348            em.A = e.A;
349          }
350          #endregion
[3303]351        }
352      ParameterizeOperators();
353      OnOperatorsChanged();
[3187]354    }
[3150]355    private void ParameterizeSolutionCreator() {
356      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
357    }
358    private void ParameterizeEvaluator() {
359      Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[3781]360      BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize.Value);
[3150]361    }
362    private void ParameterizeOperators() {
363      foreach (IRealVectorCrossover op in Operators.OfType<IRealVectorCrossover>()) {
364        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
365        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[3182]366        op.BoundsParameter.ActualName = BoundsParameter.Name;
[3150]367      }
368      foreach (IRealVectorManipulator op in Operators.OfType<IRealVectorManipulator>()) {
369        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[3182]370        op.BoundsParameter.ActualName = BoundsParameter.Name;
[3150]371      }
[3187]372      foreach (IRealVectorMoveOperator op in Operators.OfType<IRealVectorMoveOperator>()) {
373        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[3150]374      }
[3187]375      foreach (IRealVectorMoveGenerator op in Operators.OfType<IRealVectorMoveGenerator>()) {
376        op.BoundsParameter.ActualName = BoundsParameter.Name;
377      }
378      foreach (ISingleObjectiveTestFunctionAdditiveMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
379        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
380        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
381      }
[3682]382      foreach (IRealVectorPSOEncoder op in Operators.OfType<IRealVectorPSOEncoder>()) {
[3719]383        ((ILookupParameter)op.OriginalRealVectorParameter).ActualName = SolutionCreator.RealVectorParameter.ActualName;
384        op.BoundsParameter.Value = (DoubleMatrix)BoundsParameter.Value.Clone();
385        op.BoundsParameter.ActualName = "ParticleBounds";
[3682]386      }
[3150]387    }
[3781]388    private void UpdateStrategyVectorBounds() {
389      DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
390      for (int i = 0; i < strategyBounds.Rows; i++)
391        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
392      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
393    }
[3150]394    #endregion
395  }
396}
Note: See TracBrowser for help on using the repository browser.