Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: refactored multi-objective problems' maximization

  • Add ForceValue method to IValueParameter to perform changes even when it is read-only
  • Add MaximizationChanged event handler
File size: 8.7 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 HEAL.Attic;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Problems.Instances;
31
32namespace HeuristicLab.Problems.TestFunctions.MultiObjective {
33  [StorableType("AB0C6A73-C432-46FD-AE3B-9841EAB2478C")]
34  [Creatable(CreatableAttribute.Categories.Problems, Priority = 95)]
35  [Item("Test Function (multi-objective)", "Test functions with real valued inputs and multiple objectives.")]
36  public class MultiObjectiveTestFunctionProblem : RealVectorMultiObjectiveProblem, IProblemInstanceConsumer<MOTFData>, IMultiObjectiveProblemDefinition<RealVectorEncoding, RealVector> {
37    #region Parameter Properties
38    public IFixedValueParameter<IntValue> ProblemSizeParameter {
39      get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; }
40    }
41    public IFixedValueParameter<IntValue> ObjectivesParameter {
42      get { return (IFixedValueParameter<IntValue>)Parameters["Objectives"]; }
43    }
44    public IValueParameter<DoubleMatrix> BoundsParameter {
45      get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
46    }
47    public IValueParameter<IMultiObjectiveTestFunction> TestFunctionParameter {
48      get { return (IValueParameter<IMultiObjectiveTestFunction>)Parameters["TestFunction"]; }
49    }
50    #endregion
51
52    #region Properties
53    public int ProblemSize {
54      get { return ProblemSizeParameter.Value.Value; }
55      set { ProblemSizeParameter.Value.Value = value; }
56    }
57    public new int Objectives {
58      get { return ObjectivesParameter.Value.Value; }
59      set { ObjectivesParameter.Value.Value = value; }
60    }
61    public DoubleMatrix Bounds {
62      get { return BoundsParameter.Value; }
63      set { BoundsParameter.Value = value; }
64    }
65    public IMultiObjectiveTestFunction TestFunction {
66      get { return TestFunctionParameter.Value; }
67      set { TestFunctionParameter.Value = value; }
68    }
69    #endregion
70
71    [StorableConstructor]
72    protected MultiObjectiveTestFunctionProblem(StorableConstructorFlag _) : base(_) { }
73    [StorableHook(HookType.AfterDeserialization)]
74    private void AfterDeserialization() {
75      RegisterEventHandlers();
76    }
77
78    protected MultiObjectiveTestFunctionProblem(MultiObjectiveTestFunctionProblem original, Cloner cloner) : base(original, cloner) {
79      RegisterEventHandlers();
80    }
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new MultiObjectiveTestFunctionProblem(this, cloner);
83    }
84
85    public MultiObjectiveTestFunctionProblem() : base() {
86      Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2)));
87      Parameters.Add(new FixedValueParameter<IntValue>("Objectives", "The dimensionality of the solution vector (number of objectives).", new IntValue(2)));
88      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 } })));
89      Parameters.Add(new ValueParameter<IMultiObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Fonseca()));
90
91      Encoding.LengthParameter = ProblemSizeParameter;
92      Encoding.BoundsParameter = BoundsParameter;
93      BestKnownFrontParameter.Hidden = true;
94
95      UpdateParameterValues();
96      InitializeOperators();
97      RegisterEventHandlers();
98    }
99
100    private void RegisterEventHandlers() {
101      TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
102      ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
103      ObjectivesParameter.Value.ValueChanged += ObjectivesOnValueChanged;
104    }
105
106
107    public override void Analyze(RealVector[] solutions, double[][] qualities, ResultCollection results, IRandom random) {
108      base.Analyze(solutions, qualities, results, random);
109      if (results.ContainsKey("Pareto Front"))
110        ((DoubleMatrix)results["Pareto Front"].Value).SortableView = true;
111    }
112
113    /// <summary>
114    /// Checks whether a given solution violates the contraints of this function.
115    /// </summary>
116    /// <param name="individual"></param>
117    /// <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>
118    public double[] CheckContraints(RealVector individual) {
119      var constrainedTestFunction = (IConstrainedTestFunction)TestFunction;
120      return constrainedTestFunction != null ? constrainedTestFunction.CheckConstraints(individual, Objectives) : new double[0];
121    }
122
123    public override double[] Evaluate(RealVector solution, IRandom random) {
124      return TestFunction.Evaluate(solution, Objectives);
125    }
126
127
128    public void Load(MOTFData data) {
129      TestFunction = data.TestFunction;
130    }
131
132    #region Events
133    private void UpdateParameterValues() {
134      Maximization = TestFunction.Maximization(Objectives);
135
136      Parameters.Remove(BestKnownFrontParameterName);
137      var front = TestFunction.OptimalParetoFront(Objectives);
138      var bkf = front != null ? (DoubleMatrix)Utilities.ToMatrix(front).AsReadOnly() : null;
139      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));
140
141      Parameters.Remove(ReferencePointParameterName);
142      Parameters.Add(new FixedValueParameter<DoubleArray>(ReferencePointParameterName, "The reference point for hypervolume calculations on this problem", new DoubleArray(TestFunction.ReferencePoint(Objectives))));
143
144      BoundsParameter.Value = new DoubleMatrix(TestFunction.Bounds(Objectives));
145    }
146
147    protected override void OnEncodingChanged() {
148      base.OnEncodingChanged();
149      UpdateParameterValues();
150    }
151
152    protected override void OnEvaluatorChanged() {
153      base.OnEvaluatorChanged();
154      UpdateParameterValues();
155    }
156
157    private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
158      ProblemSize = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(ProblemSize, TestFunction.MaximumSolutionLength));
159      Objectives = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives));
160      Parameters.Remove(ReferencePointParameterName);
161      Parameters.Add(new FixedValueParameter<DoubleArray>(ReferencePointParameterName, "The reference point for hypervolume calculations on this problem", new DoubleArray(TestFunction.ReferencePoint(Objectives))));
162      UpdateParameterValues();
163      OnReset();
164    }
165
166    private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) {
167      ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize));
168      UpdateParameterValues();
169    }
170
171    private void ObjectivesOnValueChanged(object sender, EventArgs eventArgs) {
172      Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
173      UpdateParameterValues();
174    }
175    #endregion
176
177    #region Helpers
178    private void InitializeOperators() {
179      Operators.Add(new CrowdingAnalyzer());
180      Operators.Add(new GenerationalDistanceAnalyzer());
181      Operators.Add(new InvertedGenerationalDistanceAnalyzer());
182      Operators.Add(new HypervolumeAnalyzer());
183      Operators.Add(new SpacingAnalyzer());
184      Operators.Add(new TimelineAnalyzer());
185    }
186    #endregion
187  }
188}
Note: See TracBrowser for help on using the repository browser.