Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1966_HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Evaluators/BinUtilizationEvaluator.cs @ 16100

Last change on this file since 16100 was 14128, checked in by gkronber, 8 years ago

#1966: refactoring of bin packing implementation

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
23using HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using HeuristicLab.Data;
26using HeuristicLab.Common;
27using HeuristicLab.Encodings.PackingEncoding;
28
29namespace HeuristicLab.Problems.BinPacking {
30  [Item("Bin-Utilization Evaluator", "<Description missing> Found in Lee:2010")] // TODO
31  [StorableClass]
32  public abstract class BinUtilizationEvaluator<D, B, I> : EvaluatorBase<D, B, I>
33    where D : class, IPackingPosition
34    where B : PackingShape<D>
35    where I : PackingShape<D>, IPackingItem {
36
37    [StorableConstructor]
38    protected BinUtilizationEvaluator(bool deserializing) : base(deserializing) { }
39    protected BinUtilizationEvaluator(BinUtilizationEvaluator<D, B, I> original, Cloner cloner)
40      : base(original, cloner) {
41    }
42    public BinUtilizationEvaluator() : base() { }
43
44    protected override double Evaluate() {
45      DoubleValue quality = new DoubleValue(0);
46
47      IPackingPlan plan = PackingPlanParameter.ActualValue;
48      B binMeasure = PackingBinMeasuresParameter.ActualValue;
49      ItemList<I> itemMeasures = PackingItemMeasuresParameter.ActualValue;
50
51      // TODO: check and remove code
52
53      //Check if data is valid
54      //if (plan.PackingItemPositions.Count != itemMeasures.Count)
55      //  throw new Exception("ERROR: ItemMeasures.count does not match packingPosition.count");
56
57
58      ////Check if any items are overlapping or not contained in their bins
59      //bool itemPositionsAreValid = !HasOverlappingOrNotContainedItems(plan.PackingItemPositions, binMeasure, itemMeasures, nrOfBins);
60
61
62
63      //if (itemPositionsAreValid)
64      return CalculateBinUtilization(plan as PackingPlan<D, B, I>);
65
66      //return quality;
67    }
68
69    public static double CalculateBinUtilization(PackingPlan<D, B, I> plan) {
70      int nrOfBins = plan.NrOfBins;
71      double result = 0;
72
73      //for (int i = 0; i < nrOfBins; i++) {
74      //  double usableSpace = plan.GetPackingBinMeasuresForBinNr(i).MultipliedMeasures;
75      //  var indexes = plan.PackingItemPositions.Select((Value, Index) => new { Value, Index }).Where(s => s.Value.Value.AssignedBin == i).Select(s => s.Index);
76      //  var packedItemsInThisBin = plan.PackingItemMeasures.Select((Value, Index) => new { Value, Index }).Where(s => indexes.Contains(s.Index));
77      //  double usedSpaceInThisBin = packedItemsInThisBin.Select(s => s.Value.MultipliedMeasures).Sum();
78      //  result += (usedSpaceInThisBin / usableSpace);
79      //}
80
81      //result = result / nrOfBins;
82      //return new DoubleValue (result);
83
84      double totalUsedSpace = 0;
85      double totalUsableSpace = 0;
86
87      for (int i = 0; i < nrOfBins; i++) {
88        double usableSpace = plan.BinPackings[i].BinMeasures.Volume;
89        //var indexes = plan.PackingItemPositions.Select((Value, Index) => new { Value, Index }).Where(s => s.Value.Value.AssignedBin == i).Select(s => s.Index);
90        //var packedItemsInThisBin = plan.PackingItemMeasures.Select((Value, Index) => new { Value, Index }).Where(s => indexes.Contains(s.Index));
91        //double usedSpaceInThisBin = packedItemsInThisBin.Select(s => s.Value.MultipliedMeasures).Sum();
92        //result += (usedSpaceInThisBin / usableSpace);
93        totalUsableSpace += usableSpace;
94        //totalUsedSpace += usedSpaceInThisBin;
95        totalUsedSpace += plan.BinPackings[i].PackingDensity * usableSpace;
96      }
97
98      result = totalUsedSpace / totalUsableSpace;
99      return result;
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.