Changeset 9473 for branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Decoders/3D
- Timestamp:
- 05/09/13 15:03:41 (12 years ago)
- Location:
- branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Decoders/3D/EP
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Decoders/3D/EP/ExtremePointsFunctions3D.cs
r9440 r9473 5 5 using HeuristicLab.Collections; 6 6 using HeuristicLab.Core; 7 using HeuristicLab.Encodings.IntegerVectorEncoding; 7 8 using HeuristicLab.Encodings.PackingEncoding.GroupingVector; 8 9 using HeuristicLab.Encodings.PackingEncoding.MultiComponentVector; 9 10 using HeuristicLab.Encodings.PackingEncoding.PackingSequence; 11 using HeuristicLab.Encodings.PackingEncoding.Potvin; 10 12 using HeuristicLab.Problems.BinPacking.Dimensions; 11 13 using HeuristicLab.Problems.BinPacking.Interfaces; … … 18 20 19 21 22 23 public static ObservableDictionary<int, ThreeDimensionalPacking> ExtremePointBasedPacking(ref PotvinEncoding solution, 24 ItemList<CuboidPackingItem> itemMeasures, CuboidPackingBin binMeasures) { 25 if (!solution.DelimiterInitialized) { 26 #region Preperations 27 var itemPositions = new ObservableDictionary<int, ThreeDimensionalPacking>(); 28 int nrOfBins = 1; 29 30 var occupiedPoints = new List<int[, ,]>(); 31 occupiedPoints = IncreaseBinCountForOccupiedPoints(occupiedPoints, binMeasures); 32 33 HashSet<ThreeDimensionalPacking> extremePoints = new HashSet<ThreeDimensionalPacking>(); 34 extremePoints.Add(new ThreeDimensionalPacking(0, 0, 0, 0, false)); 35 #endregion 36 37 Dictionary<int, List<int>> itemIndexesForBin = new Dictionary<int, List<int>>(); 38 itemIndexesForBin[0] = new List<int>(); 39 40 foreach (int itemIndex in solution.IntVector) { 41 var item = itemMeasures[itemIndex]; 42 extremePoints = OrderExtremePoints(extremePoints, occupiedPoints, binMeasures); 43 var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePoints, occupiedPoints, itemPositions); 44 if (positionFound != null) { 45 extremePoints.Remove(positionFound); 46 } else { 47 positionFound = new ThreeDimensionalPacking(nrOfBins, 0, 0, 0); 48 occupiedPoints = IncreaseBinCountForOccupiedPoints(occupiedPoints, binMeasures); 49 itemIndexesForBin[nrOfBins] = new List<int>(); 50 nrOfBins++; 51 } 52 itemPositions[itemIndex] = positionFound; 53 itemIndexesForBin[positionFound.AssignedBin].Add(itemIndex); 54 occupiedPoints = OccupyPointsForNewItem(occupiedPoints, item, positionFound, itemIndex); 55 extremePoints = GenerateNewExtremePointsForNewItem(extremePoints, occupiedPoints, item, positionFound, binMeasures); 56 } 57 List<int> intVector = new List<int>(); 58 foreach (var indexes in itemIndexesForBin) { 59 foreach (var index in indexes.Value) { 60 intVector.Add(index); 61 } 62 intVector.Add(-1); 63 } 64 intVector.RemoveAt(intVector.Count - 1); 65 solution.IntVector = new IntegerVector (intVector.ToArray()); 66 67 return itemPositions; 68 } else { 69 #region Preperations 70 int nrOfBins = solution.IntVector.Count(i => i.Equals (-1)) + 1; 71 72 //Get all indexes of items for every bin according to int vector 73 Dictionary<int, List<int>> unpackedItemIndexesPerBin = new Dictionary<int, List<int>>(); 74 Dictionary<int, HashSet<ThreeDimensionalPacking>> extremePointsForBin = new Dictionary<int, HashSet<ThreeDimensionalPacking>>(); 75 var occupiedPoints = new List<int[, ,]>(); 76 77 int binCounter = 0; 78 unpackedItemIndexesPerBin[binCounter] = new List<int>(); 79 for (int i = 0; i < solution.IntVector.Length; i++) { 80 if (solution.IntVector[i].Equals(-1)) { 81 unpackedItemIndexesPerBin[binCounter++] = new List<int>(); 82 extremePointsForBin[i] = new HashSet<ThreeDimensionalPacking>(); 83 extremePointsForBin[i].Add(new ThreeDimensionalPacking(i, 0, 0, 0, false)); 84 occupiedPoints = IncreaseBinCountForOccupiedPoints(occupiedPoints, binMeasures); 85 } else { 86 unpackedItemIndexesPerBin[binCounter].Add(solution.IntVector[i]); 87 } 88 89 } 90 91 92 ObservableDictionary<int, ThreeDimensionalPacking> itemPositions = new ObservableDictionary<int, ThreeDimensionalPacking>(); 93 var remainingItems = new List<int>(); 94 95 #endregion Preperations 96 97 98 99 //Iterate over all bin-lists 100 for (int binNr = 0; binNr < nrOfBins; binNr++) { 101 //Iterate over the current bin-item-list and find feasible positions in the current bin for each item 102 var unpackedItems = unpackedItemIndexesPerBin[binNr]; 103 for (int i = 0; i < unpackedItems.Count; i++) { 104 var itemIndex = unpackedItems[i]; 105 var item = itemMeasures[itemIndex]; 106 107 extremePointsForBin[binNr] = OrderExtremePoints(extremePointsForBin[binNr], occupiedPoints, binMeasures); 108 var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePointsForBin[binNr], occupiedPoints, itemPositions); 109 if (positionFound != null) { 110 extremePointsForBin[binNr].Remove(positionFound); 111 itemPositions[itemIndex] = positionFound; 112 occupiedPoints = OccupyPointsForNewItem(occupiedPoints, item, positionFound, itemIndex); 113 extremePointsForBin[binNr] = GenerateNewExtremePointsForNewItem(extremePointsForBin[binNr], occupiedPoints, item, positionFound, binMeasures); 114 } else 115 remainingItems.Add(itemIndex); 116 } 117 } 118 119 120 121 //Packing of remaining items 122 //Iterate over all bin-lists 123 for (int binNr = 0; binNr < nrOfBins && remainingItems.Count > 0; binNr++) { 124 var unpackedItems = new List<int>(remainingItems); 125 //Iterate over all the remaining items 126 for (int i = 0; i < unpackedItems.Count; i++) { 127 var itemIndex = unpackedItems[i]; 128 var item = itemMeasures[itemIndex]; 129 130 extremePointsForBin[binNr] = OrderExtremePoints(extremePointsForBin[binNr], occupiedPoints, binMeasures); 131 var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePointsForBin[binNr], occupiedPoints, itemPositions); 132 if (positionFound != null) { 133 extremePointsForBin[binNr].Remove(positionFound); 134 InsertItemgeneAfterLastItemgeneOfUsedBin(ref solution, itemIndex, binNr); 135 itemPositions[itemIndex] = positionFound; 136 occupiedPoints = OccupyPointsForNewItem(occupiedPoints, item, positionFound, itemIndex); 137 extremePointsForBin[binNr] = GenerateNewExtremePointsForNewItem(extremePointsForBin[binNr], occupiedPoints, item, positionFound, binMeasures); 138 remainingItems.Remove(itemIndex); 139 } 140 } 141 if (remainingItems.Count > 0 && binNr + 1 >= nrOfBins) { 142 extremePointsForBin[nrOfBins] = new HashSet<ThreeDimensionalPacking>(); 143 extremePointsForBin[nrOfBins].Add(new ThreeDimensionalPacking(nrOfBins, 0, 0, 0, false)); 144 occupiedPoints = IncreaseBinCountForOccupiedPoints(occupiedPoints, binMeasures); 145 nrOfBins++; 146 } 147 } 148 149 return itemPositions; 150 } 151 } 152 private static void InsertItemgeneAfterLastItemgeneOfUsedBin(ref PotvinEncoding solution, int itemIndex, int binNr) { 153 List<int> temp = new List<int> (solution.IntVector); 154 temp.Remove(itemIndex); 155 var delimiterIndexes = temp.Select((value, index) => value == -1).Select((value, index) => index); 156 int insertionIndex = temp.Count; 157 if (binNr < delimiterIndexes.Count()) { 158 insertionIndex = delimiterIndexes.ElementAt(binNr); 159 } 160 temp.Insert(insertionIndex, itemIndex); 161 solution.IntVector = new IntegerVector (temp.ToArray()); 162 } 20 163 21 164 … … 56 199 var item = itemMeasures[itemIndex]; 57 200 58 extremePointsForBin[binNr] = new HashSet<ThreeDimensionalPacking>(extremePointsForBin[binNr].OrderBy(ep => ep.AssignedBin).ThenBy(ep => ShortestPossibleSideFromEP(ep, occupiedPoints, binMeasures)));201 extremePointsForBin[binNr] = OrderExtremePoints(extremePointsForBin[binNr], occupiedPoints, binMeasures); 59 202 var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePointsForBin[binNr], occupiedPoints, itemPositions, unpackedItems[i].Rotated); 60 203 if (positionFound != null) { … … 78 221 var item = itemMeasures[itemIndex]; 79 222 80 extremePointsForBin[binNr] = new HashSet<ThreeDimensionalPacking>(extremePointsForBin[binNr].OrderBy(ep => ep.AssignedBin).ThenBy(ep => ShortestPossibleSideFromEP(ep, occupiedPoints, binMeasures)));223 extremePointsForBin[binNr] = OrderExtremePoints(extremePointsForBin[binNr], occupiedPoints, binMeasures); 81 224 var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePointsForBin[binNr], occupiedPoints, itemPositions, unpackedItems[i].Rotated); 82 225 if (positionFound != null) { … … 147 290 var item = itemMeasures[itemIndex]; 148 291 149 extremePointsForBin[binNr] = new HashSet<ThreeDimensionalPacking>(extremePointsForBin[binNr].OrderBy(ep => ep.AssignedBin).ThenBy(ep => ShortestPossibleSideFromEP(ep, occupiedPoints, binMeasures)));292 extremePointsForBin[binNr] = OrderExtremePoints(extremePointsForBin[binNr], occupiedPoints, binMeasures); 150 293 var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePointsForBin[binNr], occupiedPoints, itemPositions); 151 294 if (positionFound != null) { … … 170 313 var item = itemMeasures[itemIndex]; 171 314 172 extremePointsForBin[binNr] = new HashSet<ThreeDimensionalPacking>(extremePointsForBin[binNr].OrderBy(ep => ep.AssignedBin).ThenBy(ep => ShortestPossibleSideFromEP(ep, occupiedPoints, binMeasures)));315 extremePointsForBin[binNr] = OrderExtremePoints(extremePointsForBin[binNr], occupiedPoints, binMeasures); 173 316 var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePointsForBin[binNr], occupiedPoints, itemPositions); 174 317 if (positionFound != null) { … … 208 351 foreach (int itemIndex in solution.PackingSequence) { 209 352 var item = itemMeasures[itemIndex]; 210 extremePoints = new HashSet<ThreeDimensionalPacking>(extremePoints.OrderBy(ep => ep.AssignedBin).ThenBy(ep => ShortestPossibleSideFromEP(ep, occupiedPoints, binMeasures)));353 extremePoints = OrderExtremePoints(extremePoints, occupiedPoints, binMeasures); 211 354 var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePoints, occupiedPoints, itemPositions); 212 355 if (positionFound != null) { … … 373 516 return extremePoints; 374 517 } 518 519 520 private static HashSet<ThreeDimensionalPacking> OrderExtremePoints(HashSet<ThreeDimensionalPacking> hashSet, List<int[, ,]> occupiedPoints, CuboidPackingBin binMeasures) { 521 return new HashSet<ThreeDimensionalPacking>(hashSet.OrderBy(ep => ep.AssignedBin).ThenBy(ep => ep.Z).ThenBy(ep => ep.X).ThenBy(ep => ep.Y).ThenBy(ep => ShortestPossibleSideFromEP(ep, occupiedPoints, binMeasures))); 522 } 375 523 private static int ShortestPossibleSideFromEP(ThreeDimensionalPacking ep, List<int[, ,]> occupiedPoints, CuboidPackingBin binMeasures) { 376 524 int shortestSide = int.MaxValue;
Note: See TracChangeset
for help on using the changeset viewer.