Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17690 was 17690, checked in by abeham, 4 years ago

#2521: worked on multi-objective test function

File size: 6.7 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
[17690]21
[13672]22using System;
[17320]23using System.Threading;
[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> ObjectivesParameter {
[13672]41      get { return (IFixedValueParameter<IntValue>)Parameters["Objectives"]; }
[13421]42    }
43    public IValueParameter<IMultiObjectiveTestFunction> TestFunctionParameter {
44      get { return (IValueParameter<IMultiObjectiveTestFunction>)Parameters["TestFunction"]; }
45    }
46    #endregion
47
48    #region Properties
[16950]49    public new int Objectives {
[13672]50      get { return ObjectivesParameter.Value.Value; }
51      set { ObjectivesParameter.Value.Value = value; }
[13421]52    }
53    public IMultiObjectiveTestFunction TestFunction {
54      get { return TestFunctionParameter.Value; }
55      set { TestFunctionParameter.Value = value; }
56    }
57    #endregion
58
59    [StorableConstructor]
[16723]60    protected MultiObjectiveTestFunctionProblem(StorableConstructorFlag _) : base(_) { }
[14073]61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
63      RegisterEventHandlers();
64    }
65
[17225]66    protected MultiObjectiveTestFunctionProblem(MultiObjectiveTestFunctionProblem original, Cloner cloner) : base(original, cloner) {
[13421]67      RegisterEventHandlers();
68    }
[14073]69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new MultiObjectiveTestFunctionProblem(this, cloner);
71    }
72
[17225]73    public MultiObjectiveTestFunctionProblem() : base() {
[13672]74      Parameters.Add(new FixedValueParameter<IntValue>("Objectives", "The dimensionality of the solution vector (number of objectives).", new IntValue(2)));
[13421]75      Parameters.Add(new ValueParameter<IMultiObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Fonseca()));
76
[13725]77      BestKnownFrontParameter.Hidden = true;
[17690]78      BestKnownFrontParameter.ReadOnly = true;
79      ReferencePointParameter.ReadOnly = true;
[13421]80
[14073]81      UpdateParameterValues();
[13421]82      InitializeOperators();
83      RegisterEventHandlers();
84    }
[14073]85
86    private void RegisterEventHandlers() {
[17690]87      IntValueParameterChangeHandler.Create(ObjectivesParameter, ObjectivesOnChanged);
88      ParameterChangeHandler<IMultiObjectiveTestFunction>.Create(TestFunctionParameter, TestFunctionOnChanged);
[13421]89    }
90
[14073]91
[16807]92    public override void Analyze(RealVector[] solutions, double[][] qualities, ResultCollection results, IRandom random) {
93      base.Analyze(solutions, qualities, results, random);
[17225]94      if (results.ContainsKey("Pareto Front"))
[13725]95        ((DoubleMatrix)results["Pareto Front"].Value).SortableView = true;
[13421]96    }
97
[13776]98    /// <summary>
99    /// Checks whether a given solution violates the contraints of this function.
100    /// </summary>
101    /// <param name="individual"></param>
102    /// <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]103    public double[] CheckContraints(RealVector individual) {
104      var constrainedTestFunction = (IConstrainedTestFunction)TestFunction;
[17225]105      return constrainedTestFunction != null ? constrainedTestFunction.CheckConstraints(individual, Objectives) : new double[0];
[13776]106    }
107
[17320]108    public override double[] Evaluate(RealVector solution, IRandom random, CancellationToken cancellationToken) {
[16807]109      return TestFunction.Evaluate(solution, Objectives);
[13421]110    }
[13448]111
[13421]112
[13620]113    public void Load(MOTFData data) {
[14065]114      TestFunction = data.TestFunction;
[13620]115    }
[13448]116
[14073]117    #region Events
[17544]118    protected override void DimensionOnChanged() {
119      base.DimensionOnChanged();
120      if (Dimension < TestFunction.MinimumSolutionLength || Dimension > TestFunction.MaximumSolutionLength)
121        Dimension = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, Dimension));
122      UpdateParameterValues();
[17690]123      OnReset();
[17544]124    }
125
[17690]126    protected virtual void TestFunctionOnChanged() {
[17544]127      Dimension = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(Dimension, TestFunction.MaximumSolutionLength));
[14073]128      Objectives = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives));
129      UpdateParameterValues();
[13421]130      OnReset();
131    }
132
[17690]133    protected virtual void ObjectivesOnChanged() {
[14073]134      Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
135      UpdateParameterValues();
[17690]136      OnReset();
[13421]137    }
138    #endregion
139
140    #region Helpers
[17690]141    private void UpdateParameterValues() {
142      Maximization = TestFunction.Maximization(Objectives);
143
144      BestKnownFrontParameter.Value = DoubleMatrix.FromRows(TestFunction.OptimalParetoFront(Objectives));
145      ReferencePoint = TestFunction.ReferencePoint(Objectives);
146
147      BoundsRefParameter.Value = new DoubleMatrix(TestFunction.Bounds(Objectives));
148    }
149
[13421]150    private void InitializeOperators() {
[13672]151      Operators.Add(new CrowdingAnalyzer());
152      Operators.Add(new GenerationalDistanceAnalyzer());
153      Operators.Add(new InvertedGenerationalDistanceAnalyzer());
154      Operators.Add(new HypervolumeAnalyzer());
155      Operators.Add(new SpacingAnalyzer());
[17225]156      Operators.Add(new TimelineAnalyzer());
[13421]157    }
158    #endregion
159  }
[17225]160}
Note: See TracBrowser for help on using the repository browser.