Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: worked on multi-objective test function

File size: 6.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
21
22using System;
23using System.Threading;
24using HEAL.Attic;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.RealVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Problems.Instances;
33
34namespace HeuristicLab.Problems.TestFunctions.MultiObjective {
35  [StorableType("AB0C6A73-C432-46FD-AE3B-9841EAB2478C")]
36  [Creatable(CreatableAttribute.Categories.Problems, Priority = 95)]
37  [Item("Test Function (multi-objective)", "Test functions with real valued inputs and multiple objectives.")]
38  public class MultiObjectiveTestFunctionProblem : RealVectorMultiObjectiveProblem, IProblemInstanceConsumer<MOTFData>, IMultiObjectiveProblemDefinition<RealVectorEncoding, RealVector> {
39    #region Parameter Properties
40    public IFixedValueParameter<IntValue> ObjectivesParameter {
41      get { return (IFixedValueParameter<IntValue>)Parameters["Objectives"]; }
42    }
43    public IValueParameter<IMultiObjectiveTestFunction> TestFunctionParameter {
44      get { return (IValueParameter<IMultiObjectiveTestFunction>)Parameters["TestFunction"]; }
45    }
46    #endregion
47
48    #region Properties
49    public new int Objectives {
50      get { return ObjectivesParameter.Value.Value; }
51      set { ObjectivesParameter.Value.Value = value; }
52    }
53    public IMultiObjectiveTestFunction TestFunction {
54      get { return TestFunctionParameter.Value; }
55      set { TestFunctionParameter.Value = value; }
56    }
57    #endregion
58
59    [StorableConstructor]
60    protected MultiObjectiveTestFunctionProblem(StorableConstructorFlag _) : base(_) { }
61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
63      RegisterEventHandlers();
64    }
65
66    protected MultiObjectiveTestFunctionProblem(MultiObjectiveTestFunctionProblem original, Cloner cloner) : base(original, cloner) {
67      RegisterEventHandlers();
68    }
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new MultiObjectiveTestFunctionProblem(this, cloner);
71    }
72
73    public MultiObjectiveTestFunctionProblem() : base() {
74      Parameters.Add(new FixedValueParameter<IntValue>("Objectives", "The dimensionality of the solution vector (number of objectives).", new IntValue(2)));
75      Parameters.Add(new ValueParameter<IMultiObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Fonseca()));
76
77      BestKnownFrontParameter.Hidden = true;
78      BestKnownFrontParameter.ReadOnly = true;
79      ReferencePointParameter.ReadOnly = true;
80
81      UpdateParameterValues();
82      InitializeOperators();
83      RegisterEventHandlers();
84    }
85
86    private void RegisterEventHandlers() {
87      IntValueParameterChangeHandler.Create(ObjectivesParameter, ObjectivesOnChanged);
88      ParameterChangeHandler<IMultiObjectiveTestFunction>.Create(TestFunctionParameter, TestFunctionOnChanged);
89    }
90
91
92    public override void Analyze(RealVector[] solutions, double[][] qualities, ResultCollection results, IRandom random) {
93      base.Analyze(solutions, qualities, results, random);
94      if (results.ContainsKey("Pareto Front"))
95        ((DoubleMatrix)results["Pareto Front"].Value).SortableView = true;
96    }
97
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>
103    public double[] CheckContraints(RealVector individual) {
104      var constrainedTestFunction = (IConstrainedTestFunction)TestFunction;
105      return constrainedTestFunction != null ? constrainedTestFunction.CheckConstraints(individual, Objectives) : new double[0];
106    }
107
108    public override double[] Evaluate(RealVector solution, IRandom random, CancellationToken cancellationToken) {
109      return TestFunction.Evaluate(solution, Objectives);
110    }
111
112
113    public void Load(MOTFData data) {
114      TestFunction = data.TestFunction;
115    }
116
117    #region Events
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();
123      OnReset();
124    }
125
126    protected virtual void TestFunctionOnChanged() {
127      Dimension = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(Dimension, TestFunction.MaximumSolutionLength));
128      Objectives = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives));
129      UpdateParameterValues();
130      OnReset();
131    }
132
133    protected virtual void ObjectivesOnChanged() {
134      Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
135      UpdateParameterValues();
136      OnReset();
137    }
138    #endregion
139
140    #region Helpers
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
150    private void InitializeOperators() {
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());
156      Operators.Add(new TimelineAnalyzer());
157    }
158    #endregion
159  }
160}
Note: See TracBrowser for help on using the repository browser.