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