Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs @ 7317

Last change on this file since 7317 was 7270, checked in by spimming, 13 years ago

#1680:

  • merged changes from trunk into branch
File size: 18.8 KB
RevLine 
[3150]1#region License Information
2/* HeuristicLab
[7270]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3150]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
[3170]34namespace HeuristicLab.Problems.TestFunctions {
[3221]35  [Item("Single Objective Test Function", "Test function with real valued inputs and a single objective.")]
[3150]36  [StorableClass]
37  [Creatable("Problems")]
[6938]38  public sealed class SingleObjectiveTestFunctionProblem : SingleObjectiveHeuristicOptimizationProblem<ISingleObjectiveTestFunctionProblemEvaluator, IRealVectorCreator>, IStorableContent {
[4419]39    public string Filename { get; set; }
40
[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    #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    }
[3781]55    public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
56      get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
57    }
[3150]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    }
[3789]69    private BestSingleObjectiveTestFunctionSolutionAnalyzer BestSingleObjectiveTestFunctionSolutionAnalyzer {
[6938]70      get { return Operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); }
[3647]71    }
[3150]72    #endregion
73
[6938]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
[4098]86
[3150]87    [StorableConstructor]
[4118]88    private SingleObjectiveTestFunctionProblem(bool deserializing) : base(deserializing) { }
[4722]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      AttachEventHandlers();
95    }
[3170]96    public SingleObjectiveTestFunctionProblem()
[6938]97      : base(new AckleyEvaluator(), new UniformRandomRealVectorCreator()) {
98      Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", Evaluator.Bounds));
[3150]99      Parameters.Add(new ValueParameter<IntValue>("ProblemSize", "The dimension of the problem.", new IntValue(2)));
[3781]100      Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
[3150]101
[6939]102      Maximization.Value = Evaluator.Maximization;
103      BestKnownQuality.Value = Evaluator.BestKnownQuality;
104
[3450]105      strategyVectorCreator = new StdDevStrategyVectorCreator();
[3336]106      strategyVectorCreator.LengthParameter.ActualName = ProblemSizeParameter.Name;
[3450]107      strategyVectorCrossover = new StdDevStrategyVectorCrossover();
108      strategyVectorManipulator = new StdDevStrategyVectorManipulator();
109      strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
110      strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
[3336]111
[6938]112      SolutionCreator.RealVectorParameter.ActualName = "Point";
[3150]113      ParameterizeSolutionCreator();
114      ParameterizeEvaluator();
115
[4098]116      InitializeOperators();
117      AttachEventHandlers();
[3783]118      UpdateStrategyVectorBounds();
[3150]119    }
120
121    public override IDeepCloneable Clone(Cloner cloner) {
[4722]122      return new SingleObjectiveTestFunctionProblem(this, cloner);
[3150]123    }
124
[4098]125    private bool IsNotFieldReferenced(IOperator x) {
126      return !(x == strategyVectorCreator
127        || x == strategyVectorCrossover
128        || x == strategyVectorManipulator);
129    }
130
[3150]131    #region Events
[6938]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);
[3150]138    }
[6938]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();
[3150]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) {
[3336]160      if (ProblemSize.Value < 1) ProblemSize.Value = 1;
[3150]161      ParameterizeSolutionCreator();
[3781]162      ParameterizeEvaluator();
[3336]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)));
[3781]165      OnReset();
[3150]166    }
167    private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
168      ParameterizeEvaluator();
169      ParameterizeOperators();
[3647]170      ParameterizeAnalyzers();
[3150]171    }
172    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
173      ParameterizeOperators();
174    }
[3154]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);
[3781]182      ParameterizeOperators();
183      UpdateStrategyVectorBounds();
[3154]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;
[3781]190      ParameterizeOperators();
191      UpdateStrategyVectorBounds();
[3154]192    }
[3187]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      }
[3182]198    }
[3318]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    }
[3336]216    private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) {
[3781]217      strategyVectorManipulator.BoundsParameter.Value = (DoubleMatrix)strategyVectorCreator.BoundsParameter.Value.Clone();
[3336]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    }
[3150]225    #endregion
226
227    #region Helpers
228    [StorableHook(HookType.AfterDeserialization)]
[4722]229    private void AfterDeserialization() {
[4118]230      // BackwardsCompatibility3.3
231      #region Backwards compatible code (remove with 3.4)
[6938]232      if (Operators.Count == 0) InitializeOperators();
[4118]233      #endregion
234      AttachEventHandlers();
235    }
236
[4098]237    private void AttachEventHandlers() {
[3150]238      ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
239      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
[3154]240      BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
241      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
242      Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
[3150]243      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
244      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[3336]245      strategyVectorCreator.BoundsParameter.ValueChanged += new EventHandler(strategyVectorCreator_BoundsParameter_ValueChanged);
246      strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
[3150]247    }
[3647]248    private void ParameterizeAnalyzers() {
[3789]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;
[3894]256      BestSingleObjectiveTestFunctionSolutionAnalyzer.BoundsParameter.ActualName = BoundsParameter.Name;
[3647]257    }
[3150]258    private void InitializeOperators() {
[6938]259      Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
[3647]260      ParameterizeAnalyzers();
[6938]261      Operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
262      Operators.Add(strategyVectorCreator);
263      Operators.Add(strategyVectorCrossover);
264      Operators.Add(strategyVectorManipulator);
[3209]265      UpdateMoveEvaluators();
266      ParameterizeOperators();
[3187]267      InitializeMoveGenerators();
[3150]268    }
[3187]269    private void InitializeMoveGenerators() {
270      foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
[3150]271        if (op is IMoveGenerator) {
[3187]272          op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
[3150]273        }
274      }
[3187]275    }
276    private void UpdateMoveEvaluators() {
[3303]277      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
[6938]278        Operators.Remove(op);
[3303]279      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
280        if (op.EvaluatorType == Evaluator.GetType()) {
[6938]281          Operators.Add(op);
[3318]282          #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
283          if (op is ISphereMoveEvaluator) {
284            SphereEvaluator e = (Evaluator as SphereEvaluator);
285            e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
286            e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
287            ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
288            em.C = e.C;
289            em.Alpha = e.Alpha;
290          } else if (op is IRastriginMoveEvaluator) {
291            RastriginEvaluator e = (Evaluator as RastriginEvaluator);
292            e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
293            IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
294            em.A = e.A;
295          }
296          #endregion
[3303]297        }
298      ParameterizeOperators();
299      OnOperatorsChanged();
[3187]300    }
[3150]301    private void ParameterizeSolutionCreator() {
302      SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
[6051]303      SolutionCreator.LengthParameter.Hidden = true;
304      SolutionCreator.BoundsParameter.ActualName = BoundsParameter.Name;
305      SolutionCreator.BoundsParameter.Hidden = true;
[3150]306    }
307    private void ParameterizeEvaluator() {
308      Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[6051]309      Evaluator.PointParameter.Hidden = true;
[3781]310      BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize.Value);
[3150]311    }
312    private void ParameterizeOperators() {
313      foreach (IRealVectorCrossover op in Operators.OfType<IRealVectorCrossover>()) {
314        op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[6051]315        op.ParentsParameter.Hidden = true;
[3150]316        op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[6051]317        op.ChildParameter.Hidden = true;
[3182]318        op.BoundsParameter.ActualName = BoundsParameter.Name;
[6051]319        op.BoundsParameter.Hidden = true;
[3150]320      }
321      foreach (IRealVectorManipulator op in Operators.OfType<IRealVectorManipulator>()) {
322        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[6051]323        op.RealVectorParameter.Hidden = true;
[3182]324        op.BoundsParameter.ActualName = BoundsParameter.Name;
[6051]325        op.BoundsParameter.Hidden = true;
[3150]326      }
[3187]327      foreach (IRealVectorMoveOperator op in Operators.OfType<IRealVectorMoveOperator>()) {
328        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[6051]329        op.RealVectorParameter.Hidden = true;
[3150]330      }
[3187]331      foreach (IRealVectorMoveGenerator op in Operators.OfType<IRealVectorMoveGenerator>()) {
332        op.BoundsParameter.ActualName = BoundsParameter.Name;
[6051]333        op.BoundsParameter.Hidden = true;
[3187]334      }
335      foreach (ISingleObjectiveTestFunctionAdditiveMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
336        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
[6051]337        op.QualityParameter.Hidden = true;
[3187]338        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[6051]339        op.RealVectorParameter.Hidden = true;
[3187]340      }
[5560]341      foreach (IRealVectorParticleCreator op in Operators.OfType<IRealVectorParticleCreator>()) {
342        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[6051]343        op.RealVectorParameter.Hidden = true;
[5560]344        op.BoundsParameter.ActualName = BoundsParameter.Name;
[6051]345        op.BoundsParameter.Hidden = true;
[5951]346        op.ProblemSizeParameter.ActualName = ProblemSizeParameter.Name;
[6051]347        op.ProblemSizeParameter.Hidden = true;
[5560]348      }
349      foreach (IRealVectorParticleUpdater op in Operators.OfType<IRealVectorParticleUpdater>()) {
350        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[6051]351        op.RealVectorParameter.Hidden = true;
[5951]352        op.BoundsParameter.ActualName = BoundsParameter.Name;
[6051]353        op.BoundsParameter.Hidden = true;
[5560]354      }
[5561]355      foreach (IRealVectorSwarmUpdater op in Operators.OfType<IRealVectorSwarmUpdater>()) {
356        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[6051]357        op.RealVectorParameter.Hidden = true;
[5951]358        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
[6051]359        op.MaximizationParameter.Hidden = true;
[5561]360      }
[6051]361      foreach (var op in Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()) {
[6042]362        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
[6051]363        op.RealVectorParameter.Hidden = true;
364      }
[3150]365    }
[3781]366    private void UpdateStrategyVectorBounds() {
367      DoubleMatrix strategyBounds = (DoubleMatrix)Bounds.Clone();
[5381]368      for (int i = 0; i < strategyBounds.Rows; i++) {
[3781]369        if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
[5381]370        strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
371      }
[3781]372      strategyVectorCreator.BoundsParameter.Value = strategyBounds;
373    }
[3150]374    #endregion
375  }
376}
Note: See TracBrowser for help on using the repository browser.