Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/ThreeOpt/TranslocationMoveHardTabuCriterion.cs @ 17511

Last change on this file since 17511 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 9.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
29
30namespace HeuristicLab.Encodings.PermutationEncoding {
31  [Item("TranslocationMoveHardTabuCriterion", @"For relative postion encoded permutations it prevents readding of previously deleted edges as well as deleting previously added edges.
32  For absolute position encoded permutations it prevents moving a number if it was previously moved.
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  [StorableType("FD8C3ED7-734F-4B90-9539-2367C62FA7EB")]
36  public class TranslocationMoveHardTabuCriterion : 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 TranslocationMoveHardTabuCriterion(StorableConstructorFlag _) : base(_) { }
69    protected TranslocationMoveHardTabuCriterion(TranslocationMoveHardTabuCriterion original, Cloner cloner) : base(original, cloner) { }
70    public TranslocationMoveHardTabuCriterion()
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 TranslocationMoveHardTabuCriterion(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]) {
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 (permutation.PermutationType == PermutationTypes.RelativeUndirected) {
142                if (// if previously added Edge3Source-Edge1Target is deleted
143                  attribute.Edge3Source == E1S && attribute.Edge1Target == E1T || attribute.Edge3Source == E1T && attribute.Edge1Target == E1S
144                  || attribute.Edge3Source == E2S && attribute.Edge1Target == E2T || attribute.Edge3Source == E2T && attribute.Edge1Target == E2S
145                  || attribute.Edge3Source == E3S && attribute.Edge1Target == E3T || attribute.Edge3Source == E3T && attribute.Edge1Target == E3S
146                  // if previously added Edge2Source-Edge3Target is deleted
147                  || attribute.Edge2Source == E1S && attribute.Edge3Target == E1T || attribute.Edge2Source == E1T && attribute.Edge3Target == E1S
148                  || attribute.Edge2Source == E2S && attribute.Edge3Target == E2T || attribute.Edge2Source == E2T && attribute.Edge3Target == E2S
149                  || attribute.Edge2Source == E3S && attribute.Edge3Target == E3T || attribute.Edge2Source == E3T && attribute.Edge3Target == E3S
150                  // if previously added Edge1Source-Edge2Target is deleted
151                  || attribute.Edge1Source == E1S && attribute.Edge2Target == E1T || attribute.Edge1Source == E1T && attribute.Edge2Target == E1S
152                  || attribute.Edge1Source == E2S && attribute.Edge2Target == E2T || attribute.Edge1Source == E2T && attribute.Edge2Target == E2S
153                  || attribute.Edge1Source == E3S && attribute.Edge2Target == E3T || attribute.Edge1Source == E3T && attribute.Edge2Target == E3S) {
154                  isTabu = true;
155                  break;
156                }
157              } else {
158                if (// if previously added Edge3Source-Edge1Target is deleted
159                  attribute.Edge3Source == E1S && attribute.Edge1Target == E1T
160                  || attribute.Edge3Source == E2S && attribute.Edge1Target == E2T
161                  || attribute.Edge3Source == E3S && attribute.Edge1Target == E3T
162                  // if previously added Edge2Source-Edge3Target is deleted
163                  || attribute.Edge2Source == E1S && attribute.Edge3Target == E1T
164                  || attribute.Edge2Source == E2S && attribute.Edge3Target == E2T
165                  || attribute.Edge2Source == E3S && attribute.Edge3Target == E3T
166                  // if previously added Edge1Source-Edge2Target is deleted
167                  || attribute.Edge1Source == E1S && attribute.Edge2Target == E1T
168                  || attribute.Edge1Source == E2S && attribute.Edge2Target == E2T
169                  || attribute.Edge1Source == E3S && attribute.Edge2Target == E3T) {
170                  isTabu = true;
171                  break;
172                }
173              }
174            }
175          }
176        }
177      }
178      MoveTabuParameter.ActualValue = new BoolValue(isTabu);
179      return base.Apply();
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.