Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3376 was 3376, checked in by swagner, 14 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

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