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.SingleObjective {
|
---|
37 | [Item("SingleObjective TestFunction", "Test function with real valued inputs and a single objective.")]
|
---|
38 | [StorableClass]
|
---|
39 | [Creatable("Problems")]
|
---|
40 | public sealed class SingleObjectiveTestFunction : ParameterizedNamedItem, ISingleObjectiveProblem {
|
---|
41 | public override Image ItemImage {
|
---|
42 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | #region Parameter Properties
|
---|
46 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
47 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
48 | }
|
---|
49 | IParameter ISingleObjectiveProblem.MaximizationParameter {
|
---|
50 | get { return MaximizationParameter; }
|
---|
51 | }
|
---|
52 | public ValueParameter<DoubleMatrix> BoundsParameter {
|
---|
53 | get { return (ValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
54 | }
|
---|
55 | public ValueParameter<IntValue> ProblemSizeParameter {
|
---|
56 | get { return (ValueParameter<IntValue>)Parameters["ProblemSize"]; }
|
---|
57 | }
|
---|
58 | public ValueParameter<IRealVectorCreator> SolutionCreatorParameter {
|
---|
59 | get { return (ValueParameter<IRealVectorCreator>)Parameters["SolutionCreator"]; }
|
---|
60 | }
|
---|
61 | IParameter IProblem.SolutionCreatorParameter {
|
---|
62 | get { return SolutionCreatorParameter; }
|
---|
63 | }
|
---|
64 | public ValueParameter<ISingleObjectiveTestFunctionEvaluator> EvaluatorParameter {
|
---|
65 | get { return (ValueParameter<ISingleObjectiveTestFunctionEvaluator>)Parameters["Evaluator"]; }
|
---|
66 | }
|
---|
67 | IParameter IProblem.EvaluatorParameter {
|
---|
68 | get { return EvaluatorParameter; }
|
---|
69 | }
|
---|
70 | public OptionalValueParameter<ISingleObjectiveTestFunctionSolutionsVisualizer> VisualizerParameter {
|
---|
71 | get { return (OptionalValueParameter<ISingleObjectiveTestFunctionSolutionsVisualizer>)Parameters["Visualizer"]; }
|
---|
72 | }
|
---|
73 | IParameter IProblem.VisualizerParameter {
|
---|
74 | get { return VisualizerParameter; }
|
---|
75 | }
|
---|
76 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
77 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
78 | }
|
---|
79 | IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
|
---|
80 | get { return BestKnownQualityParameter; }
|
---|
81 | }
|
---|
82 | #endregion
|
---|
83 |
|
---|
84 | #region Properties
|
---|
85 | public BoolValue Maximization {
|
---|
86 | get { return MaximizationParameter.Value; }
|
---|
87 | set { MaximizationParameter.Value = value; }
|
---|
88 | }
|
---|
89 | public DoubleMatrix Bounds {
|
---|
90 | get { return BoundsParameter.Value; }
|
---|
91 | set { BoundsParameter.Value = value; }
|
---|
92 | }
|
---|
93 | public IntValue ProblemSize {
|
---|
94 | get { return ProblemSizeParameter.Value; }
|
---|
95 | set { ProblemSizeParameter.Value = value; }
|
---|
96 | }
|
---|
97 | public IRealVectorCreator SolutionCreator {
|
---|
98 | get { return SolutionCreatorParameter.Value; }
|
---|
99 | set { SolutionCreatorParameter.Value = value; }
|
---|
100 | }
|
---|
101 | ISolutionCreator IProblem.SolutionCreator {
|
---|
102 | get { return SolutionCreatorParameter.Value; }
|
---|
103 | }
|
---|
104 | public ISingleObjectiveTestFunctionEvaluator Evaluator {
|
---|
105 | get { return EvaluatorParameter.Value; }
|
---|
106 | set { EvaluatorParameter.Value = value; }
|
---|
107 | }
|
---|
108 | ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
|
---|
109 | get { return EvaluatorParameter.Value; }
|
---|
110 | }
|
---|
111 | IEvaluator IProblem.Evaluator {
|
---|
112 | get { return EvaluatorParameter.Value; }
|
---|
113 | }
|
---|
114 | public ISingleObjectiveTestFunctionSolutionsVisualizer Visualizer {
|
---|
115 | get { return VisualizerParameter.Value; }
|
---|
116 | set { VisualizerParameter.Value = value; }
|
---|
117 | }
|
---|
118 | ISolutionsVisualizer IProblem.Visualizer {
|
---|
119 | get { return VisualizerParameter.Value; }
|
---|
120 | }
|
---|
121 | public DoubleValue BestKnownQuality {
|
---|
122 | get { return BestKnownQualityParameter.Value; }
|
---|
123 | set { BestKnownQualityParameter.Value = value; }
|
---|
124 | }
|
---|
125 | private List<IRealVectorOperator> operators;
|
---|
126 | public IEnumerable<IOperator> Operators {
|
---|
127 | get { return operators.Cast<IOperator>(); }
|
---|
128 | }
|
---|
129 | #endregion
|
---|
130 |
|
---|
131 | [StorableConstructor]
|
---|
132 | private SingleObjectiveTestFunction(bool deserializing) : base() { }
|
---|
133 | public SingleObjectiveTestFunction()
|
---|
134 | : base() {
|
---|
135 | UniformRandomRealVectorCreator creator = new UniformRandomRealVectorCreator();
|
---|
136 | AckleyEvaluator evaluator = new AckleyEvaluator();
|
---|
137 |
|
---|
138 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(evaluator.Maximization)));
|
---|
139 | Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", evaluator.Bounds));
|
---|
140 | Parameters.Add(new ValueParameter<IntValue>("ProblemSize", "The dimension of the problem.", new IntValue(2)));
|
---|
141 | Parameters.Add(new ValueParameter<IRealVectorCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
|
---|
142 | Parameters.Add(new ValueParameter<ISingleObjectiveTestFunctionEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
|
---|
143 | Parameters.Add(new OptionalValueParameter<ISingleObjectiveTestFunctionSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize TSP solutions."));
|
---|
144 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance.", new DoubleValue(evaluator.BestKnownQuality)));
|
---|
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 | SingleObjectiveTestFunction clone = (SingleObjectiveTestFunction)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 VisualizerChanged;
|
---|
172 | private void OnVisualizerChanged() {
|
---|
173 | if (VisualizerChanged != null)
|
---|
174 | VisualizerChanged(this, EventArgs.Empty);
|
---|
175 | }
|
---|
176 | public event EventHandler OperatorsChanged;
|
---|
177 | private void OnOperatorsChanged() {
|
---|
178 | if (OperatorsChanged != null)
|
---|
179 | OperatorsChanged(this, EventArgs.Empty);
|
---|
180 | }
|
---|
181 | private void ProblemSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
182 | ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
|
---|
183 | ProblemSize_ValueChanged(null, EventArgs.Empty);
|
---|
184 | }
|
---|
185 | private void ProblemSize_ValueChanged(object sender, EventArgs e) {
|
---|
186 | ParameterizeSolutionCreator();
|
---|
187 | }
|
---|
188 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
189 | ParameterizeSolutionCreator();
|
---|
190 | SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
|
---|
191 | }
|
---|
192 | private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
193 | ParameterizeEvaluator();
|
---|
194 | ParameterizeOperators();
|
---|
195 | ParameterizeVisualizer();
|
---|
196 | }
|
---|
197 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
198 | ParameterizeEvaluator();
|
---|
199 | Maximization.Value = Evaluator.Maximization;
|
---|
200 | BoundsParameter.Value = Evaluator.Bounds;
|
---|
201 | if (ProblemSize.Value < Evaluator.MinimumProblemSize)
|
---|
202 | ProblemSize.Value = Evaluator.MinimumProblemSize;
|
---|
203 | else if (ProblemSize.Value > Evaluator.MaximumProblemSize)
|
---|
204 | ProblemSize.Value = Evaluator.MaximumProblemSize;
|
---|
205 | BestKnownQuality = new DoubleValue(Evaluator.BestKnownQuality);
|
---|
206 | Evaluator_QualityParameter_ActualNameChanged(null, EventArgs.Empty);
|
---|
207 | }
|
---|
208 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
209 | ParameterizeOperators();
|
---|
210 | }
|
---|
211 | private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
212 | ParameterizeVisualizer();
|
---|
213 | }
|
---|
214 | private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
215 | Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
|
---|
216 | Bounds_ToStringChanged(null, EventArgs.Empty);
|
---|
217 | }
|
---|
218 | private void Bounds_ToStringChanged(object sender, EventArgs e) {
|
---|
219 | if (Bounds.Columns != 2 || Bounds.Rows < 1)
|
---|
220 | Bounds = new DoubleMatrix(1, 2);
|
---|
221 | }
|
---|
222 | private void Bounds_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
223 | if (e.Value2 == 0 && Bounds[e.Value, 1] <= Bounds[e.Value, 0])
|
---|
224 | Bounds[e.Value, 1] = Bounds[e.Value, 0] + 0.1;
|
---|
225 | if (e.Value2 == 1 && Bounds[e.Value, 0] >= Bounds[e.Value, 1])
|
---|
226 | Bounds[e.Value, 0] = Bounds[e.Value, 1] - 0.1;
|
---|
227 | }
|
---|
228 | #endregion
|
---|
229 |
|
---|
230 | #region Helpers
|
---|
231 | [StorableHook(HookType.AfterDeserialization)]
|
---|
232 | private void Initialize() {
|
---|
233 | InitializeOperators();
|
---|
234 | ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
|
---|
235 | ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
|
---|
236 | BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
|
---|
237 | Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
|
---|
238 | Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged);
|
---|
239 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
240 | SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
|
---|
241 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
242 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
243 | VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
|
---|
244 | }
|
---|
245 |
|
---|
246 | private void InitializeOperators() {
|
---|
247 | operators = new List<IRealVectorOperator>();
|
---|
248 | if (ApplicationManager.Manager != null) {
|
---|
249 | operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>());
|
---|
250 | ParameterizeOperators();
|
---|
251 | }
|
---|
252 | //InitializeMoveGenerators();
|
---|
253 | }
|
---|
254 | /*private void InitializeMoveGenerators() {
|
---|
255 | foreach (ITwoOptPermutationMoveOperator op in Operators.OfType<ITwoOptPermutationMoveOperator>()) {
|
---|
256 | if (op is IMoveGenerator) {
|
---|
257 | op.TwoOptMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TwoOptMoveParameter_ActualNameChanged);
|
---|
258 | }
|
---|
259 | }
|
---|
260 | foreach (IThreeOptPermutationMoveOperator op in Operators.OfType<IThreeOptPermutationMoveOperator>()) {
|
---|
261 | if (op is IMoveGenerator) {
|
---|
262 | op.ThreeOptMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_ThreeOptMoveParameter_ActualNameChanged);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | }*/
|
---|
266 | private void ParameterizeSolutionCreator() {
|
---|
267 | SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
|
---|
268 | }
|
---|
269 | private void ParameterizeEvaluator() {
|
---|
270 | Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
271 | }
|
---|
272 | private void ParameterizeVisualizer() {
|
---|
273 | if (Visualizer != null) {
|
---|
274 | Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
275 | Visualizer.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
276 | }
|
---|
277 | }
|
---|
278 | private void ParameterizeOperators() {
|
---|
279 | foreach (IRealVectorCrossover op in Operators.OfType<IRealVectorCrossover>()) {
|
---|
280 | op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
281 | op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
282 | }
|
---|
283 | foreach (IRealVectorManipulator op in Operators.OfType<IRealVectorManipulator>()) {
|
---|
284 | op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
|
---|
285 | }
|
---|
286 | /*foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
|
---|
287 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
288 | }
|
---|
289 | foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
|
---|
290 | op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
291 | op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
292 | op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
293 | }*/
|
---|
294 | }
|
---|
295 | #endregion
|
---|
296 | }
|
---|
297 | }
|
---|