Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/ThreeOpt/TranslocationMoveHardTabuCriterion.cs @ 3340

Last change on this file since 3340 was 3340, checked in by abeham, 14 years ago

Updated Tabu search, permutation move operators, real vector move operators, binary vector move operators #840
Added a Tabu Search TSP workbench

File size: 9.5 KB
Line 
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
22using System;
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("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  [StorableClass]
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    public TranslocationMoveHardTabuCriterion()
68      : base() {
69      Parameters.Add(new LookupParameter<TranslocationMove>("TranslocationMove", "The move to evaluate."));
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."));
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."));
76    }
77
78    public override IOperation Apply() {
79      ItemList<IItem> tabuList = TabuListParameter.ActualValue;
80      TranslocationMove move = TranslocationMoveParameter.ActualValue;
81      Permutation permutation = PermutationParameter.ActualValue;
82      int length = permutation.Length;
83      double moveQuality = MoveQualityParameter.ActualValue.Value;
84      bool maximization = MaximizationParameter.ActualValue.Value;
85      bool useAspiration = UseAspirationCriterion.Value;
86      bool isTabu = false;
87
88      if (permutation.PermutationType == PermutationTypes.Absolute) {
89        int count = move.Index2 - move.Index1 + 1;
90        int[] numbers = new int[count];
91        for (int i = move.Index1; i <= move.Index2; i++)
92          numbers[i - move.Index1] = permutation[i];
93
94        foreach (IItem tabuMove in tabuList) {
95          TranslocationMoveAbsoluteAttribute attribute = (tabuMove as TranslocationMoveAbsoluteAttribute);
96          if (attribute != null) {
97            if (!useAspiration
98              || maximization && moveQuality <= attribute.MoveQuality
99              || !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
100
101              for (int i = 0; i < count; i++) {
102                for (int j = 0; j < attribute.Number.Length; j++) {
103                  if (attribute.Number[j] == numbers[i]) {
104                    isTabu = true;
105                    break;
106                  }
107                }
108                if (isTabu) break;
109              }
110            }
111          }
112          if (isTabu) break;
113        }
114      } else {
115        int E1S = permutation.GetCircular(move.Index1 - 1);
116        int E1T = permutation[move.Index1];
117        int E2S = permutation[move.Index2];
118        int E2T = permutation.GetCircular(move.Index2 + 1);
119        int E3S, E3T;
120        if (move.Index3 > move.Index1) {
121          E3S = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1);
122          E3T = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1 + 1);
123        } else {
124          E3S = permutation.GetCircular(move.Index3 - 1);
125          E3T = permutation[move.Index3];
126        }
127        foreach (IItem tabuMove in tabuList) {
128          TranslocationMoveRelativeAttribute attribute = (tabuMove as TranslocationMoveRelativeAttribute);
129          if (attribute != null) {
130            if (!useAspiration
131              || maximization && moveQuality <= attribute.MoveQuality
132              || !maximization && moveQuality >= attribute.MoveQuality) {
133
134              if (permutation.PermutationType == PermutationTypes.RelativeUndirected) {
135                if (// if previously added Edge3Source-Edge1Target is deleted
136                  attribute.Edge3Source == E1S && attribute.Edge1Target == E1T || attribute.Edge3Source == E1T && attribute.Edge1Target == E1S
137                  || attribute.Edge3Source == E2S && attribute.Edge1Target == E2T || attribute.Edge3Source == E2T && attribute.Edge1Target == E2S
138                  || attribute.Edge3Source == E3S && attribute.Edge1Target == E3T || attribute.Edge3Source == E3T && attribute.Edge1Target == E3S
139                  // if previously added Edge2Source-Edge3Target is deleted
140                  || attribute.Edge2Source == E1S && attribute.Edge3Target == E1T || attribute.Edge2Source == E1T && attribute.Edge3Target == E1S
141                  || attribute.Edge2Source == E2S && attribute.Edge3Target == E2T || attribute.Edge2Source == E2T && attribute.Edge3Target == E2S
142                  || attribute.Edge2Source == E3S && attribute.Edge3Target == E3T || attribute.Edge2Source == E3T && attribute.Edge3Target == E3S
143                  // if previously added Edge1Source-Edge2Target is deleted
144                  || attribute.Edge1Source == E1S && attribute.Edge2Target == E1T || attribute.Edge1Source == E1T && attribute.Edge2Target == E1S
145                  || attribute.Edge1Source == E2S && attribute.Edge2Target == E2T || attribute.Edge1Source == E2T && attribute.Edge2Target == E2S
146                  || attribute.Edge1Source == E3S && attribute.Edge2Target == E3T || attribute.Edge1Source == E3T && attribute.Edge2Target == E3S) {
147                  isTabu = true;
148                  break;
149                }
150              } else {
151                if (// if previously added Edge3Source-Edge1Target is deleted
152                  attribute.Edge3Source == E1S && attribute.Edge1Target == E1T
153                  || attribute.Edge3Source == E2S && attribute.Edge1Target == E2T
154                  || attribute.Edge3Source == E3S && attribute.Edge1Target == E3T
155                  // if previously added Edge2Source-Edge3Target is deleted
156                  || attribute.Edge2Source == E1S && attribute.Edge3Target == E1T
157                  || attribute.Edge2Source == E2S && attribute.Edge3Target == E2T
158                  || attribute.Edge2Source == E3S && attribute.Edge3Target == E3T
159                  // if previously added Edge1Source-Edge2Target is deleted
160                  || attribute.Edge1Source == E1S && attribute.Edge2Target == E1T
161                  || attribute.Edge1Source == E2S && attribute.Edge2Target == E2T
162                  || attribute.Edge1Source == E3S && attribute.Edge2Target == 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.