#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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 System.Collections.Generic; 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; using HeuristicLab.Problems.BinPacking.Interfaces; namespace HeuristicLab.Encodings.PackingEncoding.GroupingVector { [Item("SingleGroupingMoveHardTabuCriterion", "")] [StorableClass] public class SingleGroupingMoveHardTabuCriterion : SingleSuccessorOperator, IGroupingVectorMoveOperator, ITabuChecker { public override bool CanChangeName { get { return false; } } public ILookupParameter SingleGroupingMoveParameter { get { return (ILookupParameter)Parameters["SingleGroupingMove"]; } } public ILookupParameter GroupingVectorParameter { get { return (ILookupParameter)Parameters["GroupingVector"]; } } public ILookupParameter> TabuListParameter { get { return (ILookupParameter>)Parameters["TabuList"]; } } public ILookupParameter MoveTabuParameter { get { return (ILookupParameter)Parameters["MoveTabu"]; } } public IValueLookupParameter MaximizationParameter { get { return (IValueLookupParameter)Parameters["Maximization"]; } } public ILookupParameter MoveQualityParameter { get { return (ILookupParameter)Parameters["MoveQuality"]; } } public ValueParameter UseAspirationCriterionParameter { get { return (ValueParameter)Parameters["UseAspirationCriterion"]; } } [StorableConstructor] protected SingleGroupingMoveHardTabuCriterion(bool deserializing) : base(deserializing) { } protected SingleGroupingMoveHardTabuCriterion(SingleGroupingMoveHardTabuCriterion original, Cloner cloner) : base(original, cloner) { } public SingleGroupingMoveHardTabuCriterion() : base() { Parameters.Add(new LookupParameter("SingleGroupingMove", "The move to evaluate.")); Parameters.Add(new LookupParameter("GroupingVector", "The solution to evaluate.")); Parameters.Add(new LookupParameter("MoveTabu", "The variable to store if a move was tabu.")); Parameters.Add(new LookupParameter>("TabuList", "The tabu list.")); Parameters.Add(new ValueLookupParameter("Maximization", "True if the problem is a maximization problem, else if it is a minimization problem.")); Parameters.Add(new LookupParameter("MoveQuality", "The quality of the current move.")); Parameters.Add(new ValueParameter("UseAspirationCriterion", "Whether to use the aspiration criterion or not.", new BoolValue(true))); } public override IDeepCloneable Clone(Cloner cloner) { return new SingleGroupingMoveHardTabuCriterion(this, cloner); } public override IOperation Apply() { ItemList tabuList = TabuListParameter.ActualValue; SingleGroupingMove move = SingleGroupingMoveParameter.ActualValue; GroupingVectorEncoding groupingVector = GroupingVectorParameter.ActualValue; int length = groupingVector.GroupingVector.Length; double moveQuality = MoveQualityParameter.ActualValue.Value; bool maximization = MaximizationParameter.ActualValue.Value; bool useAspiration = UseAspirationCriterionParameter.Value.Value; bool isTabu = IsMoveTabu(tabuList, move, groupingVector, moveQuality, maximization, useAspiration); MoveTabuParameter.ActualValue = new BoolValue(isTabu); return base.Apply(); } private bool IsMoveTabu(ItemList tabuList, SingleGroupingMove move, GroupingVectorEncoding groupingVector, double moveQuality, bool maximization, bool useAspiration) { var movedGroupingVector = new IntegerVector(groupingVector.GroupingVector); movedGroupingVector[move.Index] = move.NewGroup; foreach (IItem tabuMove in tabuList) { SingleGroupingMoveAttribute sgmAttr = (tabuMove as SingleGroupingMoveAttribute); if (sgmAttr != null && (!useAspiration || maximization && moveQuality <= sgmAttr.MoveQuality || !maximization && moveQuality >= sgmAttr.MoveQuality)) { if (move.Index == sgmAttr.ItemIndex || groupingVector.GroupingVector[move.Index] == sgmAttr.AssignedBin) return true; } } return false; } } }