Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/Swap2/Swap2MoveHardTabuCriterion.cs @ 11147

Last change on this file since 11147 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 8.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Collections.Generic;
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("Swap2MoveHardTabuCriterion", @"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 Swap2MoveHardTabuCriterion : SingleSuccessorOperator, IPermutationSwap2MoveOperator, ITabuChecker {
38    public override bool CanChangeName {
39      get { return false; }
40    }
41    public ILookupParameter<Swap2Move> Swap2MoveParameter {
42      get { return (LookupParameter<Swap2Move>)Parameters["Swap2Move"]; }
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    [StorableConstructor]
69    protected Swap2MoveHardTabuCriterion(bool deserializing) : base(deserializing) { }
70    protected Swap2MoveHardTabuCriterion(Swap2MoveHardTabuCriterion original, Cloner cloner) : base(original, cloner) { }
71    public Swap2MoveHardTabuCriterion()
72      : base() {
73      Parameters.Add(new LookupParameter<Swap2Move>("Swap2Move", "The move to evaluate."));
74      Parameters.Add(new LookupParameter<BoolValue>("MoveTabu", "The variable to store if a move was tabu."));
75      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
76      Parameters.Add(new LookupParameter<ItemList<IItem>>("TabuList", "The tabu list."));
77      Parameters.Add(new ValueParameter<BoolValue>("UseAspirationCriterion", "Whether to use the aspiration criterion or not.", new BoolValue(true)));
78      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, else if it is a minimization problem."));
79      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the current move."));
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new Swap2MoveHardTabuCriterion(this, cloner);
84    }
85
86    public override IOperation Apply() {
87      ItemList<IItem> tabuList = TabuListParameter.ActualValue;
88      Swap2Move move = Swap2MoveParameter.ActualValue;
89      Permutation permutation = PermutationParameter.ActualValue;
90      int length = permutation.Length;
91      double moveQuality = MoveQualityParameter.ActualValue.Value;
92      bool maximization = MaximizationParameter.ActualValue.Value;
93      bool useAspiration = UseAspirationCriterion.Value;
94      bool isTabu = false;
95
96      if (permutation.PermutationType != PermutationTypes.Absolute)
97        isTabu = EvaluateRelativeTabuState(tabuList, move, permutation, moveQuality, maximization, useAspiration);
98      else isTabu = EvaluateAbsoluteTabuState(tabuList, move, permutation, moveQuality, maximization, useAspiration);
99
100      MoveTabuParameter.ActualValue = new BoolValue(isTabu);
101      return base.Apply();
102    }
103
104    private static bool EvaluateRelativeTabuState(ItemList<IItem> tabuList, Swap2Move move, Permutation permutation, double moveQuality, bool maximization, bool useAspiration) {
105      bool isTabu = false;
106      StandardEdgeEqualityComparer eq = new StandardEdgeEqualityComparer();
107      bool bidirectional = permutation.PermutationType == PermutationTypes.RelativeUndirected;
108      List<Edge> deleted = new List<Edge>();
109      deleted.Add(new Edge(permutation.GetCircular(move.Index1 - 1), permutation[move.Index1], bidirectional));
110      deleted.Add(new Edge(permutation[move.Index1], permutation.GetCircular(move.Index1 + 1), bidirectional));
111      deleted.Add(new Edge(permutation.GetCircular(move.Index2 - 1), permutation[move.Index2], bidirectional));
112      deleted.Add(new Edge(permutation[move.Index2], permutation.GetCircular(move.Index2 + 1), bidirectional));
113      List<Edge> added = new List<Edge>();
114      added.Add(new Edge(permutation.GetCircular(move.Index1 - 1), permutation[move.Index2], bidirectional));
115      added.Add(new Edge(permutation[move.Index2], permutation.GetCircular(move.Index1 + 1), bidirectional));
116      added.Add(new Edge(permutation.GetCircular(move.Index2 - 1), permutation[move.Index1], bidirectional));
117      added.Add(new Edge(permutation[move.Index1], permutation.GetCircular(move.Index2 + 1), bidirectional));
118
119      foreach (IItem tabuMove in tabuList) {
120        Swap2MoveRelativeAttribute relAttrib = (tabuMove as Swap2MoveRelativeAttribute);
121        if (relAttrib != null
122          && (!useAspiration
123              || maximization && moveQuality <= relAttrib.MoveQuality
124              || !maximization && moveQuality >= relAttrib.MoveQuality)) {
125          for (int i = 0; i < relAttrib.AddedEdges.Count; i++) {
126            isTabu = eq.Equals(relAttrib.AddedEdges[i], deleted[0])
127                  || eq.Equals(relAttrib.AddedEdges[i], deleted[1])
128                  || eq.Equals(relAttrib.AddedEdges[i], deleted[2])
129                  || eq.Equals(relAttrib.AddedEdges[i], deleted[3]);
130            if (isTabu) break;
131          }
132          if (isTabu) break;
133          for (int i = 0; i < relAttrib.DeletedEdges.Count; i++) {
134            isTabu = eq.Equals(relAttrib.DeletedEdges[i], added[0])
135                  || eq.Equals(relAttrib.DeletedEdges[i], added[1])
136                  || eq.Equals(relAttrib.DeletedEdges[i], added[2])
137                  || eq.Equals(relAttrib.DeletedEdges[i], added[3]);
138            if (isTabu) break;
139          }
140        }
141        if (isTabu) break;
142      }
143      return isTabu;
144    }
145
146    private bool EvaluateAbsoluteTabuState(ItemList<IItem> tabuList, Swap2Move move, Permutation permutation, double moveQuality, bool maximization, bool useAspiration) {
147      bool isTabu = false;
148      foreach (IItem tabuMove in tabuList) {
149        Swap2MoveAbsoluteAttribute attrib = (tabuMove as Swap2MoveAbsoluteAttribute);
150        if (attrib != null
151          && (!useAspiration
152              || maximization && moveQuality <= attrib.MoveQuality
153              || !maximization && moveQuality >= attrib.MoveQuality)) {
154          int i1 = move.Index1;
155          int n1 = permutation[move.Index1];
156          int i2 = move.Index2;
157          int n2 = permutation[move.Index2];
158          if (attrib != null) {
159            if (attrib.Number1 == n1 || attrib.Number1 == n2
160              || attrib.Number2 == n2 || attrib.Number2 == n1)
161              isTabu = true;
162          }
163        }
164        if (isTabu) break;
165      }
166      return isTabu;
167    }
168  }
169}
Note: See TracBrowser for help on using the repository browser.