Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4046 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

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