Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2645_ContextAlgorithms/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/ThreeOpt/TranslocationMoveSoftTabuCriterion.cs @ 16617

Last change on this file since 16617 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 9.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.PermutationEncoding {
31  [Item("TranslocationMoveSoftTabuCriterion", @"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
34If 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.")]
35  [StorableClass]
36  public class TranslocationMoveSoftTabuCriterion : SingleSuccessorOperator, IPermutationTranslocationMoveOperator, ITabuChecker {
37    public override bool CanChangeName {
38      get { return false; }
39    }
40    public ILookupParameter<TranslocationMove> TranslocationMoveParameter {
41      get { return (LookupParameter<TranslocationMove>)Parameters["TranslocationMove"]; }
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    }
52    public IValueLookupParameter<BoolValue> MaximizationParameter {
53      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
54    }
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    }
61
62    public BoolValue UseAspirationCriterion {
63      get { return UseAspirationCriterionParameter.Value; }
64      set { UseAspirationCriterionParameter.Value = value; }
65    }
66
67    [StorableConstructor]
68    protected TranslocationMoveSoftTabuCriterion(bool deserializing) : base(deserializing) { }
69    protected TranslocationMoveSoftTabuCriterion(TranslocationMoveSoftTabuCriterion original, Cloner cloner) : base(original, cloner) { }
70    public TranslocationMoveSoftTabuCriterion()
71      : base() {
72      Parameters.Add(new LookupParameter<TranslocationMove>("TranslocationMove", "The move to evaluate."));
73      Parameters.Add(new LookupParameter<BoolValue>("MoveTabu", "The variable to store if a move was tabu."));
74      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
75      Parameters.Add(new LookupParameter<ItemList<IItem>>("TabuList", "The tabu list."));
76      Parameters.Add(new ValueParameter<BoolValue>("UseAspirationCriterion", "Whether to use the aspiration criterion or not.", new BoolValue(true)));
77      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, else if it is a minimization problem."));
78      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the current move."));
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new TranslocationMoveSoftTabuCriterion(this, cloner);
83    }
84
85    public override IOperation Apply() {
86      ItemList<IItem> tabuList = TabuListParameter.ActualValue;
87      TranslocationMove move = TranslocationMoveParameter.ActualValue;
88      Permutation permutation = PermutationParameter.ActualValue;
89      int length = permutation.Length;
90      double moveQuality = MoveQualityParameter.ActualValue.Value;
91      bool maximization = MaximizationParameter.ActualValue.Value;
92      bool useAspiration = UseAspirationCriterion.Value;
93      bool isTabu = false;
94
95      if (permutation.PermutationType == PermutationTypes.Absolute) {
96        int count = move.Index2 - move.Index1 + 1;
97        int[] numbers = new int[count];
98        for (int i = move.Index1; i <= move.Index2; i++)
99          numbers[i - move.Index1] = permutation[i];
100
101        foreach (IItem tabuMove in tabuList) {
102          TranslocationMoveAbsoluteAttribute attribute = (tabuMove as TranslocationMoveAbsoluteAttribute);
103          if (attribute != null) {
104            if (!useAspiration
105              || maximization && moveQuality <= attribute.MoveQuality
106              || !maximization && moveQuality >= attribute.MoveQuality) { // if the move quality is improving beyond what was recorded when the move in the tabu list was recorded the move is regarded as okay
107
108              for (int i = 0; i < count; i++) {
109                for (int j = 0; j < attribute.Number.Length; j++) {
110                  if (attribute.Number[j] == numbers[i] && attribute.OldPosition + j == move.Index3 + i) {
111                    isTabu = true;
112                    break;
113                  }
114                }
115                if (isTabu) break;
116              }
117            }
118          }
119          if (isTabu) break;
120        }
121      } else {
122        int E1S = permutation.GetCircular(move.Index1 - 1);
123        int E1T = permutation[move.Index1];
124        int E2S = permutation[move.Index2];
125        int E2T = permutation.GetCircular(move.Index2 + 1);
126        int E3S, E3T;
127        if (move.Index3 > move.Index1) {
128          E3S = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1);
129          E3T = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1 + 1);
130        } else {
131          E3S = permutation.GetCircular(move.Index3 - 1);
132          E3T = permutation[move.Index3];
133        }
134        foreach (IItem tabuMove in tabuList) {
135          TranslocationMoveRelativeAttribute attribute = (tabuMove as TranslocationMoveRelativeAttribute);
136          if (attribute != null) {
137            if (!useAspiration
138              || maximization && moveQuality <= attribute.MoveQuality
139              || !maximization && moveQuality >= attribute.MoveQuality) {
140
141              // if previously deleted Edge1Source-Target is readded
142              if (permutation.PermutationType == PermutationTypes.RelativeUndirected) {
143                if (attribute.Edge1Source == E3S && attribute.Edge1Target == E1T || attribute.Edge1Source == E1T && attribute.Edge1Target == E3S
144                  || attribute.Edge1Source == E2S && attribute.Edge1Target == E3T || attribute.Edge1Source == E3T && attribute.Edge1Target == E2S
145                  // if previously deleted Edge2Source-Target is readded
146                  || attribute.Edge2Source == E3S && attribute.Edge2Target == E1T || attribute.Edge2Source == E1T && attribute.Edge2Target == E3S
147                  || attribute.Edge2Source == E2S && attribute.Edge2Target == E3T || attribute.Edge2Source == E3T && attribute.Edge2Target == E2S
148                  // if previously deleted Edge3Source-Target is readded
149                  || attribute.Edge3Source == E3S && attribute.Edge3Target == E1T || attribute.Edge3Source == E1T && attribute.Edge3Target == E3S
150                  || attribute.Edge3Source == E2S && attribute.Edge3Target == E3T || attribute.Edge3Source == E3T && attribute.Edge3Target == E2S) {
151                  isTabu = true;
152                  break;
153                }
154              } else {
155                if (attribute.Edge1Source == E3S && attribute.Edge1Target == E1T
156                  || attribute.Edge1Source == E2S && attribute.Edge1Target == E3T
157                  // if previously deleted Edge2Source-Target is readded
158                  || attribute.Edge2Source == E3S && attribute.Edge2Target == E1T
159                  || attribute.Edge2Source == E2S && attribute.Edge2Target == E3T
160                  // if previously deleted Edge3Source-Target is readded
161                  || attribute.Edge3Source == E3S && attribute.Edge3Target == E1T
162                  || attribute.Edge3Source == E2S && attribute.Edge3Target == E3T) {
163                  isTabu = true;
164                  break;
165                }
166              }
167            }
168          }
169        }
170      }
171      MoveTabuParameter.ActualValue = new BoolValue(isTabu);
172      return base.Apply();
173    }
174  }
175}
Note: See TracBrowser for help on using the repository browser.