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

Last change on this file since 17544 was 17544, checked in by abeham, 8 weeks ago

#2521: worked on refactoring, worked a lot on binary encoding / problems

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