Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3195 was 3189, checked in by abeham, 15 years ago

Prevent starting of algorithm when problem or vital operators are null #933

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  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    protected override void OnPrepared() {
178      base.OnPrepared();
179      if (Engine != null) {
180        if (Problem == null || MoveGenerator == null || MoveMaker == null || MoveEvaluator == null
181          || TabuMoveEvaluator == null || TabuMoveMaker == null)
182          Engine.Prepare(null);
183      }
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 (ITabuMoveEvaluator op in Problem.Operators.OfType<ITabuMoveEvaluator>()) {
195        op.MoveTabuParameter.ActualNameChanged += new EventHandler(TabuMoveEvaluator_MoveTabuParameter_ActualNameChanged);
196      }
197      ParameterizeSolutionsCreator();
198      ParameterizeMainLoop();
199      UpdateMoveGenerator();
200      UpdateMoveParameters();
201      ParameterizeMoveGenerators();
202      ParameterizeMoveEvaluator();
203      ParameterizeMoveMaker();
204      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
205      base.OnProblemChanged();
206    }
207    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
208      ParameterizeStochasticOperator(Problem.SolutionCreator);
209      ParameterizeSolutionsCreator();
210      base.Problem_SolutionCreatorChanged(sender, e);
211    }
212    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
213      ParameterizeStochasticOperator(Problem.Evaluator);
214      ParameterizeSolutionsCreator();
215      ParameterizeMainLoop();
216      ParameterizeMoveEvaluator();
217      ParameterizeMoveMaker();
218      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
219      base.Problem_EvaluatorChanged(sender, e);
220    }
221    protected override void Problem_VisualizerChanged(object sender, EventArgs e) {
222      ParameterizeStochasticOperator(Problem.Visualizer);
223      ParameterizeMainLoop();
224      if (Problem.Visualizer != null) Problem.Visualizer.VisualizationParameter.ActualNameChanged += new EventHandler(Visualizer_VisualizationParameter_ActualNameChanged);
225      base.Problem_VisualizerChanged(sender, e);
226    }
227    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
228      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
229      // This may seem pointless, but some operators already have the eventhandler registered, others don't
230      // FIXME: Is there another way to solve this problem?
231      foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
232        op.MoveQualityParameter.ActualNameChanged -= new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
233        op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
234      }
235      foreach (ITabuMoveEvaluator op in Problem.Operators.OfType<ITabuMoveEvaluator>()) {
236        op.MoveTabuParameter.ActualNameChanged -= new EventHandler(TabuMoveEvaluator_MoveTabuParameter_ActualNameChanged);
237        op.MoveTabuParameter.ActualNameChanged += new EventHandler(TabuMoveEvaluator_MoveTabuParameter_ActualNameChanged);
238      }
239      UpdateMoveGenerator();
240      UpdateMoveParameters();
241      ParameterizeMainLoop();
242      ParameterizeMoveGenerators();
243      ParameterizeMoveEvaluator();
244      ParameterizeMoveMaker();
245      base.Problem_OperatorsChanged(sender, e);
246    }
247    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
248      ParameterizeMainLoop();
249      ParameterizeMoveEvaluator();
250      ParameterizeMoveMaker();
251    }
252    private void MoveGeneratorParameter_ValueChanged(object sender, EventArgs e) {
253      UpdateMoveParameters();
254    }
255    private void MoveEvaluatorParameter_ValueChanged(object sender, EventArgs e) {
256      ParameterizeMainLoop();
257      ParameterizeMoveEvaluator();
258      ParameterizeMoveMaker();
259    }
260    private void MoveEvaluator_MoveQualityParameter_ActualNameChanged(object sender, EventArgs e) {
261      ParameterizeMainLoop();
262      ParameterizeMoveEvaluator();
263      ParameterizeMoveMaker();
264    }
265    private void TabuMoveEvaluatorParameter_ValueChanged(object sender, EventArgs e) {
266      ParameterizeMainLoop();
267    }
268    private void TabuMoveEvaluator_MoveTabuParameter_ActualNameChanged(object sender, EventArgs e) {
269      ParameterizeMainLoop();
270    }
271    private void Visualizer_VisualizationParameter_ActualNameChanged(object sender, EventArgs e) {
272      ParameterizeMainLoop();
273    }
274    private void SampleSizeParameter_NameChanged(object sender, EventArgs e) {
275      ParameterizeMoveGenerators();
276    }
277    #endregion
278
279    #region Helpers
280    [StorableHook(HookType.AfterDeserialization)]
281    private void Initialize() {
282      if (Problem != null) {
283        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
284        foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
285          op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);
286        }
287        if (Problem.Visualizer != null) Problem.Visualizer.VisualizationParameter.ActualNameChanged += new EventHandler(Visualizer_VisualizationParameter_ActualNameChanged);
288      }
289      MoveGeneratorParameter.ValueChanged += new EventHandler(MoveGeneratorParameter_ValueChanged);
290      MoveEvaluatorParameter.ValueChanged += new EventHandler(MoveEvaluatorParameter_ValueChanged);
291      TabuMoveEvaluatorParameter.ValueChanged += new EventHandler(TabuMoveEvaluatorParameter_ValueChanged);
292      SampleSizeParameter.NameChanged += new EventHandler(SampleSizeParameter_NameChanged);
293    }
294    private void UpdateMoveGenerator() {
295      IMoveGenerator oldMoveGenerator = MoveGenerator;
296      MoveGeneratorParameter.ValidValues.Clear();
297      if (Problem != null) {
298        foreach (IMoveGenerator generator in Problem.Operators.OfType<IMoveGenerator>().OrderBy(x => x.Name)) {
299          MoveGeneratorParameter.ValidValues.Add(generator);
300        }
301      }
302      if (oldMoveGenerator != null && MoveGeneratorParameter.ValidValues.Any(x => x.GetType() == oldMoveGenerator.GetType()))
303        MoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
304      if (MoveGenerator == null) {
305        ClearMoveParameters();
306      }
307    }
308    private void UpdateMoveParameters() {
309      IMoveMaker oldMoveMaker = MoveMaker;
310      ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
311      ITabuMoveEvaluator oldTabuMoveEvaluator = TabuMoveEvaluator;
312      ITabuMoveMaker oldTabuMoveMaker = TabuMoveMaker;
313      ClearMoveParameters();
314      if (MoveGenerator != null) {
315        List<Type> moveTypes = MoveGenerator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();
316        foreach (Type type in moveTypes.ToList()) {
317          if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t)))
318            moveTypes.Remove(type);
319        }
320        foreach (Type type in moveTypes) {
321          var operators = Problem.Operators.Where(x => type.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
322          foreach (IMoveMaker moveMaker in operators.OfType<IMoveMaker>())
323            MoveMakerParameter.ValidValues.Add(moveMaker);
324          foreach (ISingleObjectiveMoveEvaluator moveEvaluator in operators.OfType<ISingleObjectiveMoveEvaluator>())
325            MoveEvaluatorParameter.ValidValues.Add(moveEvaluator);
326          foreach (ITabuMoveEvaluator tabuMoveEvaluator in operators.OfType<ITabuMoveEvaluator>())
327            TabuMoveEvaluatorParameter.ValidValues.Add(tabuMoveEvaluator);
328          foreach (ITabuMoveMaker tabuMoveMaker in operators.OfType<ITabuMoveMaker>())
329            TabuMoveMakerParameter.ValidValues.Add(tabuMoveMaker);
330        }
331        if (oldMoveMaker != null) {
332          IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
333          if (mm != null) MoveMaker = mm;
334        }
335        if (oldMoveEvaluator != null) {
336          ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
337          if (me != null) MoveEvaluator = me;
338        }
339        if (oldTabuMoveMaker != null) {
340          ITabuMoveMaker tmm = TabuMoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuMoveMaker.GetType());
341          if (tmm != null) TabuMoveMaker = tmm;
342        }
343        if (oldTabuMoveEvaluator != null) {
344          ITabuMoveEvaluator tme = TabuMoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuMoveEvaluator.GetType());
345          if (tme != null) TabuMoveEvaluator = tme;
346        }
347      }
348    }
349    private void ClearMoveParameters() {
350      MoveMakerParameter.ValidValues.Clear();
351      MoveEvaluatorParameter.ValidValues.Clear();
352      TabuMoveEvaluatorParameter.ValidValues.Clear();
353      TabuMoveMakerParameter.ValidValues.Clear();
354    }
355    private void ParameterizeSolutionsCreator() {
356      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
357      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
358    }
359    private void ParameterizeMainLoop() {
360      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
361      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
362      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
363      if (MoveEvaluator != null)
364        MainLoop.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
365      if (TabuMoveEvaluator != null)
366        MainLoop.MoveTabuParameter.ActualName = TabuMoveEvaluator.MoveTabuParameter.ActualName;
367      MainLoop.VisualizerParameter.ActualName = Problem.VisualizerParameter.Name;
368      if (Problem.Visualizer != null)
369        MainLoop.VisualizationParameter.ActualName = Problem.Visualizer.VisualizationParameter.ActualName;
370    }
371    private void ParameterizeStochasticOperator(IOperator op) {
372      if (op is IStochasticOperator)
373        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
374    }
375    private void ParameterizeMoveGenerators() {
376      if (Problem != null) {
377        foreach (IMultiMoveGenerator generator in Problem.Operators.OfType<IMultiMoveGenerator>())
378          generator.SampleSizeParameter.ActualName = SampleSizeParameter.Name;
379      }
380    }
381    private void ParameterizeMoveEvaluator() {
382      foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
383        op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
384      }
385    }
386    private void ParameterizeMoveMaker() {
387      foreach (IMoveMaker op in Problem.Operators.OfType<IMoveMaker>()) {
388        op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
389        if (MoveEvaluator != null)
390          op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
391      }
392    }
393    #endregion
394  }
395}
Note: See TracBrowser for help on using the repository browser.