Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17261 was 17261, checked in by mkommend, 4 years ago

#2521: Refactored multi-obj test functions and CMA-ES.

File size: 9.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Linq;
23using HEAL.Attic;
24using HeuristicLab.Analysis;
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, IProblemInstanceConsumer<MOTFData>, IMultiObjectiveProblemDefinition<RealVectorEncoding, RealVector> {
38    #region Parameter Properties
39    public IFixedValueParameter<IntValue> ProblemSizeParameter {
40      get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; }
41    }
42    public IFixedValueParameter<IntValue> ObjectivesParameter {
43      get { return (IFixedValueParameter<IntValue>)Parameters["Objectives"]; }
44    }
45    public IValueParameter<DoubleMatrix> BoundsParameter {
46      get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
47    }
48    public IValueParameter<IMultiObjectiveTestFunction> TestFunctionParameter {
49      get { return (IValueParameter<IMultiObjectiveTestFunction>)Parameters["TestFunction"]; }
50    }
51    #endregion
52
53    #region Properties
54    public override bool[] Maximization {
55      get {
56        //necessary because of virtual member call in base ctor
57        if (!Parameters.ContainsKey("TestFunction")) return new bool[0];
58        return TestFunction.Maximization(Objectives).ToArray();
59      }
60    }
61
62    public int ProblemSize {
63      get { return ProblemSizeParameter.Value.Value; }
64      set { ProblemSizeParameter.Value.Value = value; }
65    }
66    public new int Objectives {
67      get { return ObjectivesParameter.Value.Value; }
68      set { ObjectivesParameter.Value.Value = value; }
69    }
70    public DoubleMatrix Bounds {
71      get { return BoundsParameter.Value; }
72      set { BoundsParameter.Value = value; }
73    }
74    public IMultiObjectiveTestFunction TestFunction {
75      get { return TestFunctionParameter.Value; }
76      set { TestFunctionParameter.Value = value; }
77    }
78    #endregion
79
80    [StorableConstructor]
81    protected MultiObjectiveTestFunctionProblem(StorableConstructorFlag _) : base(_) { }
82    [StorableHook(HookType.AfterDeserialization)]
83    private void AfterDeserialization() {
84      RegisterEventHandlers();
85    }
86
87    protected MultiObjectiveTestFunctionProblem(MultiObjectiveTestFunctionProblem original, Cloner cloner) : base(original, cloner) {
88      RegisterEventHandlers();
89    }
90    public override IDeepCloneable Clone(Cloner cloner) {
91      return new MultiObjectiveTestFunctionProblem(this, cloner);
92    }
93
94    public MultiObjectiveTestFunctionProblem() : base() {
95      Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2)));
96      Parameters.Add(new FixedValueParameter<IntValue>("Objectives", "The dimensionality of the solution vector (number of objectives).", new IntValue(2)));
97      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 } })));
98      Parameters.Add(new ValueParameter<IMultiObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Fonseca()));
99
100      Encoding.LengthParameter = ProblemSizeParameter;
101      Encoding.BoundsParameter = BoundsParameter;
102      BestKnownFrontParameter.Hidden = true;
103
104      UpdateParameterValues();
105      InitializeOperators();
106      RegisterEventHandlers();
107    }
108
109    private void RegisterEventHandlers() {
110      TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
111      ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
112      ObjectivesParameter.Value.ValueChanged += ObjectivesOnValueChanged;
113    }
114
115
116    public override void Analyze(RealVector[] solutions, double[][] qualities, ResultCollection results, IRandom random) {
117      base.Analyze(solutions, qualities, results, random);
118      if (results.ContainsKey("Pareto Front"))
119        ((DoubleMatrix)results["Pareto Front"].Value).SortableView = true;
120    }
121
122    /// <summary>
123    /// Checks whether a given solution violates the contraints of this function.
124    /// </summary>
125    /// <param name="individual"></param>
126    /// <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>
127    public double[] CheckContraints(RealVector individual) {
128      var constrainedTestFunction = (IConstrainedTestFunction)TestFunction;
129      return constrainedTestFunction != null ? constrainedTestFunction.CheckConstraints(individual, Objectives) : new double[0];
130    }
131
132    public override double[] Evaluate(RealVector solution, IRandom random) {
133      return TestFunction.Evaluate(solution, Objectives);
134    }
135
136
137    public void Load(MOTFData data) {
138      TestFunction = data.TestFunction;
139    }
140
141    #region Events
142    private void UpdateParameterValues() {
143      Parameters.Remove(MaximizationParameterName);
144      Parameters.Add(new FixedValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(TestFunction.Maximization(Objectives)).AsReadOnly()));
145
146      Parameters.Remove(BestKnownFrontParameterName);
147      var front = TestFunction.OptimalParetoFront(Objectives);
148      var bkf = front != null ? (DoubleMatrix)Utilities.ToMatrix(front).AsReadOnly() : null;
149      Parameters.Add(new FixedValueParameter<DoubleMatrix>(BestKnownFrontParameterName, "A double matrix representing the best known qualities for this problem (aka points on the Pareto front). Points are to be given in a row-wise fashion.", bkf));
150
151      Parameters.Remove(ReferencePointParameterName);
152      Parameters.Add(new FixedValueParameter<DoubleArray>(ReferencePointParameterName, "The reference point for hypervolume calculations on this problem", new DoubleArray(TestFunction.ReferencePoint(Objectives))));
153
154      BoundsParameter.Value = new DoubleMatrix(TestFunction.Bounds(Objectives));
155    }
156
157    protected override void OnEncodingChanged() {
158      base.OnEncodingChanged();
159      UpdateParameterValues();
160    }
161
162    protected override void OnEvaluatorChanged() {
163      base.OnEvaluatorChanged();
164      UpdateParameterValues();
165    }
166
167    private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
168      ProblemSize = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(ProblemSize, TestFunction.MaximumSolutionLength));
169      Objectives = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives));
170      Parameters.Remove(ReferencePointParameterName);
171      Parameters.Add(new FixedValueParameter<DoubleArray>(ReferencePointParameterName, "The reference point for hypervolume calculations on this problem", new DoubleArray(TestFunction.ReferencePoint(Objectives))));
172      UpdateParameterValues();
173      OnReset();
174    }
175
176    private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) {
177      ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize));
178      UpdateParameterValues();
179    }
180
181    private void ObjectivesOnValueChanged(object sender, EventArgs eventArgs) {
182      Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
183      UpdateParameterValues();
184    }
185    #endregion
186
187    #region Helpers
188    private void InitializeOperators() {
189      Operators.Add(new CrowdingAnalyzer());
190      Operators.Add(new GenerationalDistanceAnalyzer());
191      Operators.Add(new InvertedGenerationalDistanceAnalyzer());
192      Operators.Add(new HypervolumeAnalyzer());
193      Operators.Add(new SpacingAnalyzer());
194      Operators.Add(new TimelineAnalyzer());
195    }
196    #endregion
197  }
198}
Note: See TracBrowser for help on using the repository browser.