Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs @ 13893

Last change on this file since 13893 was 13403, checked in by abeham, 9 years ago

#2521:

  • Adapted single-objective test function problem to new problem infrastructure
  • Added additional interfaces to RealVectorEncoding
  • Fixed IParticleUpdater interface (must implement IStochasticOperator if it contains a Random parameter)
File size: 9.8 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.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.Problems.Instances;
35
36namespace HeuristicLab.Problems.TestFunctions {
37  [Item("Test Function (single-objective)", "Test function with real valued inputs and a single objective.")]
38  [StorableClass]
39  [Creatable(CreatableAttribute.Categories.Problems, Priority = 90)]
40  public sealed class SingleObjectiveTestFunctionProblem :
41    SingleObjectiveProblem<RealVectorEncoding, RealVector>,
42    IProblemInstanceConsumer<SOTFData> {
43
44    public override bool Maximization {
45      get { return Parameters.ContainsKey("TestFunction") && TestFunction.Maximization; }
46    }
47
48    #region Parameter Properties
49    private IFixedValueParameter<IntValue> ProblemSizeParameter {
50      get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; }
51    }
52    private IValueParameter<DoubleMatrix> BoundsParameter {
53      get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
54    }
55    public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
56      get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
57    }
58    public IValueParameter<ISingleObjectiveTestFunction> TestFunctionParameter {
59      get { return (IValueParameter<ISingleObjectiveTestFunction>)Parameters["TestFunction"]; }
60    }
61    #endregion
62
63    #region Properties
64    public int ProblemSize {
65      get { return ProblemSizeParameter.Value.Value; }
66      set { ProblemSizeParameter.Value.Value = value; }
67    }
68    public DoubleMatrix Bounds {
69      get { return BoundsParameter.Value; }
70      set { BoundsParameter.Value = value; }
71    }
72    public ISingleObjectiveTestFunction TestFunction {
73      get { return TestFunctionParameter.Value; }
74      set { TestFunctionParameter.Value = value; }
75    }
76
77    private BestSingleObjectiveTestFunctionSolutionAnalyzer BestSingleObjectiveTestFunctionSolutionAnalyzer {
78      get { return Operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); }
79    }
80    #endregion
81
82    [StorableConstructor]
83    private SingleObjectiveTestFunctionProblem(bool deserializing) : base(deserializing) { }
84    private SingleObjectiveTestFunctionProblem(SingleObjectiveTestFunctionProblem original, Cloner cloner)
85      : base(original, cloner) {
86      RegisterEventHandlers();
87    }
88    public SingleObjectiveTestFunctionProblem()
89      : base(new RealVectorEncoding("Point")) {
90      Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2)));
91      Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The bounds of the solution given as either one line for all variables or a line for each variable. The first column specifies lower bound, the second upper bound.", new DoubleMatrix(new double[,] { { -100, 100 } })));
92      Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
93      Parameters.Add(new ValueParameter<ISingleObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Ackley()));
94
95      Encoding.LengthParameter = ProblemSizeParameter;
96      Encoding.BoundsParameter = BoundsParameter;
97      BestKnownQuality = TestFunction.BestKnownQuality;
98
99      InitializeOperators();
100      RegisterEventHandlers();
101    }
102
103    public override IDeepCloneable Clone(Cloner cloner) {
104      return new SingleObjectiveTestFunctionProblem(this, cloner);
105    }
106
107    [StorableHook(HookType.AfterDeserialization)]
108    private void AfterDeserialization() {
109      RegisterEventHandlers();
110    }
111
112    private void RegisterEventHandlers() {
113      Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
114      TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
115      ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
116      BoundsParameter.ValueChanged += BoundsParameterOnValueChanged;
117    }
118
119    public override double Evaluate(RealVector individual, IRandom random) {
120      return TestFunction.Evaluate(individual);
121    }
122
123    #region Events
124    protected override void OnEncodingChanged() {
125      base.OnEncodingChanged();
126      Parameterize();
127    }
128    protected override void OnEvaluatorChanged() {
129      base.OnEvaluatorChanged();
130      Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
131      Parameterize();
132    }
133    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
134      Parameterize();
135    }
136    private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
137      var problemSizeChange = ProblemSize < TestFunction.MinimumProblemSize
138                              || ProblemSize > TestFunction.MaximumProblemSize;
139      if (problemSizeChange) {
140        ProblemSize = Math.Max(TestFunction.MinimumProblemSize, Math.Min(ProblemSize, TestFunction.MaximumProblemSize));
141      }
142      BestKnownQuality = TestFunction.BestKnownQuality;
143      Bounds = (DoubleMatrix)TestFunction.Bounds.Clone();
144      var bestSolution = TestFunction.GetBestKnownSolution(ProblemSize);
145      BestKnownSolutionParameter.Value = bestSolution;
146
147      OnReset();
148    }
149    private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) {
150      if (ProblemSize < TestFunction.MinimumProblemSize
151        || ProblemSize > TestFunction.MaximumProblemSize)
152        ProblemSize = Math.Min(TestFunction.MaximumProblemSize, Math.Max(TestFunction.MinimumProblemSize, ProblemSize));
153    }
154    private void BoundsParameterOnValueChanged(object sender, EventArgs eventArgs) {
155      Parameterize();
156    }
157    #endregion
158
159    #region Helpers
160    private void InitializeOperators() {
161      Operators.Add(new SingleObjectiveTestFunctionImprovementOperator());
162      Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
163      Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
164      Operators.Add(new QualitySimilarityCalculator());
165      Operators.Add(new NoSimilarityCalculator());
166      Operators.Add(new AdditiveMoveEvaluator());
167
168      Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
169      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
170      Parameterize();
171    }
172
173    private void Parameterize() {
174      var operators = new List<IItem>();
175      if (BestSingleObjectiveTestFunctionSolutionAnalyzer != null) {
176        operators.Add(BestSingleObjectiveTestFunctionSolutionAnalyzer);
177        BestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
178        BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
179        BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
180        BestSingleObjectiveTestFunctionSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
181        BestSingleObjectiveTestFunctionSolutionAnalyzer.TestFunctionParameter.ActualName = TestFunctionParameter.Name;
182      }
183      foreach (var op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
184        operators.Add(op);
185        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
186        op.QualityParameter.Hidden = true;
187        foreach (var movOp in Encoding.Operators.OfType<IRealVectorAdditiveMoveQualityOperator>())
188          movOp.MoveQualityParameter.ActualName = op.MoveQualityParameter.ActualName;
189      }
190      foreach (var op in Operators.OfType<IRealVectorSwarmUpdater>()) {
191        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
192        op.MaximizationParameter.Hidden = true;
193      }
194      foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
195        operators.Add(op);
196        op.SolutionParameter.ActualName = Encoding.Name;
197        op.SolutionParameter.Hidden = true;
198      }
199      foreach (var op in Operators.OfType<ITestFunctionSolutionSimilarityCalculator>()) {
200        operators.Add(op);
201        op.SolutionVariableName = Encoding.Name;
202        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
203        op.Bounds = Bounds;
204      }
205
206      if (operators.Count > 0) Encoding.ConfigureOperators(operators);
207    }
208    #endregion
209
210    public void Load(SOTFData data) {
211      Name = data.Name;
212      Description = data.Description;
213      TestFunction = data.TestFunction;
214    }
215  }
216}
Note: See TracBrowser for help on using the repository browser.