Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2936_GQAPIntegration/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Moves/NMoveTabuChecker.cs @ 17328

Last change on this file since 17328 was 16712, checked in by gkronber, 6 years ago

#2936: adapted branch to new persistence (works with HL trunk r16711)

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