1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Analysis;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Optimization.Operators;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HEAL.Attic;
|
---|
34 | using HeuristicLab.Problems.Instances;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Problems.TestFunctions {
|
---|
37 | [Item("Test Function (single-objective)", "Test function with real valued inputs and a single objective.")]
|
---|
38 | [StorableType("F0AB7236-2C9B-49DC-9D4F-A3558FD9E992")]
|
---|
39 | [Creatable(CreatableAttribute.Categories.Problems, Priority = 90)]
|
---|
40 | public sealed class SingleObjectiveTestFunctionProblem :
|
---|
41 | SingleObjectiveProblem<RealVectorEncoding, RealVector>,
|
---|
42 | IProblemInstanceConsumer<SOTFData> {
|
---|
43 |
|
---|
44 | public override bool Maximization {
|
---|
45 | get { return Parameters.ContainsKey("TestFunction") && TestFunction.Maximization; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | #region Parameter Properties
|
---|
49 | private IFixedValueParameter<IntValue> ProblemSizeParameter {
|
---|
50 | get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; }
|
---|
51 | }
|
---|
52 | private IValueParameter<DoubleMatrix> BoundsParameter {
|
---|
53 | get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
54 | }
|
---|
55 | public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
|
---|
56 | get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
|
---|
57 | }
|
---|
58 | public IValueParameter<ISingleObjectiveTestFunction> TestFunctionParameter {
|
---|
59 | get { return (IValueParameter<ISingleObjectiveTestFunction>)Parameters["TestFunction"]; }
|
---|
60 | }
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | #region Properties
|
---|
64 | public int ProblemSize {
|
---|
65 | get { return ProblemSizeParameter.Value.Value; }
|
---|
66 | set { ProblemSizeParameter.Value.Value = value; }
|
---|
67 | }
|
---|
68 | public DoubleMatrix Bounds {
|
---|
69 | get { return BoundsParameter.Value; }
|
---|
70 | set { BoundsParameter.Value = value; }
|
---|
71 | }
|
---|
72 | public ISingleObjectiveTestFunction TestFunction {
|
---|
73 | get { return TestFunctionParameter.Value; }
|
---|
74 | set { TestFunctionParameter.Value = value; }
|
---|
75 | }
|
---|
76 |
|
---|
77 | private BestSingleObjectiveTestFunctionSolutionAnalyzer BestSingleObjectiveTestFunctionSolutionAnalyzer {
|
---|
78 | get { return Operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); }
|
---|
79 | }
|
---|
80 | #endregion
|
---|
81 |
|
---|
82 | [StorableConstructor]
|
---|
83 | private SingleObjectiveTestFunctionProblem(StorableConstructorFlag _) : base(_) { }
|
---|
84 | private SingleObjectiveTestFunctionProblem(SingleObjectiveTestFunctionProblem original, Cloner cloner)
|
---|
85 | : base(original, cloner) {
|
---|
86 | RegisterEventHandlers();
|
---|
87 | }
|
---|
88 | public SingleObjectiveTestFunctionProblem()
|
---|
89 | : base(new RealVectorEncoding("Point")) {
|
---|
90 | Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2)));
|
---|
91 | 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[,] { { -100, 100 } })));
|
---|
92 | Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
|
---|
93 | Parameters.Add(new ValueParameter<ISingleObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Ackley()));
|
---|
94 |
|
---|
95 | Encoding.LengthParameter = ProblemSizeParameter;
|
---|
96 | Encoding.BoundsParameter = BoundsParameter;
|
---|
97 | BestKnownQuality = TestFunction.BestKnownQuality;
|
---|
98 |
|
---|
99 | InitializeOperators();
|
---|
100 | RegisterEventHandlers();
|
---|
101 | }
|
---|
102 |
|
---|
103 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
104 | return new SingleObjectiveTestFunctionProblem(this, cloner);
|
---|
105 | }
|
---|
106 |
|
---|
107 | [StorableHook(HookType.AfterDeserialization)]
|
---|
108 | private void AfterDeserialization() {
|
---|
109 | RegisterEventHandlers();
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void RegisterEventHandlers() {
|
---|
113 | Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
114 | TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
|
---|
115 | ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
|
---|
116 | BoundsParameter.ValueChanged += BoundsParameterOnValueChanged;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public override double Evaluate(RealVector individual, IRandom random) {
|
---|
120 | return TestFunction.Evaluate(individual);
|
---|
121 | }
|
---|
122 |
|
---|
123 | #region Events
|
---|
124 | protected override void OnEncodingChanged() {
|
---|
125 | base.OnEncodingChanged();
|
---|
126 | Parameterize();
|
---|
127 | }
|
---|
128 | protected override void OnEvaluatorChanged() {
|
---|
129 | base.OnEvaluatorChanged();
|
---|
130 | Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
131 | Parameterize();
|
---|
132 | }
|
---|
133 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
134 | Parameterize();
|
---|
135 | }
|
---|
136 | private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
137 | var problemSizeChange = ProblemSize < TestFunction.MinimumProblemSize
|
---|
138 | || ProblemSize > TestFunction.MaximumProblemSize;
|
---|
139 | if (problemSizeChange) {
|
---|
140 | ProblemSize = Math.Max(TestFunction.MinimumProblemSize, Math.Min(ProblemSize, TestFunction.MaximumProblemSize));
|
---|
141 | }
|
---|
142 | BestKnownQuality = TestFunction.BestKnownQuality;
|
---|
143 | Bounds = (DoubleMatrix)TestFunction.Bounds.Clone();
|
---|
144 | var bestSolution = TestFunction.GetBestKnownSolution(ProblemSize);
|
---|
145 | BestKnownSolutionParameter.Value = bestSolution;
|
---|
146 |
|
---|
147 | OnReset();
|
---|
148 | }
|
---|
149 | private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
150 | if (ProblemSize < TestFunction.MinimumProblemSize
|
---|
151 | || ProblemSize > TestFunction.MaximumProblemSize)
|
---|
152 | ProblemSize = Math.Min(TestFunction.MaximumProblemSize, Math.Max(TestFunction.MinimumProblemSize, ProblemSize));
|
---|
153 | }
|
---|
154 | private void BoundsParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
155 | Parameterize();
|
---|
156 | }
|
---|
157 | #endregion
|
---|
158 |
|
---|
159 | #region Helpers
|
---|
160 | private void InitializeOperators() {
|
---|
161 | Operators.Add(new SingleObjectiveTestFunctionImprovementOperator());
|
---|
162 | Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
|
---|
163 | Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
|
---|
164 | Operators.Add(new HammingSimilarityCalculator());
|
---|
165 | Operators.Add(new EuclideanSimilarityCalculator());
|
---|
166 | Operators.Add(new QualitySimilarityCalculator());
|
---|
167 | Operators.Add(new AdditiveMoveEvaluator());
|
---|
168 |
|
---|
169 | Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
|
---|
170 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
171 | Parameterize();
|
---|
172 | }
|
---|
173 |
|
---|
174 | private void Parameterize() {
|
---|
175 | var operators = new List<IItem>();
|
---|
176 | if (BestSingleObjectiveTestFunctionSolutionAnalyzer != null) {
|
---|
177 | operators.Add(BestSingleObjectiveTestFunctionSolutionAnalyzer);
|
---|
178 | BestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
179 | BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
180 | BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
181 | BestSingleObjectiveTestFunctionSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
182 | BestSingleObjectiveTestFunctionSolutionAnalyzer.TestFunctionParameter.ActualName = TestFunctionParameter.Name;
|
---|
183 | }
|
---|
184 | foreach (var op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
|
---|
185 | operators.Add(op);
|
---|
186 | op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
187 | op.QualityParameter.Hidden = true;
|
---|
188 | foreach (var movOp in Encoding.Operators.OfType<IRealVectorAdditiveMoveQualityOperator>())
|
---|
189 | movOp.MoveQualityParameter.ActualName = op.MoveQualityParameter.ActualName;
|
---|
190 | }
|
---|
191 | foreach (var op in Operators.OfType<IRealVectorParticleCreator>()) {
|
---|
192 | // TODO: unified encoding parameters
|
---|
193 | op.RealVectorParameter.ActualName = ((IRealVectorSolutionOperator)Encoding.SolutionCreator).RealVectorParameter.ActualName;
|
---|
194 | op.RealVectorParameter.Hidden = true;
|
---|
195 | op.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
196 | op.BoundsParameter.Hidden = true;
|
---|
197 | }
|
---|
198 | foreach (var op in Operators.OfType<IRealVectorParticleUpdater>()) {
|
---|
199 | // TODO: unified encoding parameters
|
---|
200 | op.RealVectorParameter.ActualName = ((IRealVectorSolutionOperator)Encoding.SolutionCreator).RealVectorParameter.ActualName;
|
---|
201 | op.RealVectorParameter.Hidden = true;
|
---|
202 | op.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
203 | op.BoundsParameter.Hidden = true;
|
---|
204 | }
|
---|
205 | foreach (var op in Operators.OfType<IRealVectorSwarmUpdater>()) {
|
---|
206 | op.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
207 | op.MaximizationParameter.Hidden = true;
|
---|
208 | }
|
---|
209 | foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
|
---|
210 | operators.Add(op);
|
---|
211 | op.SolutionParameter.ActualName = Encoding.Name;
|
---|
212 | op.SolutionParameter.Hidden = true;
|
---|
213 | }
|
---|
214 | foreach (var op in Operators.OfType<ITestFunctionSolutionSimilarityCalculator>()) {
|
---|
215 | operators.Add(op);
|
---|
216 | op.SolutionVariableName = Encoding.Name;
|
---|
217 | op.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
218 | op.Bounds = Bounds;
|
---|
219 | }
|
---|
220 |
|
---|
221 | if (operators.Count > 0) Encoding.ConfigureOperators(operators);
|
---|
222 | }
|
---|
223 | #endregion
|
---|
224 |
|
---|
225 | public void Load(SOTFData data) {
|
---|
226 | Name = data.Name;
|
---|
227 | Description = data.Description;
|
---|
228 | TestFunction = data.TestFunction;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|