#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Joseph Helm and 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 HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Data; using HeuristicLab.Common; using HeuristicLab.Encodings.PackingEncoding; namespace HeuristicLab.Problems.BinPacking { [Item("Bin-Utilization Evaluator", " Found in Lee:2010")] // TODO [StorableClass] public abstract class BinUtilizationEvaluator : EvaluatorBase where D : class, IPackingPosition where B : PackingShape where I : PackingShape, IPackingItem { [StorableConstructor] protected BinUtilizationEvaluator(bool deserializing) : base(deserializing) { } protected BinUtilizationEvaluator(BinUtilizationEvaluator original, Cloner cloner) : base(original, cloner) { } public BinUtilizationEvaluator() : base() { } protected override DoubleValue Evaluate() { DoubleValue quality = new DoubleValue(0); IPackingPlan plan = PackingPlanParameter.ActualValue; B binMeasure = PackingBinMeasuresParameter.ActualValue; ItemList itemMeasures = PackingItemMeasuresParameter.ActualValue; // TODO: check and remove code //Check if data is valid //if (plan.PackingItemPositions.Count != itemMeasures.Count) // throw new Exception("ERROR: ItemMeasures.count does not match packingPosition.count"); ////Check if any items are overlapping or not contained in their bins //bool itemPositionsAreValid = !HasOverlappingOrNotContainedItems(plan.PackingItemPositions, binMeasure, itemMeasures, nrOfBins); //if (itemPositionsAreValid) return CalculateBinUtilization(plan as PackingPlan); //return quality; } public static DoubleValue CalculateBinUtilization(PackingPlan plan) { int nrOfBins = plan.NrOfBins; double result = 0; //for (int i = 0; i < nrOfBins; i++) { // double usableSpace = plan.GetPackingBinMeasuresForBinNr(i).MultipliedMeasures; // var indexes = plan.PackingItemPositions.Select((Value, Index) => new { Value, Index }).Where(s => s.Value.Value.AssignedBin == i).Select(s => s.Index); // var packedItemsInThisBin = plan.PackingItemMeasures.Select((Value, Index) => new { Value, Index }).Where(s => indexes.Contains(s.Index)); // double usedSpaceInThisBin = packedItemsInThisBin.Select(s => s.Value.MultipliedMeasures).Sum(); // result += (usedSpaceInThisBin / usableSpace); //} //result = result / nrOfBins; //return new DoubleValue (result); double totalUsedSpace = 0; double totalUsableSpace = 0; for (int i = 0; i < nrOfBins; i++) { double usableSpace = plan.BinPackings[i].BinMeasures.Volume; //var indexes = plan.PackingItemPositions.Select((Value, Index) => new { Value, Index }).Where(s => s.Value.Value.AssignedBin == i).Select(s => s.Index); //var packedItemsInThisBin = plan.PackingItemMeasures.Select((Value, Index) => new { Value, Index }).Where(s => indexes.Contains(s.Index)); //double usedSpaceInThisBin = packedItemsInThisBin.Select(s => s.Value.MultipliedMeasures).Sum(); //result += (usedSpaceInThisBin / usableSpace); totalUsableSpace += usableSpace; //totalUsedSpace += usedSpaceInThisBin; totalUsedSpace += plan.BinPackings[i].PackingDensity * usableSpace; } result = totalUsedSpace / totalUsableSpace; return new DoubleValue(result); } } }