[3104] | 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 HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
[3340] | 31 | [Item("InversionMoveSoftTabuCriterion", @"For relative postion encoded permutations it just prevents readding of previously deleted edges, but allows deleting previously added edges.
|
---|
| 32 | For absolute position encoded permutations it prevents moving a number to a position it has previously occupied.
|
---|
| 33 |
|
---|
| 34 | If the aspiration condition is activated, a move will not be considered tabu against a move in the tabu list if it leads to a better solution than the quality recorded with the move in the tabu list.")]
|
---|
[3104] | 35 | [StorableClass]
|
---|
[3340] | 36 | public class InversionMoveSoftTabuCriterion : SingleSuccessorOperator, IPermutationInversionMoveOperator, ITabuChecker {
|
---|
[3221] | 37 | public override bool CanChangeName {
|
---|
| 38 | get { return false; }
|
---|
| 39 | }
|
---|
[3232] | 40 | public ILookupParameter<InversionMove> InversionMoveParameter {
|
---|
| 41 | get { return (LookupParameter<InversionMove>)Parameters["InversionMove"]; }
|
---|
[3104] | 42 | }
|
---|
| 43 | public ILookupParameter<Permutation> PermutationParameter {
|
---|
| 44 | get { return (LookupParameter<Permutation>)Parameters["Permutation"]; }
|
---|
| 45 | }
|
---|
| 46 | public ILookupParameter<ItemList<IItem>> TabuListParameter {
|
---|
| 47 | get { return (ILookupParameter<ItemList<IItem>>)Parameters["TabuList"]; }
|
---|
| 48 | }
|
---|
| 49 | public ILookupParameter<BoolValue> MoveTabuParameter {
|
---|
| 50 | get { return (ILookupParameter<BoolValue>)Parameters["MoveTabu"]; }
|
---|
| 51 | }
|
---|
[3340] | 52 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 53 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
[3104] | 54 | }
|
---|
[3340] | 55 | public ILookupParameter<DoubleValue> MoveQualityParameter {
|
---|
| 56 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
| 57 | }
|
---|
| 58 | public ValueParameter<BoolValue> UseAspirationCriterionParameter {
|
---|
| 59 | get { return (ValueParameter<BoolValue>)Parameters["UseAspirationCriterion"]; }
|
---|
| 60 | }
|
---|
[3104] | 61 |
|
---|
[3340] | 62 | public BoolValue UseAspirationCriterion {
|
---|
| 63 | get { return UseAspirationCriterionParameter.Value; }
|
---|
| 64 | set { UseAspirationCriterionParameter.Value = value; }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public InversionMoveSoftTabuCriterion()
|
---|
[3104] | 68 | : base() {
|
---|
[3232] | 69 | Parameters.Add(new LookupParameter<InversionMove>("InversionMove", "The move to evaluate."));
|
---|
[3104] | 70 | Parameters.Add(new LookupParameter<BoolValue>("MoveTabu", "The variable to store if a move was tabu."));
|
---|
| 71 | Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
|
---|
| 72 | Parameters.Add(new LookupParameter<ItemList<IItem>>("TabuList", "The tabu list."));
|
---|
[3340] | 73 | Parameters.Add(new ValueParameter<BoolValue>("UseAspirationCriterion", "Whether to use the aspiration criterion or not.", new BoolValue(true)));
|
---|
| 74 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, else if it is a minimization problem."));
|
---|
| 75 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the current move."));
|
---|
[3104] | 76 | }
|
---|
| 77 |
|
---|
| 78 | public override IOperation Apply() {
|
---|
| 79 | ItemList<IItem> tabuList = TabuListParameter.ActualValue;
|
---|
[3232] | 80 | InversionMove move = InversionMoveParameter.ActualValue;
|
---|
[3104] | 81 | Permutation permutation = PermutationParameter.ActualValue;
|
---|
| 82 | int length = permutation.Length;
|
---|
[3340] | 83 | double moveQuality = MoveQualityParameter.ActualValue.Value;
|
---|
| 84 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 85 | bool useAspiration = UseAspirationCriterion.Value;
|
---|
[3233] | 86 | bool isTabu = false;
|
---|
[3340] | 87 |
|
---|
[3233] | 88 | foreach (IItem tabuMove in tabuList) {
|
---|
[3340] | 89 | PermutationMoveAttribute attrib = (tabuMove as PermutationMoveAttribute);
|
---|
| 90 | if (!useAspiration
|
---|
| 91 | || maximization && moveQuality <= attrib.MoveQuality
|
---|
| 92 | || !maximization && moveQuality >= attrib.MoveQuality) {
|
---|
| 93 | switch (permutation.PermutationType) {
|
---|
| 94 | case PermutationTypes.RelativeUndirected: {
|
---|
| 95 | int E1S = permutation.GetCircular(move.Index1 - 1);
|
---|
| 96 | int E1T = permutation[move.Index1];
|
---|
| 97 | int E2S = permutation[move.Index2];
|
---|
| 98 | int E2T = permutation.GetCircular(move.Index2 + 1);
|
---|
| 99 | InversionMoveRelativeAttribute relAttrib = (attrib as InversionMoveRelativeAttribute);
|
---|
| 100 | if (relAttrib != null) {
|
---|
| 101 | // if previously deleted Edge1Source-Target is readded
|
---|
| 102 | if (relAttrib.Edge1Source == E1S && relAttrib.Edge1Target == E2S || relAttrib.Edge1Source == E2S && relAttrib.Edge1Target == E1S
|
---|
| 103 | || relAttrib.Edge1Source == E1T && relAttrib.Edge1Target == E2T || relAttrib.Edge1Source == E2T && relAttrib.Edge1Target == E1T
|
---|
| 104 | // if previously deleted Edge2Source-Target is readded
|
---|
| 105 | || relAttrib.Edge2Source == E1T && relAttrib.Edge2Target == E2T || relAttrib.Edge2Source == E2T && relAttrib.Edge2Target == E1T
|
---|
| 106 | || relAttrib.Edge2Source == E1S && relAttrib.Edge2Target == E2S || relAttrib.Edge2Source == E2S && relAttrib.Edge2Target == E1S) {
|
---|
| 107 | isTabu = true;
|
---|
| 108 | }
|
---|
[3233] | 109 | }
|
---|
| 110 | }
|
---|
[3340] | 111 | break;
|
---|
| 112 | case PermutationTypes.RelativeDirected: {
|
---|
| 113 | int E1S = permutation.GetCircular(move.Index1 - 1);
|
---|
| 114 | int E1T = permutation[move.Index1];
|
---|
| 115 | int E2S = permutation[move.Index2];
|
---|
| 116 | int E2T = permutation.GetCircular(move.Index2 + 1);
|
---|
| 117 | InversionMoveRelativeAttribute relAttrib = (attrib as InversionMoveRelativeAttribute);
|
---|
| 118 | if (relAttrib != null) {
|
---|
| 119 | if (relAttrib.Edge1Source == E1S && relAttrib.Edge1Target == E2S
|
---|
| 120 | || relAttrib.Edge1Source == E1T && relAttrib.Edge1Target == E2T
|
---|
| 121 | // if previously deleted Edge2Source-Target is readded
|
---|
| 122 | || relAttrib.Edge2Source == E1T && relAttrib.Edge2Target == E2T
|
---|
| 123 | || relAttrib.Edge2Source == E1S && relAttrib.Edge2Target == E2S) {
|
---|
| 124 | isTabu = true;
|
---|
| 125 | }
|
---|
[3233] | 126 | }
|
---|
| 127 | }
|
---|
[3340] | 128 | break;
|
---|
| 129 | case PermutationTypes.Absolute: {
|
---|
| 130 | int i1 = move.Index1;
|
---|
| 131 | int n1 = permutation[move.Index1];
|
---|
| 132 | int i2 = move.Index2;
|
---|
| 133 | int n2 = permutation[move.Index2];
|
---|
| 134 | InversionMoveAbsoluteAttribute absAttrib = (attrib as InversionMoveAbsoluteAttribute);
|
---|
| 135 | if (absAttrib != null) {
|
---|
| 136 | if ((absAttrib.Index1 == i1 || absAttrib.Index1 == i2) && (absAttrib.Number1 == n1 || absAttrib.Number1 == n2)
|
---|
| 137 | || (absAttrib.Index2 == i2 || absAttrib.Index2 == i1) && (absAttrib.Number2 == n2 || absAttrib.Number2 == n1))
|
---|
| 138 | isTabu = true;
|
---|
[3233] | 139 |
|
---|
[3340] | 140 | }
|
---|
[3233] | 141 | }
|
---|
[3340] | 142 | break;
|
---|
| 143 | default: {
|
---|
| 144 | throw new InvalidOperationException(Name + ": Unknown permutation type.");
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
[3104] | 147 | }
|
---|
[3233] | 148 | if (isTabu) break;
|
---|
[3104] | 149 | }
|
---|
| 150 | MoveTabuParameter.ActualValue = new BoolValue(isTabu);
|
---|
| 151 | return base.Apply();
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|