1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Optimization.Operators;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 | using HeuristicLab.Random;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.SimulatedAnnealing
|
---|
37 | {
|
---|
38 | [Item("Simulated Annealing", "An advanced simulated annealing algorithm.")]
|
---|
39 | [Creatable("Algorithms")]
|
---|
40 | [StorableClass]
|
---|
41 | public sealed class SimulatedAnnealing : HeuristicOptimizationEngineAlgorithm, IStorableContent
|
---|
42 | {
|
---|
43 | #region Strings
|
---|
44 | private const string SeedName = "Seed";
|
---|
45 | private const string SetSeedRandomlyName = "SetSeedRandomly";
|
---|
46 | private const string MoveGeneratorName = "MoveGenerator";
|
---|
47 | private const string MoveEvaluatorName = "MoveEvaluator";
|
---|
48 | private const string MoveMakerName = "MoveMaker";
|
---|
49 | private const string AnnealingOperatorName = "AnnealingOperator";
|
---|
50 | private const string ReheatingOperatorName = "ReheatingOperator";
|
---|
51 | private const string MaximumIterationsName = "MaximumIterations";
|
---|
52 | private const string InitialTemperatureName = "InitialTemperature";
|
---|
53 | private const string LowerTemperatureName = "LowerTemperature";
|
---|
54 | private const string AnalyzerName = "Analyzer";
|
---|
55 | private const string RandomName = "Random";
|
---|
56 | private const string EvaluatedMovesName = "EvaluatedMoves";
|
---|
57 | private const string IterationsName = "Iterations";
|
---|
58 | private const string TemperatureStartIndexName = "TemperatureStartIndex";
|
---|
59 | private const string CoolingName = "Cooling";
|
---|
60 | private const string StartTemperatureName = "StartTemperature";
|
---|
61 | private const string EndTemperatureName = "EndTemperature";
|
---|
62 | private const string TemperatureName = "Temperature";
|
---|
63 | private const string ResultsName = "Results";
|
---|
64 | private const string TemperatureChartName = "Temperature Chart";
|
---|
65 | private const string TemperatureAnalyzerName = "Temperature Analyzer";
|
---|
66 | #endregion
|
---|
67 |
|
---|
68 | public string Filename { get; set; }
|
---|
69 |
|
---|
70 | #region Problem Properties
|
---|
71 | public override Type ProblemType
|
---|
72 | {
|
---|
73 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
74 | }
|
---|
75 | public new ISingleObjectiveHeuristicOptimizationProblem Problem
|
---|
76 | {
|
---|
77 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
78 | set { base.Problem = value; }
|
---|
79 | }
|
---|
80 | #endregion
|
---|
81 |
|
---|
82 | #region Parameter Properties
|
---|
83 | private ValueParameter<IntValue> SeedParameter
|
---|
84 | {
|
---|
85 | get { return (ValueParameter<IntValue>)Parameters[SeedName]; }
|
---|
86 | }
|
---|
87 | private ValueParameter<BoolValue> SetSeedRandomlyParameter
|
---|
88 | {
|
---|
89 | get { return (ValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; }
|
---|
90 | }
|
---|
91 | public IConstrainedValueParameter<IMultiMoveGenerator> MoveGeneratorParameter
|
---|
92 | {
|
---|
93 | get { return (IConstrainedValueParameter<IMultiMoveGenerator>)Parameters[MoveGeneratorName]; }
|
---|
94 | }
|
---|
95 | public IConstrainedValueParameter<IMoveMaker> MoveMakerParameter
|
---|
96 | {
|
---|
97 | get { return (IConstrainedValueParameter<IMoveMaker>)Parameters[MoveMakerName]; }
|
---|
98 | }
|
---|
99 | public IConstrainedValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter
|
---|
100 | {
|
---|
101 | get { return (IConstrainedValueParameter<ISingleObjectiveMoveEvaluator>)Parameters[MoveEvaluatorName]; }
|
---|
102 | }
|
---|
103 | public IConstrainedValueParameter<IDiscreteDoubleValueModifier> AnnealingOperatorParameter
|
---|
104 | {
|
---|
105 | get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters[AnnealingOperatorName]; }
|
---|
106 | }
|
---|
107 |
|
---|
108 | public IConstrainedValueParameter<IReheatingOperator> ReheatingOperatorParameter
|
---|
109 | {
|
---|
110 | get { return (IConstrainedValueParameter<IReheatingOperator>)Parameters[ReheatingOperatorName]; }
|
---|
111 | }
|
---|
112 |
|
---|
113 | private ValueParameter<IntValue> MaximumIterationsParameter
|
---|
114 | {
|
---|
115 | get { return (ValueParameter<IntValue>)Parameters[MaximumIterationsName]; }
|
---|
116 | }
|
---|
117 | private ValueParameter<DoubleValue> InitialTemperatureParameter
|
---|
118 | {
|
---|
119 | get { return (ValueParameter<DoubleValue>)Parameters[InitialTemperatureName]; }
|
---|
120 | }
|
---|
121 | private ValueParameter<DoubleValue> LowerTemperatureParameter
|
---|
122 | {
|
---|
123 | get { return (ValueParameter<DoubleValue>)Parameters[LowerTemperatureName]; }
|
---|
124 | }
|
---|
125 | private ValueParameter<MultiAnalyzer> AnalyzerParameter
|
---|
126 | {
|
---|
127 | get { return (ValueParameter<MultiAnalyzer>)Parameters[AnalyzerName]; }
|
---|
128 | }
|
---|
129 | #endregion
|
---|
130 |
|
---|
131 | #region Properties
|
---|
132 | public IntValue Seed
|
---|
133 | {
|
---|
134 | get { return SeedParameter.Value; }
|
---|
135 | set { SeedParameter.Value = value; }
|
---|
136 | }
|
---|
137 | public BoolValue SetSeedRandomly
|
---|
138 | {
|
---|
139 | get { return SetSeedRandomlyParameter.Value; }
|
---|
140 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
141 | }
|
---|
142 | public IMultiMoveGenerator MoveGenerator
|
---|
143 | {
|
---|
144 | get { return MoveGeneratorParameter.Value; }
|
---|
145 | set { MoveGeneratorParameter.Value = value; }
|
---|
146 | }
|
---|
147 | public IMoveMaker MoveMaker
|
---|
148 | {
|
---|
149 | get { return MoveMakerParameter.Value; }
|
---|
150 | set { MoveMakerParameter.Value = value; }
|
---|
151 | }
|
---|
152 | public ISingleObjectiveMoveEvaluator MoveEvaluator
|
---|
153 | {
|
---|
154 | get { return MoveEvaluatorParameter.Value; }
|
---|
155 | set { MoveEvaluatorParameter.Value = value; }
|
---|
156 | }
|
---|
157 | public IDiscreteDoubleValueModifier AnnealingOperator
|
---|
158 | {
|
---|
159 | get { return AnnealingOperatorParameter.Value; }
|
---|
160 | set { AnnealingOperatorParameter.Value = value; }
|
---|
161 | }
|
---|
162 |
|
---|
163 | public IReheatingOperator ReheatingOperator
|
---|
164 | {
|
---|
165 | get { return ReheatingOperatorParameter.Value; }
|
---|
166 | set { ReheatingOperatorParameter.Value = value; }
|
---|
167 | }
|
---|
168 |
|
---|
169 | public IntValue MaximumIterations
|
---|
170 | {
|
---|
171 | get { return MaximumIterationsParameter.Value; }
|
---|
172 | set { MaximumIterationsParameter.Value = value; }
|
---|
173 | }
|
---|
174 | public DoubleValue InitialTemperature
|
---|
175 | {
|
---|
176 | get { return InitialTemperatureParameter.Value; }
|
---|
177 | set { InitialTemperatureParameter.Value = value; }
|
---|
178 | }
|
---|
179 | public DoubleValue LowerTemperature
|
---|
180 | {
|
---|
181 | get { return LowerTemperatureParameter.Value; }
|
---|
182 | set { LowerTemperatureParameter.Value = value; }
|
---|
183 | }
|
---|
184 | public MultiAnalyzer Analyzer
|
---|
185 | {
|
---|
186 | get { return AnalyzerParameter.Value; }
|
---|
187 | set { AnalyzerParameter.Value = value; }
|
---|
188 | }
|
---|
189 | private RandomCreator RandomCreator
|
---|
190 | {
|
---|
191 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
192 | }
|
---|
193 | private SolutionsCreator SolutionsCreator
|
---|
194 | {
|
---|
195 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
196 | }
|
---|
197 | private SimulatedAnnealingMainLoop MainLoop
|
---|
198 | {
|
---|
199 | get { return OperatorGraph.Iterate().OfType<SimulatedAnnealingMainLoop>().First(); }
|
---|
200 | }
|
---|
201 | #endregion
|
---|
202 |
|
---|
203 | [StorableConstructor]
|
---|
204 | private SimulatedAnnealing(bool deserializing) : base(deserializing) { }
|
---|
205 | private SimulatedAnnealing(SimulatedAnnealing original, Cloner cloner)
|
---|
206 | : base(original, cloner)
|
---|
207 | {
|
---|
208 | RegisterEventHandlers();
|
---|
209 | }
|
---|
210 | public SimulatedAnnealing()
|
---|
211 | : base()
|
---|
212 | {
|
---|
213 | Parameters.Add(new ValueParameter<IntValue>(SeedName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
214 | Parameters.Add(new ValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
215 | Parameters.Add(new ConstrainedValueParameter<IMultiMoveGenerator>(MoveGeneratorName, "The operator used to generate moves to the neighborhood of the current solution."));
|
---|
216 | Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>(MoveEvaluatorName, "The operator used to evaluate a move."));
|
---|
217 | Parameters.Add(new ConstrainedValueParameter<IMoveMaker>(MoveMakerName, "The operator used to perform a move."));
|
---|
218 | Parameters.Add(new ConstrainedValueParameter<IDiscreteDoubleValueModifier>(AnnealingOperatorName, "The operator used to cool the temperature."));
|
---|
219 | Parameters.Add(new ConstrainedValueParameter<IReheatingOperator>(ReheatingOperatorName, "The operator used to reheat the temperature, if necessary."));
|
---|
220 | Parameters.Add(new ValueParameter<IntValue>(MaximumIterationsName, "The maximum number of generations which should be processed.", new IntValue(10000)));
|
---|
221 | Parameters.Add(new ValueParameter<DoubleValue>(InitialTemperatureName, "The initial temperature.", new DoubleValue(100)));
|
---|
222 | Parameters.Add(new ValueParameter<DoubleValue>(LowerTemperatureName, "The lower bound for the temperature.", new DoubleValue(1e-6)));
|
---|
223 | Parameters.Add(new ValueParameter<MultiAnalyzer>(AnalyzerName, "The operator used to analyze each iteration.", new MultiAnalyzer()));
|
---|
224 |
|
---|
225 | var randomCreator = new RandomCreator();
|
---|
226 | var solutionsCreator = new SolutionsCreator();
|
---|
227 | var variableCreator = new VariableCreator();
|
---|
228 | var startTemperatureAssigner = new Assigner();
|
---|
229 | var endTemperatureAssigner = new Assigner();
|
---|
230 | var temperatureAssigner = new Assigner();
|
---|
231 | var resultsCollector = new ResultsCollector();
|
---|
232 | var mainLoop = new SimulatedAnnealingMainLoop();
|
---|
233 | OperatorGraph.InitialOperator = randomCreator;
|
---|
234 |
|
---|
235 | randomCreator.RandomParameter.ActualName = RandomName;
|
---|
236 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
237 | randomCreator.SeedParameter.Value = null;
|
---|
238 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
239 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
240 | randomCreator.Successor = solutionsCreator;
|
---|
241 |
|
---|
242 | solutionsCreator.NumberOfSolutions = new IntValue(1);
|
---|
243 | solutionsCreator.Successor = variableCreator;
|
---|
244 |
|
---|
245 | variableCreator.Name = "Initialize Variables";
|
---|
246 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(EvaluatedMovesName, new IntValue()));
|
---|
247 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(IterationsName, new IntValue(0)));
|
---|
248 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(TemperatureStartIndexName, new IntValue(0)));
|
---|
249 | variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>(CoolingName, new BoolValue(true)));
|
---|
250 | variableCreator.Successor = startTemperatureAssigner;
|
---|
251 |
|
---|
252 | startTemperatureAssigner.Name = "Assign Start Temperature";
|
---|
253 | startTemperatureAssigner.LeftSideParameter.ActualName = StartTemperatureName;
|
---|
254 | startTemperatureAssigner.RightSideParameter.ActualName = InitialTemperatureParameter.Name;
|
---|
255 | startTemperatureAssigner.Successor = endTemperatureAssigner;
|
---|
256 |
|
---|
257 | endTemperatureAssigner.Name = "Assign End Temperature";
|
---|
258 | endTemperatureAssigner.LeftSideParameter.ActualName = EndTemperatureName;
|
---|
259 | endTemperatureAssigner.RightSideParameter.ActualName = LowerTemperatureParameter.Name;
|
---|
260 | endTemperatureAssigner.Successor = temperatureAssigner;
|
---|
261 |
|
---|
262 | temperatureAssigner.Name = "Initialize Temperature";
|
---|
263 | temperatureAssigner.LeftSideParameter.ActualName = TemperatureName;
|
---|
264 | temperatureAssigner.RightSideParameter.ActualName = StartTemperatureName;
|
---|
265 | temperatureAssigner.Successor = resultsCollector;
|
---|
266 |
|
---|
267 | resultsCollector.CopyValue = new BoolValue(false);
|
---|
268 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>(EvaluatedMovesName, null));
|
---|
269 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>(IterationsName, null));
|
---|
270 | resultsCollector.ResultsParameter.ActualName = ResultsName;
|
---|
271 | resultsCollector.Successor = mainLoop;
|
---|
272 |
|
---|
273 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
274 | mainLoop.AnnealingOperatorParameter.ActualName = AnnealingOperatorParameter.Name;
|
---|
275 | mainLoop.CoolingParameter.ActualName = CoolingName;
|
---|
276 | mainLoop.EndTemperatureParameter.ActualName = EndTemperatureName;
|
---|
277 | mainLoop.EvaluatedMovesParameter.ActualName = EvaluatedMovesName;
|
---|
278 |
|
---|
279 | mainLoop.IterationsParameter.ActualName = IterationsName;
|
---|
280 | mainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
281 | mainLoop.MoveEvaluatorParameter.ActualName = MoveEvaluatorParameter.Name;
|
---|
282 | mainLoop.MoveGeneratorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
283 | mainLoop.MoveMakerParameter.ActualName = MoveMakerParameter.Name;
|
---|
284 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
285 | mainLoop.ResultsParameter.ActualName = ResultsName;
|
---|
286 | mainLoop.StartTemperatureParameter.ActualName = StartTemperatureName;
|
---|
287 | mainLoop.TemperatureStartIndexParameter.ActualName = TemperatureStartIndexName;
|
---|
288 | mainLoop.TemperatureParameter.ActualName = TemperatureName;
|
---|
289 |
|
---|
290 | foreach (var op in ApplicationManager.Manager.GetInstances<IReheatingOperator>().OrderBy(x => x.Name))
|
---|
291 | {
|
---|
292 |
|
---|
293 | ReheatingOperatorParameter.ValidValues.Add(op);
|
---|
294 | }
|
---|
295 |
|
---|
296 | foreach (var op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
|
---|
297 | {
|
---|
298 | AnnealingOperatorParameter.ValidValues.Add(op);
|
---|
299 | }
|
---|
300 |
|
---|
301 | UpdateAnalyzers();
|
---|
302 |
|
---|
303 | Parameterize();
|
---|
304 |
|
---|
305 | RegisterEventHandlers();
|
---|
306 | }
|
---|
307 |
|
---|
308 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
309 | {
|
---|
310 | return new SimulatedAnnealing(this, cloner);
|
---|
311 | }
|
---|
312 |
|
---|
313 | [StorableHook(HookType.AfterDeserialization)]
|
---|
314 | private void AfterDeserialization()
|
---|
315 | {
|
---|
316 | RegisterEventHandlers();
|
---|
317 | }
|
---|
318 |
|
---|
319 | private void RegisterEventHandlers()
|
---|
320 | {
|
---|
321 | if (Problem != null)
|
---|
322 | {
|
---|
323 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
324 | foreach (var op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>())
|
---|
325 | {
|
---|
326 | op.MoveQualityParameter.ActualNameChanged += MoveEvaluator_MoveQualityParameter_ActualNameChanged;
|
---|
327 | }
|
---|
328 | }
|
---|
329 | MoveGeneratorParameter.ValueChanged += MoveGeneratorParameter_ValueChanged;
|
---|
330 | MoveEvaluatorParameter.ValueChanged += MoveEvaluatorParameter_ValueChanged;
|
---|
331 | }
|
---|
332 |
|
---|
333 | public override void Prepare()
|
---|
334 | {
|
---|
335 | if (Problem != null && MoveGenerator != null && MoveMaker != null && MoveEvaluator != null)
|
---|
336 | base.Prepare();
|
---|
337 | }
|
---|
338 |
|
---|
339 | #region Events
|
---|
340 | protected override void OnProblemChanged()
|
---|
341 | {
|
---|
342 | foreach (var op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>())
|
---|
343 | {
|
---|
344 | op.MoveQualityParameter.ActualNameChanged += MoveEvaluator_MoveQualityParameter_ActualNameChanged;
|
---|
345 | }
|
---|
346 | UpdateMoveGenerator();
|
---|
347 | UpdateMoveOperators();
|
---|
348 | UpdateAnalyzers();
|
---|
349 | Parameterize();
|
---|
350 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
351 | base.OnProblemChanged();
|
---|
352 | }
|
---|
353 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e)
|
---|
354 | {
|
---|
355 | Parameterize();
|
---|
356 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
357 | }
|
---|
358 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e)
|
---|
359 | {
|
---|
360 | Parameterize();
|
---|
361 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
362 | base.Problem_EvaluatorChanged(sender, e);
|
---|
363 | }
|
---|
364 | protected override void Problem_OperatorsChanged(object sender, EventArgs e)
|
---|
365 | {
|
---|
366 | foreach (var op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>())
|
---|
367 | {
|
---|
368 | op.MoveQualityParameter.ActualNameChanged -= MoveEvaluator_MoveQualityParameter_ActualNameChanged;
|
---|
369 | op.MoveQualityParameter.ActualNameChanged += MoveEvaluator_MoveQualityParameter_ActualNameChanged;
|
---|
370 | }
|
---|
371 | UpdateMoveGenerator();
|
---|
372 | UpdateMoveOperators();
|
---|
373 | UpdateAnalyzers();
|
---|
374 | Parameterize();
|
---|
375 | base.Problem_OperatorsChanged(sender, e);
|
---|
376 | }
|
---|
377 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e)
|
---|
378 | {
|
---|
379 | Parameterize();
|
---|
380 | }
|
---|
381 | private void MoveGeneratorParameter_ValueChanged(object sender, EventArgs e)
|
---|
382 | {
|
---|
383 | UpdateMoveOperators();
|
---|
384 | }
|
---|
385 | private void MoveEvaluatorParameter_ValueChanged(object sender, EventArgs e)
|
---|
386 | {
|
---|
387 | Parameterize();
|
---|
388 | }
|
---|
389 | private void MoveEvaluator_MoveQualityParameter_ActualNameChanged(object sender, EventArgs e)
|
---|
390 | {
|
---|
391 | if (sender == MoveEvaluator) Parameterize();
|
---|
392 | }
|
---|
393 | #endregion
|
---|
394 |
|
---|
395 | #region Helpers
|
---|
396 |
|
---|
397 | private void UpdateMoveGenerator()
|
---|
398 | {
|
---|
399 | var oldMoveGenerator = MoveGenerator;
|
---|
400 | var defaultMoveGenerator = Problem.Operators.OfType<IMultiMoveGenerator>().FirstOrDefault();
|
---|
401 |
|
---|
402 | MoveGeneratorParameter.ValidValues.Clear();
|
---|
403 |
|
---|
404 | if (Problem != null)
|
---|
405 | {
|
---|
406 | foreach (var generator in Problem.Operators.OfType<IMultiMoveGenerator>().OrderBy(x => x.Name))
|
---|
407 | MoveGeneratorParameter.ValidValues.Add(generator);
|
---|
408 | }
|
---|
409 |
|
---|
410 | if (oldMoveGenerator != null)
|
---|
411 | {
|
---|
412 | var newMoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
|
---|
413 | if (newMoveGenerator != null) MoveGenerator = newMoveGenerator;
|
---|
414 | else oldMoveGenerator = null;
|
---|
415 | }
|
---|
416 | if (oldMoveGenerator == null && defaultMoveGenerator != null)
|
---|
417 | MoveGenerator = defaultMoveGenerator;
|
---|
418 |
|
---|
419 | UpdateMoveOperators();
|
---|
420 | }
|
---|
421 |
|
---|
422 | private void UpdateMoveOperators()
|
---|
423 | {
|
---|
424 | var oldMoveMaker = MoveMaker;
|
---|
425 | var oldMoveEvaluator = MoveEvaluator;
|
---|
426 |
|
---|
427 | MoveMakerParameter.ValidValues.Clear();
|
---|
428 | MoveEvaluatorParameter.ValidValues.Clear();
|
---|
429 |
|
---|
430 | if (MoveGenerator != null)
|
---|
431 | {
|
---|
432 | var moveTypes = MoveGenerator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();
|
---|
433 | foreach (var type in moveTypes.ToList())
|
---|
434 | {
|
---|
435 | if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t)))
|
---|
436 | moveTypes.Remove(type);
|
---|
437 | }
|
---|
438 | var operators = Problem.Operators.Where(op => moveTypes.Any(m => m.IsInstanceOfType(op))).ToList();
|
---|
439 | var defaultMoveMaker = operators.OfType<IMoveMaker>().FirstOrDefault();
|
---|
440 | var defaultMoveEvaluator = operators.OfType<ISingleObjectiveMoveEvaluator>().FirstOrDefault();
|
---|
441 |
|
---|
442 | foreach (var moveMaker in operators.OfType<IMoveMaker>().OrderBy(op => op.Name))
|
---|
443 | MoveMakerParameter.ValidValues.Add(moveMaker);
|
---|
444 |
|
---|
445 | foreach (var moveEvaluator in operators.OfType<ISingleObjectiveMoveEvaluator>().OrderBy(op => op.Name))
|
---|
446 | MoveEvaluatorParameter.ValidValues.Add(moveEvaluator);
|
---|
447 |
|
---|
448 | if (oldMoveMaker != null)
|
---|
449 | {
|
---|
450 | var mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
|
---|
451 | if (mm != null) MoveMaker = mm;
|
---|
452 | else oldMoveMaker = null;
|
---|
453 | }
|
---|
454 | if (oldMoveMaker == null && defaultMoveMaker != null)
|
---|
455 | MoveMaker = defaultMoveMaker;
|
---|
456 |
|
---|
457 | if (oldMoveEvaluator != null)
|
---|
458 | {
|
---|
459 | var me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
|
---|
460 | if (me != null) MoveEvaluator = me;
|
---|
461 | else oldMoveEvaluator = null;
|
---|
462 | }
|
---|
463 | if (oldMoveEvaluator == null & defaultMoveEvaluator != null)
|
---|
464 | MoveEvaluator = defaultMoveEvaluator;
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | private void UpdateAnalyzers()
|
---|
469 | {
|
---|
470 | Analyzer.Operators.Clear();
|
---|
471 | if (Problem != null)
|
---|
472 | {
|
---|
473 | foreach (var analyzer in Problem.Operators.OfType<IAnalyzer>())
|
---|
474 | {
|
---|
475 | foreach (var param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
476 | param.Depth = 0;
|
---|
477 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
478 | }
|
---|
479 | }
|
---|
480 | var qualityAnalyzer = new QualityAnalyzer();
|
---|
481 | var temperatureAnalyzer = new SingleValueAnalyzer { Name = TemperatureAnalyzerName };
|
---|
482 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
483 | Analyzer.Operators.Add(temperatureAnalyzer, temperatureAnalyzer.EnabledByDefault);
|
---|
484 | }
|
---|
485 |
|
---|
486 | private void Parameterize()
|
---|
487 | {
|
---|
488 |
|
---|
489 | #region IStochasticOperator
|
---|
490 | if (Problem != null)
|
---|
491 | {
|
---|
492 | foreach (var op in Problem.Operators.OfType<IStochasticOperator>())
|
---|
493 | {
|
---|
494 | op.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
495 | op.RandomParameter.Hidden = true;
|
---|
496 | }
|
---|
497 | }
|
---|
498 | #endregion
|
---|
499 |
|
---|
500 | #region IIterationBasedOperator
|
---|
501 | if (Problem != null)
|
---|
502 | {
|
---|
503 | foreach (var op in Problem.Operators.OfType<IIterationBasedOperator>())
|
---|
504 | {
|
---|
505 | op.IterationsParameter.ActualName = IterationsName;
|
---|
506 | op.IterationsParameter.Hidden = true;
|
---|
507 | op.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
508 | op.MaximumIterationsParameter.Hidden = true;
|
---|
509 | }
|
---|
510 | }
|
---|
511 | #endregion
|
---|
512 |
|
---|
513 | #region Analyzers
|
---|
514 |
|
---|
515 | foreach (var qualityAnalyzer in Analyzer.Operators.OfType<QualityAnalyzer>())
|
---|
516 | {
|
---|
517 | qualityAnalyzer.ResultsParameter.ActualName = ResultsName;
|
---|
518 | if (Problem != null)
|
---|
519 | {
|
---|
520 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
521 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
522 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
523 | qualityAnalyzer.QualityParameter.Depth = 0;
|
---|
524 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
525 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
526 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
527 | }
|
---|
528 | else
|
---|
529 | {
|
---|
530 | qualityAnalyzer.MaximizationParameter.Hidden = false;
|
---|
531 | qualityAnalyzer.QualityParameter.Hidden = false;
|
---|
532 | qualityAnalyzer.BestKnownQualityParameter.Hidden = false;
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 | var temperatureAnalyzer = Analyzer.Operators.OfType<SingleValueAnalyzer>().FirstOrDefault(x => x.Name == TemperatureAnalyzerName);
|
---|
537 | if (temperatureAnalyzer != null)
|
---|
538 | {
|
---|
539 | temperatureAnalyzer.ResultsParameter.ActualName = ResultsName;
|
---|
540 | temperatureAnalyzer.ResultsParameter.Hidden = true;
|
---|
541 | temperatureAnalyzer.ValueParameter.ActualName = TemperatureName;
|
---|
542 | temperatureAnalyzer.ValueParameter.Hidden = true;
|
---|
543 | temperatureAnalyzer.ValuesParameter.ActualName = TemperatureChartName;
|
---|
544 | temperatureAnalyzer.ValuesParameter.Hidden = true;
|
---|
545 | }
|
---|
546 |
|
---|
547 | #endregion
|
---|
548 |
|
---|
549 | #region SolutionCreator
|
---|
550 | if (Problem != null)
|
---|
551 | {
|
---|
552 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
553 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
554 | }
|
---|
555 | #endregion
|
---|
556 |
|
---|
557 | #region Annealing/Reheating Operators
|
---|
558 | foreach (var op in AnnealingOperatorParameter.ValidValues)
|
---|
559 | {
|
---|
560 | op.IndexParameter.ActualName = IterationsName;
|
---|
561 | op.IndexParameter.Hidden = true;
|
---|
562 | op.StartIndexParameter.Value = null;
|
---|
563 | op.StartIndexParameter.ActualName = TemperatureStartIndexName;
|
---|
564 | op.EndIndexParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
565 | op.ValueParameter.ActualName = TemperatureName;
|
---|
566 | op.ValueParameter.Hidden = true;
|
---|
567 | op.StartValueParameter.ActualName = StartTemperatureName;
|
---|
568 | op.StartValueParameter.Hidden = true;
|
---|
569 | op.EndValueParameter.ActualName = LowerTemperatureParameter.Name;
|
---|
570 | op.EndValueParameter.Hidden = true;
|
---|
571 | }
|
---|
572 | foreach (var op in ReheatingOperatorParameter.ValidValues)
|
---|
573 | {
|
---|
574 | op.Parameterize();
|
---|
575 | }
|
---|
576 | #endregion
|
---|
577 |
|
---|
578 | #region Move Operators
|
---|
579 | if (Problem != null)
|
---|
580 | {
|
---|
581 | foreach (var op in Problem.Operators.OfType<IMultiMoveGenerator>())
|
---|
582 | {
|
---|
583 | op.SampleSizeParameter.Value = new IntValue(1);
|
---|
584 | op.SampleSizeParameter.Hidden = true;
|
---|
585 | }
|
---|
586 | foreach (var op in Problem.Operators.OfType<IMoveMaker>())
|
---|
587 | {
|
---|
588 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
589 | op.QualityParameter.Hidden = true;
|
---|
590 | if (MoveEvaluator != null)
|
---|
591 | {
|
---|
592 | op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
593 | op.MoveQualityParameter.Hidden = true;
|
---|
594 | }
|
---|
595 | else
|
---|
596 | {
|
---|
597 | op.MoveQualityParameter.Hidden = false;
|
---|
598 | }
|
---|
599 | }
|
---|
600 | foreach (var op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>())
|
---|
601 | {
|
---|
602 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
603 | op.QualityParameter.Hidden = true;
|
---|
604 | }
|
---|
605 | }
|
---|
606 | #endregion
|
---|
607 |
|
---|
608 | #region MainLoop
|
---|
609 | if (Problem != null)
|
---|
610 | {
|
---|
611 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
612 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
613 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
614 | if (MoveEvaluator != null)
|
---|
615 | MainLoop.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
616 | }
|
---|
617 | #endregion
|
---|
618 |
|
---|
619 | }
|
---|
620 | #endregion
|
---|
621 | }
|
---|
622 | } |
---|