Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/MultiObjectiveTestFunctionProblem.cs @ 14821

Last change on this file since 14821 was 14150, checked in by pfleck, 8 years ago

#1087 Changed BestKnownFront parameter to OptionalValueParameter to allow null if no best front is known (DTLZ8 and SchafferN2 and DTLZs with objectives > 2).

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