Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.TS/3.3/TabuSelector.cs @ 2981

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

Added a TS operator and a default TabuSelector #840

File size: 6.3 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 HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Selection;
30
31namespace HeuristicLab.Algorithms.TS {
32  /// <summary>
33  /// The tabu selector is a selection operator that separates the best n moves that are either not tabu or satisfy the default aspiration criterion from the rest. It expects the move subscopes to be sorted.
34  /// </summary>
35  /// <remarks>
36  /// For different aspiration criteria a new operator should be implemented.
37  /// </remarks>
38  [Item("TabuSelector", "An operator that selects the best move that is either not tabu or satisfies the aspiration criterion. It expects the move subscopes to be sorted.")]
39  [EmptyStorableClass]
40  public class TabuSelector : Selector {
41    /// <summary>
42    /// The quality of the current solution.
43    /// </summary>
44    public LookupParameter<DoubleData> QualityParameter {
45      get { return (LookupParameter<DoubleData>)Parameters["Quality"]; }
46    }
47    /// <summary>
48    /// The best found quality so far.
49    /// </summary>
50    public LookupParameter<DoubleData> BestQualityParameter {
51      get { return (LookupParameter<DoubleData>)Parameters["BestQuality"]; }
52    }
53    /// <summary>
54    /// Whether to use the default aspiration criteria or not.
55    /// </summary>
56    public ValueLookupParameter<BoolData> AspirationParameter {
57      get { return (ValueLookupParameter<BoolData>)Parameters["Aspiration"]; }
58    }
59    /// <summary>
60    /// Whether the problem is a maximization problem or not.
61    /// </summary>
62    public IValueLookupParameter<BoolData> MaximizationParameter {
63      get { return (IValueLookupParameter<BoolData>)Parameters["Maximization"]; }
64    }
65    /// <summary>
66    /// The parameter for the move qualities.
67    /// </summary>
68    public ILookupParameter<ItemArray<DoubleData>> MoveQualityParameter {
69      get { return (ILookupParameter<ItemArray<DoubleData>>)Parameters["MoveQuality"]; }
70    }
71    /// <summary>
72    /// The parameter for the tabu status of the moves.
73    /// </summary>
74    public ILookupParameter<ItemArray<BoolData>> MoveTabuParameter {
75      get { return (ILookupParameter<ItemArray<BoolData>>)Parameters["MoveTabu"]; }
76    }
77
78    /// <summary>
79    /// Initializes a new intsance with 6 parameters (<c>Quality</c>, <c>BestQuality</c>,
80    /// <c>Aspiration</c>, <c>Maximization</c>, <c>MoveQuality</c>, and <c>MoveTabu</c>).
81    /// </summary>
82    public TabuSelector()
83      : base() {
84      Parameters.Add(new LookupParameter<DoubleData>("Quality", "The quality of the current solution."));
85      Parameters.Add(new LookupParameter<DoubleData>("BestQuality", "The best found quality so far."));
86      Parameters.Add(new ValueLookupParameter<BoolData>("Aspiration", "Whether the default aspiration criterion should be used or not. The default aspiration criterion accepts a tabu move if it results in a better solution than the best solution found so far.", new BoolData(true)));
87      Parameters.Add(new ValueLookupParameter<BoolData>("Maximization", "Whether the problem is a maximization or minimization problem (used to decide whether a solution is better"));
88      Parameters.Add(new SubScopesLookupParameter<DoubleData>("MoveQuality", "The quality of the move."));
89      Parameters.Add(new SubScopesLookupParameter<BoolData>("MoveTabu", "The tabu status of the move."));
90    }
91
92    /// <summary>
93    /// Implements the tabu selection with the default aspiration criteria (choose a tabu move when it is better than the best so far).
94    /// </summary>
95    /// <param name="scopes">The scopes from which to select.</param>
96    /// <returns>The selected scopes.</returns>
97    protected override IScope[] Select(List<IScope> scopes) {
98      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
99      bool copy = CopySelectedParameter.Value.Value;
100      bool aspiration = AspirationParameter.ActualValue.Value;
101      bool maximization = MaximizationParameter.ActualValue.Value;
102      double bestQuality = BestQualityParameter.ActualValue.Value;
103      double quality = QualityParameter.ActualValue.Value;
104      ItemArray<DoubleData> moveQualities = MoveQualityParameter.ActualValue;
105      ItemArray<BoolData> moveTabus = MoveTabuParameter.ActualValue;
106
107      IScope[] selected = new IScope[count];
108
109      // remember scopes that should be removed
110      List<int> scopesToRemove = new List<int>();
111      for (int i = 0; i < scopes.Count; i++) {
112        if (count > 0 && (!moveTabus[i].Value || aspiration &&
113          (maximization && moveQualities[i].Value + quality > bestQuality
114          || !maximization && moveQualities[i].Value + quality < bestQuality))) {
115          scopesToRemove.Add(i);
116          if (copy) selected[selected.Length - count] = (IScope)scopes[i].Clone();
117          else selected[selected.Length - count] = scopes[i];
118          count--;
119          if (count == 0) break;
120        }
121      }
122
123      if (count > 0) throw new InvalidOperationException("TabuSelector: The neighborhood contained no or too little moves that are not tabu.");
124
125      // remove from last to first so that the stored indices remain the same
126      if (!copy) {
127        while (scopesToRemove.Count > 0) {
128          scopes.RemoveAt(scopesToRemove[scopesToRemove.Count - 1]);
129          scopesToRemove.RemoveAt(scopesToRemove.Count - 1);
130        }
131      }
132
133      return selected;
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.