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