[9348] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[13032] | 3 | * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9348] | 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 |
|
---|
[13032] | 22 |
|
---|
[9348] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
[14046] | 27 | using HeuristicLab.Encodings.PackingEncoding;
|
---|
[9348] | 28 |
|
---|
[14046] | 29 | namespace HeuristicLab.Problems.BinPacking {
|
---|
[9348] | 30 | [StorableClass]
|
---|
[14050] | 31 | public abstract class PackingRatioEvaluator<D, B, I> : EvaluatorBase<D, B, I>
|
---|
[14048] | 32 | where D : class, IPackingPosition
|
---|
[14045] | 33 | where B : PackingShape<D>
|
---|
[14043] | 34 | where I : PackingShape<D>, IPackingItem {
|
---|
[9348] | 35 |
|
---|
| 36 | [StorableConstructor]
|
---|
[14050] | 37 | protected PackingRatioEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 38 | protected PackingRatioEvaluator(PackingRatioEvaluator<D, B, I> original, Cloner cloner)
|
---|
[9348] | 39 | : base(original, cloner) {
|
---|
| 40 | }
|
---|
[14050] | 41 | public PackingRatioEvaluator() : base() { }
|
---|
[9348] | 42 |
|
---|
[14128] | 43 | protected override double Evaluate() {
|
---|
[9348] | 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)
|
---|
[9563] | 62 | return CalculatePackingRatio(plan as PackingPlan<D, B, I>);
|
---|
[9348] | 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 | */
|
---|
[14128] | 76 | public static double CalculatePackingRatio(PackingPlan<D, B, I> plan) {
|
---|
[9440] | 77 | int nrOfBins = plan.NrOfBins;
|
---|
[9348] | 78 | double result = 0;
|
---|
| 79 |
|
---|
| 80 | //C
|
---|
[9563] | 81 | //double usableSpace = binMeasure.MultipliedMeasures;
|
---|
[9348] | 82 | //nrOfBins = N
|
---|
| 83 | for (int i = 0; i < nrOfBins; i++) {
|
---|
[9563] | 84 | //C
|
---|
[9593] | 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));
|
---|
[9348] | 88 | //Fi
|
---|
[9593] | 89 | //double usedSpaceInThisBin = packedItemsInThisBin.Select(s => s.Value.MultipliedMeasures).Sum();
|
---|
[9348] | 90 | //k = 2 --> (Fi/C)*(Fi/C)
|
---|
[9593] | 91 | //result += (((usedSpaceInThisBin) / (usableSpace)) * ((usedSpaceInThisBin) / (usableSpace))) / (i*i + 1);
|
---|
[14128] | 92 | var pd = plan.BinPackings[i].PackingDensity;
|
---|
| 93 | result += (pd * pd) / (i + 1);
|
---|
[9348] | 94 | }
|
---|
| 95 |
|
---|
| 96 | result = result / nrOfBins;
|
---|
[14128] | 97 | return result;
|
---|
[9348] | 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|