Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Merged trunk changes into problem refactoring branch.

File size: 10.1 KB
RevLine 
[13672]1#region License Information
2/* HeuristicLab
[17226]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[13672]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;
[13620]22using System.Collections.Generic;
[13672]23using System.Linq;
[16807]24using HEAL.Attic;
[17225]25using HeuristicLab.Analysis;
[13421]26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.RealVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
[13620]32using HeuristicLab.Problems.Instances;
[13421]33
[14111]34namespace HeuristicLab.Problems.TestFunctions.MultiObjective {
[16723]35  [StorableType("AB0C6A73-C432-46FD-AE3B-9841EAB2478C")]
[14073]36  [Creatable(CreatableAttribute.Categories.Problems, Priority = 95)]
37  [Item("Test Function (multi-objective)", "Test functions with real valued inputs and multiple objectives.")]
[17225]38  public class MultiObjectiveTestFunctionProblem : RealVectorMultiObjectiveProblem, IProblemInstanceConsumer<MOTFData>, IMultiObjectiveProblemDefinition<RealVectorEncoding, RealVector> {
[13672]39    #region Parameter Properties
[14073]40    public IFixedValueParameter<IntValue> ProblemSizeParameter {
[13421]41      get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; }
42    }
[14073]43    public IFixedValueParameter<IntValue> ObjectivesParameter {
[13672]44      get { return (IFixedValueParameter<IntValue>)Parameters["Objectives"]; }
[13421]45    }
[14073]46    public IValueParameter<DoubleMatrix> BoundsParameter {
[13421]47      get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
48    }
49    public IValueParameter<IMultiObjectiveTestFunction> TestFunctionParameter {
50      get { return (IValueParameter<IMultiObjectiveTestFunction>)Parameters["TestFunction"]; }
51    }
52    #endregion
53
54    #region Properties
[14073]55    public override bool[] Maximization {
56      get {
[16807]57        //necessary because of virtual member call in base ctor
58        if (!Parameters.ContainsKey("TestFunction")) return new bool[0];
59        return TestFunction.Maximization(Objectives).ToArray();
[14073]60      }
[13672]61    }
62
63    public int ProblemSize {
[13421]64      get { return ProblemSizeParameter.Value.Value; }
65      set { ProblemSizeParameter.Value.Value = value; }
66    }
[16950]67    public new int Objectives {
[13672]68      get { return ObjectivesParameter.Value.Value; }
69      set { ObjectivesParameter.Value.Value = value; }
[13421]70    }
71    public DoubleMatrix Bounds {
72      get { return BoundsParameter.Value; }
73      set { BoundsParameter.Value = value; }
74    }
75    public IMultiObjectiveTestFunction TestFunction {
76      get { return TestFunctionParameter.Value; }
77      set { TestFunctionParameter.Value = value; }
78    }
79    #endregion
80
81    [StorableConstructor]
[16723]82    protected MultiObjectiveTestFunctionProblem(StorableConstructorFlag _) : base(_) { }
[14073]83    [StorableHook(HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85      RegisterEventHandlers();
86    }
87
[17225]88    protected MultiObjectiveTestFunctionProblem(MultiObjectiveTestFunctionProblem original, Cloner cloner) : base(original, cloner) {
[13421]89      RegisterEventHandlers();
90    }
[14073]91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new MultiObjectiveTestFunctionProblem(this, cloner);
93    }
94
[17225]95    public MultiObjectiveTestFunctionProblem() : base() {
[13421]96      Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2)));
[13672]97      Parameters.Add(new FixedValueParameter<IntValue>("Objectives", "The dimensionality of the solution vector (number of objectives).", new IntValue(2)));
[13448]98      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 } })));
[13421]99      Parameters.Add(new ValueParameter<IMultiObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Fonseca()));
100
101      Encoding.LengthParameter = ProblemSizeParameter;
102      Encoding.BoundsParameter = BoundsParameter;
[13725]103      BestKnownFrontParameter.Hidden = true;
[13421]104
[14073]105      UpdateParameterValues();
[13421]106      InitializeOperators();
107      RegisterEventHandlers();
108    }
[14073]109
110    private void RegisterEventHandlers() {
111      TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
112      ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
113      ObjectivesParameter.Value.ValueChanged += ObjectivesOnValueChanged;
[13421]114    }
115
[14073]116
[16807]117    public override void Analyze(RealVector[] solutions, double[][] qualities, ResultCollection results, IRandom random) {
118      base.Analyze(solutions, qualities, results, random);
[17225]119      if (results.ContainsKey("Pareto Front"))
[13725]120        ((DoubleMatrix)results["Pareto Front"].Value).SortableView = true;
[13421]121    }
122
[13776]123    /// <summary>
124    /// Checks whether a given solution violates the contraints of this function.
125    /// </summary>
126    /// <param name="individual"></param>
127    /// <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>
[14068]128    public double[] CheckContraints(RealVector individual) {
129      var constrainedTestFunction = (IConstrainedTestFunction)TestFunction;
[17225]130      return constrainedTestFunction != null ? constrainedTestFunction.CheckConstraints(individual, Objectives) : new double[0];
[13776]131    }
132
[16807]133    public override double[] Evaluate(RealVector solution, IRandom random) {
134      return TestFunction.Evaluate(solution, Objectives);
[13421]135    }
[13448]136
[13421]137
[13620]138    public void Load(MOTFData data) {
[14065]139      TestFunction = data.TestFunction;
[13620]140    }
[13448]141
[14073]142    #region Events
143    private void UpdateParameterValues() {
[17225]144      Parameters.Remove(MaximizationParameterName);
145      Parameters.Add(new FixedValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(TestFunction.Maximization(Objectives)).AsReadOnly()));
[14073]146
[17225]147      Parameters.Remove(BestKnownFrontParameterName);
[14085]148      var front = TestFunction.OptimalParetoFront(Objectives);
[17225]149      var bkf = front != null ? (DoubleMatrix)Utilities.ToMatrix(front).AsReadOnly() : null;
150      Parameters.Add(new FixedValueParameter<DoubleMatrix>(BestKnownFrontParameterName, "A double matrix representing the best known qualites for this problem (aka points on the Pareto front). Points are to be given in a row-wise fashion.", bkf));
[14085]151
[17225]152      Parameters.Remove(ReferencePointParameterName);
153      Parameters.Add(new FixedValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem", new DoubleArray(TestFunction.ReferencePoint(Objectives))));
[14085]154
155      BoundsParameter.Value = new DoubleMatrix(TestFunction.Bounds(Objectives));
[13672]156    }
157
[13421]158    protected override void OnEncodingChanged() {
159      base.OnEncodingChanged();
[14073]160      UpdateParameterValues();
[13672]161      ParameterizeAnalyzers();
[13421]162    }
[17225]163
[13421]164    protected override void OnEvaluatorChanged() {
165      base.OnEvaluatorChanged();
[14073]166      UpdateParameterValues();
[13672]167      ParameterizeAnalyzers();
[13421]168    }
[13448]169
[13421]170    private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
[14073]171      ProblemSize = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(ProblemSize, TestFunction.MaximumSolutionLength));
172      Objectives = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives));
[17225]173      Parameters.Remove(ReferencePointParameterName);
174      Parameters.Add(new FixedValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem", new DoubleArray(TestFunction.ReferencePoint(Objectives))));
[13672]175      ParameterizeAnalyzers();
[14073]176      UpdateParameterValues();
[13421]177      OnReset();
178    }
179
180    private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) {
[14073]181      ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize));
182      UpdateParameterValues();
[13421]183    }
184
[13672]185    private void ObjectivesOnValueChanged(object sender, EventArgs eventArgs) {
[14073]186      Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
187      UpdateParameterValues();
[13421]188    }
189    #endregion
190
191    #region Helpers
192    private void InitializeOperators() {
[13672]193      Operators.Add(new CrowdingAnalyzer());
194      Operators.Add(new GenerationalDistanceAnalyzer());
195      Operators.Add(new InvertedGenerationalDistanceAnalyzer());
196      Operators.Add(new HypervolumeAnalyzer());
197      Operators.Add(new SpacingAnalyzer());
[17225]198      Operators.Add(new TimelineAnalyzer());
[13672]199      Operators.Add(new ScatterPlotAnalyzer());
200      ParameterizeAnalyzers();
[13421]201    }
202
[14073]203    private IEnumerable<IMultiObjectiveTestFunctionAnalyzer> Analyzers {
204      get { return Operators.OfType<IMultiObjectiveTestFunctionAnalyzer>(); }
[13421]205    }
[13672]206
207    private void ParameterizeAnalyzers() {
208      foreach (var analyzer in Analyzers) {
209        analyzer.ResultsParameter.ActualName = "Results";
210        analyzer.QualitiesParameter.ActualName = Evaluator.QualitiesParameter.ActualName;
[13725]211        analyzer.TestFunctionParameter.ActualName = TestFunctionParameter.Name;
212        analyzer.BestKnownFrontParameter.ActualName = BestKnownFrontParameter.Name;
[14044]213        var scatterPlotAnalyzer = analyzer as ScatterPlotAnalyzer;
[17225]214        if (scatterPlotAnalyzer != null)
[14044]215          scatterPlotAnalyzer.IndividualsParameter.ActualName = Encoding.Name;
[13672]216      }
217    }
[13421]218    #endregion
219  }
[17225]220}
Note: See TracBrowser for help on using the repository browser.