Repository navigation
Expand file tree
/
Copy pathbound.go
More file actions
207 lines (157 loc) · 4.05 KB
/
Copy pathbound.go
File metadata and controls
207 lines (157 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package boxpacker3
import (
"math"
"slices"
)
type Bound struct {
Boxes int
Volume float64
}
const l2Limit = 2000
func boundOf(boxes []*Box, pieces []*piece) Bound {
bound := Bound{Boxes: 0, Volume: 0}
shippable := make([]*piece, 0, len(pieces))
for _, item := range pieces {
if fitsNoBox(item, boxes) {
continue
}
shippable = append(shippable, item)
bound.Volume += item.volume
}
roomiest := roomiestBox(boxes)
if roomiest == nil || len(shippable) == 0 {
return bound
}
bound.Boxes = max(continuousBound(roomiest, shippable), oneDimensionalBound(roomiest, shippable))
if len(shippable) <= l2Limit {
bound.Boxes = max(bound.Boxes, twoDimensionalBound(roomiest, shippable))
}
return bound
}
func roomiestBox(boxes []*Box) *Box {
var roomiest *Box
for _, box := range boxes {
if box != nil && (roomiest == nil || box.volume > roomiest.volume) {
roomiest = box
}
}
return roomiest
}
func continuousBound(box *Box, pieces []*piece) int {
if box.volume <= 0 {
return 0
}
volume := 0.0
for _, item := range pieces {
volume += item.volume
}
return int(math.Ceil(volume/box.volume - epsilon))
}
func smallestFit(item *piece, axis Axis) float64 {
smallest := math.Inf(1)
for _, orientation := range item.rotations {
smallest = min(smallest, rotatedDimension(item.Item, orientation)[axis])
}
if math.IsInf(smallest, 1) {
return 0
}
return smallest
}
func oneDimensionalBound(box *Box, pieces []*piece) int {
bound := 0
for _, axes := range [3][3]Axis{
{WidthAxis, HeightAxis, DepthAxis},
{WidthAxis, DepthAxis, HeightAxis},
{HeightAxis, DepthAxis, WidthAxis},
} {
bound = max(bound, stackedAlong(box, pieces, axes[0], axes[1], axes[2]))
}
return bound
}
func stackedAlong(box *Box, pieces []*piece, first, second, third Axis) int {
lengths := make([]float64, 0, len(pieces))
for _, item := range pieces {
if smallestFit(item, first) > box.boxDimension(first)/2 &&
smallestFit(item, second) > box.boxDimension(second)/2 {
lengths = append(lengths, smallestFit(item, third))
}
}
return martelloToth(lengths, box.boxDimension(third))
}
func martelloToth(lengths []float64, capacity float64) int {
if len(lengths) == 0 || capacity <= 0 {
return 0
}
sorted := slices.Clone(lengths)
slices.Sort(sorted)
bound := 0
for _, cut := range cutPoints(sorted, capacity) {
bound = max(bound, martelloTothAt(sorted, capacity, cut))
}
return bound
}
func cutPoints(sorted []float64, capacity float64) []float64 {
points := make([]float64, 0, len(sorted)+1)
points = append(points, 0)
for _, length := range sorted {
if length > 0 && length <= capacity/2 {
points = append(points, length)
}
}
return points
}
func martelloTothAt(sorted []float64, capacity, cut float64) int {
var (
big int
large int
largeUp float64
small float64
)
for _, length := range sorted {
switch {
case length > capacity-cut+epsilon:
big++
case length > capacity/2+epsilon:
large++
largeUp += capacity - length
case length >= cut-epsilon:
small += length
}
}
spare := 0.0
if small > largeUp {
spare = math.Ceil((small - largeUp) / capacity)
}
return big + large + int(spare)
}
func twoDimensionalBound(box *Box, pieces []*piece) int {
bound := 0
for _, axes := range [3][3]Axis{
{WidthAxis, HeightAxis, DepthAxis},
{WidthAxis, DepthAxis, HeightAxis},
{HeightAxis, DepthAxis, WidthAxis},
} {
bound = max(bound, volumeOverStack(box, pieces, axes[0], axes[1], axes[2]))
}
return bound
}
func volumeOverStack(box *Box, pieces []*piece, first, second, third Axis) int {
stacked := stackedAlong(box, pieces, first, second, third)
if stacked == 0 || box.volume <= 0 {
return 0
}
var inStack, rest float64
for _, item := range pieces {
if smallestFit(item, first) > box.boxDimension(first)/2 &&
smallestFit(item, second) > box.boxDimension(second)/2 {
inStack += item.volume
continue
}
rest += item.volume
}
spare := float64(stacked)*box.volume - inStack
if rest <= spare {
return stacked
}
return stacked + int(math.Ceil((rest-spare)/box.volume-epsilon))
}