Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.TabuSearch/3.3/TabuSearch.cs @ 3623

Last change on this file since 3623 was 3616, checked in by swagner, 15 years ago

Worked on refactoring of algorithm analysis and tracing (#999)

  • adapted GA and TSP
  • removed stuff related to visualizers
File size: 19.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Random;
35
36namespace HeuristicLab.Algorithms.TabuSearch {
37  [Item("Tabu Search", "A tabu search algorithm.")]
38  [Creatable("Algorithms")]
39  [StorableClass]
40  public sealed class TabuSearch : EngineAlgorithm {
41    #region Problem Properties
42    public override Type ProblemType {
43      get { return typeof(ISingleObjectiveProblem); }
44    }
45    public new ISingleObjectiveProblem Problem {
46      get { return (ISingleObjectiveProblem)base.Problem; }
47      set { base.Problem = value; }
48    }
49    #endregion
50
51    #region Parameter Properties
52    private ValueParameter<IntValue> SeedParameter {
53      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
54    }
55    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
56      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
57    }
58    private ConstrainedValueParameter<IMoveGenerator> MoveGeneratorParameter {
59      get { return (ConstrainedValueParameter<IMoveGenerator>)Parameters["MoveGenerator"]; }
60    }
61    private ConstrainedValueParameter<IMoveMaker> MoveMakerParameter {
62      get { return (ConstrainedValueParameter<IMoveMaker>)Parameters["MoveMaker"]; }
63    }
64    private ConstrainedValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter {
65      get { return (ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>)Parameters["MoveEvaluator"]; }
66    }
67    private ConstrainedValueParameter<ITabuChecker> TabuCheckerParameter {
68      get { return (ConstrainedValueParameter<ITabuChecker>)Parameters["TabuChecker"]; }
69    }
70    private ConstrainedValueParameter<ITabuMaker> TabuMakerParameter {
71      get { return (ConstrainedValueParameter<ITabuMaker>)Parameters["TabuMaker"]; }
72    }
73    private ValueParameter<IntValue> TabuTenureParameter {
74      get { return (ValueParameter<IntValue>)Parameters["TabuTenure"]; }
75    }
76    private ValueParameter<IntValue> MaximumIterationsParameter {
77      get { return (ValueParameter<IntValue>)Parameters["MaximumIterations"]; }
78    }
79    private ValueParameter<IntValue> SampleSizeParameter {
80      get { return (ValueParameter<IntValue>)Parameters["SampleSize"]; }
81    }
82    #endregion
83
84    #region Properties
85    public IntValue Seed {
86      get { return SeedParameter.Value; }
87      set { SeedParameter.Value = value; }
88    }
89    public BoolValue SetSeedRandomly {
90      get { return SetSeedRandomlyParameter.Value; }
91      set { SetSeedRandomlyParameter.Value = value; }
92    }
93    public IMoveGenerator MoveGenerator {
94      get { return MoveGeneratorParameter.Value; }
95      set { MoveGeneratorParameter.Value = value; }
96    }
97    public IMoveMaker MoveMaker {
98      get { return MoveMakerParameter.Value; }
99      set { MoveMakerParameter.Value = value; }
100    }
101    public ISingleObjectiveMoveEvaluator MoveEvaluator {
102      get { return MoveEvaluatorParameter.Value; }
103      set { MoveEvaluatorParameter.Value = value; }
104    }
105    public ITabuChecker TabuChecker {
106      get { return TabuCheckerParameter.Value; }
107      set { TabuCheckerParameter.Value = value; }
108    }
109    public ITabuMaker TabuMaker {
110      get { return TabuMakerParameter.Value; }
111      set { TabuMakerParameter.Value = value; }
112    }
113    public IntValue TabuTenure {
114      get { return TabuTenureParameter.Value; }
115      set { TabuTenureParameter.Value = value; }
116    }
117    public IntValue MaximumIterations {
118      get { return MaximumIterationsParameter.Value; }
119      set { MaximumIterationsParameter.Value = value; }
120    }
121    private RandomCreator RandomCreator {
122      get { return (RandomCreator)OperatorGraph.InitialOperator; }
123    }
124    private SolutionsCreator SolutionsCreator {
125      get { return (SolutionsCreator)RandomCreator.Successor; }
126    }
127    private TabuSearchMainLoop MainLoop {
128      get { return (TabuSearchMainLoop)SolutionsCreator.Successor; }
129    }
130    #endregion
131
132    public TabuSearch()
133      : base() {
134      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
135      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
136      Parameters.Add(new ConstrainedValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));
137      Parameters.Add(new ConstrainedValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move."));
138      Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move."));
139      Parameters.Add(new ConstrainedValueParameter<ITabuChecker>("TabuChecker", "The operator to check whether a move is tabu or not."));
140      Parameters.Add(new ConstrainedValueParameter<ITabuMaker>("TabuMaker", "The operator used to insert attributes of a move into the tabu list."));
141      Parameters.Add(new ValueParameter<IntValue>("TabuTenure", "The length of the tabu list.", new IntValue(10)));
142      Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(1000)));
143      Parameters.Add(new ValueParameter<IntValue>("SampleSize", "The neighborhood size for stochastic sampling move generators", new IntValue(100)));
144
145      RandomCreator randomCreator = new RandomCreator();
146      SolutionsCreator solutionsCreator = new SolutionsCreator();
147      TabuSearchMainLoop tsMainLoop = new TabuSearchMainLoop();
148      OperatorGraph.InitialOperator = randomCreator;
149
150      randomCreator.RandomParameter.ActualName = "Random";
151      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
152      randomCreator.SeedParameter.Value = null;
153      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
154      randomCreator.SetSeedRandomlyParameter.Value = null;
155      randomCreator.Successor = solutionsCreator;
156
157      solutionsCreator.NumberOfSolutions = new IntValue(1);
158      solutionsCreator.Successor = tsMainLoop;
159
160      tsMainLoop.MoveGeneratorParameter.ActualName = MoveGeneratorParameter.Name;
161      tsMainLoop.MoveMakerParameter.ActualName = MoveMakerParameter.Name;
162      tsMainLoop.MoveEvaluatorParameter.ActualName = MoveEvaluatorParameter.Name;
163      tsMainLoop.TabuCheckerParameter.ActualName = TabuCheckerParameter.Name;
164      tsMainLoop.TabuMakerParameter.ActualName = TabuMakerParameter.Name;
165      tsMainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
166      tsMainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
167      tsMainLoop.ResultsParameter.ActualName = "Results";
168
169      Initialize();
170    }
171    [StorableConstructor]
172    private TabuSearch(bool deserializing) : base(deserializing) { }
173
174    public override IDeepCloneable Clone(Cloner cloner) {
175      TabuSearch clone = (TabuSearch)base.Clone(cloner);
176      clone.Initialize();
177      return clone;
178    }
179
180    public override void Prepare() {
181      if (Problem != null && MoveGenerator != null && MoveMaker != null && MoveEvaluator != null &&
182          TabuChecker != null && TabuMaker != null)
183        base.Prepare();
184    }
185
186    #region Events
187    protected override void OnProblemChanged() {
188      ParameterizeStochasticOperator(Problem.SolutionCreator);
189      ParameterizeStochasticOperator(Problem.Evaluator);
190      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
191      foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
192        op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
193      }
194      foreach (ITabuChecker op in Problem.Operators.OfType<ITabuChecker>()) {
195        op.MoveTabuParameter.ActualNameChanged += new EventHandler(TabuChecker_MoveTabuParameter_ActualNameChanged);
196      }
197      ParameterizeSolutionsCreator();
198      ParameterizeMainLoop();
199      UpdateMoveGenerator();
200      UpdateMoveParameters();
201      ParameterizeMoveGenerators();
202      ParameterizeMoveEvaluator();
203      ParameterizeMoveMaker();
204      ParameterizeTabuMaker();
205      ParameterizeTabuChecker();
206      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
207      base.OnProblemChanged();
208    }
209    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
210      ParameterizeStochasticOperator(Problem.SolutionCreator);
211      ParameterizeSolutionsCreator();
212      base.Problem_SolutionCreatorChanged(sender, e);
213    }
214    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
215      ParameterizeStochasticOperator(Problem.Evaluator);
216      ParameterizeSolutionsCreator();
217      ParameterizeMainLoop();
218      ParameterizeMoveEvaluator();
219      ParameterizeMoveMaker();
220      ParameterizeTabuMaker();
221      ParameterizeTabuChecker();
222      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
223      base.Problem_EvaluatorChanged(sender, e);
224    }
225    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
226      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
227      // This may seem pointless, but some operators already have the eventhandler registered, others don't
228      // FIXME: Is there another way to solve this problem?
229      foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
230        op.MoveQualityParameter.ActualNameChanged -= new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
231        op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
232      }
233      foreach (ITabuChecker op in Problem.Operators.OfType<ITabuChecker>()) {
234        op.MoveTabuParameter.ActualNameChanged -= new EventHandler(TabuChecker_MoveTabuParameter_ActualNameChanged);
235        op.MoveTabuParameter.ActualNameChanged += new EventHandler(TabuChecker_MoveTabuParameter_ActualNameChanged);
236      }
237      UpdateMoveGenerator();
238      UpdateMoveParameters();
239      ParameterizeMainLoop();
240      ParameterizeMoveGenerators();
241      ParameterizeMoveEvaluator();
242      ParameterizeMoveMaker();
243      ParameterizeTabuMaker();
244      ParameterizeTabuChecker();
245      base.Problem_OperatorsChanged(sender, e);
246    }
247    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
248      ParameterizeMainLoop();
249      ParameterizeMoveEvaluator();
250      ParameterizeMoveMaker();
251      ParameterizeTabuMaker();
252      ParameterizeTabuChecker();
253    }
254    private void MoveGeneratorParameter_ValueChanged(object sender, EventArgs e) {
255      UpdateMoveParameters();
256    }
257    private void MoveEvaluatorParameter_ValueChanged(object sender, EventArgs e) {
258      ParameterizeMainLoop();
259      ParameterizeMoveEvaluator();
260      ParameterizeMoveMaker();
261      ParameterizeTabuMaker();
262      ParameterizeTabuChecker();
263    }
264    private void MoveEvaluator_MoveQualityParameter_ActualNameChanged(object sender, EventArgs e) {
265      ParameterizeMainLoop();
266      ParameterizeMoveEvaluator();
267      ParameterizeMoveMaker();
268      ParameterizeTabuMaker();
269      ParameterizeTabuChecker();
270    }
271    private void TabuCheckerParameter_ValueChanged(object sender, EventArgs e) {
272      ParameterizeMainLoop();
273    }
274    private void TabuChecker_MoveTabuParameter_ActualNameChanged(object sender, EventArgs e) {
275      ParameterizeMainLoop();
276    }
277    private void SampleSizeParameter_NameChanged(object sender, EventArgs e) {
278      ParameterizeMoveGenerators();
279    }
280    #endregion
281
282    #region Helpers
283    [StorableHook(HookType.AfterDeserialization)]
284    private void Initialize() {
285      if (Problem != null) {
286        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
287        foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
288          op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
289        }
290      }
291      MoveGeneratorParameter.ValueChanged += new EventHandler(MoveGeneratorParameter_ValueChanged);
292      MoveEvaluatorParameter.ValueChanged += new EventHandler(MoveEvaluatorParameter_ValueChanged);
293      TabuCheckerParameter.ValueChanged += new EventHandler(TabuCheckerParameter_ValueChanged);
294      SampleSizeParameter.NameChanged += new EventHandler(SampleSizeParameter_NameChanged);
295    }
296    private void UpdateMoveGenerator() {
297      IMoveGenerator oldMoveGenerator = MoveGenerator;
298      MoveGeneratorParameter.ValidValues.Clear();
299      if (Problem != null) {
300        foreach (IMoveGenerator generator in Problem.Operators.OfType<IMoveGenerator>().OrderBy(x => x.Name)) {
301          MoveGeneratorParameter.ValidValues.Add(generator);
302        }
303      }
304      if (oldMoveGenerator != null && MoveGeneratorParameter.ValidValues.Any(x => x.GetType() == oldMoveGenerator.GetType()))
305        MoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
306      if (MoveGenerator == null) {
307        ClearMoveParameters();
308      }
309    }
310    private void UpdateMoveParameters() {
311      IMoveMaker oldMoveMaker = MoveMaker;
312      ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
313      ITabuChecker oldTabuMoveEvaluator = TabuChecker;
314      ITabuMaker oldTabuMoveMaker = TabuMaker;
315      ClearMoveParameters();
316      if (MoveGenerator != null) {
317        List<Type> moveTypes = MoveGenerator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();
318        foreach (Type type in moveTypes.ToList()) {
319          if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t)))
320            moveTypes.Remove(type);
321        }
322        foreach (Type type in moveTypes) {
323          var operators = Problem.Operators.Where(x => type.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
324          foreach (IMoveMaker moveMaker in operators.OfType<IMoveMaker>())
325            MoveMakerParameter.ValidValues.Add(moveMaker);
326          foreach (ISingleObjectiveMoveEvaluator moveEvaluator in operators.OfType<ISingleObjectiveMoveEvaluator>())
327            MoveEvaluatorParameter.ValidValues.Add(moveEvaluator);
328          foreach (ITabuChecker tabuMoveEvaluator in operators.OfType<ITabuChecker>())
329            TabuCheckerParameter.ValidValues.Add(tabuMoveEvaluator);
330          foreach (ITabuMaker tabuMoveMaker in operators.OfType<ITabuMaker>())
331            TabuMakerParameter.ValidValues.Add(tabuMoveMaker);
332        }
333        if (oldMoveMaker != null) {
334          IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
335          if (mm != null) MoveMaker = mm;
336        }
337        if (oldMoveEvaluator != null) {
338          ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
339          if (me != null) MoveEvaluator = me;
340        }
341        if (oldTabuMoveMaker != null) {
342          ITabuMaker tmm = TabuMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuMoveMaker.GetType());
343          if (tmm != null) TabuMaker = tmm;
344        }
345        if (oldTabuMoveEvaluator != null) {
346          ITabuChecker tme = TabuCheckerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuMoveEvaluator.GetType());
347          if (tme != null) TabuChecker = tme;
348        }
349      }
350    }
351    private void ClearMoveParameters() {
352      MoveMakerParameter.ValidValues.Clear();
353      MoveEvaluatorParameter.ValidValues.Clear();
354      TabuCheckerParameter.ValidValues.Clear();
355      TabuMakerParameter.ValidValues.Clear();
356    }
357    private void ParameterizeSolutionsCreator() {
358      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
359      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
360    }
361    private void ParameterizeMainLoop() {
362      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
363      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
364      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
365      if (MoveEvaluator != null)
366        MainLoop.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
367      if (TabuChecker != null)
368        MainLoop.MoveTabuParameter.ActualName = TabuChecker.MoveTabuParameter.ActualName;
369    }
370    private void ParameterizeStochasticOperator(IOperator op) {
371      if (op is IStochasticOperator)
372        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
373    }
374    private void ParameterizeMoveGenerators() {
375      if (Problem != null) {
376        foreach (IMultiMoveGenerator generator in Problem.Operators.OfType<IMultiMoveGenerator>())
377          generator.SampleSizeParameter.ActualName = SampleSizeParameter.Name;
378      }
379    }
380    private void ParameterizeMoveEvaluator() {
381      foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
382        op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
383      }
384    }
385    private void ParameterizeMoveMaker() {
386      foreach (IMoveMaker op in Problem.Operators.OfType<IMoveMaker>()) {
387        op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
388        if (MoveEvaluator != null)
389          op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
390      }
391    }
392    private void ParameterizeTabuMaker() {
393      foreach (ITabuMaker op in Problem.Operators.OfType<ITabuMaker>()) {
394        op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
395        if (MoveEvaluator != null)
396          op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
397      }
398    }
399    private void ParameterizeTabuChecker() {
400      foreach (ITabuChecker op in Problem.Operators.OfType<ITabuChecker>()) {
401        if (MoveEvaluator != null)
402          op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
403        if (TabuChecker != null)
404          op.MoveTabuParameter.ActualName = TabuChecker.MoveTabuParameter.ActualName;
405      }
406    }
407    #endregion
408  }
409}
Note: See TracBrowser for help on using the repository browser.