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

Location:
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/PackingPlans
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/PackingPlans/BinPacking.cs

    r14151 r14154  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    3031  [Item("BinPacking", "Represents a single-bin packing for a bin-packing problem.")]
    3132  [StorableClass]
    32   public abstract class BinPacking<D, B, I> : Item
    33     where D : class, IPackingPosition
    34     where B : PackingShape<D>
    35     where I : PackingShape<D> {
     33  public abstract class BinPacking<TPos, TBin, TItem> : Item
     34    where TPos : class, IPackingPosition
     35    where TBin : PackingShape<TPos>
     36    where TItem : PackingShape<TPos> {
    3637    #region Properties
    3738    [Storable]
    38     public ObservableDictionary<int, D> ItemPositions { get; private set; }
     39    public ObservableDictionary<int, TPos> Positions { get; private set; }
    3940
    4041    [Storable]
    41     public ObservableDictionary<int, I> ItemMeasures { get; private set; } // TODO: rename to items
     42    public ObservableDictionary<int, TItem> Items { get; private set; }
    4243
    4344    [Storable]
    44     public B BinMeasures { get; private set; }
     45    public TBin BinShape { get; private set; }
    4546
    4647    [Storable]
    47     public SortedSet<D> ExtremePoints { get; protected set; }
     48    public SortedSet<TPos> ExtremePoints { get; protected set; }
    4849
    4950    [Storable]
     
    5253    #endregion Properties
    5354
    54     protected BinPacking(B binMeasures)
     55    protected BinPacking(TBin binShape)
    5556      : base() {
    56       ItemPositions = new ObservableDictionary<int, D>();
    57       ItemMeasures = new ObservableDictionary<int, I>();
    58       BinMeasures = (B)binMeasures.Clone();
     57      Positions = new ObservableDictionary<int, TPos>();
     58      Items = new ObservableDictionary<int, TItem>();
     59      BinShape = (TBin)binShape.Clone();
    5960      OccupationLayers = new Dictionary<int, List<int>>();
    60       InitializeOccupationLayers(); // TODO
    6161    }
    6262
     
    6464    [StorableConstructor]
    6565    protected BinPacking(bool deserializing) : base(deserializing) { }
    66     protected BinPacking(BinPacking<D, B, I> original, Cloner cloner)
     66    protected BinPacking(BinPacking<TPos, TBin, TItem> original, Cloner cloner)
    6767      : base(original, cloner) {
    68       this.ItemPositions = new ObservableDictionary<int, D>(original.ItemPositions);
    69       this.ItemMeasures = new ObservableDictionary<int, I>(original.ItemMeasures);
    70       this.BinMeasures = (B)original.BinMeasures.Clone(cloner);
    71       this.OccupationLayers = new Dictionary<int, List<int>>(original.OccupationLayers);
     68      this.Positions = new ObservableDictionary<int, TPos>();
     69      foreach (var kvp in original.Positions) {
     70        Positions.Add(kvp.Key, cloner.Clone(kvp.Value));
     71      }
     72      this.Items = new ObservableDictionary<int, TItem>();
     73      foreach (var kvp in original.Items) {
     74        Items.Add(kvp.Key, cloner.Clone(kvp.Value));
     75      }
     76      this.BinShape = (TBin)original.BinShape.Clone(cloner);
     77      this.OccupationLayers = new Dictionary<int, List<int>>();
     78      foreach (var kvp in original.OccupationLayers) {
     79        OccupationLayers.Add(kvp.Key, new List<int>(kvp.Value));
     80      }
    7281    }
    7382
    74     protected abstract void GenerateNewExtremePointsForNewItem(I measures, D position);
     83    protected abstract void GenerateNewExtremePointsForNewItem(TItem item, TPos position);
    7584
    76     public abstract D FindExtremePointForItem(I measures, bool rotated, bool stackingConstraints);
    77     public abstract D FindPositionBySliding(I measures, bool rotated);
     85    public abstract TPos FindExtremePointForItem(TItem item, bool rotated, bool stackingConstraints);
     86    public abstract TPos FindPositionBySliding(TItem item, bool rotated);
    7887
    79     public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<I> itemMeasures);
    80     public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<I> itemMeasures, Dictionary<int, bool> rotationArray);
    81     public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<I> itemMeasures, bool stackingConstraints);
    82     public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<I> itemMeasures, bool stackingConstraints, Dictionary<int, bool> rotationArray);
     88    public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<TItem> items);
     89    public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<TItem> items, Dictionary<int, bool> rotationArray);
     90    public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<TItem> items, bool stackingConstraints);
     91    public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<TItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray);
    8392
    84     public void PackItem(int itemID, I measures, D position) {
    85       ItemMeasures[itemID] = measures;
    86       ItemPositions[itemID] = position;
     93    public void PackItem(int itemID, TItem item, TPos position) {
     94      Items[itemID] = item;
     95      Positions[itemID] = position;
    8796      ExtremePoints.Remove(position);
    88       foreach (int id in ItemMeasures.Select(x => x.Key))
    89         GenerateNewExtremePointsForNewItem(ItemMeasures[id], ItemPositions[id]);
     97      foreach (int id in Items.Select(x => x.Key))
     98        GenerateNewExtremePointsForNewItem(Items[id], Positions[id]);
    9099
    91       AddNewItemToOccupationLayers(itemID, measures, position);
     100      AddNewItemToOccupationLayers(itemID, item, position);
    92101    }
    93102
     
    95104      get {
    96105        double result = 0;
    97         foreach (var entry in ItemMeasures)
     106        foreach (var entry in Items)
    98107          result += entry.Value.Volume;
    99         result /= BinMeasures.Volume;
     108        result /= BinShape.Volume;
    100109        return result;
    101110      }
     
    103112
    104113
    105     public int PointOccupation(D position) {
     114    public int PointOccupation(TPos position) {
    106115      foreach (var id in GetLayerItemIDs(position)) {
    107         if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
     116        if (Items[id].EnclosesPoint(Positions[id], position))
    108117          return id;
    109118      }
     
    111120    }
    112121
    113     public bool IsPointOccupied(D position) {
     122    public bool IsPointOccupied(TPos position) {
    114123      foreach (var id in GetLayerItemIDs(position)) {
    115         if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
     124        if (Items[id].EnclosesPoint(Positions[id], position))
    116125          return true;
    117126      }
    118127      return false;
    119128    }
    120     public bool IsPositionFeasible(I measures, D position) {
     129    public bool IsPositionFeasible(TItem item, TPos position) {
    121130      //In this case feasability is defined as following: 1. the item fits into the bin-borders; 2. the point is supported by something; 3. the item does not collide with another already packed item
    122       if (!BinMeasures.Encloses(position, measures))
     131      if (!BinShape.Encloses(position, item))
    123132        return false;
    124133
    125       foreach (var id in GetLayerItemIDs(measures, position)) {
    126         if (ItemMeasures[id].Overlaps(ItemPositions[id], position, measures))
     134      foreach (var id in GetLayerItemIDs(item, position)) {
     135        if (Items[id].Overlaps(Positions[id], position, item))
    127136          return false;
    128137      }
     
    130139      return true;
    131140    }
    132     public abstract int ShortestPossibleSideFromPoint(D position);
    133     public abstract bool IsStaticStable(I measures, D position);
     141    public abstract int ShortestPossibleSideFromPoint(TPos position);
     142    public abstract bool IsStaticStable(TItem measures, TPos position);
    134143
    135144
    136145    protected abstract void InitializeOccupationLayers();
    137     protected abstract void AddNewItemToOccupationLayers(int itemID, I measures, D position);
    138     protected abstract List<int> GetLayerItemIDs(D position);
    139     protected abstract List<int> GetLayerItemIDs(I measures, D position);
     146    protected abstract void AddNewItemToOccupationLayers(int itemID, TItem item, TPos position);
     147    protected abstract List<int> GetLayerItemIDs(TPos position);
     148    protected abstract List<int> GetLayerItemIDs(TItem item, TPos position);
    140149  }
    141150}
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/PackingPlans/PackingPlan.cs

    r14153 r14154  
    8686
    8787    public void UpdateBinPackings() {
    88       BinPackings.RemoveAll(x => x.ItemPositions.Count == 0);
     88      BinPackings.RemoveAll(x => x.Positions.Count == 0);
    8989      BinPackings = new ObservableList<BinPacking<D, B, I>>(BinPackings.OrderByDescending(bp => bp.PackingDensity));
    9090    }
Note: See TracChangeset for help on using the changeset viewer.