#region License Information
/* HeuristicLab
* Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.IntegerVectorEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Moves {
[Item("N-Move TabuChecker", "Checks if a certain N-Move is tabu.")]
[StorableClass]
public class NMoveTabuChecker : SingleSuccessorOperator, ITabuChecker,
IGQAPNMoveOperator, IMoveQualityAwareGQAPOperator {
public override bool CanChangeName {
get { return false; }
}
public IValueLookupParameter MaximizationParameter {
get { return (IValueLookupParameter)Parameters["Maximization"]; }
}
public ILookupParameter MoveParameter {
get { return (ILookupParameter)Parameters["Move"]; }
}
public ILookupParameter AssignmentParameter {
get { return (ILookupParameter)Parameters["Assignment"]; }
}
public ILookupParameter> TabuListParameter {
get { return (ILookupParameter>)Parameters["TabuList"]; }
}
public ILookupParameter MoveTabuParameter {
get { return (ILookupParameter)Parameters["MoveTabu"]; }
}
public ILookupParameter MoveQualityParameter {
get { return (ILookupParameter)Parameters["MoveQuality"]; }
}
public ILookupParameter MoveEvaluationParameter {
get { return (ILookupParameter)Parameters["MoveEvaluation"]; }
}
public IValueParameter UseAspirationCriterionParameter {
get { return (IValueParameter)Parameters["UseAspirationCriterion"]; }
}
public BoolValue UseAspirationCriterion {
get { return UseAspirationCriterionParameter.Value; }
set { UseAspirationCriterionParameter.Value = value; }
}
[StorableConstructor]
protected NMoveTabuChecker(bool deserializing) : base(deserializing) { }
protected NMoveTabuChecker(NMoveTabuChecker original, Cloner cloner) : base(original, cloner) { }
public NMoveTabuChecker()
: base() {
Parameters.Add(new ValueLookupParameter("Maximization", ""));
Parameters.Add(new LookupParameter("Move", GQAPNMoveGenerator.MoveDescription));
Parameters.Add(new LookupParameter("Assignment", GQAPSolutionCreator.AssignmentDescription));
Parameters.Add(new LookupParameter>("TabuList", "The tabu list contains previous move attributes."));
Parameters.Add(new LookupParameter("MoveTabu", "Declares if a move is tabu or not."));
Parameters.Add(new LookupParameter("MoveQuality", GQAPNMoveEvaluator.MoveQualityDescription));
Parameters.Add(new LookupParameter("MoveEvaluation", GQAPNMoveEvaluator.MoveEvaluationDescription));
Parameters.Add(new ValueParameter("UseAspirationCriterion", "Whether moves can be aspired.", new BoolValue(true)));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new NMoveTabuChecker(this, cloner);
}
public override IOperation Apply() {
ItemList tabuList = TabuListParameter.ActualValue;
var move = MoveParameter.ActualValue;
var assignment = AssignmentParameter.ActualValue;
int length = assignment.Length;
double moveQuality = MoveQualityParameter.ActualValue.Value;
bool maximization = MaximizationParameter.ActualValue.Value;
bool useAspiration = UseAspirationCriterion.Value;
bool isTabu = EvaluateTabuState(tabuList, move, assignment, moveQuality, maximization, useAspiration);
MoveTabuParameter.ActualValue = new BoolValue(isTabu);
return base.Apply();
}
private bool EvaluateTabuState(ItemList tabuList, NMove move, IntegerVector assignment, double moveQuality, bool maximization, bool useAspiration) {
bool isTabu = false;
foreach (var t in tabuList) {
var attribute = (t as NMoveAbsoluteTabuAttribute);
if (attribute == null) continue;
if (!useAspiration
|| maximization && moveQuality <= attribute.MoveQuality
|| !maximization && moveQuality >= attribute.MoveQuality) {
isTabu = true;
foreach (var kvp in attribute.OldAssignments) {
var assignedLoc = move.Reassignments[kvp.Item1] - 1; // location is given 1-based
if (assignedLoc >= 0 && assignedLoc != kvp.Item2) {
isTabu = false;
break;
}
}
}
if (isTabu) break;
}
return isTabu;
}
}
}