1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Collections.ObjectModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Linq;
|
---|
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.Persistence.Default.CompositeSerializers.Storable;
|
---|
34 | using HeuristicLab.PluginInfrastructure;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Problems.TestFunctions {
|
---|
37 | [Item("Single Objective Test Function", "Test function with real valued inputs and a single objective.")]
|
---|
38 | [StorableClass]
|
---|
39 | [Creatable("Problems")]
|
---|
40 | public sealed class SingleObjectiveTestFunctionProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
|
---|
41 | [Storable]
|
---|
42 | private StdDevStrategyVectorCreator strategyVectorCreator;
|
---|
43 | [Storable]
|
---|
44 | private StdDevStrategyVectorCrossover strategyVectorCrossover;
|
---|
45 | [Storable]
|
---|
46 | private StdDevStrategyVectorManipulator strategyVectorManipulator;
|
---|
47 |
|
---|
48 | public override Image ItemImage {
|
---|
49 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | #region Parameter Properties
|
---|
53 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
54 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
55 | }
|
---|
56 | IParameter ISingleObjectiveProblem.MaximizationParameter {
|
---|
57 | get { return MaximizationParameter; }
|
---|
58 | }
|
---|
59 | public ValueParameter<DoubleMatrix> BoundsParameter {
|
---|
60 | get { return (ValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
61 | }
|
---|
62 | public ValueParameter<IntValue> ProblemSizeParameter {
|
---|
63 | get { return (ValueParameter<IntValue>)Parameters["ProblemSize"]; }
|
---|
64 | }
|
---|
65 | public ValueParameter<IRealVectorCreator> SolutionCreatorParameter {
|
---|
66 | get { return (ValueParameter<IRealVectorCreator>)Parameters["SolutionCreator"]; }
|
---|
67 | }
|
---|
68 | IParameter IProblem.SolutionCreatorParameter {
|
---|
69 | get { return SolutionCreatorParameter; }
|
---|
70 | }
|
---|
71 | public ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
|
---|
72 | get { return (ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
|
---|
73 | }
|
---|
74 | IParameter IProblem.EvaluatorParameter {
|
---|
75 | get { return EvaluatorParameter; }
|
---|
76 | }
|
---|
77 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
78 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
79 | }
|
---|
80 | IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
|
---|
81 | get { return BestKnownQualityParameter; }
|
---|
82 | }
|
---|
83 | #endregion
|
---|
84 |
|
---|
85 | #region Properties
|
---|
86 | public BoolValue Maximization {
|
---|
87 | get { return MaximizationParameter.Value; }
|
---|
88 | set { MaximizationParameter.Value = value; }
|
---|
89 | }
|
---|
90 | public DoubleMatrix Bounds {
|
---|
91 | get { return BoundsParameter.Value; }
|
---|
92 | set { BoundsParameter.Value = value; }
|
---|
93 | }
|
---|
94 | public IntValue ProblemSize {
|
---|
95 | get { return ProblemSizeParameter.Value; }
|
---|
96 | set { ProblemSizeParameter.Value = value; }
|
---|
97 | }
|
---|
98 | public IRealVectorCreator SolutionCreator {
|
---|
99 | get { return SolutionCreatorParameter.Value; }
|
---|
100 | set { SolutionCreatorParameter.Value = value; }
|
---|
101 | }
|
---|
102 | ISolutionCreator IProblem.SolutionCreator {
|
---|
103 | get { return SolutionCreatorParameter.Value; }
|
---|
104 | }
|
---|
105 | public ISingleObjectiveTestFunctionProblemEvaluator Evaluator {
|
---|
106 | get { return EvaluatorParameter.Value; }
|
---|
107 | set { EvaluatorParameter.Value = value; }
|
---|
108 | }
|
---|
109 | ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
|
---|
110 | get { return EvaluatorParameter.Value; }
|
---|
111 | }
|
---|
112 | IEvaluator IProblem.Evaluator {
|
---|
113 | get { return EvaluatorParameter.Value; }
|
---|
114 | }
|
---|
115 | public DoubleValue BestKnownQuality {
|
---|
116 | get { return BestKnownQualityParameter.Value; }
|
---|
117 | set { BestKnownQualityParameter.Value = value; }
|
---|
118 | }
|
---|
119 | private List<IOperator> operators;
|
---|
120 | public IEnumerable<IOperator> Operators {
|
---|
121 | get { return operators; }
|
---|
122 | }
|
---|
123 | #endregion
|
---|
124 |
|
---|
125 | [StorableConstructor]
|
---|
126 | private SingleObjectiveTestFunctionProblem(bool deserializing) : base() { }
|
---|
127 | public SingleObjectiveTestFunctionProblem()
|
---|
128 | : base() {
|
---|
129 | UniformRandomRealVectorCreator creator = new UniformRandomRealVectorCreator();
|
---|
130 | AckleyEvaluator evaluator = new AckleyEvaluator();
|
---|
131 |
|
---|
132 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(evaluator.Maximization)));
|
---|
133 | Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", evaluator.Bounds));
|
---|
134 | Parameters.Add(new ValueParameter<IntValue>("ProblemSize", "The dimension of the problem.", new IntValue(2)));
|
---|
135 | Parameters.Add(new ValueParameter<IRealVectorCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
|
---|
136 | Parameters.Add(new ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
|
---|
137 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance.", new DoubleValue(evaluator.BestKnownQuality)));
|
---|
138 |
|
---|
139 | strategyVectorCreator = new StdDevStrategyVectorCreator();
|
---|
140 | strategyVectorCreator.LengthParameter.ActualName = ProblemSizeParameter.Name;
|
---|
141 | strategyVectorCrossover = new StdDevStrategyVectorCrossover();
|
---|
142 | strategyVectorManipulator = new StdDevStrategyVectorManipulator();
|
---|
143 | strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
|
---|
144 | strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
|
---|
145 |
|
---|
146 | creator.RealVectorParameter.ActualName = "Point";
|
---|
147 | ParameterizeSolutionCreator();
|
---|
148 | ParameterizeEvaluator();
|
---|
149 | ParameterizeVisualizer();
|
---|
150 |
|
---|
151 | Initialize();
|
---|
152 | }
|
---|
153 |
|
---|
154 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
155 | SingleObjectiveTestFunctionProblem clone = (SingleObjectiveTestFunctionProblem)base.Clone(cloner);
|
---|
156 | clone.Initialize();
|
---|
157 | return clone;
|
---|
158 | }
|
---|
159 |
|
---|
160 | #region Events
|
---|
161 | public event EventHandler SolutionCreatorChanged;
|
---|
162 | private void OnSolutionCreatorChanged() {
|
---|
163 | if (SolutionCreatorChanged != null)
|
---|
164 | SolutionCreatorChanged(this, EventArgs.Empty);
|
---|
165 | }
|
---|
166 | public event EventHandler EvaluatorChanged;
|
---|
167 | private void OnEvaluatorChanged() {
|
---|
168 | if (EvaluatorChanged != null)
|
---|
169 | EvaluatorChanged(this, EventArgs.Empty);
|
---|
170 | }
|
---|
171 | public event EventHandler OperatorsChanged;
|
---|
172 | private void OnOperatorsChanged() {
|
---|
173 | if (OperatorsChanged != null)
|
---|
174 | OperatorsChanged(this, EventArgs.Empty);
|
---|
175 | }
|
---|
176 | private void ProblemSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
177 | ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
|
---|
178 | ProblemSize_ValueChanged(null, EventArgs.Empty);
|
---|
179 | }
|
---|
180 | private void ProblemSize_ValueChanged(object sender, EventArgs e) {
|
---|
181 | if (ProblemSize.Value < 1) ProblemSize.Value = 1;
|
---|
182 | ParameterizeSolutionCreator();
|
---|
183 | strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize.Value));
|
---|
184 | strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize.Value)));
|
---|
185 | }
|
---|
186 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
187 | ParameterizeSolutionCreator();
|
---|
188 | SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
|
---|
189 | }
|
---|
190 | private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
191 | ParameterizeEvaluator();
|
---|
192 | ParameterizeOperators();
|
---|
193 | ParameterizeVisualizer();
|
---|
194 | }
|
---|
195 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
196 | ParameterizeEvaluator();
|
---|
197 | UpdateMoveEvaluators();
|
---|
198 | Maximization.Value = Evaluator.Maximization;
|
---|
199 | BoundsParameter.Value = Evaluator.Bounds;
|
---|
200 | if (ProblemSize.Value < Evaluator.MinimumProblemSize)
|
---|
201 | ProblemSize.Value = Evaluator.MinimumProblemSize;
|
---|
202 | else if (ProblemSize.Value > Evaluator.MaximumProblemSize)
|
---|
203 | ProblemSize.Value = Evaluator.MaximumProblemSize;
|
---|
204 | BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
|
---|
205 | Evaluator_QualityParameter_ActualNameChanged(null, EventArgs.Empty);
|
---|
206 | }
|
---|
207 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
208 | ParameterizeOperators();
|
---|
209 | }
|
---|
210 | private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
211 | Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
|
---|
212 | Bounds_ToStringChanged(null, EventArgs.Empty);
|
---|
213 | }
|
---|
214 | private void Bounds_ToStringChanged(object sender, EventArgs e) {
|
---|
215 | if (Bounds.Columns != 2 || Bounds.Rows < 1)
|
---|
216 | Bounds = new DoubleMatrix(1, 2);
|
---|
217 | }
|
---|
218 | private void Bounds_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
219 | if (e.Value2 == 0 && Bounds[e.Value, 1] <= Bounds[e.Value, 0])
|
---|
220 | Bounds[e.Value, 1] = Bounds[e.Value, 0] + 0.1;
|
---|
221 | if (e.Value2 == 1 && Bounds[e.Value, 0] >= Bounds[e.Value, 1])
|
---|
222 | Bounds[e.Value, 0] = Bounds[e.Value, 1] - 0.1;
|
---|
223 | }
|
---|
224 | private void MoveGenerator_AdditiveMoveParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
225 | string name = ((ILookupParameter<AdditiveMove>)sender).ActualName;
|
---|
226 | foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
|
---|
227 | op.AdditiveMoveParameter.ActualName = name;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | private void SphereEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
|
---|
231 | SphereEvaluator eval = (Evaluator as SphereEvaluator);
|
---|
232 | if (eval != null) {
|
---|
233 | foreach (ISphereMoveEvaluator op in Operators.OfType<ISphereMoveEvaluator>()) {
|
---|
234 | op.C = eval.C;
|
---|
235 | op.Alpha = eval.Alpha;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 | private void RastriginEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
|
---|
240 | RastriginEvaluator eval = (Evaluator as RastriginEvaluator);
|
---|
241 | if (eval != null) {
|
---|
242 | foreach (IRastriginMoveEvaluator op in Operators.OfType<IRastriginMoveEvaluator>()) {
|
---|
243 | op.A = eval.A;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 | private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
248 | strategyVectorManipulator.BoundsParameter.Value = strategyVectorCreator.BoundsParameter.Value;
|
---|
249 | }
|
---|
250 | private void strategyVectorCreator_StrategyParameterParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
251 | string name = strategyVectorCreator.StrategyParameterParameter.ActualName;
|
---|
252 | strategyVectorCrossover.ParentsParameter.ActualName = name;
|
---|
253 | strategyVectorCrossover.StrategyParameterParameter.ActualName = name;
|
---|
254 | strategyVectorManipulator.StrategyParameterParameter.ActualName = name;
|
---|
255 | }
|
---|
256 | #endregion
|
---|
257 |
|
---|
258 | #region Helpers
|
---|
259 | [StorableHook(HookType.AfterDeserialization)]
|
---|
260 | private void Initialize() {
|
---|
261 | InitializeOperators();
|
---|
262 | ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
|
---|
263 | ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
|
---|
264 | BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
|
---|
265 | Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
|
---|
266 | Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
|
---|
267 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
268 | SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
|
---|
269 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
270 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
271 | strategyVectorCreator.BoundsParameter.ValueChanged += new EventHandler(strategyVectorCreator_BoundsParameter_ValueChanged);
|
---|
272 | strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
|
---|
273 | }
|
---|
274 | private void InitializeOperators() {
|
---|
275 | operators = new List<IOperator>();
|
---|
276 | operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());
|
---|
277 | operators.Add(strategyVectorCreator);
|
---|
278 | operators.Add(strategyVectorCrossover);
|
---|
279 | operators.Add(strategyVectorManipulator);
|
---|
280 | UpdateMoveEvaluators();
|
---|
281 | ParameterizeOperators();
|
---|
282 | InitializeMoveGenerators();
|
---|
283 | }
|
---|
284 | private void InitializeMoveGenerators() {
|
---|
285 | foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType<IAdditiveRealVectorMoveOperator>()) {
|
---|
286 | if (op is IMoveGenerator) {
|
---|
287 | op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 | private void UpdateMoveEvaluators() {
|
---|
292 | foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList())
|
---|
293 | operators.Remove(op);
|
---|
294 | foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>())
|
---|
295 | if (op.EvaluatorType == Evaluator.GetType()) {
|
---|
296 | operators.Add(op);
|
---|
297 | #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
|
---|
298 | if (op is ISphereMoveEvaluator) {
|
---|
299 | SphereEvaluator e = (Evaluator as SphereEvaluator);
|
---|
300 | e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
|
---|
301 | e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
|
---|
302 | ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
|
---|
303 | em.C = e.C;
|
---|
304 | em.Alpha = e.Alpha;
|
---|
305 | } else if (op is IRastriginMoveEvaluator) {
|
---|
306 | RastriginEvaluator e = (Evaluator as RastriginEvaluator);
|
---|
307 | e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
|
---|
308 | IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
|
---|
309 | em.A = e.A;
|
---|
310 | }
|
---|
311 | #endregion
|
---|
312 | }
|
---|
313 | ParameterizeOperators();
|
---|
314 | OnOperatorsChanged();
|
---|
315 | }
|
---|
316 | private void ParameterizeSolutionCreator() {
|
---|
317 | SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
|
---|
318 | }
|
---|
319 | private void ParameterizeEvaluator() {
|
---|
320 | Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
321 | }
|
---|
322 | private void ParameterizeVisualizer() {
|
---|
323 | //if (Visualizer != null) {
|
---|
324 | // Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
325 | // Visualizer.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
326 | //}
|
---|
327 | }
|
---|
328 | private void ParameterizeOperators() {
|
---|
329 | foreach (IRealVectorCrossover op in Operators.OfType<IRealVectorCrossover>()) {
|
---|
330 | op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
331 | op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
332 | op.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
333 | }
|
---|
334 | foreach (IRealVectorManipulator op in Operators.OfType<IRealVectorManipulator>()) {
|
---|
335 | op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
336 | op.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
337 | }
|
---|
338 | foreach (IRealVectorMoveOperator op in Operators.OfType<IRealVectorMoveOperator>()) {
|
---|
339 | op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
340 | }
|
---|
341 | foreach (IRealVectorMoveGenerator op in Operators.OfType<IRealVectorMoveGenerator>()) {
|
---|
342 | op.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
343 | }
|
---|
344 | foreach (ISingleObjectiveTestFunctionAdditiveMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
|
---|
345 | op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
346 | op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
347 | }
|
---|
348 | }
|
---|
349 | #endregion
|
---|
350 | }
|
---|
351 | }
|
---|