Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Algorithms.TabuSearch/3.3/TabuSelector.cs @ 17616

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

Changed some more parameters to be hidden per default and corrected wrong layout in some parameter views (#1377)

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