Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4401 was 4118, checked in by abeham, 14 years ago

#1090

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