1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Optimization.Operators;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
34 | using HeuristicLab.Random;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.TabuSearch {
|
---|
37 | [Item("Tabu Search", "A tabu search algorithm.")]
|
---|
38 | [Creatable("Algorithms")]
|
---|
39 | [StorableClass]
|
---|
40 | public sealed class TabuSearch : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
41 | public string Filename { get; set; }
|
---|
42 |
|
---|
43 | #region Problem Properties
|
---|
44 | public override Type ProblemType {
|
---|
45 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
46 | }
|
---|
47 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
48 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
49 | set { base.Problem = value; }
|
---|
50 | }
|
---|
51 | #endregion
|
---|
52 |
|
---|
53 | #region Parameter Properties
|
---|
54 | private ValueParameter<IntValue> SeedParameter {
|
---|
55 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
56 | }
|
---|
57 | private ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
58 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
59 | }
|
---|
60 | private ConstrainedValueParameter<IMoveGenerator> MoveGeneratorParameter {
|
---|
61 | get { return (ConstrainedValueParameter<IMoveGenerator>)Parameters["MoveGenerator"]; }
|
---|
62 | }
|
---|
63 | private ConstrainedValueParameter<IMoveMaker> MoveMakerParameter {
|
---|
64 | get { return (ConstrainedValueParameter<IMoveMaker>)Parameters["MoveMaker"]; }
|
---|
65 | }
|
---|
66 | private ConstrainedValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter {
|
---|
67 | get { return (ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>)Parameters["MoveEvaluator"]; }
|
---|
68 | }
|
---|
69 | private ConstrainedValueParameter<ITabuChecker> TabuCheckerParameter {
|
---|
70 | get { return (ConstrainedValueParameter<ITabuChecker>)Parameters["TabuChecker"]; }
|
---|
71 | }
|
---|
72 | private ConstrainedValueParameter<ITabuMaker> TabuMakerParameter {
|
---|
73 | get { return (ConstrainedValueParameter<ITabuMaker>)Parameters["TabuMaker"]; }
|
---|
74 | }
|
---|
75 | private ValueParameter<IntValue> TabuTenureParameter {
|
---|
76 | get { return (ValueParameter<IntValue>)Parameters["TabuTenure"]; }
|
---|
77 | }
|
---|
78 | private ValueParameter<IntValue> MaximumIterationsParameter {
|
---|
79 | get { return (ValueParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
80 | }
|
---|
81 | private ValueParameter<IntValue> SampleSizeParameter {
|
---|
82 | get { return (ValueParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
83 | }
|
---|
84 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
85 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
86 | }
|
---|
87 | #endregion
|
---|
88 |
|
---|
89 | #region Properties
|
---|
90 | public IntValue Seed {
|
---|
91 | get { return SeedParameter.Value; }
|
---|
92 | set { SeedParameter.Value = value; }
|
---|
93 | }
|
---|
94 | public BoolValue SetSeedRandomly {
|
---|
95 | get { return SetSeedRandomlyParameter.Value; }
|
---|
96 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
97 | }
|
---|
98 | public IMoveGenerator MoveGenerator {
|
---|
99 | get { return MoveGeneratorParameter.Value; }
|
---|
100 | set { MoveGeneratorParameter.Value = value; }
|
---|
101 | }
|
---|
102 | public IMoveMaker MoveMaker {
|
---|
103 | get { return MoveMakerParameter.Value; }
|
---|
104 | set { MoveMakerParameter.Value = value; }
|
---|
105 | }
|
---|
106 | public ISingleObjectiveMoveEvaluator MoveEvaluator {
|
---|
107 | get { return MoveEvaluatorParameter.Value; }
|
---|
108 | set { MoveEvaluatorParameter.Value = value; }
|
---|
109 | }
|
---|
110 | public ITabuChecker TabuChecker {
|
---|
111 | get { return TabuCheckerParameter.Value; }
|
---|
112 | set { TabuCheckerParameter.Value = value; }
|
---|
113 | }
|
---|
114 | public ITabuMaker TabuMaker {
|
---|
115 | get { return TabuMakerParameter.Value; }
|
---|
116 | set { TabuMakerParameter.Value = value; }
|
---|
117 | }
|
---|
118 | public IntValue TabuTenure {
|
---|
119 | get { return TabuTenureParameter.Value; }
|
---|
120 | set { TabuTenureParameter.Value = value; }
|
---|
121 | }
|
---|
122 | public IntValue MaximumIterations {
|
---|
123 | get { return MaximumIterationsParameter.Value; }
|
---|
124 | set { MaximumIterationsParameter.Value = value; }
|
---|
125 | }
|
---|
126 | public MultiAnalyzer Analyzer {
|
---|
127 | get { return AnalyzerParameter.Value; }
|
---|
128 | set { AnalyzerParameter.Value = value; }
|
---|
129 | }
|
---|
130 | private RandomCreator RandomCreator {
|
---|
131 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
132 | }
|
---|
133 | private SolutionsCreator SolutionsCreator {
|
---|
134 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
135 | }
|
---|
136 | private TabuSearchMainLoop MainLoop {
|
---|
137 | get { return FindMainLoop(SolutionsCreator.Successor); }
|
---|
138 | }
|
---|
139 | [Storable]
|
---|
140 | private BestAverageWorstQualityAnalyzer moveQualityAnalyzer;
|
---|
141 | [Storable]
|
---|
142 | private TabuNeighborhoodAnalyzer tabuNeighborhoodAnalyzer;
|
---|
143 | #endregion
|
---|
144 |
|
---|
145 | public TabuSearch()
|
---|
146 | : base() {
|
---|
147 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
148 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
149 | Parameters.Add(new ConstrainedValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));
|
---|
150 | Parameters.Add(new ConstrainedValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move."));
|
---|
151 | Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move."));
|
---|
152 | Parameters.Add(new ConstrainedValueParameter<ITabuChecker>("TabuChecker", "The operator to check whether a move is tabu or not."));
|
---|
153 | Parameters.Add(new ConstrainedValueParameter<ITabuMaker>("TabuMaker", "The operator used to insert attributes of a move into the tabu list."));
|
---|
154 | Parameters.Add(new ValueParameter<IntValue>("TabuTenure", "The length of the tabu list.", new IntValue(10)));
|
---|
155 | Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
156 | Parameters.Add(new ValueParameter<IntValue>("SampleSize", "The neighborhood size for stochastic sampling move generators", new IntValue(100)));
|
---|
157 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the solution.", new MultiAnalyzer()));
|
---|
158 |
|
---|
159 | RandomCreator randomCreator = new RandomCreator();
|
---|
160 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
161 | VariableCreator variableCreator = new VariableCreator();
|
---|
162 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
163 | TabuSearchMainLoop mainLoop = new TabuSearchMainLoop();
|
---|
164 | OperatorGraph.InitialOperator = randomCreator;
|
---|
165 |
|
---|
166 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
167 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
168 | randomCreator.SeedParameter.Value = null;
|
---|
169 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
170 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
171 | randomCreator.Successor = solutionsCreator;
|
---|
172 |
|
---|
173 | solutionsCreator.NumberOfSolutions = new IntValue(1);
|
---|
174 | solutionsCreator.Successor = variableCreator;
|
---|
175 |
|
---|
176 | variableCreator.Name = "Initialize EvaluatedMoves";
|
---|
177 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedMoves", new IntValue()));
|
---|
178 | variableCreator.Successor = resultsCollector;
|
---|
179 |
|
---|
180 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
|
---|
181 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
182 | resultsCollector.Successor = mainLoop;
|
---|
183 |
|
---|
184 | mainLoop.MoveGeneratorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
185 | mainLoop.MoveMakerParameter.ActualName = MoveMakerParameter.Name;
|
---|
186 | mainLoop.MoveEvaluatorParameter.ActualName = MoveEvaluatorParameter.Name;
|
---|
187 | mainLoop.TabuCheckerParameter.ActualName = TabuCheckerParameter.Name;
|
---|
188 | mainLoop.TabuMakerParameter.ActualName = TabuMakerParameter.Name;
|
---|
189 | mainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
190 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
191 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
192 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
193 | mainLoop.EvaluatedMovesParameter.ActualName = "EvaluatedMoves";
|
---|
194 |
|
---|
195 | moveQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
196 | tabuNeighborhoodAnalyzer = new TabuNeighborhoodAnalyzer();
|
---|
197 | ParameterizeAnalyzers();
|
---|
198 | UpdateAnalyzers();
|
---|
199 |
|
---|
200 | Initialize();
|
---|
201 | }
|
---|
202 | [StorableConstructor]
|
---|
203 | private TabuSearch(bool deserializing) : base(deserializing) { }
|
---|
204 | [StorableHook(HookType.AfterDeserialization)]
|
---|
205 | private void AfterDeserialization() {
|
---|
206 | Initialize();
|
---|
207 | }
|
---|
208 | private TabuSearch(TabuSearch original, Cloner cloner)
|
---|
209 | : base(original, cloner) {
|
---|
210 | moveQualityAnalyzer = cloner.Clone(original.moveQualityAnalyzer);
|
---|
211 | tabuNeighborhoodAnalyzer = cloner.Clone(original.tabuNeighborhoodAnalyzer);
|
---|
212 | Initialize();
|
---|
213 | }
|
---|
214 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
215 | return new TabuSearch(this, cloner);
|
---|
216 | }
|
---|
217 |
|
---|
218 | public override void Prepare() {
|
---|
219 | if (Problem != null && MoveGenerator != null && MoveMaker != null && MoveEvaluator != null &&
|
---|
220 | TabuChecker != null && TabuMaker != null)
|
---|
221 | base.Prepare();
|
---|
222 | }
|
---|
223 |
|
---|
224 | #region Events
|
---|
225 | protected override void OnProblemChanged() {
|
---|
226 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
227 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
228 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
229 | foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
|
---|
230 | op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
|
---|
231 | }
|
---|
232 | foreach (ITabuChecker op in Problem.Operators.OfType<ITabuChecker>()) {
|
---|
233 | op.MoveTabuParameter.ActualNameChanged += new EventHandler(TabuChecker_MoveTabuParameter_ActualNameChanged);
|
---|
234 | }
|
---|
235 | ParameterizeSolutionsCreator();
|
---|
236 | ParameterizeMainLoop();
|
---|
237 | UpdateMoveGenerator();
|
---|
238 | UpdateMoveParameters();
|
---|
239 | UpdateAnalyzers();
|
---|
240 | ParameterizeMoveGenerators();
|
---|
241 | ParameterizeMoveEvaluator();
|
---|
242 | ParameterizeMoveMaker();
|
---|
243 | ParameterizeTabuMaker();
|
---|
244 | ParameterizeTabuChecker();
|
---|
245 | ParameterizeAnalyzers();
|
---|
246 | ParameterizeIterationBasedOperators();
|
---|
247 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
248 | base.OnProblemChanged();
|
---|
249 | }
|
---|
250 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
251 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
252 | ParameterizeSolutionsCreator();
|
---|
253 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
254 | }
|
---|
255 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
256 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
257 | ParameterizeSolutionsCreator();
|
---|
258 | ParameterizeMainLoop();
|
---|
259 | ParameterizeMoveEvaluator();
|
---|
260 | ParameterizeMoveMaker();
|
---|
261 | ParameterizeTabuMaker();
|
---|
262 | ParameterizeTabuChecker();
|
---|
263 | ParameterizeAnalyzers();
|
---|
264 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
265 | base.Problem_EvaluatorChanged(sender, e);
|
---|
266 | }
|
---|
267 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
268 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
269 | // This may seem pointless, but some operators already have the eventhandler registered, others don't
|
---|
270 | // FIXME: Is there another way to solve this problem?
|
---|
271 | foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
|
---|
272 | op.MoveQualityParameter.ActualNameChanged -= new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
|
---|
273 | op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
|
---|
274 | }
|
---|
275 | foreach (ITabuChecker op in Problem.Operators.OfType<ITabuChecker>()) {
|
---|
276 | op.MoveTabuParameter.ActualNameChanged -= new EventHandler(TabuChecker_MoveTabuParameter_ActualNameChanged);
|
---|
277 | op.MoveTabuParameter.ActualNameChanged += new EventHandler(TabuChecker_MoveTabuParameter_ActualNameChanged);
|
---|
278 | }
|
---|
279 | UpdateMoveGenerator();
|
---|
280 | UpdateMoveParameters();
|
---|
281 | UpdateAnalyzers();
|
---|
282 | ParameterizeMainLoop();
|
---|
283 | ParameterizeMoveGenerators();
|
---|
284 | ParameterizeMoveEvaluator();
|
---|
285 | ParameterizeMoveMaker();
|
---|
286 | ParameterizeTabuMaker();
|
---|
287 | ParameterizeTabuChecker();
|
---|
288 | ParameterizeAnalyzers();
|
---|
289 | ParameterizeIterationBasedOperators();
|
---|
290 | base.Problem_OperatorsChanged(sender, e);
|
---|
291 | }
|
---|
292 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
293 | ParameterizeMainLoop();
|
---|
294 | ParameterizeMoveEvaluator();
|
---|
295 | ParameterizeMoveMaker();
|
---|
296 | ParameterizeTabuMaker();
|
---|
297 | ParameterizeTabuChecker();
|
---|
298 | }
|
---|
299 | private void MoveGeneratorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
300 | UpdateMoveParameters();
|
---|
301 | }
|
---|
302 | private void MoveEvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
303 | ParameterizeMainLoop();
|
---|
304 | ParameterizeMoveEvaluator();
|
---|
305 | ParameterizeMoveMaker();
|
---|
306 | ParameterizeTabuMaker();
|
---|
307 | ParameterizeTabuChecker();
|
---|
308 | ParameterizeAnalyzers();
|
---|
309 | }
|
---|
310 | private void MoveEvaluator_MoveQualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
311 | ParameterizeMainLoop();
|
---|
312 | ParameterizeMoveEvaluator();
|
---|
313 | ParameterizeMoveMaker();
|
---|
314 | ParameterizeTabuMaker();
|
---|
315 | ParameterizeTabuChecker();
|
---|
316 | ParameterizeAnalyzers();
|
---|
317 | }
|
---|
318 | private void TabuCheckerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
319 | ParameterizeMainLoop();
|
---|
320 | ParameterizeAnalyzers();
|
---|
321 | }
|
---|
322 | private void TabuChecker_MoveTabuParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
323 | ParameterizeMainLoop();
|
---|
324 | ParameterizeAnalyzers();
|
---|
325 | }
|
---|
326 | private void SampleSizeParameter_NameChanged(object sender, EventArgs e) {
|
---|
327 | ParameterizeMoveGenerators();
|
---|
328 | }
|
---|
329 | #endregion
|
---|
330 |
|
---|
331 | #region Helpers
|
---|
332 | private void Initialize() {
|
---|
333 | if (Problem != null) {
|
---|
334 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
335 | foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
|
---|
336 | op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
|
---|
337 | }
|
---|
338 | }
|
---|
339 | MoveGeneratorParameter.ValueChanged += new EventHandler(MoveGeneratorParameter_ValueChanged);
|
---|
340 | MoveEvaluatorParameter.ValueChanged += new EventHandler(MoveEvaluatorParameter_ValueChanged);
|
---|
341 | TabuCheckerParameter.ValueChanged += new EventHandler(TabuCheckerParameter_ValueChanged);
|
---|
342 | SampleSizeParameter.NameChanged += new EventHandler(SampleSizeParameter_NameChanged);
|
---|
343 | }
|
---|
344 | private void UpdateMoveGenerator() {
|
---|
345 | IMoveGenerator oldMoveGenerator = MoveGenerator;
|
---|
346 | MoveGeneratorParameter.ValidValues.Clear();
|
---|
347 | if (Problem != null) {
|
---|
348 | foreach (IMoveGenerator generator in Problem.Operators.OfType<IMoveGenerator>().OrderBy(x => x.Name)) {
|
---|
349 | MoveGeneratorParameter.ValidValues.Add(generator);
|
---|
350 | }
|
---|
351 | }
|
---|
352 | if (oldMoveGenerator != null && MoveGeneratorParameter.ValidValues.Any(x => x.GetType() == oldMoveGenerator.GetType()))
|
---|
353 | MoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
|
---|
354 | if (MoveGenerator == null) {
|
---|
355 | ClearMoveParameters();
|
---|
356 | }
|
---|
357 | }
|
---|
358 | private void UpdateMoveParameters() {
|
---|
359 | IMoveMaker oldMoveMaker = MoveMaker;
|
---|
360 | ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
|
---|
361 | ITabuChecker oldTabuMoveEvaluator = TabuChecker;
|
---|
362 | ITabuMaker oldTabuMoveMaker = TabuMaker;
|
---|
363 | ClearMoveParameters();
|
---|
364 | if (MoveGenerator != null) {
|
---|
365 | List<Type> moveTypes = MoveGenerator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();
|
---|
366 | foreach (Type type in moveTypes.ToList()) {
|
---|
367 | if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t)))
|
---|
368 | moveTypes.Remove(type);
|
---|
369 | }
|
---|
370 | foreach (Type type in moveTypes) {
|
---|
371 | var operators = Problem.Operators.Where(x => type.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
|
---|
372 | foreach (IMoveMaker moveMaker in operators.OfType<IMoveMaker>())
|
---|
373 | MoveMakerParameter.ValidValues.Add(moveMaker);
|
---|
374 | foreach (ISingleObjectiveMoveEvaluator moveEvaluator in operators.OfType<ISingleObjectiveMoveEvaluator>())
|
---|
375 | MoveEvaluatorParameter.ValidValues.Add(moveEvaluator);
|
---|
376 | foreach (ITabuChecker tabuMoveEvaluator in operators.OfType<ITabuChecker>())
|
---|
377 | TabuCheckerParameter.ValidValues.Add(tabuMoveEvaluator);
|
---|
378 | foreach (ITabuMaker tabuMoveMaker in operators.OfType<ITabuMaker>())
|
---|
379 | TabuMakerParameter.ValidValues.Add(tabuMoveMaker);
|
---|
380 | }
|
---|
381 | if (oldMoveMaker != null) {
|
---|
382 | IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
|
---|
383 | if (mm != null) MoveMaker = mm;
|
---|
384 | }
|
---|
385 | if (oldMoveEvaluator != null) {
|
---|
386 | ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
|
---|
387 | if (me != null) MoveEvaluator = me;
|
---|
388 | }
|
---|
389 | if (oldTabuMoveMaker != null) {
|
---|
390 | ITabuMaker tmm = TabuMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuMoveMaker.GetType());
|
---|
391 | if (tmm != null) TabuMaker = tmm;
|
---|
392 | }
|
---|
393 | if (oldTabuMoveEvaluator != null) {
|
---|
394 | ITabuChecker tme = TabuCheckerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuMoveEvaluator.GetType());
|
---|
395 | if (tme != null) TabuChecker = tme;
|
---|
396 | }
|
---|
397 | }
|
---|
398 | }
|
---|
399 | private void UpdateAnalyzers() {
|
---|
400 | Analyzer.Operators.Clear();
|
---|
401 | if (Problem != null) {
|
---|
402 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
403 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
404 | param.Depth = 0;
|
---|
405 | Analyzer.Operators.Add(analyzer);
|
---|
406 | }
|
---|
407 | }
|
---|
408 | Analyzer.Operators.Add(moveQualityAnalyzer);
|
---|
409 | Analyzer.Operators.Add(tabuNeighborhoodAnalyzer);
|
---|
410 | }
|
---|
411 | private void ClearMoveParameters() {
|
---|
412 | MoveMakerParameter.ValidValues.Clear();
|
---|
413 | MoveEvaluatorParameter.ValidValues.Clear();
|
---|
414 | TabuCheckerParameter.ValidValues.Clear();
|
---|
415 | TabuMakerParameter.ValidValues.Clear();
|
---|
416 | }
|
---|
417 | private void ParameterizeSolutionsCreator() {
|
---|
418 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
419 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
420 | }
|
---|
421 | private void ParameterizeMainLoop() {
|
---|
422 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
423 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
424 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
425 | if (MoveEvaluator != null)
|
---|
426 | MainLoop.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
427 | if (TabuChecker != null)
|
---|
428 | MainLoop.MoveTabuParameter.ActualName = TabuChecker.MoveTabuParameter.ActualName;
|
---|
429 | }
|
---|
430 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
431 | if (op is IStochasticOperator) {
|
---|
432 | IStochasticOperator stOp = (IStochasticOperator)op;
|
---|
433 | stOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
434 | stOp.RandomParameter.Hidden = true;
|
---|
435 | }
|
---|
436 | }
|
---|
437 | private void ParameterizeMoveGenerators() {
|
---|
438 | if (Problem != null) {
|
---|
439 | foreach (IMultiMoveGenerator generator in Problem.Operators.OfType<IMultiMoveGenerator>()) {
|
---|
440 | generator.SampleSizeParameter.ActualName = SampleSizeParameter.Name;
|
---|
441 | generator.SampleSizeParameter.Hidden = true;
|
---|
442 | }
|
---|
443 | }
|
---|
444 | }
|
---|
445 | private void ParameterizeMoveEvaluator() {
|
---|
446 | foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
|
---|
447 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
448 | op.QualityParameter.Hidden = true;
|
---|
449 | }
|
---|
450 | }
|
---|
451 | private void ParameterizeMoveMaker() {
|
---|
452 | foreach (IMoveMaker op in Problem.Operators.OfType<IMoveMaker>()) {
|
---|
453 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
454 | op.QualityParameter.Hidden = true;
|
---|
455 | if (MoveEvaluator != null) {
|
---|
456 | op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
457 | op.MoveQualityParameter.Hidden = true;
|
---|
458 | } else {
|
---|
459 | op.MoveQualityParameter.Hidden = false;
|
---|
460 | }
|
---|
461 | }
|
---|
462 | }
|
---|
463 | private void ParameterizeTabuMaker() {
|
---|
464 | foreach (ITabuMaker op in Problem.Operators.OfType<ITabuMaker>()) {
|
---|
465 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
466 | op.QualityParameter.Hidden = true;
|
---|
467 | if (MoveEvaluator != null) {
|
---|
468 | op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
469 | op.MoveQualityParameter.Hidden = true;
|
---|
470 | } else {
|
---|
471 | op.MoveQualityParameter.Hidden = false;
|
---|
472 | }
|
---|
473 | }
|
---|
474 | }
|
---|
475 | private void ParameterizeTabuChecker() {
|
---|
476 | foreach (ITabuChecker op in Problem.Operators.OfType<ITabuChecker>()) {
|
---|
477 | if (MoveEvaluator != null) {
|
---|
478 | op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
479 | op.MoveQualityParameter.Hidden = true;
|
---|
480 | } else {
|
---|
481 | op.MoveQualityParameter.Hidden = false;
|
---|
482 | }
|
---|
483 | if (TabuChecker != null) {
|
---|
484 | op.MoveTabuParameter.ActualName = TabuChecker.MoveTabuParameter.ActualName;
|
---|
485 | op.MoveTabuParameter.Hidden = true;
|
---|
486 | } else {
|
---|
487 | op.MoveTabuParameter.Hidden = false;
|
---|
488 | }
|
---|
489 | }
|
---|
490 | }
|
---|
491 | private void ParameterizeAnalyzers() {
|
---|
492 | moveQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
493 | moveQualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
494 | tabuNeighborhoodAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
495 | tabuNeighborhoodAnalyzer.ResultsParameter.Hidden = true;
|
---|
496 | tabuNeighborhoodAnalyzer.PercentTabuParameter.ActualName = "PercentTabu";
|
---|
497 | tabuNeighborhoodAnalyzer.PercentTabuParameter.Hidden = true;
|
---|
498 | if (Problem != null) {
|
---|
499 | moveQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
500 | moveQualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
501 | if (MoveEvaluator != null) {
|
---|
502 | moveQualityAnalyzer.QualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
503 | moveQualityAnalyzer.QualityParameter.Hidden = true;
|
---|
504 | } else {
|
---|
505 | moveQualityAnalyzer.QualityParameter.Hidden = false;
|
---|
506 | }
|
---|
507 | moveQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
508 | moveQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
509 | if (TabuChecker != null) {
|
---|
510 | tabuNeighborhoodAnalyzer.IsTabuParameter.ActualName = TabuChecker.MoveTabuParameter.ActualName;
|
---|
511 | tabuNeighborhoodAnalyzer.IsTabuParameter.Hidden = true;
|
---|
512 | } else {
|
---|
513 | tabuNeighborhoodAnalyzer.IsTabuParameter.Hidden = false;
|
---|
514 | }
|
---|
515 | } else {
|
---|
516 | moveQualityAnalyzer.MaximizationParameter.Hidden = false;
|
---|
517 | moveQualityAnalyzer.QualityParameter.Hidden = false;
|
---|
518 | moveQualityAnalyzer.BestKnownQualityParameter.Hidden = false;
|
---|
519 | tabuNeighborhoodAnalyzer.IsTabuParameter.Hidden = false;
|
---|
520 | }
|
---|
521 | }
|
---|
522 | private void ParameterizeIterationBasedOperators() {
|
---|
523 | if (Problem != null) {
|
---|
524 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
525 | op.IterationsParameter.ActualName = "Iterations";
|
---|
526 | op.IterationsParameter.Hidden = true;
|
---|
527 | op.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
528 | op.MaximumIterationsParameter.Hidden = true;
|
---|
529 | }
|
---|
530 | }
|
---|
531 | }
|
---|
532 | private TabuSearchMainLoop FindMainLoop(IOperator start) {
|
---|
533 | IOperator mainLoop = start;
|
---|
534 | while (mainLoop != null && !(mainLoop is TabuSearchMainLoop))
|
---|
535 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
536 | if (mainLoop == null) return null;
|
---|
537 | else return (TabuSearchMainLoop)mainLoop;
|
---|
538 | }
|
---|
539 | #endregion
|
---|
540 | }
|
---|
541 | }
|
---|