[14147]  1  #region License Information


 2  /* HeuristicLab


 3  * Copyright (C) 20022015 Joseph Helm and 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 


 22  using HeuristicLab.Common;


 23  using HeuristicLab.Core;


 24  using HeuristicLab.Data;


 25  using HeuristicLab.Encodings.PermutationEncoding;


 26  using HeuristicLab.Operators;


 27  using HeuristicLab.Optimization;


 28  using HeuristicLab.Parameters;


 29  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


 30 


 31  namespace HeuristicLab.Problems.BinPacking2D {


 32  [Item("Swap2MoveEvaluator", "Move evaluator for 2opt moves.")]


 33  [StorableClass]


 34  public sealed class Swap2MoveEvaluator : SingleSuccessorOperator,


 35  ISingleObjectiveMoveEvaluator, ISingleObjectiveMoveOperator, IPermutationSwap2MoveOperator {


 36  public override bool CanChangeName {


 37  get { return false; }


 38  }


 39  public ILookupParameter<Permutation> PermutationParameter {


 40  get { return (ILookupParameter<Permutation>)Parameters["PackingSequence"]; }


 41  }


 42  public ILookupParameter<DoubleValue> QualityParameter {


 43  get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }


 44  }


 45  public ILookupParameter<DoubleValue> MoveQualityParameter {


 46  get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }


 47  }


 48  public ILookupParameter<ReadOnlyItemList<PackingItem>> PackingItemMeasuresParameter {


 49  get { return (LookupParameter<ReadOnlyItemList<PackingItem>>)Parameters["Items"]; }


 50  }


 51  public ILookupParameter<PackingShape> PackingBinMeasuresParameter {


 52  get { return (LookupParameter<PackingShape>)Parameters["BinShape"]; }


 53  }


 54  public ILookupParameter<IPermutationDecoder> PackingSolutionDecoderParameter {


 55  get { return (ILookupParameter<IPermutationDecoder>)Parameters["Decoder"]; }


 56  }


 57  public ILookupParameter<IEvaluator> PackingPlanEvaluatorParameter {


 58  get { return (ILookupParameter<IEvaluator>)Parameters["SolutionEvaluator"]; }


 59  }


 60 


 61  public ILookupParameter<Swap2Move> Swap2MoveParameter {


 62  get { return (ILookupParameter<Swap2Move>)Parameters["Swap2Move"]; }


 63  }


 64 


 65  [StorableConstructor]


 66  private Swap2MoveEvaluator(bool deserializing) : base(deserializing) { }


 67  private Swap2MoveEvaluator(Swap2MoveEvaluator original, Cloner cloner) : base(original, cloner) { }


 68  public Swap2MoveEvaluator()


 69  : base() {


 70  Parameters.Add(new LookupParameter<Permutation>("PackingSequence", "The encoded solution candidate to evaluate"));


 71  Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of a packing solution."));


 72  Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The evaluated quality of a move on a packing solution."));


 73  Parameters.Add(new LookupParameter<ReadOnlyItemList<PackingItem>>("Items", "Packingitem data taken from the binpacking probleminstance."));


 74  Parameters.Add(new LookupParameter<PackingShape>("BinShape", "Packingbin data taken from the binpacking probleminstance."));


 75  Parameters.Add(new LookupParameter<IPermutationDecoder>("Decoder", "The decoding operator that is used to calculate a packing plan from the used representation."));


 76  Parameters.Add(new LookupParameter<IEvaluator>("SolutionEvaluator", "The actual packing plan evaluation operator."));


 77  Parameters.Add(new LookupParameter<Swap2Move>("Swap2Move", "The move to evaluate."));


 78  }


 79 


 80  public override IDeepCloneable Clone(Cloner cloner) {


 81  return new Swap2MoveEvaluator(this, cloner);


 82  }


 83 


 84 


 85  public override IOperation Apply() {


 86  var move = Swap2MoveParameter.ActualValue;


 87  var permutation = PermutationParameter.ActualValue;


 88  var binShape = PackingBinMeasuresParameter.ActualValue;


 89  var items = PackingItemMeasuresParameter.ActualValue;


 90 


 91  // uses full evaluation


 92  Swap2Manipulator.Apply(permutation, move.Index1, move.Index2);


 93  var solution = PackingSolutionDecoderParameter.ActualValue.Decode(permutation, binShape, items);


 94 


 95  var quality = PackingPlanEvaluatorParameter.ActualValue.Evaluate(solution);


 96 


 97  double moveQuality = quality;


 98 


 99  if (MoveQualityParameter.ActualValue == null)


 100  MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);


 101  else


 102  MoveQualityParameter.ActualValue.Value = moveQuality;


 103  return base.Apply();


 104  }


 105  }


 106  }

