Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/ThreeOpt/TranslocationMoveSoftTabuCriterion.cs @ 4656

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

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

File size: 8.8 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("TranslocationMoveSoftTabuCriterion", @"For relative postion encoded permutations it just prevents readding of previously deleted edges, but allows deleting previously added edges.
31  For absolute position encoded permutations it prevents moving a number to a position it has previously occupied.
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 TranslocationMoveSoftTabuCriterion : 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 TranslocationMoveSoftTabuCriterion()
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] && attribute.OldPosition + j == move.Index3 + 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 previously deleted Edge1Source-Target is readded
134              if (permutation.PermutationType == PermutationTypes.RelativeUndirected) {
135                if (attribute.Edge1Source == E3S && attribute.Edge1Target == E1T || attribute.Edge1Source == E1T && attribute.Edge1Target == E3S
136                  || attribute.Edge1Source == E2S && attribute.Edge1Target == E3T || attribute.Edge1Source == E3T && attribute.Edge1Target == E2S
137                  // if previously deleted Edge2Source-Target is readded
138                  || attribute.Edge2Source == E3S && attribute.Edge2Target == E1T || attribute.Edge2Source == E1T && attribute.Edge2Target == E3S
139                  || attribute.Edge2Source == E2S && attribute.Edge2Target == E3T || attribute.Edge2Source == E3T && attribute.Edge2Target == E2S
140                  // if previously deleted Edge3Source-Target is readded
141                  || attribute.Edge3Source == E3S && attribute.Edge3Target == E1T || attribute.Edge3Source == E1T && attribute.Edge3Target == E3S
142                  || attribute.Edge3Source == E2S && attribute.Edge3Target == E3T || attribute.Edge3Source == E3T && attribute.Edge3Target == E2S) {
143                  isTabu = true;
144                  break;
145                }
146              } else {
147                if (attribute.Edge1Source == E3S && attribute.Edge1Target == E1T
148                  || attribute.Edge1Source == E2S && attribute.Edge1Target == E3T
149                  // if previously deleted Edge2Source-Target is readded
150                  || attribute.Edge2Source == E3S && attribute.Edge2Target == E1T
151                  || attribute.Edge2Source == E2S && attribute.Edge2Target == E3T
152                  // if previously deleted Edge3Source-Target is readded
153                  || attribute.Edge3Source == E3S && attribute.Edge3Target == E1T
154                  || attribute.Edge3Source == E2S && attribute.Edge3Target == E3T) {
155                  isTabu = true;
156                  break;
157                }
158              }
159            }
160          }
161        }
162      }
163      MoveTabuParameter.ActualValue = new BoolValue(isTabu);
164      return base.Apply();
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.