Changeset 15617 for branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/ExtremePointCreation/LineProjectionBasedEPCreator.cs
- Timestamp:
- 01/16/18 15:40:43 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/ExtremePointCreation/LineProjectionBasedEPCreator.cs
r15585 r15617 1 using HeuristicLab.Common; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2018 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 using HeuristicLab.Common; 2 23 using HeuristicLab.Problems.BinPacking3D.Geometry; 3 24 using HeuristicLab.Problems.BinPacking3D.ResidualSpaceCalculation; … … 7 28 using System.Text; 8 29 using System.Threading.Tasks; 30 using System.Collections.Concurrent; 9 31 10 32 namespace HeuristicLab.Problems.BinPacking3D.ExtremePointCreation { … … 15 37 public class LineProjectionBasedEPCreator : ExtremePointCreator { 16 38 39 /// <summary> 40 /// This lock object is needed because of creating the extreme points in an parallel for loop. 41 /// </summary> 42 object _lockAddExtremePoint = new object(); 43 17 44 protected override void UpdateExtremePoints(BinPacking3D binPacking, PackingItem item, PackingPosition position) { 18 45 binPacking.ExtremePoints.Clear(); 19 46 20 foreach (var i in binPacking.Items) { 47 // generate all new extreme points parallel. This speeds up the creator. 48 Parallel.ForEach(binPacking.Items, i => { 21 49 PackingItem it = i.Value; 22 50 PackingPosition p = binPacking.Positions[i.Key]; 23 51 GenerateNewExtremePointsForItem(binPacking, it, p); 24 } 25 52 }); 53 26 54 // remove not needed extreme points. 27 55 foreach (var extremePoint in binPacking.ExtremePoints.ToList()) { … … 37 65 } 38 66 } 39 } 40 67 68 } 69 41 70 /// <summary> 42 71 /// Returns true if a given residual space can be removed. … … 60 89 if (itemBelowEp || !itemBelowEp && !itemBelowPos) { 61 90 return true; 62 } 91 } 63 92 } 64 93 } 65 94 return false; 66 95 } 67 96 68 97 /// <summary> 69 98 /// Returns true if the given position lies on an item or an the ground. … … 80 109 var itemPosition = binPacking.Positions[x.Key]; 81 110 var item = x.Value; 82 int width = item Position.Rotated ? item.Depth : item.Width;83 int depth = item Position.Rotated ? item.Width : item.Depth;111 int width = item.Width; 112 int depth = item.Depth; 84 113 85 114 return itemPosition.Y + item.Height == position.Y && … … 110 139 } 111 140 112 if (binPacking.ExtremePoints.ContainsKey(position)) { 113 return false; 114 } 115 116 var rs = CalculateResidualSpace(binPacking, new Vector3D(position)); 117 118 if (rs.Count() <= 0) { 119 return false; 120 } 121 122 binPacking.ExtremePoints.Add(position, rs); 123 return true; 141 // this is necessary if the extreme points are being created in a parallel loop. 142 lock (_lockAddExtremePoint) { 143 if (binPacking.ExtremePoints.ContainsKey(position)) { 144 return false; 145 } 146 147 var rs = CalculateResidualSpace(binPacking, new Vector3D(position)); 148 149 if (rs.Count() <= 0) { 150 return false; 151 } 152 153 binPacking.ExtremePoints.Add(position, rs); 154 return true; 155 } 124 156 } 125 157 … … 148 180 /// <param name="position"></param> 149 181 private void PointProjectionForNewItem(BinPacking3D binPacking, PackingItem newItem, PackingPosition position) { 150 int newWidth = position.Rotated ? newItem.Depth :newItem.Width;151 int newDepth = position.Rotated ? newItem.Width :newItem.Depth;182 int newWidth = newItem.Width; 183 int newDepth = newItem.Depth; 152 184 var binShape = binPacking.BinShape; 153 185 var sourcePoint = new PackingPosition(position.AssignedBin, position.X + newWidth, position.Y, position.Z); … … 196 228 /// <param name="position"></param> 197 229 private void EdgeProjectionForNewItem(BinPacking3D binPacking, PackingItem newItem, PackingPosition position) { 198 int newWidth = position.Rotated ? newItem.Depth :newItem.Width;199 int newDepth = position.Rotated ? newItem.Width :newItem.Depth;230 int newWidth = newItem.Width; 231 int newDepth = newItem.Depth; 200 232 var binShape = binPacking.BinShape; 201 233 … … 235 267 foreach (var item in binPacking.Items) { 236 268 PackingPosition position = binPacking.Positions[item.Key]; 237 var depth = position.Rotated ? item.Value.Width :item.Value.Depth;238 var width = position.Rotated ? item.Value.Depth :item.Value.Width;269 var depth = item.Value.Depth; 270 var width = item.Value.Width; 239 271 if (position.X <= point.X && point.X < position.X + width && 240 272 position.Y <= point.Y && point.Y < position.Y + item.Value.Height && … … 444 476 /// <returns></returns> 445 477 private Vector3D[] GetVertices(PackingItem item, PackingPosition position) { 446 int width = position.Rotated ? item.Depth :item.Width;447 int depth = position.Rotated ? item.Width :item.Depth;478 int width = item.Width; 479 int depth = item.Depth; 448 480 return new Vector3D[] { 449 481 new Vector3D(position.X + 0, position.Y + 0, position.Z + 0), // (0,0,0) … … 574 606 } 575 607 } 608 576 609 return eps as IEnumerable<Vector3D>; 577 610 }
Note: See TracChangeset
for help on using the changeset viewer.