Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3244 was 3232, checked in by abeham, 15 years ago

Renamed operators according to the underlying operation #889

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