# source:branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Sorting/PermutationPackingItemSorter.cs@15473

Last change on this file since 15473 was 15473, checked in by rhanghof, 3 years ago
• Refactoring of bp 3D
File size: 11.4 KB
Line
1using HeuristicLab.Encodings.PermutationEncoding;
2using HeuristicLab.Problems.BinPacking2D;
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Text;
8
9namespace HeuristicLab.Problems.BinPacking3D.Sorting {
10
11  /// <summary>
12  /// This is a extension class for sorting a permutation.
13  /// They are extension methods for the class Permutation.
14  /// </summary>
15  public static class PermutationPackingItemSorter {
16
17    /// <summary>
18    /// Sorts a given permutation first by the volume and secoundly by the height.
19    /// </summary>
20    /// <param name="items">Permuation which should be sorted</param>
21    /// <returns>A new sorted permutation</returns>
22    public static Permutation SortByVolumeHeight(this IList<PackingItem> items) {
23      return new Permutation(PermutationTypes.Absolute,
24                    items.Select((v, i) => new { Index = i, Item = v })
25                         .OrderByDescending(x => x.Item.Depth * x.Item.Width * x.Item.Height)
26                         .ThenByDescending(x => x.Item.Height)
27                         .Select(x => x.Index).ToArray());
28    }
29
30    /// <summary>
31    /// Sorts a given permutation first by the heigth and secoundly by the volume.
32    /// </summary>
33    /// <param name="items">Permuation which should be sorted</param>
34    /// <returns>A new sorted permutation</returns>
35    public static Permutation SortByHeightVolume(this IList<PackingItem> items) {
36      return new Permutation(PermutationTypes.Absolute,
37                    items.Select((v, i) => new { Index = i, Item = v })
38                         .OrderByDescending(x => x.Item.Height)
39                         .ThenByDescending(x => x.Item.Depth * x.Item.Width * x.Item.Height)
40                         .Select(x => x.Index).ToArray());
41    }
42
43    /// <summary>
44    /// Sorts a given permutation first by the area and secondly by the height.
45    /// </summary>
46    /// <param name="items">Permuation which should be sorted</param>
47    /// <returns>A new sorted permutation</returns>
48    public static Permutation SortByAreaHeight(this IList<PackingItem> items) {
49      return new Permutation(PermutationTypes.Absolute,
50                    items.Select((v, i) => new { Index = i, Item = v })
51                         .OrderByDescending(x => x.Item.Depth * x.Item.Width)
52                         .ThenByDescending(x => x.Item.Height)
53                         .Select(x => x.Index).ToArray());
54    }
55
56    /// <summary>
57    /// Sorts a given permuation first by the height and secoundly by the area.
58    /// </summary>
59    /// <param name="items">Permuation which should be sorted</param>
60    /// <returns>A new sorted permutation</returns>
61    public static Permutation SortByHeightArea(this IList<PackingItem> items) {
62      return new Permutation(PermutationTypes.Absolute,
63                    items.Select((v, i) => new { Index = i, Item = v })
64                         .OrderByDescending(x => x.Item.Height)
65                         .ThenByDescending(x => x.Item.Depth * x.Item.Width)
66                         .Select(x => x.Index).ToArray());
67    }
68
69    /// <summary>
70    /// Sorts a given permutation. The items are being grouped by the cluster id.
71    /// The cluster id is calulated as followed: clusterId = Ceiling( (width * depth) / (width * depth * delta))
72    /// The permutation is first being sorted by the area and secoundly by the height.
73    /// </summary>
74    /// <param name="items">Permuation which should be sorted</param>
75    /// <param name="bin">The bin is needed for building the cluster</param>
76    /// <param name="delta">The delta is needed for building the cluster</param>
77    /// <returns>A new sorted permutation</returns>
78    public static Permutation SortByClusteredAreaHeight(this IList<PackingItem> items, PackingShape bin, double delta) {
79      double clusterRange = bin.Width * bin.Depth * delta;
80      return new Permutation(PermutationTypes.Absolute,
81                items.Select((v, i) => new { Index = i, Item = v, ClusterId = (int)(Math.Ceiling(v.Width * v.Depth / clusterRange)) })
82                    .GroupBy(x => x.ClusterId)
83                    .Select(x => new { Cluster = x.Key, Items = x.OrderByDescending(y => y.Item.Height).ToList() })
84                    .OrderByDescending(x => x.Cluster)
85                    .SelectMany(x => x.Items)
86                    .Select(x => x.Index).ToArray());
87    }
88
89    /// <summary>
90    /// Sorts a given permutation. The items are being grouped by the cluster id.
91    /// The cluster id is calulated as followed: clusterId = Ceiling( (height) / (height * delta))
92    /// The permutation is first being sorted by the height and secoundly by the area.
93    /// </summary>
94    /// <param name="items">Permuation which should be sorted</param>
95    /// <param name="bin">The bin is needed for building the cluster</param>
96    /// <param name="delta">The delta is needed for building the cluster</param>
97    /// <returns>A new sorted permutation</returns>
98    public static Permutation SortByClusteredHeightArea(this IList<PackingItem> items, PackingShape bin, double delta) {
99      double clusterRange2 = bin.Height * delta;
100      return new Permutation(PermutationTypes.Absolute,
101                items.Select((v, i) => new { Index = i, Item = v, ClusterId = (int)(Math.Ceiling(v.Height / clusterRange2)) })
102                    .GroupBy(x => x.ClusterId)
103                    .Select(x => new { Cluster = x.Key, Items = x.OrderByDescending(y => y.Item.Depth * y.Item.Width).ToList() })
104                    .OrderByDescending(x => x.Cluster)
105                    .SelectMany(x => x.Items)
106                    .Select(x => x.Index).ToArray());
107    }
108
109    /// <summary>
110    /// Sorts a given permutation first by the material, secoundly by the volume and finally by the height.
111    /// </summary>
112    /// <param name="items">Permuation which should be sorted</param>
113    /// <returns>A new sorted permutation</returns>
114    public static Permutation SortByMaterialVolumeHeight(this IList<PackingItem> items) {
115      return new Permutation(PermutationTypes.Absolute,
116                    items.Select((v, i) => new { Index = i, Item = v })
117                         .OrderByDescending(x => x.Item.Material)
118                         .ThenByDescending(x => x.Item.Depth * x.Item.Width * x.Item.Height)
119                         .ThenByDescending(x => x.Item.Height)
120                         .Select(x => x.Index).ToArray());
121    }
122
123    /// <summary>
124    /// Sorts a given permutation first by the material, secoundly by the heigth and finally by the volume.
125    /// </summary>
126    /// <param name="items">Permuation which should be sorted</param>
127    /// <returns>A new sorted permutation</returns>
128    public static Permutation SortByMaterialHeightVolume(this IList<PackingItem> items) {
129      return new Permutation(PermutationTypes.Absolute,
130                    items.Select((v, i) => new { Index = i, Item = v })
131                         .OrderByDescending(x => x.Item.Material)
132                         .ThenByDescending(x => x.Item.Height)
133                         .ThenByDescending(x => x.Item.Depth * x.Item.Width * x.Item.Height)
134                         .Select(x => x.Index).ToArray());
135    }
136
137    /// <summary>
138    /// Sorts a given permutation first by the material, secoundly by the area and finally by the height.
139    /// </summary>
140    /// <param name="items">Permuation which should be sorted</param>
141    /// <returns>A new sorted permutation</returns>
142    public static Permutation SortByMaterialAreaHeight(this IList<PackingItem> items) {
143      return new Permutation(PermutationTypes.Absolute,
144                    items.Select((v, i) => new { Index = i, Item = v })
145                         .OrderByDescending(x => x.Item.Material)
146                         .ThenByDescending(x => x.Item.Depth * x.Item.Width)
147                         .ThenByDescending(x => x.Item.Height)
148                         .Select(x => x.Index).ToArray());
149    }
150
151    /// <summary>
152    /// Sorts a given permuation first by the material, secoundly by the height and finally by the area.
153    /// </summary>
154    /// <param name="items">Permuation which should be sorted</param>
155    /// <returns>A new sorted permutation</returns>
156    public static Permutation SortByMaterialHeightArea(this IList<PackingItem> items) {
157      return new Permutation(PermutationTypes.Absolute,
158                    items.Select((v, i) => new { Index = i, Item = v })
159                         .OrderByDescending(x => x.Item.Material)
160                         .ThenByDescending(x => x.Item.Height)
161                         .ThenByDescending(x => x.Item.Depth * x.Item.Width)
162                         .Select(x => x.Index).ToArray());
163    }
164
165    /// <summary>
166    /// Sorts a given permutation. The items are being grouped by the cluster id.
167    /// The cluster id is calulated as followed: clusterId = Ceiling( (width * depth) / (width * depth * delta))
168    /// The permutation is being clusterd by the area, first sorted by the material, secoundly by the height.
169    /// </summary>
170    /// <param name="items">Permuation which should be sorted</param>
171    /// <param name="bin">The bin is needed for building the cluster</param>
172    /// <param name="delta">The delta is needed for building the cluster</param>
173    /// <returns>A new sorted permutation</returns>
174    public static Permutation SortByMaterialClusteredAreaHeight(this IList<PackingItem> items, PackingShape bin, double delta) {
175      double clusterRange = bin.Width * bin.Depth * delta;
176      return new Permutation(PermutationTypes.Absolute,
177                items.Select((v, i) => new { Index = i, Item = v, ClusterId = (int)(Math.Ceiling(v.Width * v.Depth / clusterRange)) })
178                    .GroupBy(x => x.ClusterId)
179                    .Select(x => new { Cluster = x.Key, Items = x.OrderByDescending(z => z.Item.Material).ThenByDescending(y => y.Item.Height).ToList() })
180                    .OrderByDescending(x => x.Cluster)
181                    .SelectMany(x => x.Items)
182                    .Select(x => x.Index).ToArray());
183    }
184
185    /// <summary>
186    /// Sorts a given permutation. The items are being grouped by the cluster id.
187    /// The cluster id is calulated as followed: clusterId = Ceiling( (height) / (height * delta))
188    /// The permutation is being clusterd by the height, first sorted by the material, secoundly by the area.
189    /// </summary>
190    /// <param name="items">Permuation which should be sorted</param>
191    /// <param name="bin">The bin is needed for building the cluster</param>
192    /// <param name="delta">The delta is needed for building the cluster</param>
193    /// <returns>A new sorted permutation</returns>
194    public static Permutation SortByMaterialClusteredHeightArea(this IList<PackingItem> items, PackingShape bin,  double delta) {
195      double clusterRange2 = bin.Height * delta;
196      return new Permutation(PermutationTypes.Absolute,
197                items.Select((v, i) => new { Index = i, Item = v, ClusterId = (int)(Math.Ceiling(v.Height / clusterRange2)) })
198                    .GroupBy(x => x.ClusterId)
199                    .Select(x => new { Cluster = x.Key, Items = x.OrderByDescending(z => z.Item.Material).ThenByDescending(y => y.Item.Depth * y.Item.Width).ToList() })
200                    .OrderByDescending(x => x.Cluster)
201                    .SelectMany(x => x.Items)
202                    .Select(x => x.Index).ToArray());
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.