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 


23  using HeuristicLab.Core;


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


25  using HeuristicLab.Data;


26  using HeuristicLab.Common;


27  using HeuristicLab.Encodings.PackingEncoding;


28 


29  namespace HeuristicLab.Problems.BinPacking {


30  [StorableClass]


31  public abstract class PackingRatioEvaluator<D, B, I> : EvaluatorBase<D, B, I>


32  where D : class, IPackingPosition


33  where B : PackingShape<D>


34  where I : PackingShape<D>, IPackingItem {


35 


36  [StorableConstructor]


37  protected PackingRatioEvaluator(bool deserializing) : base(deserializing) { }


38  protected PackingRatioEvaluator(PackingRatioEvaluator<D, B, I> original, Cloner cloner)


39  : base(original, cloner) {


40  }


41  public PackingRatioEvaluator() : base() { }


42 


43  protected override double Evaluate() {


44  DoubleValue quality = new DoubleValue(0);


45 


46  IPackingPlan plan = PackingPlanParameter.ActualValue;


47  B binMeasure = PackingBinMeasuresParameter.ActualValue;


48  ItemList<I> itemMeasures = PackingItemMeasuresParameter.ActualValue;


49 


50 


51  //Check if data is valid


52  //if (plan.PackingItemPositions.Count != itemMeasures.Count)


53  // throw new Exception("ERROR: ItemMeasures.count does not match packingPosition.count");


54 


55 


56  ////Check if any items are overlapping or not contained in their bins


57  //bool itemPositionsAreValid = !HasOverlappingOrNotContainedItems(plan.PackingItemPositions, binMeasure, itemMeasures, nrOfBins);


58 


59 


60 


61  //if (itemPositionsAreValid)


62  return CalculatePackingRatio(plan as PackingPlan<D, B, I>);


63 


64  //return quality;


65  }


66 


67  /*


68  Falkenauer:1996  A Hybrid Grouping Genetic Algorithm for Bin Packing


69 


70  fBPP = (SUM[i=1..N](Fi / C)^k)/N


71  N.......the number of bins used in the solution,


72  Fi......the sum of sizes of the items in the bin i (the fill of the bin),


73  C.......the bin capacity and


74  k.......a constant, k>1.


75  */


76  public static double CalculatePackingRatio(PackingPlan<D, B, I> plan) {


77  int nrOfBins = plan.NrOfBins;


78  double result = 0;


79 


80  //C


81  //double usableSpace = binMeasure.MultipliedMeasures;


82  //nrOfBins = N


83  for (int i = 0; i < nrOfBins; i++) {


84  //C


85  //double usableSpace = plan.GetPackingBinMeasuresForBinNr(0).MultipliedMeasures;//plan.GetPackingBinMeasuresForBinNr(i).MultipliedMeasures;


86  //var indexes = plan.PackingItemPositions.Select((Value, Index) => new { Value, Index }).Where(s => s.Value.Value.AssignedBin == i).Select(s => s.Index);


87  //var packedItemsInThisBin = plan.PackingItemMeasures.Select((Value, Index) => new { Value, Index }).Where(s => indexes.Contains(s.Index));


88  //Fi


89  //double usedSpaceInThisBin = packedItemsInThisBin.Select(s => s.Value.MultipliedMeasures).Sum();


90  //k = 2 > (Fi/C)*(Fi/C)


91  //result += (((usedSpaceInThisBin) / (usableSpace)) * ((usedSpaceInThisBin) / (usableSpace))) / (i*i + 1);


92  var pd = plan.BinPackings[i].PackingDensity;


93  result += (pd * pd) / (i + 1);


94  }


95 


96  result = result / nrOfBins;


97  return result;


98  }


99  }


100  }

