Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.TestFunctions/3.4/TestFunctionProblem.cs @ 11711

Last change on this file since 11711 was 11655, checked in by mkommend, 10 years ago

#2174: Updated programmable problem assemblies to .NET 4.5.

File size: 12.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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;
35using HeuristicLab.Problems.Programmable;
36
37namespace HeuristicLab.Problems.TestFunctions {
38  [Item("Test Function", "Test function with real valued inputs and a single objective.")]
39  [StorableClass]
40  [Creatable("Problems")]
41  public sealed class TestFunctionProblem : SingleObjectiveHeuristicOptimizationProblem<ISingleObjectiveTestFunctionProblemEvaluator, IRealVectorCreator>, IStorableContent, IProblemInstanceConsumer<SOTFData> {
42    public string Filename { get; set; }
43
44    [Storable]
45    private RealEncoding encoding;
46    public RealEncoding Encoding {
47      get { return encoding; }
48    }
49
50    #region Parameter Properties
51    public ValueParameter<DoubleMatrix> BoundsParameter {
52      get { return (ValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
53    }
54    public IFixedValueParameter<IntValue> ProblemSizeParameter {
55      get { return (IFixedValueParameter<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 int ProblemSize {
68      get { return ProblemSizeParameter.Value.Value; }
69      set { ProblemSizeParameter.Value.Value = value; }
70    }
71    private BestSingleObjectiveTestFunctionSolutionAnalyzer BestSingleObjectiveTestFunctionSolutionAnalyzer {
72      get { return Operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); }
73    }
74    private SingleObjectivePopulationDiversityAnalyzer SingleObjectivePopulationDiversityAnalyzer {
75      get { return Operators.OfType<SingleObjectivePopulationDiversityAnalyzer>().FirstOrDefault(); }
76    }
77    #endregion
78
79
80    [StorableConstructor]
81    private TestFunctionProblem(bool deserializing) : base(deserializing) { }
82    [StorableHook(HookType.AfterDeserialization)]
83    private void AfterDeserialization() {
84      RegisterEventHandlers();
85    }
86
87    private TestFunctionProblem(TestFunctionProblem original, Cloner cloner)
88      : base(original, cloner) {
89      encoding = cloner.Clone(original.encoding);
90      RegisterEventHandlers();
91    }
92    public override IDeepCloneable Clone(Cloner cloner) { return new TestFunctionProblem(this, cloner); }
93
94    public TestFunctionProblem()
95      : base(new AckleyEvaluator(), new UniformRandomRealVectorCreator()) {
96      encoding = new RealEncoding("Point", 2, Evaluator.Bounds[0, 0], Evaluator.Bounds[0, 1]);
97
98      Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", Evaluator.Bounds));
99      Parameters.Add(new FixedValueParameter<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      encoding.BoundsParameter = BoundsParameter;
103      encoding.LengthParameter = ProblemSizeParameter;
104      encoding.SolutionCreator = SolutionCreator;
105
106      Maximization.Value = Evaluator.Maximization;
107      BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
108
109
110      Operators.Add(new SingleObjectiveTestFunctionImprovementOperator());
111      Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
112      Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
113
114      Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
115      Operators.Add(new SingleObjectivePopulationDiversityAnalyzer());
116      ConfigureOperators();
117      UpdateMoveEvaluators();
118
119      RegisterEventHandlers();
120    }
121
122
123    protected override IEnumerable<IItem> GetOperators() {
124      return Operators.Concat(Encoding.Operators.Except(Operators, new TypeEqualityComparer<IItem>()));
125    }
126
127    #region Events
128    private void RegisterEventHandlers() {
129      ProblemSizeParameter.Value.ValueChanged += new EventHandler(ProblemSize_Changed);
130      BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
131      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
132      Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
133      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
134    }
135
136    protected override void OnSolutionCreatorChanged() {
137      base.OnSolutionCreatorChanged();
138      Encoding.SolutionCreator = SolutionCreator;
139      ConfigureOperators();
140    }
141
142    protected override void OnEvaluatorChanged() {
143      base.OnEvaluatorChanged();
144      ProblemSize = Math.Max(Evaluator.MinimumProblemSize, Math.Min(ProblemSize, Evaluator.MaximumProblemSize));
145      UpdateMoveEvaluators();
146      Maximization.Value = Evaluator.Maximization;
147      BoundsParameter.Value = Evaluator.Bounds;
148      BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
149      Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
150      ConfigureOperators();
151      OnReset();
152    }
153
154    private void ProblemSize_Changed(object sender, EventArgs e) {
155      if (ProblemSize < 1) ProblemSize = 1;
156      ConfigureEvaluator();
157      OnReset();
158    }
159    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
160      ConfigureOperators();
161    }
162
163    private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
164      Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
165      Bounds_ToStringChanged(null, EventArgs.Empty);
166    }
167
168    private void Bounds_ToStringChanged(object sender, EventArgs e) {
169      if (Bounds.Columns != 2 || Bounds.Rows < 1)
170        Bounds = new DoubleMatrix(1, 2);
171    }
172    private void Bounds_ItemChanged(object sender, EventArgs<int, int> e) {
173      if (e.Value2 == 0 && Bounds[e.Value, 1] <= Bounds[e.Value, 0])
174        Bounds[e.Value, 1] = Bounds[e.Value, 0] + 0.1;
175      if (e.Value2 == 1 && Bounds[e.Value, 0] >= Bounds[e.Value, 1])
176        Bounds[e.Value, 0] = Bounds[e.Value, 1] - 0.1;
177    }
178
179    #endregion
180
181    #region Helpers
182    private void UpdateMoveEvaluators() {
183      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
184        Operators.Remove(op);
185      foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
186        if (op.EvaluatorType == Evaluator.GetType()) {
187          Operators.Add(op);
188          #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
189          if (op is ISphereMoveEvaluator) {
190            SphereEvaluator e = (Evaluator as SphereEvaluator);
191            e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
192            e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
193            ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
194            em.C = e.C;
195            em.Alpha = e.Alpha;
196          } else if (op is IRastriginMoveEvaluator) {
197            RastriginEvaluator e = (Evaluator as RastriginEvaluator);
198            e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
199            IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
200            em.A = e.A;
201          }
202          #endregion
203        }
204      ConfigureOperators();
205      OnOperatorsChanged();
206    }
207
208    private void SphereEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
209      SphereEvaluator eval = (Evaluator as SphereEvaluator);
210      if (eval != null) {
211        foreach (ISphereMoveEvaluator op in Operators.OfType<ISphereMoveEvaluator>()) {
212          op.C = eval.C;
213          op.Alpha = eval.Alpha;
214        }
215      }
216    }
217    private void RastriginEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
218      RastriginEvaluator eval = (Evaluator as RastriginEvaluator);
219      if (eval != null) {
220        foreach (IRastriginMoveEvaluator op in Operators.OfType<IRastriginMoveEvaluator>()) {
221          op.A = eval.A;
222        }
223      }
224    }
225
226    private void ConfigureOperators() {
227      Encoding.ConfigureOperators(Operators.OfType<IOperator>());
228
229      foreach (var op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
230        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
231      }
232      foreach (var op in Operators.OfType<IRealVectorSwarmUpdater>()) {
233        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
234      }
235      foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
236        op.SolutionParameter.ActualName = Encoding.Name;
237      }
238      foreach (var op in Operators.OfType<ISingleObjectivePathRelinker>()) {
239        op.ParentsParameter.ActualName = Encoding.Name;
240      }
241      foreach (var op in Operators.OfType<SingleObjectiveTestFunctionSimilarityCalculator>()) {
242        op.SolutionVariableName = Encoding.Name;
243        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
244        op.Bounds = Encoding.Bounds;
245      }
246
247      ConfigureEvaluator();
248      ConfigureAnalyzers();
249    }
250
251    private void ConfigureEvaluator() {
252      Evaluator.PointParameter.ActualName = Encoding.Name;
253      try {
254        BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize);
255      }
256      catch (ArgumentException e) {
257        ErrorHandling.ShowErrorDialog(e);
258        ProblemSize = Evaluator.MinimumProblemSize;
259      }
260    }
261
262    private void ConfigureAnalyzers() {
263      if (BestSingleObjectiveTestFunctionSolutionAnalyzer != null) {
264        BestSingleObjectiveTestFunctionSolutionAnalyzer.RealVectorParameter.ActualName = Encoding.Name;
265        BestSingleObjectiveTestFunctionSolutionAnalyzer.ResultsParameter.ActualName = "Results";
266        BestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
267        BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
268        BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
269        BestSingleObjectiveTestFunctionSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
270        BestSingleObjectiveTestFunctionSolutionAnalyzer.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
271        BestSingleObjectiveTestFunctionSolutionAnalyzer.BoundsParameter.ActualName = BoundsParameter.Name;
272      }
273
274      if (SingleObjectivePopulationDiversityAnalyzer != null) {
275        SingleObjectivePopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
276        SingleObjectivePopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
277        SingleObjectivePopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
278        SingleObjectivePopulationDiversityAnalyzer.SimilarityCalculator = Operators.OfType<SingleObjectiveTestFunctionSimilarityCalculator>().SingleOrDefault();
279      }
280    }
281    #endregion
282
283    public void Load(SOTFData data) {
284      Name = data.Name;
285      Description = data.Description;
286      Evaluator = data.Evaluator;
287    }
288  }
289}
Note: See TracBrowser for help on using the repository browser.