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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Threading;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HeuristicLab.Problems.Instances;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.TestFunctions {
|
---|
36 | [Item("Test Function (single-objective)", "Test function with real valued inputs and a single objective.")]
|
---|
37 | [StorableType("F0AB7236-2C9B-49DC-9D4F-A3558FD9E992")]
|
---|
38 | [Creatable(CreatableAttribute.Categories.Problems, Priority = 90)]
|
---|
39 | public sealed class SingleObjectiveTestFunctionProblem : RealVectorProblem,
|
---|
40 | IProblemInstanceConsumer<SOTFData> {
|
---|
41 |
|
---|
42 | #region Parameter Properties
|
---|
43 | public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
|
---|
44 | get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
|
---|
45 | }
|
---|
46 | public IValueParameter<ISingleObjectiveTestFunction> TestFunctionParameter {
|
---|
47 | get { return (IValueParameter<ISingleObjectiveTestFunction>)Parameters["TestFunction"]; }
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | #region Properties
|
---|
52 | public ISingleObjectiveTestFunction TestFunction {
|
---|
53 | get { return TestFunctionParameter.Value; }
|
---|
54 | set { TestFunctionParameter.Value = value; }
|
---|
55 | }
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | private SingleObjectiveTestFunctionProblem(StorableConstructorFlag _) : base(_) { }
|
---|
60 | private SingleObjectiveTestFunctionProblem(SingleObjectiveTestFunctionProblem original, Cloner cloner)
|
---|
61 | : base(original, cloner) {
|
---|
62 | RegisterEventHandlers();
|
---|
63 | }
|
---|
64 | public SingleObjectiveTestFunctionProblem()
|
---|
65 | : base(new RealVectorEncoding("Point")) {
|
---|
66 | Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
|
---|
67 | Parameters.Add(new ValueParameter<ISingleObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Ackley()));
|
---|
68 | Maximization = TestFunction.Maximization;
|
---|
69 |
|
---|
70 | BestKnownQuality = TestFunction.BestKnownQuality;
|
---|
71 | Bounds = (DoubleMatrix)TestFunction.Bounds.Clone();
|
---|
72 | Dimension = Math.Min(Math.Max(2, TestFunction.MinimumProblemSize), TestFunction.MaximumProblemSize);
|
---|
73 | BestKnownSolutionParameter.Value = TestFunction.GetBestKnownSolution(Dimension);
|
---|
74 |
|
---|
75 |
|
---|
76 | Operators.AddRange(new IItem[] {
|
---|
77 | new SingleObjectiveTestFunctionImprovementOperator(),
|
---|
78 | new SingleObjectiveTestFunctionPathRelinker(),
|
---|
79 | new SingleObjectiveTestFunctionSimilarityCalculator(),
|
---|
80 | new EuclideanSimilarityCalculator(),
|
---|
81 | new AdditiveMoveEvaluator() });
|
---|
82 |
|
---|
83 | SolutionCreatorParameter.SetValueToFirstOf(typeof(UniformRandomRealVectorCreator));
|
---|
84 |
|
---|
85 | Parameterize();
|
---|
86 | RegisterEventHandlers();
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
90 | return new SingleObjectiveTestFunctionProblem(this, cloner);
|
---|
91 | }
|
---|
92 |
|
---|
93 | [StorableHook(HookType.AfterDeserialization)]
|
---|
94 | private void AfterDeserialization() {
|
---|
95 | RegisterEventHandlers();
|
---|
96 | }
|
---|
97 |
|
---|
98 | private void RegisterEventHandlers() {
|
---|
99 | TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public override ISingleObjectiveEvaluationResult Evaluate(RealVector individual, IRandom random, CancellationToken cancellationToken) {
|
---|
103 | var quality = TestFunction.Evaluate(individual);
|
---|
104 | return new SingleObjectiveEvaluationResult(quality);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public override void Analyze(RealVector[] realVectors, double[] qualities, ResultCollection results, IRandom random) {
|
---|
108 | var best = GetBestSolution(realVectors, qualities);
|
---|
109 |
|
---|
110 | DoubleValue bestKnownQuality = BestKnownQualityParameter.Value;
|
---|
111 | RealVector bestKnownSolution = null;
|
---|
112 | var bestKnownUpdate = bestKnownQuality == null || IsBetter(best.Item2, bestKnownQuality.Value);
|
---|
113 | if (bestKnownUpdate) {
|
---|
114 | if (bestKnownQuality != null) bestKnownQuality.Value = best.Item2;
|
---|
115 | else BestKnownQualityParameter.Value = bestKnownQuality = new DoubleValue(best.Item2);
|
---|
116 | BestKnownSolutionParameter.Value = bestKnownSolution = (RealVector)best.Item1.Clone();
|
---|
117 | }
|
---|
118 |
|
---|
119 | SingleObjectiveTestFunctionSolution solution = null;
|
---|
120 | if (results.TryGetValue("Best Solution", out var res) && res.Value != null) {
|
---|
121 | solution = (SingleObjectiveTestFunctionSolution)res.Value;
|
---|
122 | if (IsBetter(best.Item2, solution.BestQuality.Value)) {
|
---|
123 | solution.BestRealVector = (RealVector)best.Item1.Clone();
|
---|
124 | solution.BestQuality = new DoubleValue(best.Item2);
|
---|
125 | }
|
---|
126 | } else {
|
---|
127 | solution = new SingleObjectiveTestFunctionSolution((RealVector)best.Item1.Clone(),
|
---|
128 | new DoubleValue(best.Item2),
|
---|
129 | TestFunctionParameter.Value) {
|
---|
130 | BestKnownRealVector = bestKnownSolution,
|
---|
131 | Bounds = BoundsRefParameter.Value
|
---|
132 | };
|
---|
133 | results.AddOrUpdateResult("Best Solution", solution);
|
---|
134 | }
|
---|
135 | if (best.Item1.Length == 2) solution.Population = new ItemArray<RealVector>(realVectors.Select(x => (RealVector)x.Clone()));
|
---|
136 | if (bestKnownUpdate) solution.BestKnownRealVector = bestKnownSolution;
|
---|
137 | }
|
---|
138 |
|
---|
139 | #region Events
|
---|
140 | protected override void ParameterizeOperators() {
|
---|
141 | base.ParameterizeOperators();
|
---|
142 | Parameterize();
|
---|
143 | }
|
---|
144 | protected override void DimensionOnChanged() {
|
---|
145 | base.DimensionOnChanged();
|
---|
146 | if (Dimension < TestFunction.MinimumProblemSize || Dimension > TestFunction.MaximumProblemSize)
|
---|
147 | Dimension = Math.Min(TestFunction.MaximumProblemSize, Math.Max(TestFunction.MinimumProblemSize, Dimension));
|
---|
148 | }
|
---|
149 | protected override void BoundsOnChanged() {
|
---|
150 | base.BoundsOnChanged();
|
---|
151 | Parameterize();
|
---|
152 | }
|
---|
153 | private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
154 | var problemSizeChange = Dimension < TestFunction.MinimumProblemSize
|
---|
155 | || Dimension > TestFunction.MaximumProblemSize;
|
---|
156 | if (problemSizeChange) {
|
---|
157 | Dimension = Math.Max(TestFunction.MinimumProblemSize, Math.Min(Dimension, TestFunction.MaximumProblemSize));
|
---|
158 | }
|
---|
159 | BestKnownQuality = TestFunction.BestKnownQuality;
|
---|
160 | Bounds = (DoubleMatrix)TestFunction.Bounds.Clone();
|
---|
161 | var bestSolution = TestFunction.GetBestKnownSolution(Dimension);
|
---|
162 | BestKnownSolutionParameter.Value = bestSolution;
|
---|
163 | Maximization = TestFunction.Maximization;
|
---|
164 |
|
---|
165 | OnReset();
|
---|
166 | }
|
---|
167 | #endregion
|
---|
168 |
|
---|
169 | #region Helpers
|
---|
170 | private void Parameterize() {
|
---|
171 | var operators = new List<IItem>();
|
---|
172 |
|
---|
173 | foreach (var op in Operators.OfType<ITestFunctionSolutionSimilarityCalculator>()) {
|
---|
174 | operators.Add(op);
|
---|
175 | op.Bounds = Bounds;
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (operators.Count > 0) Encoding.ConfigureOperators(operators);
|
---|
179 | }
|
---|
180 | #endregion
|
---|
181 |
|
---|
182 | public void Load(SOTFData data) {
|
---|
183 | Name = data.Name;
|
---|
184 | Description = data.Description;
|
---|
185 | TestFunction = data.TestFunction;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|