Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4294 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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