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