Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/21/16 15:08:21 (8 years ago)
Author:
gkronber
Message:

#1966: refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/BinPacking3D.cs

    r14153 r14154  
    3232  public class BinPacking3D : BinPacking<PackingPosition, PackingShape, PackingItem> {
    3333
    34     public BinPacking3D(PackingShape binMeasures)
    35       : base(binMeasures) {
     34    public BinPacking3D(PackingShape binShape)
     35      : base(binShape) {
    3636      ExtremePoints = new SortedSet<PackingPosition>(new EPComparer3D());
    37       ExtremePoints.Add(binMeasures.Origin);
     37      ExtremePoints.Add(binShape.Origin);
     38      InitializeOccupationLayers();
    3839    }
    3940    [StorableConstructor]
     
    4142    protected BinPacking3D(BinPacking3D original, Cloner cloner)
    4243      : base(original, cloner) {
    43       this.ExtremePoints = new SortedSet<PackingPosition>(original.ExtremePoints, new EPComparer3D());
     44      this.ExtremePoints = new SortedSet<PackingPosition>(original.ExtremePoints.Select(p => cloner.Clone(p)), new EPComparer3D());
    4445    }
    4546    public override IDeepCloneable Clone(Cloner cloner) {
     
    5354      //Find ExtremePoints beginning from sourcepointX
    5455      var sourcePointX = new PackingPosition(0, position.X + newWidth, position.Y, position.Z);
    55       if (sourcePointX.X < BinMeasures.Width && sourcePointX.Y < BinMeasures.Height && sourcePointX.Z < BinMeasures.Depth) {
     56      if (sourcePointX.X < BinShape.Width && sourcePointX.Y < BinShape.Height && sourcePointX.Z < BinShape.Depth) {
    5657        //Traversing down the y-axis 
    5758        PackingPosition current = new PackingPosition(0, sourcePointX.X, sourcePointX.Y, sourcePointX.Z);
     
    7980      //Find ExtremePoints beginning from sourcepointY
    8081      var sourcePointY = new PackingPosition(0, position.X, position.Y + newItem.Height, position.Z);
    81       if (sourcePointY.X < BinMeasures.Width && sourcePointY.Y < BinMeasures.Height && sourcePointY.Z < BinMeasures.Depth) {
     82      if (sourcePointY.X < BinShape.Width && sourcePointY.Y < BinShape.Height && sourcePointY.Z < BinShape.Depth) {
    8283        //Traversing down the x-axis         
    8384        PackingPosition current = new PackingPosition(0, sourcePointY.X, sourcePointY.Y, sourcePointY.Z);
     
    105106      //Find ExtremePoints beginning from sourcepointZ
    106107      var sourcePointZ = new PackingPosition(0, position.X, position.Y, position.Z + newDepth);
    107       if (sourcePointZ.X < BinMeasures.Width && sourcePointZ.Y < BinMeasures.Height && sourcePointZ.Z < BinMeasures.Depth) {
     108      if (sourcePointZ.X < BinShape.Width && sourcePointZ.Y < BinShape.Height && sourcePointZ.Z < BinShape.Depth) {
    108109        //Traversing down the x-axis
    109110        PackingPosition current = new PackingPosition(0, sourcePointZ.X, sourcePointZ.Y, sourcePointZ.Z);
     
    130131    }
    131132
    132     public override PackingPosition FindExtremePointForItem(PackingItem measures, bool rotated, bool stackingConstraints) {
    133 
    134       PackingItem item = new PackingItem(
    135         rotated ? measures.Depth : measures.Width,
    136         measures.Height,
    137         rotated ? measures.Width : measures.Depth,
    138         measures.TargetBin);
     133    public override PackingPosition FindExtremePointForItem(PackingItem item, bool rotated, bool stackingConstraints) {
     134
     135      PackingItem newItem = new PackingItem(
     136        rotated ? item.Depth : item.Width,
     137        item.Height,
     138        rotated ? item.Width : item.Depth,
     139        item.TargetBin);
    139140
    140141      int epIndex = 0;
    141142      while (epIndex < ExtremePoints.Count && (
    142         !IsPositionFeasible(item, ExtremePoints.ElementAt(epIndex))
    143         || !IsSupportedByAtLeastOnePoint(item, ExtremePoints.ElementAt(epIndex))
    144         || (stackingConstraints && !IsStaticStable(item, ExtremePoints.ElementAt(epIndex)))
    145         || (stackingConstraints && !IsWeightSupported(item, ExtremePoints.ElementAt(epIndex)))
     143        !IsPositionFeasible(newItem, ExtremePoints.ElementAt(epIndex))
     144        || !IsSupportedByAtLeastOnePoint(newItem, ExtremePoints.ElementAt(epIndex))
     145        || (stackingConstraints && !IsStaticStable(newItem, ExtremePoints.ElementAt(epIndex)))
     146        || (stackingConstraints && !IsWeightSupported(newItem, ExtremePoints.ElementAt(epIndex)))
    146147      )) { epIndex++; }
    147148
     
    154155    }
    155156
    156     public override PackingPosition FindPositionBySliding(PackingItem measures, bool rotated) {
     157    public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated) {
    157158      //TODO: does not support stacking constraints yet
    158159      //Starting-position at upper right corner (=left bottom point of item-rectangle is at position item.width,item.height)
    159160      PackingPosition currentPosition = new PackingPosition(0,
    160         BinMeasures.Width - (rotated ? measures.Depth : measures.Width),
    161         BinMeasures.Height - measures.Height,
    162         BinMeasures.Depth - (rotated ? measures.Width : measures.Depth), rotated);
     161        BinShape.Width - (rotated ? item.Depth : item.Width),
     162        BinShape.Height - item.Height,
     163        BinShape.Depth - (rotated ? item.Width : item.Depth), rotated);
    163164      //Slide the item as far as possible to the bottom
    164       while (IsPositionFeasible(measures, PackingPosition.MoveDown(currentPosition))
    165         || IsPositionFeasible(measures, PackingPosition.MoveLeft(currentPosition))
    166         || IsPositionFeasible(measures, PackingPosition.MoveBack(currentPosition))) {
     165      while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))
     166        || IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition))
     167        || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition))) {
    167168        //Slide the item as far as possible to the left
    168         while (IsPositionFeasible(measures, PackingPosition.MoveLeft(currentPosition))
    169       || IsPositionFeasible(measures, PackingPosition.MoveBack(currentPosition))) {
     169        while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition))
     170      || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition))) {
    170171          //Slide the item as far as possible to the back
    171           while (IsPositionFeasible(measures, PackingPosition.MoveBack(currentPosition))) {
     172          while (IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition))) {
    172173            currentPosition = PackingPosition.MoveBack(currentPosition);
    173174          }
    174           if (IsPositionFeasible(measures, PackingPosition.MoveLeft(currentPosition)))
     175          if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition)))
    175176            currentPosition = PackingPosition.MoveLeft(currentPosition);
    176177        }
    177         if (IsPositionFeasible(measures, PackingPosition.MoveDown(currentPosition)))
     178        if (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition)))
    178179          currentPosition = PackingPosition.MoveDown(currentPosition);
    179180      }
    180181
    181       return IsPositionFeasible(measures, currentPosition) ? currentPosition : null;
    182     }
    183 
    184     public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> itemMeasures) {
     182      return IsPositionFeasible(item, currentPosition) ? currentPosition : null;
     183    }
     184
     185    public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items) {
    185186      var temp = new List<int>(sequence);
    186187      for (int i = 0; i < temp.Count; i++) {
    187         var item = itemMeasures[temp[i]];
     188        var item = items[temp[i]];
    188189        var position = FindPositionBySliding(item, false);
    189190        if (position != null) {
     
    193194      }
    194195    }
    195     public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> itemMeasures, Dictionary<int, bool> rotationArray) {
     196    public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray) {
    196197      var temp = new List<int>(sequence);
    197198      for (int i = 0; i < temp.Count; i++) {
    198         var item = itemMeasures[temp[i]];
     199        var item = items[temp[i]];
    199200        var position = FindPositionBySliding(item, rotationArray[i]);
    200201        if (position != null) {
     
    204205      }
    205206    }
    206     public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> itemMeasures, bool stackingConstraints) {
     207    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) {
    207208      var temp = new List<int>(sequence);
    208209      foreach (int itemID in temp) {
    209         var item = itemMeasures[itemID];
     210        var item = items[itemID];
    210211        var positionFound = FindExtremePointForItem(item, false, stackingConstraints);
    211212        if (positionFound != null) {
     
    215216      }
    216217    }
    217     public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> itemMeasures, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
     218    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
    218219      var temp = new List<int>(sequence);
    219220      foreach (int itemID in temp) {
    220         var item = itemMeasures[itemID];
     221        var item = items[itemID];
    221222        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], stackingConstraints);
    222223        if (positionFound != null) {
     
    230231
    231232      int shortestSide = int.MaxValue;
    232       int width = BinMeasures.Width;
    233       int height = BinMeasures.Height;
    234       int depth = BinMeasures.Depth;
     233      int width = BinShape.Width;
     234      int height = BinShape.Height;
     235      int depth = BinShape.Depth;
    235236
    236237      if (position.X >= width || position.Y >= height || position.Z >= depth)
     
    287288        return true;
    288289
    289       if (ItemMeasures[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z))].SupportsStacking(item)
    290         && ItemMeasures[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z))].SupportsStacking(item)
    291         && ItemMeasures[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item)
    292         && ItemMeasures[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item))
     290      if (Items[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z))].SupportsStacking(item)
     291        && Items[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z))].SupportsStacking(item)
     292        && Items[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item)
     293        && Items[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item))
    293294        return true;
    294295
     
    298299
    299300    protected override void InitializeOccupationLayers() {
    300       for (int i = 0; i * 10 <= BinMeasures.Depth; i += 1) {
     301      for (int i = 0; i * 10 <= BinShape.Depth; i += 1) {
    301302        OccupationLayers[i] = new List<int>();
    302303      }
    303304    }
    304     protected override void AddNewItemToOccupationLayers(int itemID, PackingItem measures, PackingPosition position) {
     305    protected override void AddNewItemToOccupationLayers(int itemID, PackingItem item, PackingPosition position) {
    305306      int z1 = position.Z / 10;
    306       int z2 = (position.Z + (position.Rotated ? measures.Width : measures.Depth)) / 10;
     307      int z2 = (position.Z + (position.Rotated ? item.Width : item.Depth)) / 10;
    307308
    308309      for (int i = z1; i <= z2; i++)
     
    312313      return OccupationLayers[position.Z / 10];
    313314    }
    314     protected override List<int> GetLayerItemIDs(PackingItem measures, PackingPosition position) {
     315    protected override List<int> GetLayerItemIDs(PackingItem item, PackingPosition position) {
    315316      List<int> result = new List<int>();
    316317      int z1 = position.Z / 10;
    317       int z2 = (position.Z + (position.Rotated ? measures.Width : measures.Depth)) / 10;
     318      int z2 = (position.Z + (position.Rotated ? item.Width : item.Depth)) / 10;
    318319
    319320      for (int i = z1; i <= z2; i++)
Note: See TracChangeset for help on using the changeset viewer.