Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/MultiObjectiveTestFunctionProblem.cs @ 16949

Last change on this file since 16949 was 16949, checked in by abeham, 5 years ago

#2521: Adapted test function problems to new real vector problem

  • Made encoding readonly in symbolic expression tree problem
File size: 10.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Problems.Instances;
32
33namespace HeuristicLab.Problems.TestFunctions.MultiObjective {
34  [StorableType("AB0C6A73-C432-46FD-AE3B-9841EAB2478C")]
35  [Creatable(CreatableAttribute.Categories.Problems, Priority = 95)]
36  [Item("Test Function (multi-objective)", "Test functions with real valued inputs and multiple objectives.")]
37  public class MultiObjectiveTestFunctionProblem : RealVectorMultiObjectiveProblem,
38    IProblemInstanceConsumer<MOTFData> {
39
40    #region Parameter Properties
41    public IFixedValueParameter<IntValue> ProblemSizeParameter {
42      get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; }
43    }
44    public IFixedValueParameter<IntValue> ObjectivesParameter {
45      get { return (IFixedValueParameter<IntValue>)Parameters["Objectives"]; }
46    }
47    public IValueParameter<DoubleMatrix> BoundsParameter {
48      get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
49    }
50    public IValueParameter<IMultiObjectiveTestFunction> TestFunctionParameter {
51      get { return (IValueParameter<IMultiObjectiveTestFunction>)Parameters["TestFunction"]; }
52    }
53    public IValueParameter<DoubleArray> ReferencePointParameter {
54      get { return (IValueParameter<DoubleArray>)Parameters["ReferencePoint"]; }
55    }
56    public OptionalValueParameter<DoubleMatrix> BestKnownFrontParameter {
57      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["BestKnownFront"]; }
58    }
59
60    #endregion
61
62    #region Properties
63    public override bool[] Maximization {
64      get {
65        //necessary because of virtual member call in base ctor
66        if (!Parameters.ContainsKey("TestFunction")) return new bool[0];
67        return TestFunction.Maximization(Objectives).ToArray();
68      }
69    }
70
71    public int ProblemSize {
72      get { return ProblemSizeParameter.Value.Value; }
73      set { ProblemSizeParameter.Value.Value = value; }
74    }
75    public int Objectives {
76      get { return ObjectivesParameter.Value.Value; }
77      set { ObjectivesParameter.Value.Value = value; }
78    }
79    public DoubleMatrix Bounds {
80      get { return BoundsParameter.Value; }
81      set { BoundsParameter.Value = value; }
82    }
83    public IMultiObjectiveTestFunction TestFunction {
84      get { return TestFunctionParameter.Value; }
85      set { TestFunctionParameter.Value = value; }
86    }
87    public DoubleArray ReferencePoint {
88      get { return ReferencePointParameter.Value; }
89      set { ReferencePointParameter.Value = value; }
90    }
91    public DoubleMatrix BestKnownFront {
92      get { return BestKnownFrontParameter.Value; }
93      set { BestKnownFrontParameter.Value = value; }
94    }
95    #endregion
96
97    [StorableConstructor]
98    protected MultiObjectiveTestFunctionProblem(StorableConstructorFlag _) : base(_) { }
99    [StorableHook(HookType.AfterDeserialization)]
100    private void AfterDeserialization() {
101      RegisterEventHandlers();
102    }
103
104    protected MultiObjectiveTestFunctionProblem(MultiObjectiveTestFunctionProblem original, Cloner cloner)
105      : base(original, cloner) {
106      RegisterEventHandlers();
107    }
108    public override IDeepCloneable Clone(Cloner cloner) {
109      return new MultiObjectiveTestFunctionProblem(this, cloner);
110    }
111
112    public MultiObjectiveTestFunctionProblem()
113      : base() {
114      Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2)));
115      Parameters.Add(new FixedValueParameter<IntValue>("Objectives", "The dimensionality of the solution vector (number of objectives).", new IntValue(2)));
116      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[,] { { -4, 4 } })));
117      Parameters.Add(new ValueParameter<DoubleArray>("ReferencePoint", "The reference point used for hypervolume calculation."));
118      Parameters.Add(new ValueParameter<IMultiObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Fonseca()));
119      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("BestKnownFront", "The currently best known Pareto front"));
120
121      Encoding.LengthParameter = ProblemSizeParameter;
122      Encoding.BoundsParameter = BoundsParameter;
123      BestKnownFrontParameter.Hidden = true;
124
125      UpdateParameterValues();
126      InitializeOperators();
127      RegisterEventHandlers();
128    }
129
130    private void RegisterEventHandlers() {
131      TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
132      ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
133      ObjectivesParameter.Value.ValueChanged += ObjectivesOnValueChanged;
134    }
135
136
137    public override void Analyze(RealVector[] solutions, double[][] qualities, ResultCollection results, IRandom random) {
138      base.Analyze(solutions, qualities, results, random);
139      if (results.ContainsKey("Pareto Front")) {
140        ((DoubleMatrix)results["Pareto Front"].Value).SortableView = true;
141      }
142    }
143
144    /// <summary>
145    /// Checks whether a given solution violates the contraints of this function.
146    /// </summary>
147    /// <param name="individual"></param>
148    /// <returns>a double array that holds the distances that describe how much every contraint is violated (0 is not violated). If the current TestFunction does not have constraints an array of length 0 is returned</returns>
149    public double[] CheckContraints(RealVector individual) {
150      var constrainedTestFunction = (IConstrainedTestFunction)TestFunction;
151      if (constrainedTestFunction != null) {
152        return constrainedTestFunction.CheckConstraints(individual, Objectives);
153      }
154      return new double[0];
155    }
156
157    public override double[] Evaluate(RealVector solution, IRandom random) {
158      return TestFunction.Evaluate(solution, Objectives);
159    }
160
161
162    public void Load(MOTFData data) {
163      TestFunction = data.TestFunction;
164    }
165
166    #region Events
167    private void UpdateParameterValues() {
168      MaximizationParameter.Value = (BoolArray)new BoolArray(TestFunction.Maximization(Objectives)).AsReadOnly();
169
170      var front = TestFunction.OptimalParetoFront(Objectives);
171      if (front != null) {
172        BestKnownFrontParameter.Value = (DoubleMatrix)Utilities.ToMatrix(front).AsReadOnly();
173      } else BestKnownFrontParameter.Value = null;
174
175
176      BoundsParameter.Value = new DoubleMatrix(TestFunction.Bounds(Objectives));
177      ReferencePointParameter.Value = new DoubleArray(TestFunction.ReferencePoint(Objectives));
178    }
179
180    protected override void OnEncodingChanged() {
181      base.OnEncodingChanged();
182      UpdateParameterValues();
183      ParameterizeAnalyzers();
184    }
185    protected override void OnEvaluatorChanged() {
186      base.OnEvaluatorChanged();
187      UpdateParameterValues();
188      ParameterizeAnalyzers();
189    }
190
191    private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
192      ProblemSize = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(ProblemSize, TestFunction.MaximumSolutionLength));
193      Objectives = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives));
194      ReferencePointParameter.ActualValue = new DoubleArray(TestFunction.ReferencePoint(Objectives));
195      ParameterizeAnalyzers();
196      UpdateParameterValues();
197      OnReset();
198    }
199
200    private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) {
201      ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize));
202      UpdateParameterValues();
203    }
204
205    private void ObjectivesOnValueChanged(object sender, EventArgs eventArgs) {
206      Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
207      UpdateParameterValues();
208    }
209
210    #endregion
211
212    #region Helpers
213    private void InitializeOperators() {
214      Operators.Add(new CrowdingAnalyzer());
215      Operators.Add(new GenerationalDistanceAnalyzer());
216      Operators.Add(new InvertedGenerationalDistanceAnalyzer());
217      Operators.Add(new HypervolumeAnalyzer());
218      Operators.Add(new SpacingAnalyzer());
219      Operators.Add(new ScatterPlotAnalyzer());
220
221      ParameterizeAnalyzers();
222    }
223
224    private IEnumerable<IMultiObjectiveTestFunctionAnalyzer> Analyzers {
225      get { return Operators.OfType<IMultiObjectiveTestFunctionAnalyzer>(); }
226    }
227
228    private void ParameterizeAnalyzers() {
229      foreach (var analyzer in Analyzers) {
230        analyzer.ResultsParameter.ActualName = "Results";
231        analyzer.QualitiesParameter.ActualName = Evaluator.QualitiesParameter.ActualName;
232        analyzer.TestFunctionParameter.ActualName = TestFunctionParameter.Name;
233        analyzer.BestKnownFrontParameter.ActualName = BestKnownFrontParameter.Name;
234
235        var crowdingAnalyzer = analyzer as CrowdingAnalyzer;
236        if (crowdingAnalyzer != null) {
237          crowdingAnalyzer.BoundsParameter.ActualName = BoundsParameter.Name;
238        }
239
240        var scatterPlotAnalyzer = analyzer as ScatterPlotAnalyzer;
241        if (scatterPlotAnalyzer != null) {
242          scatterPlotAnalyzer.IndividualsParameter.ActualName = Encoding.Name;
243        }
244      }
245    }
246
247    #endregion
248  }
249}
250
Note: See TracBrowser for help on using the repository browser.