Free cookie consent management tool by TermsFeed Policy Generator

source: branches/QAPAlgorithms/HeuristicLab.Problems.QuadraticAssignment.Algorithms/3.3/RobustTabooSearch.cs @ 6569

Last change on this file since 6569 was 6351, checked in by abeham, 13 years ago

#1541

  • Added Robust Taboo Search (Taillard 1991)
File size: 14.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.Random;
35
36namespace HeuristicLab.Problems.QuadraticAssignment.Algorithms {
37  [Item("Robust Taboo Search (QAP)", "The algorithm is described in Taillard, E. 1991. Robust Taboo Search for the Quadratic Assignment Problem. Parallel Computing 17, pp. 443-455.")]
38  [Creatable("Algorithms")]
39  [StorableClass]
40  public sealed class RobustTabooSearchQAP : HeuristicOptimizationEngineAlgorithm, IStorableContent {
41    public string Filename { get; set; }
42
43    #region Problem Properties
44    public override Type ProblemType {
45      get { return typeof(QuadraticAssignmentProblem); }
46    }
47    public new QuadraticAssignmentProblem Problem {
48      get { return (QuadraticAssignmentProblem)base.Problem; }
49      set { base.Problem = value; }
50    }
51    #endregion
52
53    #region Parameter Properties
54    private FixedValueParameter<MultiAnalyzer> AnalyzerParameter {
55      get { return (FixedValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
56    }
57    private FixedValueParameter<IntValue> SeedParameter {
58      get { return (FixedValueParameter<IntValue>)Parameters["Seed"]; }
59    }
60    private FixedValueParameter<BoolValue> SetSeedRandomlyParameter {
61      get { return (FixedValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
62    }
63    private FixedValueParameter<IntValue> MaximumIterationsParameter {
64      get { return (FixedValueParameter<IntValue>)Parameters["MaximumIterations"]; }
65    }
66    private FixedValueParameter<IntValue> MinimumTabuTenureParameter {
67      get { return (FixedValueParameter<IntValue>)Parameters["MinimumTabuTenure"]; }
68    }
69    private FixedValueParameter<IntValue> MaximumTabuTenureParameter {
70      get { return (FixedValueParameter<IntValue>)Parameters["MaximumTabuTenure"]; }
71    }
72    private FixedValueParameter<IntValue> TabuTenureAdaptionIntervalParameter {
73      get { return (FixedValueParameter<IntValue>)Parameters["TabuTenureAdaptionInterval"]; }
74    }
75    private FixedValueParameter<BoolValue> UseAlternativeAspirationParameter {
76      get { return (FixedValueParameter<BoolValue>)Parameters["UseAlternativeAspiration"]; }
77    }
78    private FixedValueParameter<IntValue> AlternativeAspirationTenureParameter {
79      get { return (FixedValueParameter<IntValue>)Parameters["AlternativeAspirationTenure"]; }
80    }
81    #endregion
82
83    #region Properties
84    public int Seed {
85      get { return SeedParameter.Value.Value; }
86      set { SeedParameter.Value.Value = value; }
87    }
88    public bool SetSeedRandomly {
89      get { return SetSeedRandomlyParameter.Value.Value; }
90      set { SetSeedRandomlyParameter.Value.Value = value; }
91    }
92    public int MaximumIterations {
93      get { return MaximumIterationsParameter.Value.Value; }
94      set { MaximumIterationsParameter.Value.Value = value; }
95    }
96    public int MinimumTabuTenure {
97      get { return MinimumTabuTenureParameter.Value.Value; }
98      set { MinimumTabuTenureParameter.Value.Value = value; }
99    }
100    public int MaximumTabuTenure {
101      get { return MaximumTabuTenureParameter.Value.Value; }
102      set { MaximumTabuTenureParameter.Value.Value = value; }
103    }
104    public int TabuTenureAdaptionInterval {
105      get { return TabuTenureAdaptionIntervalParameter.Value.Value; }
106      set { TabuTenureAdaptionIntervalParameter.Value.Value = value; }
107    }
108    public bool UseAlternativeAspiration {
109      get { return UseAlternativeAspirationParameter.Value.Value; }
110      set { UseAlternativeAspirationParameter.Value.Value = value; }
111    }
112    public int AlternativeAspirationTenure {
113      get { return AlternativeAspirationTenureParameter.Value.Value; }
114      set { AlternativeAspirationTenureParameter.Value.Value = value; }
115    }
116    #endregion
117
118    [Storable]
119    private SolutionsCreator solutionsCreator;
120    [Storable]
121    private QAPRobustTabooSeachOperator mainOperator;
122    [Storable]
123    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
124
125    [StorableConstructor]
126    private RobustTabooSearchQAP(bool deserializing) : base(deserializing) { }
127    private RobustTabooSearchQAP(RobustTabooSearchQAP original, Cloner cloner)
128      : base(original, cloner) {
129      solutionsCreator = cloner.Clone(original.solutionsCreator);
130      mainOperator = cloner.Clone(original.mainOperator);
131      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
132    }
133    public RobustTabooSearchQAP() {
134      Parameters.Add(new FixedValueParameter<MultiAnalyzer>("Analyzer", "The analyzers that are applied after each iteration.", new MultiAnalyzer()));
135      Parameters.Add(new FixedValueParameter<IntValue>("Seed", "The seed value of the random number generator.", new IntValue(0)));
136      Parameters.Add(new FixedValueParameter<BoolValue>("SetSeedRandomly", "True whether the seed should be set randomly for each run, false if it should be fixed.", new BoolValue(true)));
137      Parameters.Add(new FixedValueParameter<IntValue>("MaximumIterations", "The number of iterations that the algorithm should run.", new IntValue(10000)));
138      Parameters.Add(new FixedValueParameter<IntValue>("MinimumTabuTenure", "The minimum tabu tenure.", new IntValue(20)));
139      Parameters.Add(new FixedValueParameter<IntValue>("MaximumTabuTenure", "The maximum tabu tenure.", new IntValue(10)));
140      Parameters.Add(new FixedValueParameter<IntValue>("TabuTenureAdaptionInterval", "The amount of iterations that have to pass before the tabu tenure is adapted.", new IntValue(60)));
141      Parameters.Add(new FixedValueParameter<BoolValue>("UseAlternativeAspiration", "True if the alternative aspiration condition should be used that takes moves that have not been made for some time above others.", new BoolValue(false)));
142      Parameters.Add(new FixedValueParameter<IntValue>("AlternativeAspirationTenure", "The time t that a move will be remembered for the alternative aspiration condition.", new IntValue(10000)));
143
144      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
145      qualityAnalyzer.ResultsParameter.ActualName = "Results";
146      AnalyzerParameter.Value.Operators.Add(qualityAnalyzer);
147
148      RandomCreator randomCreator = new RandomCreator();
149      randomCreator.RandomParameter.ActualName = "Random";
150      randomCreator.SeedParameter.Value = null;
151      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
152      randomCreator.SetSeedRandomlyParameter.Value = null;
153      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
154
155      OperatorGraph.InitialOperator = randomCreator;
156
157      VariableCreator variableCreator = new VariableCreator();
158      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
159      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("TabuTenure", new IntValue(0)));
160      randomCreator.Successor = variableCreator;
161
162      ResultsCollector resultsCollector = new ResultsCollector();
163      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations", "The actual iteration."));
164      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("TabuTenure", "The actual tabu tenure."));
165      variableCreator.Successor = resultsCollector;
166
167      solutionsCreator = new SolutionsCreator();
168      solutionsCreator.NumberOfSolutions = new IntValue(1);
169      resultsCollector.Successor = solutionsCreator;
170
171      Placeholder analyzer = new Placeholder();
172      analyzer.Name = "(Analyzer)";
173      analyzer.OperatorParameter.ActualName = AnalyzerParameter.Name;
174      solutionsCreator.Successor = analyzer;
175
176      UniformSubScopesProcessor ussp = new UniformSubScopesProcessor();
177      analyzer.Successor = ussp;
178
179      mainOperator = new QAPRobustTabooSeachOperator();
180      mainOperator.AlternativeAspirationTenureParameter.ActualName = AlternativeAspirationTenureParameter.Name;
181      mainOperator.BestQualityParameter.ActualName = "BestSoFarQuality";
182      mainOperator.IterationsParameter.ActualName = "Iterations";
183      mainOperator.LongTermMemoryParameter.ActualName = "LongTermMemory";
184      mainOperator.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
185      mainOperator.MaximumTabuTenureParameter.ActualName = MaximumTabuTenureParameter.Name;
186      mainOperator.MinimumTabuTenureParameter.ActualName = MinimumTabuTenureParameter.Name;
187      mainOperator.RandomParameter.ActualName = "Random";
188      mainOperator.ShortTermMemoryParameter.ActualName = "ShortTermMemory";
189      mainOperator.TabuTenureAdaptionIntervalParameter.ActualName = TabuTenureAdaptionIntervalParameter.Name;
190      mainOperator.TabuTenureParameter.ActualName = "TabuTenure";
191      mainOperator.UseAlternativeAspirationParameter.ActualName = UseAlternativeAspirationParameter.Name;
192      ussp.Operator = mainOperator;
193
194      IntCounter iterationsCounter = new IntCounter();
195      iterationsCounter.ValueParameter.ActualName = "Iterations";
196      iterationsCounter.Increment = new IntValue(1);
197      ussp.Successor = iterationsCounter;
198
199      Comparator comparator = new Comparator();
200      comparator.Name = "Iterations < MaximumIterations ?";
201      comparator.LeftSideParameter.ActualName = "Iterations";
202      comparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
203      comparator.Comparison = new Comparison(ComparisonType.Less);
204      comparator.ResultParameter.ActualName = "ContinueByIteration";
205      iterationsCounter.Successor = comparator;
206
207      ConditionalBranch branch = new ConditionalBranch();
208      branch.ConditionParameter.ActualName = "ContinueByIteration";
209      branch.TrueBranch = analyzer;
210      comparator.Successor = branch;
211
212      RegisterEventHandlers();
213    }
214
215    public override IDeepCloneable Clone(Cloner cloner) {
216      return new RobustTabooSearchQAP(this, cloner);
217    }
218
219    protected override void OnProblemChanged() {
220      base.OnProblemChanged();
221      UpdateProblemSpecificParameters();
222      ParameterizeOperators();
223      UpdateAnalyzers();
224    }
225
226    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
227      base.Problem_EvaluatorChanged(sender, e);
228      ParameterizeOperators();
229      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
230    }
231
232    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
233      base.Problem_OperatorsChanged(sender, e);
234      UpdateAnalyzers();
235    }
236
237    protected override void Problem_Reset(object sender, EventArgs e) {
238      base.Problem_Reset(sender, e);
239      UpdateProblemSpecificParameters();
240      ParameterizeOperators();
241      UpdateAnalyzers();
242    }
243
244    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
245      ParameterizeOperators();
246    }
247
248    private void RegisterEventHandlers() {
249      if (Problem != null) {
250        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
251      }
252    }
253
254    public override void Start() {
255      if (ExecutionState == ExecutionState.Prepared) {
256        DoubleMatrix shortTermMemory = new DoubleMatrix(Problem.Weights.Rows, Problem.Weights.Rows);
257        DoubleMatrix longTermMemory = new DoubleMatrix(Problem.Weights.Rows, Problem.Weights.Rows);
258        for (int i = 0; i < shortTermMemory.Rows; i++)
259          for (int j = 0; j < shortTermMemory.Rows; j++) {
260            shortTermMemory[i, j] = -1;
261            longTermMemory[i, j] = -1;
262          }
263
264        GlobalScope.Variables.Add(new Variable("ShortTermMemory", shortTermMemory));
265        GlobalScope.Variables.Add(new Variable("LongTermMemory", longTermMemory));
266      }
267      base.Start();
268    }
269
270    private void UpdateProblemSpecificParameters() {
271      MinimumTabuTenure = (int)(0.9 * Problem.Weights.Rows);
272      MaximumTabuTenure = (int)(1.1 * Problem.Weights.Rows);
273      TabuTenureAdaptionInterval = 2 * MaximumTabuTenure;
274    }
275
276    private void UpdateAnalyzers() {
277      AnalyzerParameter.Value.Operators.Clear();
278      if (Problem != null) {
279        foreach (IAnalyzer analyzer in ((IProblem)Problem).Operators.OfType<IAnalyzer>())
280          if (!(analyzer is AlleleFrequencyAnalyzer<Permutation>) && !(analyzer is PopulationDiversityAnalyzer<Permutation>))
281            AnalyzerParameter.Value.Operators.Add(analyzer);
282      }
283      AnalyzerParameter.Value.Operators.Add(qualityAnalyzer);
284    }
285
286    private void ParameterizeOperators() {
287      if (Problem != null) {
288        solutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
289        solutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
290
291        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
292
293        mainOperator.DistancesParameter.ActualName = Problem.DistancesParameter.Name;
294        mainOperator.PermutationParameter.ActualName = Problem.SolutionCreator.PermutationParameter.ActualName;
295        mainOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
296        mainOperator.WeightsParameter.ActualName = Problem.WeightsParameter.Name;
297      }
298    }
299  }
300}
Note: See TracBrowser for help on using the repository browser.