Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3155 was 3145, checked in by abeham, 15 years ago

fixed some things in trajectory based algorithms #933

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.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Algorithms.TabuSearch {
35  [Item("TabuSearch", "A tabu search algorithm.")]
36  [Creatable("Algorithms")]
37  public sealed class TabuSearch : EngineAlgorithm {
38    #region Problem Properties
39    public override Type ProblemType {
40      get { return typeof(ISingleObjectiveProblem); }
41    }
42    public new ISingleObjectiveProblem Problem {
43      get { return (ISingleObjectiveProblem)base.Problem; }
44      set { base.Problem = value; }
45    }
46    #endregion
47
48    #region Parameter Properties
49    private ValueParameter<IntValue> SeedParameter {
50      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
51    }
52    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
53      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
54    }
55    private ConstrainedValueParameter<IMoveGenerator> MoveGeneratorParameter {
56      get { return (ConstrainedValueParameter<IMoveGenerator>)Parameters["MoveGenerator"]; }
57    }
58    private ConstrainedValueParameter<IMoveMaker> MoveMakerParameter {
59      get { return (ConstrainedValueParameter<IMoveMaker>)Parameters["MoveMaker"]; }
60    }
61    private ConstrainedValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter {
62      get { return (ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>)Parameters["MoveEvaluator"]; }
63    }
64    private ConstrainedValueParameter<ITabuMoveEvaluator> TabuMoveEvaluatorParameter {
65      get { return (ConstrainedValueParameter<ITabuMoveEvaluator>)Parameters["TabuMoveEvaluator"]; }
66    }
67    private ConstrainedValueParameter<ITabuMoveMaker> TabuMoveMakerParameter {
68      get { return (ConstrainedValueParameter<ITabuMoveMaker>)Parameters["TabuMoveMaker"]; }
69    }
70    private ValueParameter<IntValue> TabuTenureParameter {
71      get { return (ValueParameter<IntValue>)Parameters["TabuTenure"]; }
72    }
73    private ValueParameter<IntValue> MaximumIterationsParameter {
74      get { return (ValueParameter<IntValue>)Parameters["MaximumIterations"]; }
75    }
76    private ValueParameter<IntValue> SampleSizeParameter {
77      get { return (ValueParameter<IntValue>)Parameters["SampleSize"]; }
78    }
79    #endregion
80
81    #region Properties
82    public IntValue Seed {
83      get { return SeedParameter.Value; }
84      set { SeedParameter.Value = value; }
85    }
86    public BoolValue SetSeedRandomly {
87      get { return SetSeedRandomlyParameter.Value; }
88      set { SetSeedRandomlyParameter.Value = value; }
89    }
90    public IMoveGenerator MoveGenerator {
91      get { return MoveGeneratorParameter.Value; }
92      set { MoveGeneratorParameter.Value = value; }
93    }
94    public IMoveMaker MoveMaker {
95      get { return MoveMakerParameter.Value; }
96      set { MoveMakerParameter.Value = value; }
97    }
98    public ISingleObjectiveMoveEvaluator MoveEvaluator {
99      get { return MoveEvaluatorParameter.Value; }
100      set { MoveEvaluatorParameter.Value = value; }
101    }
102    public ITabuMoveEvaluator TabuMoveEvaluator {
103      get { return TabuMoveEvaluatorParameter.Value; }
104      set { TabuMoveEvaluatorParameter.Value = value; }
105    }
106    public ITabuMoveMaker TabuMoveMaker {
107      get { return TabuMoveMakerParameter.Value; }
108      set { TabuMoveMakerParameter.Value = value; }
109    }
110    public IntValue TabuTenure {
111      get { return TabuTenureParameter.Value; }
112      set { TabuTenureParameter.Value = value; }
113    }
114    public IntValue MaximumIterations {
115      get { return MaximumIterationsParameter.Value; }
116      set { MaximumIterationsParameter.Value = value; }
117    }
118    private RandomCreator RandomCreator {
119      get { return (RandomCreator)OperatorGraph.InitialOperator; }
120    }
121    private SolutionsCreator SolutionsCreator {
122      get { return (SolutionsCreator)RandomCreator.Successor; }
123    }
124    private TabuSearchMainLoop MainLoop {
125      get { return (TabuSearchMainLoop)SolutionsCreator.Successor; }
126    }
127    #endregion
128
129    [StorableConstructor]
130    private TabuSearch(bool deserializing) : base() { }
131    public TabuSearch()
132      : base() {
133      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
134      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
135      Parameters.Add(new ConstrainedValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));
136      Parameters.Add(new ConstrainedValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move."));
137      Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move."));
138      Parameters.Add(new ConstrainedValueParameter<ITabuMoveEvaluator>("TabuMoveEvaluator", "The operator to evaluate whether a move is tabu or not."));
139      Parameters.Add(new ConstrainedValueParameter<ITabuMoveMaker>("TabuMoveMaker", "The operator used to insert attributes of a move into the tabu list."));
140      Parameters.Add(new ValueParameter<IntValue>("TabuTenure", "The length of the tabu list.", new IntValue(10)));
141      Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(1000)));
142      Parameters.Add(new ValueParameter<IntValue>("SampleSize", "The neighborhood size for stochastic sampling move generators", new IntValue(20)));
143
144      RandomCreator randomCreator = new RandomCreator();
145      SolutionsCreator solutionsCreator = new SolutionsCreator();
146      TabuSearchMainLoop tsMainLoop = new TabuSearchMainLoop();
147      OperatorGraph.InitialOperator = randomCreator;
148
149      randomCreator.RandomParameter.ActualName = "Random";
150      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
151      randomCreator.SeedParameter.Value = null;
152      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
153      randomCreator.SetSeedRandomlyParameter.Value = null;
154      randomCreator.Successor = solutionsCreator;
155
156      solutionsCreator.NumberOfSolutions = new IntValue(1);
157      solutionsCreator.Successor = tsMainLoop;
158
159      tsMainLoop.MoveGeneratorParameter.ActualName = MoveGeneratorParameter.Name;
160      tsMainLoop.MoveMakerParameter.ActualName = MoveMakerParameter.Name;
161      tsMainLoop.MoveEvaluatorParameter.ActualName = MoveEvaluatorParameter.Name;
162      tsMainLoop.TabuMoveEvaluatorParameter.ActualName = TabuMoveEvaluatorParameter.Name;
163      tsMainLoop.TabuMoveMakerParameter.ActualName = TabuMoveMakerParameter.Name;
164      tsMainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
165      tsMainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
166      tsMainLoop.ResultsParameter.ActualName = "Results";
167
168      Initialize();
169    }
170
171    public override IDeepCloneable Clone(Cloner cloner) {
172      TabuSearch clone = (TabuSearch)base.Clone(cloner);
173      clone.Initialize();
174      return clone;
175    }
176
177    #region Events
178    protected override void OnProblemChanged() {
179      ParameterizeStochasticOperator(Problem.SolutionCreator);
180      ParameterizeStochasticOperator(Problem.Evaluator);
181      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
182      foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
183        op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
184      }
185      foreach (ITabuMoveEvaluator op in Problem.Operators.OfType<ITabuMoveEvaluator>()) {
186        op.MoveTabuParameter.ActualNameChanged += new EventHandler(TabuMoveEvaluator_MoveTabuParameter_ActualNameChanged);
187      }
188      ParameterizeSolutionsCreator();
189      ParameterizeMainLoop();
190      ParameterizeMoveGenerators();
191      ParameterizeMoveEvaluator();
192      ParameterizeMoveMaker();
193      UpdateMoveGenerator();
194      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
195      base.OnProblemChanged();
196    }
197    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
198      ParameterizeStochasticOperator(Problem.SolutionCreator);
199      ParameterizeSolutionsCreator();
200      base.Problem_SolutionCreatorChanged(sender, e);
201    }
202    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
203      ParameterizeStochasticOperator(Problem.Evaluator);
204      ParameterizeSolutionsCreator();
205      ParameterizeMainLoop();
206      ParameterizeMoveEvaluator();
207      ParameterizeMoveMaker();
208      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
209      base.Problem_EvaluatorChanged(sender, e);
210    }
211    protected override void Problem_VisualizerChanged(object sender, EventArgs e) {
212      ParameterizeStochasticOperator(Problem.Visualizer);
213      ParameterizeMainLoop();
214      if (Problem.Visualizer != null) Problem.Visualizer.VisualizationParameter.ActualNameChanged += new EventHandler(Visualizer_VisualizationParameter_ActualNameChanged);
215      base.Problem_VisualizerChanged(sender, e);
216    }
217    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
218      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
219      // This may seem pointless, but some operators already have the eventhandler registered, others don't
220      // FIXME: Is there another way to solve this problem?
221      foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
222        op.MoveQualityParameter.ActualNameChanged -= new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
223        op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
224      }
225      foreach (ITabuMoveEvaluator op in Problem.Operators.OfType<ITabuMoveEvaluator>()) {
226        op.MoveTabuParameter.ActualNameChanged -= new EventHandler(TabuMoveEvaluator_MoveTabuParameter_ActualNameChanged);
227        op.MoveTabuParameter.ActualNameChanged += new EventHandler(TabuMoveEvaluator_MoveTabuParameter_ActualNameChanged);
228      }
229      IMoveGenerator oldMoveGenerator = MoveGenerator;
230      UpdateMoveGenerator();
231      if (oldMoveGenerator == MoveGenerator) // in this case MoveGeneratorParameter_ValueChanged did not fire
232        UpdateMoveParameters();
233      ParameterizeMainLoop();
234      ParameterizeMoveGenerators();
235      ParameterizeMoveEvaluator();
236      ParameterizeMoveMaker();
237      base.Problem_OperatorsChanged(sender, e);
238    }
239    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
240      ParameterizeMainLoop();
241      ParameterizeMoveEvaluator();
242      ParameterizeMoveMaker();
243    }
244    private void MoveGeneratorParameter_ValueChanged(object sender, EventArgs e) {
245      UpdateMoveParameters();
246    }
247    private void MoveEvaluatorParameter_ValueChanged(object sender, EventArgs e) {
248      ParameterizeMainLoop();
249      ParameterizeMoveEvaluator();
250      ParameterizeMoveMaker();
251    }
252    private void MoveEvaluator_MoveQualityParameter_ActualNameChanged(object sender, EventArgs e) {
253      ParameterizeMainLoop();
254      ParameterizeMoveEvaluator();
255      ParameterizeMoveMaker();
256    }
257    private void TabuMoveEvaluatorParameter_ValueChanged(object sender, EventArgs e) {
258      ParameterizeMainLoop();
259    }
260    private void TabuMoveEvaluator_MoveTabuParameter_ActualNameChanged(object sender, EventArgs e) {
261      ParameterizeMainLoop();
262    }
263    private void Visualizer_VisualizationParameter_ActualNameChanged(object sender, EventArgs e) {
264      ParameterizeMainLoop();
265    }
266    private void SampleSizeParameter_NameChanged(object sender, EventArgs e) {
267      ParameterizeMoveGenerators();
268    }
269    #endregion
270
271    #region Helpers
272    [StorableHook(HookType.AfterDeserialization)]
273    private void Initialize() {
274      if (Problem != null) {
275        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
276        foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
277          op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
278        }
279        if (Problem.Visualizer != null) Problem.Visualizer.VisualizationParameter.ActualNameChanged += new EventHandler(Visualizer_VisualizationParameter_ActualNameChanged);
280      }
281      MoveGeneratorParameter.ValueChanged += new EventHandler(MoveGeneratorParameter_ValueChanged);
282      MoveEvaluatorParameter.ValueChanged += new EventHandler(MoveEvaluatorParameter_ValueChanged);
283      TabuMoveEvaluatorParameter.ValueChanged += new EventHandler(TabuMoveEvaluatorParameter_ValueChanged);
284      SampleSizeParameter.NameChanged += new EventHandler(SampleSizeParameter_NameChanged);
285    }
286    private void UpdateMoveGenerator() {
287      IMoveGenerator oldMoveGenerator = MoveGenerator;
288      MoveGeneratorParameter.ValidValues.Clear();
289      if (Problem != null) {
290        foreach (IMoveGenerator generator in Problem.Operators.OfType<IMoveGenerator>().OrderBy(x => x.Name)) {
291          MoveGeneratorParameter.ValidValues.Add(generator);
292        }
293      }
294      if (oldMoveGenerator != null && MoveGeneratorParameter.ValidValues.Any(x => x.GetType() == oldMoveGenerator.GetType()))
295        MoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
296      if (MoveGenerator == null) {
297        ClearMoveParameters();
298      }
299    }
300    private void UpdateMoveParameters() {
301      IMoveMaker oldMoveMaker = MoveMaker;
302      ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
303      ITabuMoveEvaluator oldTabuMoveEvaluator = TabuMoveEvaluator;
304      ITabuMoveMaker oldTabuMoveMaker = TabuMoveMaker;
305      ClearMoveParameters();
306      if (MoveGenerator != null) {
307        List<Type> moveTypes = MoveGenerator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();
308        foreach (Type type in moveTypes.ToList()) {
309          if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t)))
310            moveTypes.Remove(type);
311        }
312        foreach (Type type in moveTypes) {
313          var operators = Problem.Operators.Where(x => type.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
314          foreach (IMoveMaker moveMaker in operators.OfType<IMoveMaker>())
315            MoveMakerParameter.ValidValues.Add(moveMaker);
316          foreach (ISingleObjectiveMoveEvaluator moveEvaluator in operators.OfType<ISingleObjectiveMoveEvaluator>())
317            MoveEvaluatorParameter.ValidValues.Add(moveEvaluator);
318          foreach (ITabuMoveEvaluator tabuMoveEvaluator in operators.OfType<ITabuMoveEvaluator>())
319            TabuMoveEvaluatorParameter.ValidValues.Add(tabuMoveEvaluator);
320          foreach (ITabuMoveMaker tabuMoveMaker in operators.OfType<ITabuMoveMaker>())
321            TabuMoveMakerParameter.ValidValues.Add(tabuMoveMaker);
322        }
323        if (oldMoveMaker != null) {
324          IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
325          if (mm != null) MoveMaker = mm;
326        }
327        if (oldMoveEvaluator != null) {
328          ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
329          if (me != null) MoveEvaluator = me;
330        }
331        if (oldTabuMoveMaker != null) {
332          ITabuMoveMaker tmm = TabuMoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuMoveMaker.GetType());
333          if (tmm != null) TabuMoveMaker = tmm;
334        }
335        if (oldTabuMoveEvaluator != null) {
336          ITabuMoveEvaluator tme = TabuMoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuMoveEvaluator.GetType());
337          if (tme != null) TabuMoveEvaluator = tme;
338        }
339      }
340    }
341    private void ClearMoveParameters() {
342      MoveMakerParameter.ValidValues.Clear();
343      MoveEvaluatorParameter.ValidValues.Clear();
344      TabuMoveEvaluatorParameter.ValidValues.Clear();
345      TabuMoveMakerParameter.ValidValues.Clear();
346    }
347    private void ParameterizeSolutionsCreator() {
348      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
349      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
350    }
351    private void ParameterizeMainLoop() {
352      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
353      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
354      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
355      if (MoveEvaluator != null)
356        MainLoop.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
357      if (TabuMoveEvaluator != null)
358        MainLoop.MoveTabuParameter.ActualName = TabuMoveEvaluator.MoveTabuParameter.ActualName;
359      MainLoop.VisualizerParameter.ActualName = Problem.VisualizerParameter.Name;
360      if (Problem.Visualizer != null)
361        MainLoop.VisualizationParameter.ActualName = Problem.Visualizer.VisualizationParameter.ActualName;
362    }
363    private void ParameterizeStochasticOperator(IOperator op) {
364      if (op is IStochasticOperator)
365        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
366    }
367    private void ParameterizeMoveGenerators() {
368      if (Problem != null) {
369        foreach (IMultiMoveGenerator generator in Problem.Operators.OfType<IMultiMoveGenerator>())
370          generator.SampleSizeParameter.ActualName = SampleSizeParameter.Name;
371      }
372    }
373    private void ParameterizeMoveEvaluator() {
374      foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
375        op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
376      }
377    }
378    private void ParameterizeMoveMaker() {
379      foreach (IMoveMaker op in Problem.Operators.OfType<IMoveMaker>()) {
380        op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
381        if (MoveEvaluator != null)
382          op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
383      }
384    }
385    #endregion
386  }
387}
Note: See TracBrowser for help on using the repository browser.