easy-box-packer 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +30 -4
- data/easy-box-packer.rb +205 -45
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9f1016f027375603eee3856800ba2d13ebcc446c2bd5e1ac4b3ab514e09b6085
|
|
4
|
+
data.tar.gz: 406910cbb9864d494f56e1d44bfa99a1005f3954bcdd57841dbbc890296755c8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e2b0d1ff63a539ca0ce6c015c1d5760537f95836dcfc1735e52949c34e365342fadc9564989fa7c90813a71b610d4d623524b11eed702f3176d816dd84b6f048
|
|
7
|
+
data.tar.gz: fc47a2d0ace0af303102e1325c7787bc176699d32739354fb6ea7a1f7238a8a8868e81f33e068733560fa53a84ab0f406c86863726efdb5d94da902cbb3859de
|
data/README.md
CHANGED
|
@@ -77,7 +77,7 @@ It works in two steps:
|
|
|
77
77
|
```ruby
|
|
78
78
|
EasyBoxPacker.find_smallest_container(
|
|
79
79
|
items: items,
|
|
80
|
-
time_limit: 0.5, # seconds (default 0.5). nil = no limit, 0 = step 1 only
|
|
80
|
+
time_limit: 0.5, # seconds (default 0.5). nil = no limit, 0 = step 1 only (not bounded)
|
|
81
81
|
max_attempts: nil, # max candidates test-packed; same result on every machine
|
|
82
82
|
max_length: 120, # longest side allowed (cm), e.g. your carrier's limit
|
|
83
83
|
max_length_plus_girth: 300, # length + 2 * (width + height) allowed (cm)
|
|
@@ -85,8 +85,10 @@ EasyBoxPacker.find_smallest_container(
|
|
|
85
85
|
)
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
-
`time_limit`
|
|
89
|
-
the
|
|
88
|
+
`time_limit` bounds the whole call (since 0.5.0): when it runs out during step 1, the
|
|
89
|
+
answer is the stacked "greedy" container (or, with upright / fragile items, all items
|
|
90
|
+
side by side), which always holds every item; the test pack running at that moment is
|
|
91
|
+
abandoned. `time_limit: 0` keeps the 0.1.0 behaviour: step 1 only, run to the end.
|
|
90
92
|
|
|
91
93
|
By default the cheapest container is the one with the smallest volume, then the
|
|
92
94
|
smallest length + girth. Carriers charge on dimensional weight (volume) plus
|
|
@@ -131,11 +133,24 @@ fills the box that packs the most item volume per cost and repeats.
|
|
|
131
133
|
| `padding` | `0` | cm of cushioning kept free on every side (a box's own `:padding` wins) |
|
|
132
134
|
| `cost` | `box[:cost]` or volume | lambda `box -> number`, lower is better |
|
|
133
135
|
| `max_items` | `nil` | raise `ArgumentError` when more items are given |
|
|
134
|
-
| `time_limit` | `nil` |
|
|
136
|
+
| `time_limit` | `nil` | seconds for the whole call, see below. `nil` = no limit |
|
|
135
137
|
| `split_strategies`, `greedy_fallback` | | passed to `pack` |
|
|
136
138
|
|
|
137
139
|
`unpacked` reasons: `:invalid_dimensions`, `:too_big`, `:too_heavy`, `:no_single_box`.
|
|
138
140
|
|
|
141
|
+
`time_limit` (since 0.5.0) is an upper bound, not a hint. When it is set, each round
|
|
142
|
+
first makes a cheap answer: one box estimated to hold all remaining items (the cheapest
|
|
143
|
+
such box), or else the box with the lowest estimated cost, filled with the first split
|
|
144
|
+
strategy in as many copies as needed. When the time runs out, the work in progress is
|
|
145
|
+
abandoned and the better of that answer and any box already packed in time is returned,
|
|
146
|
+
ranked as the full search ranks them: one box before several, then lower cost. So a call takes about `time_limit`; it can only go
|
|
147
|
+
over by one first-strategy pack of the remaining items, and only when that pack alone
|
|
148
|
+
is slower than the limit. The result is always valid: every item is placed (inside its
|
|
149
|
+
box, no overlaps, constraints and weight limits kept) or in `unpacked` with one of the
|
|
150
|
+
reasons above, as without a limit. Running out of time can cost more boxes or a higher
|
|
151
|
+
total cost than the full search would find; with a generous limit the answer is the
|
|
152
|
+
same as with `nil`.
|
|
153
|
+
|
|
139
154
|
Each box in the result also has `container_dimensions` (the frame the positions are in,
|
|
140
155
|
the third axis is up) and every placement has `step:` and `support:`, see below.
|
|
141
156
|
|
|
@@ -187,6 +202,17 @@ bundle exec rspec easy-box-packer_spec.rb
|
|
|
187
202
|
|
|
188
203
|
## Changelog
|
|
189
204
|
|
|
205
|
+
### 0.5.0
|
|
206
|
+
|
|
207
|
+
- `time_limit` is an upper bound for `select_box` and `find_smallest_container`
|
|
208
|
+
(before, both could run several times over it: 2-4 s for 200 items with 0.5).
|
|
209
|
+
Results stay valid and every item is still placed or reported; see the
|
|
210
|
+
`select_box` and `find_smallest_container` sections. `nil` is still no limit,
|
|
211
|
+
`time_limit: 0` for `find_smallest_container` still means step 1 only.
|
|
212
|
+
- Without a limit, or with a generous one, answers are unchanged.
|
|
213
|
+
- `select_box` checks which boxes each item fits faster.
|
|
214
|
+
- CI: specs on Ruby 2.7 and 3.4 (GitHub Actions).
|
|
215
|
+
|
|
190
216
|
### 0.4.0
|
|
191
217
|
|
|
192
218
|
- Item constraints: `upright: true` and `fragile: true`.
|
data/easy-box-packer.rb
CHANGED
|
@@ -18,6 +18,10 @@ module EasyBoxPacker
|
|
|
18
18
|
# Max distinct side lengths tried per axis while refining.
|
|
19
19
|
MAX_SIDE_VALUES = 1000
|
|
20
20
|
|
|
21
|
+
# Share of a box's volume assumed usable when select_box estimates how many
|
|
22
|
+
# copies it needs after its time limit (see quick_plan).
|
|
23
|
+
QUICK_PLAN_FILL = 0.8
|
|
24
|
+
|
|
21
25
|
UNPACKED_MESSAGES = {
|
|
22
26
|
invalid_dimensions: 'has invalid dimensions',
|
|
23
27
|
too_big: 'is too big for every box',
|
|
@@ -48,29 +52,7 @@ module EasyBoxPacker
|
|
|
48
52
|
def pack(container:, items:, split_strategies: SPLIT_STRATEGIES, greedy_fallback: true)
|
|
49
53
|
validate_container!(container)
|
|
50
54
|
validate_split_strategies!(split_strategies)
|
|
51
|
-
|
|
52
|
-
constrained = constrained?(items)
|
|
53
|
-
frames = constrained ? container_frames(container[:dimensions]) : [container[:dimensions].sort.reverse]
|
|
54
|
-
|
|
55
|
-
best = nil
|
|
56
|
-
frames.each do |frame|
|
|
57
|
-
split_strategies.each do |strategy|
|
|
58
|
-
result = pack_with_strategy(frame, items, weight_limit, strategy)
|
|
59
|
-
if best.nil? || ([result[:errors].size, result[:packings].size] <=> [best[:errors].size, best[:packings].size]) < 0
|
|
60
|
-
best = result
|
|
61
|
-
end
|
|
62
|
-
break if best[:packings].size <= 1
|
|
63
|
-
end
|
|
64
|
-
break if best[:packings].size <= 1 && best[:errors].empty?
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
# the flat stack ignores constraints, so it is only used without them
|
|
68
|
-
if greedy_fallback && !constrained && best[:packings].size > 1 && check_container_is_bigger_than_greedy_box(container, items)
|
|
69
|
-
{ packings: generate_packing_for_greedy_box(container, items), errors: [],
|
|
70
|
-
container_dimensions: container[:dimensions].sort.reverse }
|
|
71
|
-
else
|
|
72
|
-
best
|
|
73
|
-
end
|
|
55
|
+
pack_until(container, items, split_strategies, greedy_fallback, nil)
|
|
74
56
|
end
|
|
75
57
|
|
|
76
58
|
# Placements in the order to put them in the box: bottom layer first, then from
|
|
@@ -92,8 +74,11 @@ module EasyBoxPacker
|
|
|
92
74
|
# cost - lambda taking a box hash and returning a number, lower is better.
|
|
93
75
|
# Default: box[:cost], or the box volume when it has none.
|
|
94
76
|
# max_items - raise ArgumentError when more items are given (default nil, no limit).
|
|
95
|
-
# time_limit -
|
|
96
|
-
#
|
|
77
|
+
# time_limit - seconds for the whole call (default nil, no limit). An upper bound:
|
|
78
|
+
# when it runs out the cheaper of a quick plan made in advance and the
|
|
79
|
+
# boxes packed in time is returned (see quick_plan). Still valid, maybe
|
|
80
|
+
# more boxes or cost than needed. It can only be exceeded by one
|
|
81
|
+
# first-strategy pack of the remaining items.
|
|
97
82
|
# split_strategies,
|
|
98
83
|
# greedy_fallback - passed to #pack.
|
|
99
84
|
#
|
|
@@ -103,6 +88,7 @@ module EasyBoxPacker
|
|
|
103
88
|
# Placements are in the box's usable space (inside dimensions minus padding), sorted descending.
|
|
104
89
|
def select_box(boxes:, items:, allow_multiple_boxes: true, padding: 0, cost: nil, max_items: nil,
|
|
105
90
|
time_limit: nil, split_strategies: SPLIT_STRATEGIES, greedy_fallback: true)
|
|
91
|
+
started_at = now
|
|
106
92
|
raise ArgumentError, 'boxes must not be empty' if boxes.nil? || boxes.empty?
|
|
107
93
|
raise ArgumentError, 'items must be an array' unless items.is_a?(Array)
|
|
108
94
|
validate_non_negative!(:padding, padding)
|
|
@@ -126,34 +112,57 @@ module EasyBoxPacker
|
|
|
126
112
|
|
|
127
113
|
unpacked = []
|
|
128
114
|
remaining = []
|
|
115
|
+
# indexes of the candidates each item fits in on its own (size and weight)
|
|
116
|
+
fits = {}.compare_by_identity
|
|
129
117
|
items.each do |item|
|
|
130
118
|
if !item.is_a?(Hash) || !valid_dimensions?(item[:dimensions])
|
|
131
119
|
unpacked << { item: item, reason: :invalid_dimensions }
|
|
132
|
-
|
|
133
|
-
|
|
120
|
+
next
|
|
121
|
+
end
|
|
122
|
+
fits[item] = candidates.each_index.select { |k| fits_alone?(candidates[k][:container], item) }
|
|
123
|
+
if fits[item].empty?
|
|
124
|
+
fits_by_size = candidates.any? { |c| fits_alone?({ dimensions: c[:container][:dimensions] }, item) }
|
|
134
125
|
unpacked << { item: item, reason: fits_by_size ? :too_heavy : :too_big }
|
|
135
126
|
else
|
|
136
127
|
remaining << item
|
|
137
128
|
end
|
|
138
129
|
end
|
|
139
130
|
|
|
140
|
-
deadline = time_limit &&
|
|
131
|
+
deadline = time_limit && started_at + time_limit
|
|
141
132
|
chosen = []
|
|
133
|
+
fallback = nil
|
|
142
134
|
until remaining.empty?
|
|
135
|
+
if out_of_time?(deadline)
|
|
136
|
+
finish = fallback || quick_plan(candidates, remaining, fits, split_strategies.first, greedy_fallback, allow_multiple_boxes, nil)
|
|
137
|
+
chosen.concat(finish[:boxes])
|
|
138
|
+
unpacked.concat(finish[:unpacked])
|
|
139
|
+
break
|
|
140
|
+
end
|
|
141
|
+
# With a time limit, first make a cheap valid answer for what is left, so
|
|
142
|
+
# that running out of time later costs nothing more (see quick_plan). Its
|
|
143
|
+
# pack is remembered and reused by the round below, not done twice.
|
|
144
|
+
memo = deadline && {}
|
|
145
|
+
fallback = deadline && quick_plan(candidates, remaining, fits, split_strategies.first, greedy_fallback, allow_multiple_boxes, memo)
|
|
146
|
+
|
|
143
147
|
remaining_volume = remaining.sum { |i| volume(i[:dimensions]) }
|
|
144
148
|
remaining_weight = remaining.sum { |i| i[:weight].to_f }
|
|
145
149
|
|
|
146
|
-
|
|
150
|
+
# Every candidate is packed with what is left. When the deadline passes
|
|
151
|
+
# during the round, the round is abandoned (see quick_plan).
|
|
152
|
+
results = []
|
|
153
|
+
candidates.each do |cand|
|
|
147
154
|
# a box too small or too weak for everything can not be the single box,
|
|
148
155
|
# so only the fastest strategy is needed to rank it for filling
|
|
149
156
|
could_hold_all = volume(cand[:container][:dimensions]) + EPSILON >= remaining_volume &&
|
|
150
157
|
weight_limit_of(cand[:container]) + EPSILON >= remaining_weight
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
158
|
+
strategies = could_hold_all ? split_strategies : [split_strategies.first]
|
|
159
|
+
result = pack_until(cand[:container], remaining, strategies, greedy_fallback, deadline, memo)
|
|
160
|
+
break if result.nil?
|
|
161
|
+
results << [cand, result]
|
|
162
|
+
break if out_of_time?(deadline)
|
|
154
163
|
end
|
|
155
164
|
|
|
156
|
-
# cheapest single box that holds everything left
|
|
165
|
+
# cheapest single box that holds everything left (of the boxes packed in time)
|
|
157
166
|
single = results
|
|
158
167
|
.select { |_, r| r[:packings].size == 1 && r[:errors].empty? }
|
|
159
168
|
.min_by { |c, _| [c[:cost], volume(c[:container][:dimensions])] }
|
|
@@ -161,6 +170,12 @@ module EasyBoxPacker
|
|
|
161
170
|
chosen << box_result(single[0], single[1])
|
|
162
171
|
break
|
|
163
172
|
end
|
|
173
|
+
if results.size < candidates.size
|
|
174
|
+
# out of time: also consider the boxes packed in time that hold everything left
|
|
175
|
+
fallback = cheapest_plan(fallback, results, allow_multiple_boxes)
|
|
176
|
+
next
|
|
177
|
+
end
|
|
178
|
+
|
|
164
179
|
unless allow_multiple_boxes
|
|
165
180
|
remaining.each { |item| unpacked << { item: item, reason: :no_single_box } }
|
|
166
181
|
break
|
|
@@ -187,9 +202,10 @@ module EasyBoxPacker
|
|
|
187
202
|
|
|
188
203
|
# Find a small container (dimensions in cm, floats sorted ascending) that holds all items.
|
|
189
204
|
#
|
|
190
|
-
# time_limit -
|
|
191
|
-
#
|
|
192
|
-
#
|
|
205
|
+
# time_limit - seconds for the whole call (default 0.5), an upper bound: out of time
|
|
206
|
+
# during the heuristic, the greedy (or row) container is returned, which
|
|
207
|
+
# holds every item. nil = no time limit, 0 = heuristic only, run to the
|
|
208
|
+
# end (0.1.0 behaviour).
|
|
193
209
|
# max_attempts - max number of candidate containers test-packed while refining.
|
|
194
210
|
# Unlike time_limit it gives the same result on every machine. nil = no limit.
|
|
195
211
|
# max_length - longest side allowed (cm), e.g. a carrier limit.
|
|
@@ -208,6 +224,7 @@ module EasyBoxPacker
|
|
|
208
224
|
max_length: nil, max_length_plus_girth: nil, cost: nil,
|
|
209
225
|
beam_width: SEARCH_BEAM_WIDTH, max_side_values: MAX_SIDE_VALUES,
|
|
210
226
|
split_strategies: SPLIT_STRATEGIES, greedy_fallback: true)
|
|
227
|
+
started_at = now
|
|
211
228
|
raise ArgumentError, 'items must not be empty' if items.nil? || items.empty?
|
|
212
229
|
items.each do |item|
|
|
213
230
|
raise ArgumentError, "invalid dimensions for item: #{item}" unless valid_dimensions?(item[:dimensions])
|
|
@@ -222,9 +239,10 @@ module EasyBoxPacker
|
|
|
222
239
|
validate_split_strategies!(split_strategies)
|
|
223
240
|
pack_options = { split_strategies: split_strategies, greedy_fallback: greedy_fallback }
|
|
224
241
|
|
|
225
|
-
started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
226
242
|
limits = { max_length: max_length, max_length_plus_girth: max_length_plus_girth }
|
|
227
|
-
|
|
243
|
+
# time_limit 0 keeps the heuristic answer of 0.1.0 (deterministic, not bounded)
|
|
244
|
+
deadline = time_limit && time_limit > 0 ? started_at + time_limit : nil
|
|
245
|
+
start = heuristic_smallest_container(items, beam_width, pack_options, deadline)
|
|
228
246
|
start = nil unless within_limits?(start, limits)
|
|
229
247
|
return start && normalize(start) if items.size == 1
|
|
230
248
|
|
|
@@ -238,8 +256,146 @@ module EasyBoxPacker
|
|
|
238
256
|
|
|
239
257
|
private
|
|
240
258
|
|
|
259
|
+
def now
|
|
260
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def out_of_time?(deadline)
|
|
264
|
+
!deadline.nil? && now > deadline
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# #pack without validation. With a deadline (monotonic seconds) no further
|
|
268
|
+
# strategy or frame is tried after it, and a strategy still running is
|
|
269
|
+
# abandoned between two items. Returns nil when not even the first strategy
|
|
270
|
+
# finished. deadline nil gives exactly #pack's result.
|
|
271
|
+
#
|
|
272
|
+
# memo - optional Hash shared by calls packing the same items array: a strategy
|
|
273
|
+
# already run in a frame of the same container is not run again.
|
|
274
|
+
def pack_until(container, items, split_strategies, greedy_fallback, deadline, memo = nil)
|
|
275
|
+
weight_limit = weight_limit_of(container)
|
|
276
|
+
constrained = constrained?(items)
|
|
277
|
+
frames = constrained ? container_frames(container[:dimensions]) : [container[:dimensions].sort.reverse]
|
|
278
|
+
|
|
279
|
+
best = nil
|
|
280
|
+
frames.each do |frame|
|
|
281
|
+
split_strategies.each do |strategy|
|
|
282
|
+
key = memo && [container.object_id, frame, strategy]
|
|
283
|
+
result = (memo && memo[key]) || pack_with_strategy(frame, items, weight_limit, strategy, deadline)
|
|
284
|
+
break if result.nil?
|
|
285
|
+
memo[key] = result if memo
|
|
286
|
+
if best.nil? || ([result[:errors].size, result[:packings].size] <=> [best[:errors].size, best[:packings].size]) < 0
|
|
287
|
+
best = result
|
|
288
|
+
end
|
|
289
|
+
break if best[:packings].size <= 1 || out_of_time?(deadline)
|
|
290
|
+
end
|
|
291
|
+
break if best.nil? || out_of_time?(deadline)
|
|
292
|
+
break if best[:packings].size <= 1 && best[:errors].empty?
|
|
293
|
+
end
|
|
294
|
+
return nil if best.nil?
|
|
295
|
+
|
|
296
|
+
# the flat stack ignores constraints, so it is only used without them
|
|
297
|
+
if greedy_fallback && !constrained && best[:packings].size > 1 && check_container_is_bigger_than_greedy_box(container, items)
|
|
298
|
+
{ packings: generate_packing_for_greedy_box(container, items), errors: [],
|
|
299
|
+
container_dimensions: container[:dimensions].sort.reverse }
|
|
300
|
+
else
|
|
301
|
+
best
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# A cheap, valid answer for the remaining items, used by select_box when time
|
|
306
|
+
# runs out: { boxes:, unpacked: }. Like the full search it prefers one box: it
|
|
307
|
+
# repeatedly takes the box most of the remaining items fit in on their own, then
|
|
308
|
+
# one estimated to hold them all in a single copy (cheapest first), else the
|
|
309
|
+
# lowest estimated cost (copies needed by volume and by weight, times its cost),
|
|
310
|
+
# and packs those items into as many copies of it as needed with one split
|
|
311
|
+
# strategy: one pack per box type used, usually only one. Every item fits its box alone, so each pass places items;
|
|
312
|
+
# if a pass placed none, each gets a copy of its own. Without
|
|
313
|
+
# allow_multiple_boxes there is one pass, into the cheapest box estimated to
|
|
314
|
+
# hold everything (else the largest): one box, or every item :no_single_box.
|
|
315
|
+
def quick_plan(candidates, remaining, fits, strategy, greedy_fallback, allow_multiple_boxes, memo)
|
|
316
|
+
all_items = remaining
|
|
317
|
+
boxes = []
|
|
318
|
+
until remaining.empty?
|
|
319
|
+
k = candidates.each_index.min_by do |i|
|
|
320
|
+
c = candidates[i]
|
|
321
|
+
group = remaining.select { |item| fits[item].include?(i) }
|
|
322
|
+
copies = estimated_copies(c[:container], group)
|
|
323
|
+
single = copies == 1 ? 0 : 1
|
|
324
|
+
rank = allow_multiple_boxes ? [single, c[:cost] * copies] : [single, single.zero? ? c[:cost] : 0]
|
|
325
|
+
[-group.size, *rank, -volume(c[:container][:dimensions])]
|
|
326
|
+
end
|
|
327
|
+
cand = candidates[k]
|
|
328
|
+
group = remaining.select { |item| fits[item].include?(k) }
|
|
329
|
+
group = remaining if group.size == remaining.size
|
|
330
|
+
result = pack_until(cand[:container], group, [strategy], greedy_fallback, nil, group.equal?(all_items) ? memo : nil)
|
|
331
|
+
|
|
332
|
+
unless allow_multiple_boxes
|
|
333
|
+
if group.size == remaining.size && result[:packings].size == 1 && result[:errors].empty?
|
|
334
|
+
return { boxes: [box_result(cand, result)], unpacked: [] }
|
|
335
|
+
end
|
|
336
|
+
return { boxes: [], unpacked: remaining.map { |item| { item: item, reason: :no_single_box } } }
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
placed = {}.compare_by_identity
|
|
340
|
+
result[:packings].each do |packing|
|
|
341
|
+
boxes << box_result(cand, result.merge(packings: [packing]))
|
|
342
|
+
packing[:placements].each { |pl| placed[pl[:item]] = true }
|
|
343
|
+
end
|
|
344
|
+
if placed.empty?
|
|
345
|
+
group.each do |item|
|
|
346
|
+
boxes << alone_box_result(cand, item)
|
|
347
|
+
placed[item] = true
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
remaining = remaining.reject { |item| placed.key?(item) }
|
|
351
|
+
end
|
|
352
|
+
{ boxes: boxes, unpacked: [] }
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
# Copies of the container needed at least, by volume (at QUICK_PLAN_FILL of it)
|
|
356
|
+
# and by weight. An estimate for choosing a box, never used for placements.
|
|
357
|
+
def estimated_copies(container, items)
|
|
358
|
+
by_volume = items.sum { |i| volume(i[:dimensions]) } / (volume(container[:dimensions]) * QUICK_PLAN_FILL)
|
|
359
|
+
limit = weight_limit_of(container)
|
|
360
|
+
by_weight = limit.infinite? || limit <= 0 ? 0 : items.sum { |i| i[:weight].to_f } / limit
|
|
361
|
+
[by_volume.ceil, by_weight.ceil, 1].max
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# The better of the plan and the packs (from a round cut short by the deadline)
|
|
365
|
+
# that hold every remaining item, in as many copies of their box as they need.
|
|
366
|
+
# Better, as in the full search: nothing unpacked, then one box, then cheaper.
|
|
367
|
+
def cheapest_plan(plan, results, allow_multiple_boxes)
|
|
368
|
+
rank = ->(p) { [p[:unpacked].empty? ? 0 : 1, p[:boxes].size == 1 ? 0 : 1, p[:boxes].sum { |b| b[:cost] }] }
|
|
369
|
+
results.each do |cand, result|
|
|
370
|
+
next unless result[:errors].empty?
|
|
371
|
+
next if !allow_multiple_boxes && result[:packings].size > 1
|
|
372
|
+
other = { boxes: result[:packings].map { |pk| box_result(cand, result.merge(packings: [pk])) }, unpacked: [] }
|
|
373
|
+
plan = other if (rank.call(other) <=> rank.call(plan)) < 0
|
|
374
|
+
end
|
|
375
|
+
plan
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
# The item alone in its own copy of the candidate (it must fit alone).
|
|
379
|
+
def alone_box_result(candidate, item)
|
|
380
|
+
container = candidate[:container]
|
|
381
|
+
frames = constrained?([item]) ? container_frames(container[:dimensions]) : [container[:dimensions].sort.reverse]
|
|
382
|
+
frame, placement = frames.lazy.map { |f| [f, place(item, { dimensions: f, position: [0, 0, 0] })] }.find { |_, pl| pl }
|
|
383
|
+
box_result(candidate, { packings: [{ placements: [placement], weight: placement[:weight] }], container_dimensions: frame })
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
# Whether the item alone fits in an empty container: the same answer as
|
|
387
|
+
# packing [item] into it, without the cost of a full #pack.
|
|
388
|
+
def fits_alone?(container, item)
|
|
389
|
+
return false if item[:weight].to_f > weight_limit_of(container)
|
|
390
|
+
frames = constrained?([item]) ? container_frames(container[:dimensions]) : [container[:dimensions].sort.reverse]
|
|
391
|
+
frames.any? { |frame| place(item, { dimensions: frame, position: [0, 0, 0] }) }
|
|
392
|
+
end
|
|
393
|
+
|
|
241
394
|
# Original search: grow a container item by item, then compare with the greedy box.
|
|
242
|
-
|
|
395
|
+
# After the deadline no more candidates are test-packed (the one being packed is
|
|
396
|
+
# abandoned) and the answer is the greedy box, or the row of items when some are
|
|
397
|
+
# upright / fragile. Both hold every item by construction.
|
|
398
|
+
def heuristic_smallest_container(items, beam_width, pack_options, deadline = nil)
|
|
243
399
|
# order items from biggest to smallest
|
|
244
400
|
sorted_items = items.sort_by { |h| h[:dimensions].sort }.reverse
|
|
245
401
|
return sorted_items.first[:dimensions] if sorted_items.size == 1
|
|
@@ -254,7 +410,7 @@ module EasyBoxPacker
|
|
|
254
410
|
|
|
255
411
|
select_container = possible_containers
|
|
256
412
|
.sort_by { |a| [volume(a), a.inject(:+)] }
|
|
257
|
-
.find { |c| fits_in_one?(c, items, pack_options) }
|
|
413
|
+
.find { |c| break nil if out_of_time?(deadline); fits_in_one?(c, items, pack_options, deadline) }
|
|
258
414
|
|
|
259
415
|
if constrained?(items)
|
|
260
416
|
# the flat stack breaks upright / fragile items: fall back to the items in one row
|
|
@@ -328,7 +484,7 @@ module EasyBoxPacker
|
|
|
328
484
|
return best if deadline && Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline
|
|
329
485
|
return best if max_attempts && attempts >= max_attempts
|
|
330
486
|
attempts += 1
|
|
331
|
-
next unless fits_in_one?(candidate, items, pack_options)
|
|
487
|
+
next unless fits_in_one?(candidate, items, pack_options, deadline)
|
|
332
488
|
|
|
333
489
|
best, best_cost = candidate, cost_of.call(candidate)
|
|
334
490
|
break
|
|
@@ -374,11 +530,13 @@ module EasyBoxPacker
|
|
|
374
530
|
end
|
|
375
531
|
|
|
376
532
|
# pack_options: split_strategies:, greedy_fallback: and optionally weight_limit:
|
|
377
|
-
|
|
533
|
+
# With a deadline, false also when the pack did not finish in time.
|
|
534
|
+
def fits_in_one?(dimensions, items, pack_options = {}, deadline = nil)
|
|
378
535
|
options = pack_options.dup
|
|
379
536
|
container = { dimensions: dimensions, weight_limit: options.delete(:weight_limit) }
|
|
380
|
-
packing =
|
|
381
|
-
|
|
537
|
+
packing = pack_until(container, items, options.fetch(:split_strategies, SPLIT_STRATEGIES),
|
|
538
|
+
options.fetch(:greedy_fallback, true), deadline)
|
|
539
|
+
!packing.nil? && packing[:packings].size == 1 && packing[:errors].empty?
|
|
382
540
|
end
|
|
383
541
|
|
|
384
542
|
# result is a #pack result whose first packing is the one used for this box
|
|
@@ -437,12 +595,14 @@ module EasyBoxPacker
|
|
|
437
595
|
end
|
|
438
596
|
|
|
439
597
|
|
|
440
|
-
|
|
598
|
+
# Returns nil when the deadline passes before every item was handled.
|
|
599
|
+
def pack_with_strategy(container_dimensions, items, weight_limit, strategy, deadline = nil)
|
|
441
600
|
packings = []
|
|
442
601
|
errors = []
|
|
443
602
|
|
|
444
603
|
# pack from biggest to smallest
|
|
445
604
|
items.sort_by { |h| valid_dimensions?(h[:dimensions]) ? h[:dimensions].sort.reverse : [] }.reverse.each do |item|
|
|
605
|
+
return nil if out_of_time?(deadline)
|
|
446
606
|
unless valid_dimensions?(item[:dimensions])
|
|
447
607
|
errors << "Item: #{item} has invalid dimensions"
|
|
448
608
|
next
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: easy-box-packer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Aloha Chen
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rspec
|