Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Moves/NMoveTabuChecker.cs @ 15511

Last change on this file since 15511 was 15511, checked in by abeham, 7 years ago

#1614: Improved performance by switching from Dictionary to Array

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.IntegerVectorEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Moves {
32  [Item("N-Move TabuChecker", "Checks if a certain N-Move is tabu.")]
33  [StorableClass]
34  public class NMoveTabuChecker : SingleSuccessorOperator, ITabuChecker,
35    IGQAPNMoveOperator, IMoveQualityAwareGQAPOperator {
36    public override bool CanChangeName {
37      get { return false; }
38    }
39    public IValueLookupParameter<BoolValue> MaximizationParameter {
40      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
41    }
42    public ILookupParameter<NMove> MoveParameter {
43      get { return (ILookupParameter<NMove>)Parameters["Move"]; }
44    }
45    public ILookupParameter<IntegerVector> AssignmentParameter {
46      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
47    }
48    public ILookupParameter<ItemList<IItem>> TabuListParameter {
49      get { return (ILookupParameter<ItemList<IItem>>)Parameters["TabuList"]; }
50    }
51    public ILookupParameter<BoolValue> MoveTabuParameter {
52      get { return (ILookupParameter<BoolValue>)Parameters["MoveTabu"]; }
53    }
54    public ILookupParameter<DoubleValue> MoveQualityParameter {
55      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
56    }
57    public ILookupParameter<Evaluation> MoveEvaluationParameter {
58      get { return (ILookupParameter<Evaluation>)Parameters["MoveEvaluation"]; }
59    }
60    public IValueParameter<BoolValue> UseAspirationCriterionParameter {
61      get { return (IValueParameter<BoolValue>)Parameters["UseAspirationCriterion"]; }
62    }
63
64    public BoolValue UseAspirationCriterion {
65      get { return UseAspirationCriterionParameter.Value; }
66      set { UseAspirationCriterionParameter.Value = value; }
67    }
68
69    [StorableConstructor]
70    protected NMoveTabuChecker(bool deserializing) : base(deserializing) { }
71    protected NMoveTabuChecker(NMoveTabuChecker original, Cloner cloner) : base(original, cloner) { }
72    public NMoveTabuChecker()
73      : base() {
74      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", ""));
75      Parameters.Add(new LookupParameter<NMove>("Move", GQAPNMoveGenerator.MoveDescription));
76      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
77      Parameters.Add(new LookupParameter<ItemList<IItem>>("TabuList", "The tabu list contains previous move attributes."));
78      Parameters.Add(new LookupParameter<BoolValue>("MoveTabu", "Declares if a move is tabu or not."));
79      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", GQAPNMoveEvaluator.MoveQualityDescription));
80      Parameters.Add(new LookupParameter<Evaluation>("MoveEvaluation", GQAPNMoveEvaluator.MoveEvaluationDescription));
81      Parameters.Add(new ValueParameter<BoolValue>("UseAspirationCriterion", "Whether moves can be aspired.", new BoolValue(true)));
82    }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      return new NMoveTabuChecker(this, cloner);
86    }
87
88    public override IOperation Apply() {
89      ItemList<IItem> tabuList = TabuListParameter.ActualValue;
90      var move = MoveParameter.ActualValue;
91      var assignment = AssignmentParameter.ActualValue;
92      int length = assignment.Length;
93      double moveQuality = MoveQualityParameter.ActualValue.Value;
94      bool maximization = MaximizationParameter.ActualValue.Value;
95      bool useAspiration = UseAspirationCriterion.Value;
96     
97      bool isTabu = EvaluateTabuState(tabuList, move, assignment, moveQuality, maximization, useAspiration);
98
99      MoveTabuParameter.ActualValue = new BoolValue(isTabu);
100      return base.Apply();
101    }
102
103    private bool EvaluateTabuState(ItemList<IItem> tabuList, NMove move, IntegerVector assignment, double moveQuality, bool maximization, bool useAspiration) {
104      bool isTabu = false;
105      foreach (var t in tabuList) {
106        var attribute = (t as NMoveAbsoluteTabuAttribute);
107        if (attribute == null) continue;
108        if (!useAspiration
109          || maximization && moveQuality <= attribute.MoveQuality
110          || !maximization && moveQuality >= attribute.MoveQuality) {
111          isTabu = true;
112          foreach (var kvp in attribute.OldAssignments) {
113            var assignedLoc = move.Reassignments[kvp.Item1] - 1; // location is given 1-based
114            if (assignedLoc >= 0 && assignedLoc != kvp.Item2) {
115              isTabu = false;
116              break;
117            }
118          }
119        }
120        if (isTabu) break;
121      }
122      return isTabu;
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.