1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 | using System;
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.Instances;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
|
---|
34 | [StorableClass]
|
---|
35 | public class MultiObjectiveTestFunctionProblem : MultiObjectiveBasicProblem<RealVectorEncoding>, IProblemInstanceConsumer<MOTFData> {
|
---|
36 |
|
---|
37 | #region Parameter Properties
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Whether an objective is to be maximized or minimized
|
---|
41 | /// </summary>
|
---|
42 | private IValueParameter<BoolArray> MaximizationParameter {
|
---|
43 | get {
|
---|
44 | return (IValueParameter<BoolArray>)Parameters["Maximization"];
|
---|
45 | }
|
---|
46 | set {
|
---|
47 | Parameters["Maximization"].ActualValue = value;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// The dimensionality of the solution candidates
|
---|
53 | /// </summary>
|
---|
54 | private IFixedValueParameter<IntValue> ProblemSizeParameter {
|
---|
55 | get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /// <summary>
|
---|
59 | /// The number of objectives that are to be optimized
|
---|
60 | /// </summary>
|
---|
61 | private IFixedValueParameter<IntValue> ObjectivesParameter {
|
---|
62 | get { return (IFixedValueParameter<IntValue>)Parameters["Objectives"]; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /// <summary>
|
---|
66 | /// The bounds for the entries of the solution candidate
|
---|
67 | /// </summary>
|
---|
68 | private IValueParameter<DoubleMatrix> BoundsParameter {
|
---|
69 | get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
70 | }
|
---|
71 |
|
---|
72 | /// <summary>
|
---|
73 | /// The testfunction
|
---|
74 | /// </summary>
|
---|
75 | public IValueParameter<IMultiObjectiveTestFunction> TestFunctionParameter {
|
---|
76 | get { return (IValueParameter<IMultiObjectiveTestFunction>)Parameters["TestFunction"]; }
|
---|
77 | }
|
---|
78 | #endregion
|
---|
79 |
|
---|
80 | #region Properties
|
---|
81 | private IEnumerable<IMultiObjectiveTestFunctionAnalyzer> Analyzers {
|
---|
82 | get { return Operators.OfType<IMultiObjectiveTestFunctionAnalyzer>(); }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public int ProblemSize {
|
---|
86 | get { return ProblemSizeParameter.Value.Value; }
|
---|
87 | set { ProblemSizeParameter.Value.Value = value; }
|
---|
88 | }
|
---|
89 | public int Objectives {
|
---|
90 | get { return ObjectivesParameter.Value.Value; }
|
---|
91 | set { ObjectivesParameter.Value.Value = value; }
|
---|
92 | }
|
---|
93 | public DoubleMatrix Bounds {
|
---|
94 | get { return BoundsParameter.Value; }
|
---|
95 | set { BoundsParameter.Value = value; }
|
---|
96 | }
|
---|
97 | public IMultiObjectiveTestFunction TestFunction {
|
---|
98 | get { return TestFunctionParameter.Value; }
|
---|
99 | set { TestFunctionParameter.Value = value; }
|
---|
100 | }
|
---|
101 | #endregion
|
---|
102 |
|
---|
103 | [StorableConstructor]
|
---|
104 | private MultiObjectiveTestFunctionProblem(bool deserializing) : base(deserializing) { }
|
---|
105 | private MultiObjectiveTestFunctionProblem(MultiObjectiveTestFunctionProblem original, Cloner cloner)
|
---|
106 | : base(original, cloner) {
|
---|
107 | RegisterEventHandlers();
|
---|
108 | }
|
---|
109 | public MultiObjectiveTestFunctionProblem()
|
---|
110 | : base() {
|
---|
111 | Parameters.Remove("Maximization");
|
---|
112 | Parameters.Add(new ValueParameter<BoolArray>("Maximization", "", new BoolArray(new bool[] { false, false })));
|
---|
113 | Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2)));
|
---|
114 | Parameters.Add(new FixedValueParameter<IntValue>("Objectives", "The dimensionality of the solution vector (number of objectives).", new IntValue(2)));
|
---|
115 | 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 } })));
|
---|
116 | Parameters.Add(new ValueParameter<IMultiObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Fonseca()));
|
---|
117 |
|
---|
118 | Encoding.LengthParameter = ProblemSizeParameter;
|
---|
119 | Encoding.BoundsParameter = BoundsParameter;
|
---|
120 |
|
---|
121 | InitializeOperators();
|
---|
122 | RegisterEventHandlers();
|
---|
123 | }
|
---|
124 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
125 | return new MultiObjectiveTestFunctionProblem(this, cloner);
|
---|
126 | }
|
---|
127 | [StorableHook(HookType.AfterDeserialization)]
|
---|
128 | private void AfterDeserialization() {
|
---|
129 | RegisterEventHandlers();
|
---|
130 | }
|
---|
131 |
|
---|
132 | public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
|
---|
133 | base.Analyze(individuals, qualities, results, random);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public override bool[] Maximization {
|
---|
137 | get {
|
---|
138 | return Parameters.ContainsKey("TestFunction") ? TestFunction.Maximization(Objectives) : new bool[2];
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | public double[] Evaluate(RealVector individual, IRandom random) {
|
---|
143 | return TestFunction.Evaluate(individual, Objectives);
|
---|
144 | }
|
---|
145 |
|
---|
146 | public override double[] Evaluate(Individual individual, IRandom random) {
|
---|
147 | return Evaluate(individual.RealVector(), random);
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void Load(MOTFData data) {
|
---|
151 | TestFunction = data.Evaluator;
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void RegisterEventHandlers() {
|
---|
155 | TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
|
---|
156 | ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
|
---|
157 | ObjectivesParameter.Value.ValueChanged += ObjectivesOnValueChanged;
|
---|
158 | BoundsParameter.ValueChanged += BoundsParameterOnValueChanged;
|
---|
159 | }
|
---|
160 |
|
---|
161 | #region Events
|
---|
162 | protected override void OnEncodingChanged() {
|
---|
163 | base.OnEncodingChanged();
|
---|
164 | Parameterize();
|
---|
165 | ParameterizeAnalyzers();
|
---|
166 | }
|
---|
167 | protected override void OnEvaluatorChanged() {
|
---|
168 | base.OnEvaluatorChanged();
|
---|
169 | Parameterize();
|
---|
170 | ParameterizeAnalyzers();
|
---|
171 | }
|
---|
172 |
|
---|
173 | private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
174 | var problemSizeChange = ProblemSize < TestFunction.MinimumSolutionLength
|
---|
175 | || ProblemSize > TestFunction.MaximumSolutionLength;
|
---|
176 | if (problemSizeChange) {
|
---|
177 | ProblemSize = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(ProblemSize, TestFunction.MaximumSolutionLength));
|
---|
178 | }
|
---|
179 |
|
---|
180 | var solutionSizeChange = Objectives < TestFunction.MinimumObjectives
|
---|
181 | || Objectives > TestFunction.MaximumObjectives;
|
---|
182 | if (solutionSizeChange) {
|
---|
183 | ProblemSize = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives));
|
---|
184 | }
|
---|
185 |
|
---|
186 | Bounds = (DoubleMatrix)new DoubleMatrix(TestFunction.Bounds(Objectives)).Clone();
|
---|
187 | ParameterizeAnalyzers();
|
---|
188 | Parameterize();
|
---|
189 | OnReset();
|
---|
190 | }
|
---|
191 |
|
---|
192 | private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
193 | if (ProblemSize < TestFunction.MinimumSolutionLength
|
---|
194 | || ProblemSize > TestFunction.MaximumSolutionLength)
|
---|
195 | ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize));
|
---|
196 | if (Objectives < TestFunction.MinimumObjectives
|
---|
197 | || Objectives > TestFunction.MaximumObjectives)
|
---|
198 | Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
|
---|
199 | Parameterize();
|
---|
200 | }
|
---|
201 |
|
---|
202 | private void ObjectivesOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
203 | if (Objectives < TestFunction.MinimumObjectives
|
---|
204 | || Objectives > TestFunction.MaximumObjectives)
|
---|
205 | Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
|
---|
206 | if (ProblemSize < TestFunction.MinimumSolutionLength
|
---|
207 | || ProblemSize > TestFunction.MaximumSolutionLength)
|
---|
208 | ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize));
|
---|
209 | Parameterize();
|
---|
210 | }
|
---|
211 |
|
---|
212 | private void BoundsParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
213 | Parameterize();
|
---|
214 | }
|
---|
215 | #endregion
|
---|
216 |
|
---|
217 | #region Helpers
|
---|
218 | private void InitializeOperators() {
|
---|
219 | Operators.Add(new CrowdingAnalyzer());
|
---|
220 | Operators.Add(new GenerationalDistanceAnalyzer());
|
---|
221 | Operators.Add(new InvertedGenerationalDistanceAnalyzer());
|
---|
222 | Operators.Add(new HypervolumeAnalyzer());
|
---|
223 | Operators.Add(new SpacingAnalyzer());
|
---|
224 | Operators.Add(new ScatterPlotAnalyzer());
|
---|
225 | ParameterizeAnalyzers();
|
---|
226 | Parameterize();
|
---|
227 | }
|
---|
228 |
|
---|
229 | private void Parameterize() {
|
---|
230 | MaximizationParameter.ActualValue = new BoolArray(Maximization);
|
---|
231 | }
|
---|
232 |
|
---|
233 | private void ParameterizeAnalyzers() {
|
---|
234 | foreach (var analyzer in Analyzers) {
|
---|
235 | analyzer.ResultsParameter.ActualName = "Results";
|
---|
236 | analyzer.QualitiesParameter.ActualName = Evaluator.QualitiesParameter.ActualName;
|
---|
237 | analyzer.TestFunction = TestFunction;
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | #endregion
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|