Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/MultiObjectiveTestFunctionProblem.cs @ 17181

Last change on this file since 17181 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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