1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using 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 HEAL.Attic;
|
---|
34 | using HeuristicLab.Random;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.TabuSearch {
|
---|
37 | [Item("Tabu Search (TS)", "A tabu search algorithm.")]
|
---|
38 | [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 120)]
|
---|
39 | [StorableType("CF028C58-BE6D-4F74-96DD-B1E2CFAD5AA0")]
|
---|
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 | public IConstrainedValueParameter<IMoveGenerator> MoveGeneratorParameter {
|
---|
61 | get { return (IConstrainedValueParameter<IMoveGenerator>)Parameters["MoveGenerator"]; }
|
---|
62 | }
|
---|
63 | public IConstrainedValueParameter<IMoveMaker> MoveMakerParameter {
|
---|
64 | get { return (IConstrainedValueParameter<IMoveMaker>)Parameters["MoveMaker"]; }
|
---|
65 | }
|
---|
66 | public IConstrainedValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter {
|
---|
67 | get { return (IConstrainedValueParameter<ISingleObjectiveMoveEvaluator>)Parameters["MoveEvaluator"]; }
|
---|
68 | }
|
---|
69 | public IConstrainedValueParameter<ITabuChecker> TabuCheckerParameter {
|
---|
70 | get { return (IConstrainedValueParameter<ITabuChecker>)Parameters["TabuChecker"]; }
|
---|
71 | }
|
---|
72 | public IConstrainedValueParameter<ITabuMaker> TabuMakerParameter {
|
---|
73 | get { return (IConstrainedValueParameter<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 IntValue SampleSize {
|
---|
127 | get { return SampleSizeParameter.Value; }
|
---|
128 | set { SampleSizeParameter.Value = value; }
|
---|
129 | }
|
---|
130 | public MultiAnalyzer Analyzer {
|
---|
131 | get { return AnalyzerParameter.Value; }
|
---|
132 | set { AnalyzerParameter.Value = value; }
|
---|
133 | }
|
---|
134 | private RandomCreator RandomCreator {
|
---|
135 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
136 | }
|
---|
137 | private SolutionsCreator SolutionsCreator {
|
---|
138 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
139 | }
|
---|
140 | private TabuSearchMainLoop MainLoop {
|
---|
141 | get { return FindMainLoop(SolutionsCreator.Successor); }
|
---|
142 | }
|
---|
143 | [Storable]
|
---|
144 | private BestAverageWorstQualityAnalyzer moveQualityAnalyzer;
|
---|
145 | [Storable]
|
---|
146 | private TabuNeighborhoodAnalyzer tabuNeighborhoodAnalyzer;
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | public TabuSearch()
|
---|
150 | : base() {
|
---|
151 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
152 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
153 | Parameters.Add(new ConstrainedValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));
|
---|
154 | Parameters.Add(new ConstrainedValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move."));
|
---|
155 | Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move."));
|
---|
156 | Parameters.Add(new ConstrainedValueParameter<ITabuChecker>("TabuChecker", "The operator to check whether a move is tabu or not."));
|
---|
157 | Parameters.Add(new ConstrainedValueParameter<ITabuMaker>("TabuMaker", "The operator used to insert attributes of a move into the tabu list."));
|
---|
158 | Parameters.Add(new ValueParameter<IntValue>("TabuTenure", "The length of the tabu list.", new IntValue(10)));
|
---|
159 | Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
160 | Parameters.Add(new ValueParameter<IntValue>("SampleSize", "The neighborhood size for stochastic sampling move generators", new IntValue(100)));
|
---|
161 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the solution.", new MultiAnalyzer()));
|
---|
162 |
|
---|
163 | RandomCreator randomCreator = new RandomCreator();
|
---|
164 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
165 | VariableCreator variableCreator = new VariableCreator();
|
---|
166 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
167 | TabuSearchMainLoop mainLoop = new TabuSearchMainLoop();
|
---|
168 | OperatorGraph.InitialOperator = randomCreator;
|
---|
169 |
|
---|
170 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
171 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
172 | randomCreator.SeedParameter.Value = null;
|
---|
173 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
174 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
175 | randomCreator.Successor = solutionsCreator;
|
---|
176 |
|
---|
177 | solutionsCreator.NumberOfSolutions = new IntValue(1);
|
---|
178 | solutionsCreator.Successor = variableCreator;
|
---|
179 |
|
---|
180 | variableCreator.Name = "Initialize EvaluatedMoves";
|
---|
181 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedMoves", new IntValue()));
|
---|
182 | variableCreator.Successor = resultsCollector;
|
---|
183 |
|
---|
184 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
|
---|
185 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
186 | resultsCollector.Successor = mainLoop;
|
---|
187 |
|
---|
188 | mainLoop.MoveGeneratorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
189 | mainLoop.MoveMakerParameter.ActualName = MoveMakerParameter.Name;
|
---|
190 | mainLoop.MoveEvaluatorParameter.ActualName = MoveEvaluatorParameter.Name;
|
---|
191 | mainLoop.TabuCheckerParameter.ActualName = TabuCheckerParameter.Name;
|
---|
192 | mainLoop.TabuMakerParameter.ActualName = TabuMakerParameter.Name;
|
---|
193 | mainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
194 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
195 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
196 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
197 | mainLoop.EvaluatedMovesParameter.ActualName = "EvaluatedMoves";
|
---|
198 |
|
---|
199 | moveQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
200 | tabuNeighborhoodAnalyzer = new TabuNeighborhoodAnalyzer();
|
---|
201 | ParameterizeAnalyzers();
|
---|
202 | UpdateAnalyzers();
|
---|
203 |
|
---|
204 | Initialize();
|
---|
205 | }
|
---|
206 | [StorableConstructor]
|
---|
207 | private TabuSearch(StorableConstructorFlag _) : base(_) { }
|
---|
208 | [StorableHook(HookType.AfterDeserialization)]
|
---|
209 | private void AfterDeserialization() {
|
---|
210 | Initialize();
|
---|
211 | }
|
---|
212 | private TabuSearch(TabuSearch original, Cloner cloner)
|
---|
213 | : base(original, cloner) {
|
---|
214 | moveQualityAnalyzer = cloner.Clone(original.moveQualityAnalyzer);
|
---|
215 | tabuNeighborhoodAnalyzer = cloner.Clone(original.tabuNeighborhoodAnalyzer);
|
---|
216 | Initialize();
|
---|
217 | }
|
---|
218 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
219 | return new TabuSearch(this, cloner);
|
---|
220 | }
|
---|
221 |
|
---|
222 | public override void Prepare() {
|
---|
223 | if (Problem != null && MoveGenerator != null && MoveMaker != null && MoveEvaluator != null &&
|
---|
224 | TabuChecker != null && TabuMaker != null)
|
---|
225 | base.Prepare();
|
---|
226 | }
|
---|
227 |
|
---|
228 | #region Events
|
---|
229 | protected override void OnProblemChanged() {
|
---|
230 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
231 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
232 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
233 | foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
|
---|
234 | op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
|
---|
235 | }
|
---|
236 | foreach (ITabuChecker op in Problem.Operators.OfType<ITabuChecker>()) {
|
---|
237 | op.MoveTabuParameter.ActualNameChanged += new EventHandler(TabuChecker_MoveTabuParameter_ActualNameChanged);
|
---|
238 | }
|
---|
239 | ParameterizeSolutionsCreator();
|
---|
240 | ParameterizeMainLoop();
|
---|
241 | UpdateMoveGenerator();
|
---|
242 | UpdateMoveParameters();
|
---|
243 | UpdateAnalyzers();
|
---|
244 | ParameterizeMoveGenerators();
|
---|
245 | ParameterizeMoveEvaluator();
|
---|
246 | ParameterizeMoveMaker();
|
---|
247 | ParameterizeTabuMaker();
|
---|
248 | ParameterizeTabuChecker();
|
---|
249 | ParameterizeAnalyzers();
|
---|
250 | ParameterizeIterationBasedOperators();
|
---|
251 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
252 | base.OnProblemChanged();
|
---|
253 | }
|
---|
254 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
255 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
256 | ParameterizeSolutionsCreator();
|
---|
257 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
258 | }
|
---|
259 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
260 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
261 | ParameterizeSolutionsCreator();
|
---|
262 | ParameterizeMainLoop();
|
---|
263 | ParameterizeMoveEvaluator();
|
---|
264 | ParameterizeMoveMaker();
|
---|
265 | ParameterizeTabuMaker();
|
---|
266 | ParameterizeTabuChecker();
|
---|
267 | ParameterizeAnalyzers();
|
---|
268 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
269 | base.Problem_EvaluatorChanged(sender, e);
|
---|
270 | }
|
---|
271 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
272 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
273 | // This may seem pointless, but some operators already have the eventhandler registered, others don't
|
---|
274 | // FIXME: Is there another way to solve this problem?
|
---|
275 | foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
|
---|
276 | op.MoveQualityParameter.ActualNameChanged -= new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
|
---|
277 | op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
|
---|
278 | }
|
---|
279 | foreach (ITabuChecker op in Problem.Operators.OfType<ITabuChecker>()) {
|
---|
280 | op.MoveTabuParameter.ActualNameChanged -= new EventHandler(TabuChecker_MoveTabuParameter_ActualNameChanged);
|
---|
281 | op.MoveTabuParameter.ActualNameChanged += new EventHandler(TabuChecker_MoveTabuParameter_ActualNameChanged);
|
---|
282 | }
|
---|
283 | UpdateMoveGenerator();
|
---|
284 | UpdateMoveParameters();
|
---|
285 | UpdateAnalyzers();
|
---|
286 | ParameterizeMainLoop();
|
---|
287 | ParameterizeMoveGenerators();
|
---|
288 | ParameterizeMoveEvaluator();
|
---|
289 | ParameterizeMoveMaker();
|
---|
290 | ParameterizeTabuMaker();
|
---|
291 | ParameterizeTabuChecker();
|
---|
292 | ParameterizeAnalyzers();
|
---|
293 | ParameterizeIterationBasedOperators();
|
---|
294 | base.Problem_OperatorsChanged(sender, e);
|
---|
295 | }
|
---|
296 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
297 | ParameterizeMainLoop();
|
---|
298 | ParameterizeMoveEvaluator();
|
---|
299 | ParameterizeMoveMaker();
|
---|
300 | ParameterizeTabuMaker();
|
---|
301 | ParameterizeTabuChecker();
|
---|
302 | }
|
---|
303 | private void MoveGeneratorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
304 | UpdateMoveParameters();
|
---|
305 | }
|
---|
306 | private void MoveEvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
307 | ParameterizeMainLoop();
|
---|
308 | ParameterizeMoveEvaluator();
|
---|
309 | ParameterizeMoveMaker();
|
---|
310 | ParameterizeTabuMaker();
|
---|
311 | ParameterizeTabuChecker();
|
---|
312 | ParameterizeAnalyzers();
|
---|
313 | }
|
---|
314 | private void MoveEvaluator_MoveQualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
315 | ParameterizeMainLoop();
|
---|
316 | ParameterizeMoveEvaluator();
|
---|
317 | ParameterizeMoveMaker();
|
---|
318 | ParameterizeTabuMaker();
|
---|
319 | ParameterizeTabuChecker();
|
---|
320 | ParameterizeAnalyzers();
|
---|
321 | }
|
---|
322 | private void TabuCheckerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
323 | ParameterizeMainLoop();
|
---|
324 | ParameterizeAnalyzers();
|
---|
325 | }
|
---|
326 | private void TabuChecker_MoveTabuParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
327 | ParameterizeMainLoop();
|
---|
328 | ParameterizeAnalyzers();
|
---|
329 | }
|
---|
330 | private void SampleSizeParameter_NameChanged(object sender, EventArgs e) {
|
---|
331 | ParameterizeMoveGenerators();
|
---|
332 | }
|
---|
333 | #endregion
|
---|
334 |
|
---|
335 | #region Helpers
|
---|
336 | private void Initialize() {
|
---|
337 | if (Problem != null) {
|
---|
338 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
339 | foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
|
---|
340 | op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
|
---|
341 | }
|
---|
342 | }
|
---|
343 | MoveGeneratorParameter.ValueChanged += new EventHandler(MoveGeneratorParameter_ValueChanged);
|
---|
344 | MoveEvaluatorParameter.ValueChanged += new EventHandler(MoveEvaluatorParameter_ValueChanged);
|
---|
345 | TabuCheckerParameter.ValueChanged += new EventHandler(TabuCheckerParameter_ValueChanged);
|
---|
346 | SampleSizeParameter.NameChanged += new EventHandler(SampleSizeParameter_NameChanged);
|
---|
347 | }
|
---|
348 | private void UpdateMoveGenerator() {
|
---|
349 | IMoveGenerator oldMoveGenerator = MoveGenerator;
|
---|
350 | IMoveGenerator defaultMoveGenerator = Problem.Operators.OfType<IMoveGenerator>().FirstOrDefault();
|
---|
351 | MoveGeneratorParameter.ValidValues.Clear();
|
---|
352 | if (Problem != null) {
|
---|
353 | // only add move generators that also have a corresponding tabu-checker and tabu-maker
|
---|
354 | foreach (IMoveGenerator generator in Problem.Operators.OfType<IMoveGenerator>().OrderBy(x => x.Name)) {
|
---|
355 | // get all interfaces equal to or derived from IMoveOperator that this move generator implements
|
---|
356 | var moveTypes = generator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();
|
---|
357 | // keep only the most specific interfaces (e.g. IPermutationTranslocationMoveOperator);
|
---|
358 | // by removing all interfaces for which a derived interface is also contained in the moveTypes set
|
---|
359 | foreach (var type in moveTypes.ToList()) {
|
---|
360 | if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t))) moveTypes.Remove(type);
|
---|
361 | }
|
---|
362 | // keep move generator only if there is a tabu checker and a tabu maker that is derived from the same move interface
|
---|
363 | if (Problem.Operators.OfType<ITabuChecker>().Any(op => moveTypes.Any(m => m.IsInstanceOfType(op)))
|
---|
364 | && Problem.Operators.OfType<ITabuMaker>().Any(op => moveTypes.Any(m => m.IsInstanceOfType(op))))
|
---|
365 | MoveGeneratorParameter.ValidValues.Add(generator);
|
---|
366 | }
|
---|
367 | }
|
---|
368 | if (oldMoveGenerator != null && MoveGeneratorParameter.ValidValues.Any(x => x.GetType() == oldMoveGenerator.GetType()))
|
---|
369 | MoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
|
---|
370 | if (MoveGenerator == null && defaultMoveGenerator != null)
|
---|
371 | MoveGenerator = defaultMoveGenerator;
|
---|
372 |
|
---|
373 | if (MoveGenerator == null) {
|
---|
374 | ClearMoveParameters();
|
---|
375 | }
|
---|
376 | }
|
---|
377 | private void UpdateMoveParameters() {
|
---|
378 | IMoveMaker oldMoveMaker = MoveMaker;
|
---|
379 | ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
|
---|
380 | ITabuChecker oldTabuChecker = TabuChecker;
|
---|
381 | ITabuMaker oldTabuMoveMaker = TabuMaker;
|
---|
382 | ClearMoveParameters();
|
---|
383 | if (MoveGenerator != null) {
|
---|
384 | List<Type> moveTypes = MoveGenerator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();
|
---|
385 | foreach (Type type in moveTypes.ToList()) {
|
---|
386 | if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t)))
|
---|
387 | moveTypes.Remove(type);
|
---|
388 | }
|
---|
389 |
|
---|
390 | var operators = Problem.Operators.Where(op => moveTypes.Any(m => m.IsInstanceOfType(op))).ToList();
|
---|
391 | IMoveMaker defaultMoveMaker = operators.OfType<IMoveMaker>().FirstOrDefault();
|
---|
392 | ISingleObjectiveMoveEvaluator defaultMoveEvaluator = operators.OfType<ISingleObjectiveMoveEvaluator>().FirstOrDefault();
|
---|
393 | ITabuChecker defaultTabuChecker = operators.OfType<ITabuChecker>().FirstOrDefault();
|
---|
394 | ITabuMaker defaultTabuMaker = operators.OfType<ITabuMaker>().FirstOrDefault();
|
---|
395 |
|
---|
396 | foreach (IMoveMaker moveMaker in operators.OfType<IMoveMaker>())
|
---|
397 | MoveMakerParameter.ValidValues.Add(moveMaker);
|
---|
398 | foreach (ISingleObjectiveMoveEvaluator moveEvaluator in operators.OfType<ISingleObjectiveMoveEvaluator>())
|
---|
399 | MoveEvaluatorParameter.ValidValues.Add(moveEvaluator);
|
---|
400 | foreach (ITabuChecker tabuMoveEvaluator in operators.OfType<ITabuChecker>())
|
---|
401 | TabuCheckerParameter.ValidValues.Add(tabuMoveEvaluator);
|
---|
402 | foreach (ITabuMaker tabuMoveMaker in operators.OfType<ITabuMaker>())
|
---|
403 | TabuMakerParameter.ValidValues.Add(tabuMoveMaker);
|
---|
404 |
|
---|
405 | if (oldMoveMaker != null) {
|
---|
406 | IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
|
---|
407 | if (mm != null) MoveMaker = mm;
|
---|
408 | else oldMoveMaker = null;
|
---|
409 | }
|
---|
410 | if (oldMoveMaker == null && defaultMoveMaker != null)
|
---|
411 | MoveMaker = defaultMoveMaker;
|
---|
412 |
|
---|
413 | if (oldMoveEvaluator != null) {
|
---|
414 | ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
|
---|
415 | if (me != null) MoveEvaluator = me;
|
---|
416 | else oldMoveEvaluator = null;
|
---|
417 | }
|
---|
418 | if (oldMoveEvaluator == null && defaultMoveEvaluator != null)
|
---|
419 | MoveEvaluator = defaultMoveEvaluator;
|
---|
420 |
|
---|
421 | if (oldTabuMoveMaker != null) {
|
---|
422 | ITabuMaker tmm = TabuMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuMoveMaker.GetType());
|
---|
423 | if (tmm != null) TabuMaker = tmm;
|
---|
424 | else oldTabuMoveMaker = null;
|
---|
425 | }
|
---|
426 | if (oldTabuMoveMaker == null && defaultTabuMaker != null)
|
---|
427 | TabuMaker = defaultTabuMaker;
|
---|
428 |
|
---|
429 | if (oldTabuChecker != null) {
|
---|
430 | ITabuChecker tme = TabuCheckerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuChecker.GetType());
|
---|
431 | if (tme != null) TabuChecker = tme;
|
---|
432 | else oldTabuChecker = null;
|
---|
433 | }
|
---|
434 | if (oldTabuChecker == null && defaultTabuChecker != null)
|
---|
435 | TabuChecker = defaultTabuChecker;
|
---|
436 | }
|
---|
437 | }
|
---|
438 | private void UpdateAnalyzers() {
|
---|
439 | Analyzer.Operators.Clear();
|
---|
440 | if (Problem != null) {
|
---|
441 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
442 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
443 | param.Depth = 0;
|
---|
444 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
445 | }
|
---|
446 | }
|
---|
447 | Analyzer.Operators.Add(moveQualityAnalyzer, moveQualityAnalyzer.EnabledByDefault);
|
---|
448 | Analyzer.Operators.Add(tabuNeighborhoodAnalyzer, tabuNeighborhoodAnalyzer.EnabledByDefault);
|
---|
449 | }
|
---|
450 | private void ClearMoveParameters() {
|
---|
451 | MoveMakerParameter.ValidValues.Clear();
|
---|
452 | MoveEvaluatorParameter.ValidValues.Clear();
|
---|
453 | TabuCheckerParameter.ValidValues.Clear();
|
---|
454 | TabuMakerParameter.ValidValues.Clear();
|
---|
455 | }
|
---|
456 | private void ParameterizeSolutionsCreator() {
|
---|
457 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
458 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
459 | }
|
---|
460 | private void ParameterizeMainLoop() {
|
---|
461 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
462 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
463 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
464 | if (MoveEvaluator != null)
|
---|
465 | MainLoop.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
466 | if (TabuChecker != null)
|
---|
467 | MainLoop.MoveTabuParameter.ActualName = TabuChecker.MoveTabuParameter.ActualName;
|
---|
468 | }
|
---|
469 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
470 | if (op is IStochasticOperator) {
|
---|
471 | IStochasticOperator stOp = (IStochasticOperator)op;
|
---|
472 | stOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
473 | stOp.RandomParameter.Hidden = true;
|
---|
474 | }
|
---|
475 | }
|
---|
476 | private void ParameterizeMoveGenerators() {
|
---|
477 | if (Problem != null) {
|
---|
478 | foreach (IMultiMoveGenerator generator in Problem.Operators.OfType<IMultiMoveGenerator>()) {
|
---|
479 | generator.SampleSizeParameter.ActualName = SampleSizeParameter.Name;
|
---|
480 | generator.SampleSizeParameter.Hidden = true;
|
---|
481 | }
|
---|
482 | }
|
---|
483 | }
|
---|
484 | private void ParameterizeMoveEvaluator() {
|
---|
485 | foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
|
---|
486 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
487 | op.QualityParameter.Hidden = true;
|
---|
488 | }
|
---|
489 | }
|
---|
490 | private void ParameterizeMoveMaker() {
|
---|
491 | foreach (IMoveMaker op in Problem.Operators.OfType<IMoveMaker>()) {
|
---|
492 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
493 | op.QualityParameter.Hidden = true;
|
---|
494 | if (MoveEvaluator != null) {
|
---|
495 | op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
496 | op.MoveQualityParameter.Hidden = true;
|
---|
497 | } else {
|
---|
498 | op.MoveQualityParameter.Hidden = false;
|
---|
499 | }
|
---|
500 | }
|
---|
501 | }
|
---|
502 | private void ParameterizeTabuMaker() {
|
---|
503 | foreach (ITabuMaker op in Problem.Operators.OfType<ITabuMaker>()) {
|
---|
504 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
505 | op.QualityParameter.Hidden = true;
|
---|
506 | if (MoveEvaluator != null) {
|
---|
507 | op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
508 | op.MoveQualityParameter.Hidden = true;
|
---|
509 | } else {
|
---|
510 | op.MoveQualityParameter.Hidden = false;
|
---|
511 | }
|
---|
512 | }
|
---|
513 | }
|
---|
514 | private void ParameterizeTabuChecker() {
|
---|
515 | foreach (ITabuChecker op in Problem.Operators.OfType<ITabuChecker>()) {
|
---|
516 | if (MoveEvaluator != null) {
|
---|
517 | op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
518 | op.MoveQualityParameter.Hidden = true;
|
---|
519 | } else {
|
---|
520 | op.MoveQualityParameter.Hidden = false;
|
---|
521 | }
|
---|
522 | if (TabuChecker != null) {
|
---|
523 | op.MoveTabuParameter.ActualName = TabuChecker.MoveTabuParameter.ActualName;
|
---|
524 | op.MoveTabuParameter.Hidden = true;
|
---|
525 | } else {
|
---|
526 | op.MoveTabuParameter.Hidden = false;
|
---|
527 | }
|
---|
528 | }
|
---|
529 | }
|
---|
530 | private void ParameterizeAnalyzers() {
|
---|
531 | moveQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
532 | moveQualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
533 | tabuNeighborhoodAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
534 | tabuNeighborhoodAnalyzer.ResultsParameter.Hidden = true;
|
---|
535 | tabuNeighborhoodAnalyzer.PercentTabuParameter.ActualName = "PercentTabu";
|
---|
536 | tabuNeighborhoodAnalyzer.PercentTabuParameter.Hidden = true;
|
---|
537 | if (Problem != null) {
|
---|
538 | moveQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
539 | moveQualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
540 | if (MoveEvaluator != null) {
|
---|
541 | moveQualityAnalyzer.QualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
542 | moveQualityAnalyzer.QualityParameter.Hidden = true;
|
---|
543 | } else {
|
---|
544 | moveQualityAnalyzer.QualityParameter.Hidden = false;
|
---|
545 | }
|
---|
546 | moveQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
547 | moveQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
548 | if (TabuChecker != null) {
|
---|
549 | tabuNeighborhoodAnalyzer.IsTabuParameter.ActualName = TabuChecker.MoveTabuParameter.ActualName;
|
---|
550 | tabuNeighborhoodAnalyzer.IsTabuParameter.Hidden = true;
|
---|
551 | } else {
|
---|
552 | tabuNeighborhoodAnalyzer.IsTabuParameter.Hidden = false;
|
---|
553 | }
|
---|
554 | } else {
|
---|
555 | moveQualityAnalyzer.MaximizationParameter.Hidden = false;
|
---|
556 | moveQualityAnalyzer.QualityParameter.Hidden = false;
|
---|
557 | moveQualityAnalyzer.BestKnownQualityParameter.Hidden = false;
|
---|
558 | tabuNeighborhoodAnalyzer.IsTabuParameter.Hidden = false;
|
---|
559 | }
|
---|
560 | }
|
---|
561 | private void ParameterizeIterationBasedOperators() {
|
---|
562 | if (Problem != null) {
|
---|
563 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
564 | op.IterationsParameter.ActualName = "Iterations";
|
---|
565 | op.IterationsParameter.Hidden = true;
|
---|
566 | op.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
567 | op.MaximumIterationsParameter.Hidden = true;
|
---|
568 | }
|
---|
569 | }
|
---|
570 | }
|
---|
571 | private TabuSearchMainLoop FindMainLoop(IOperator start) {
|
---|
572 | IOperator mainLoop = start;
|
---|
573 | while (mainLoop != null && !(mainLoop is TabuSearchMainLoop))
|
---|
574 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
575 | if (mainLoop == null) return null;
|
---|
576 | else return (TabuSearchMainLoop)mainLoop;
|
---|
577 | }
|
---|
578 | #endregion
|
---|
579 | }
|
---|
580 | }
|
---|