Repository navigation
Expand file tree
/
Copy pathanalysis_measure_test.go
More file actions
272 lines (215 loc) · 7.1 KB
/
Copy pathanalysis_measure_test.go
File metadata and controls
272 lines (215 loc) · 7.1 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//go:build measure
package boxpacker3_test
import (
"fmt"
"math"
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/bavix/boxpacker3/v2"
)
type analysisReading struct {
boxes float64
volume float64
fill float64
unfit int
gap float64
elapsed time.Duration
atBound int
orders int
}
func analysisShelves() []struct {
name string
build func() []*boxpacker3.Box
} {
return []struct {
name string
build func() []*boxpacker3.Box
}{
{"seven sizes", catalogue},
{"one size, thirty of them", func() []*boxpacker3.Box {
boxes := make([]*boxpacker3.Box, 0, 30)
for i := range 30 {
boxes = append(boxes, boxpacker3.NewBox(
fmt.Sprintf("same-%d", i), 425, 265, 380, 20000))
}
return boxes
}},
{"equal bins, several needed", func() []*boxpacker3.Box {
boxes := make([]*boxpacker3.Box, 0, 30)
for i := range 30 {
boxes = append(boxes, boxpacker3.NewBox(
fmt.Sprintf("same-%d", i), 220, 180, 160, 20000))
}
return boxes
}},
}
}
type analysisWay struct {
name string
options []boxpacker3.Option
}
func analysisWays(t *testing.T) []analysisWay {
t.Helper()
search, err := boxpacker3.NewSearch(128, 5)
require.NoError(t, err)
ways := []analysisWay{}
for _, rule := range boxpacker3.EveryRuleSettings() {
ways = append(ways, analysisWay{
name: rule.Name(),
options: []boxpacker3.Option{
boxpacker3.WithAlgorithm(boxpacker3.NewGreedy(rule.Order, rule.Selection)),
},
})
}
return append(ways, analysisExtras(t, search)...)
}
func analysisExtras(t *testing.T, search *boxpacker3.Search) []analysisWay {
t.Helper()
fullest := boxpacker3.NewGreedy(boxpacker3.OrderDecreasing, boxpacker3.SelectFullestBox)
ways := []analysisWay{}
for _, goal := range boxpacker3.Goals() {
answer, ok := boxpacker3.BestForName(goal.Name())
require.True(t, ok)
ways = append(ways, analysisWay{
name: "BestFor(" + goal.Name() + ")",
options: []boxpacker3.Option{boxpacker3.WithAlgorithm(answer)},
})
}
return append(ways,
analysisWay{name: search.Name(), options: []boxpacker3.Option{boxpacker3.WithAlgorithm(search)}},
analysisWay{name: "FullestBoxDecreasing + search filler", options: []boxpacker3.Option{
boxpacker3.WithAlgorithm(fullest), boxpacker3.WithFiller(search),
}},
analysisWay{name: "FullestBoxDecreasing + space corners", options: []boxpacker3.Option{
boxpacker3.WithAlgorithm(fullest),
boxpacker3.WithRules(boxpacker3.Rules{FreeSpaceCorners: true}),
}},
analysisWay{name: "FullestBoxDecreasing + rehome", options: []boxpacker3.Option{
boxpacker3.WithAlgorithm(fullest),
boxpacker3.WithFinishers(boxpacker3.Rehome, boxpacker3.BalanceWeight{MaxBoxes: 12}),
}},
)
}
func TestMeasure_Analysis(t *testing.T) {
t.Parallel()
const orders = 120
for _, shelf := range analysisShelves() {
t.Logf("=== %s", shelf.name)
t.Logf("%-38s %8s %10s %7s %6s %6s %9s", "", "boxes", "volume", "fill", "unfit", "atLB", "time")
for _, way := range analysisWays(t) {
reading := measureWay(t, way.options, shelf.build, orders)
t.Logf("%-38s %8.2f %10.1f %6.1f%% %6d %5d%% %9s",
way.name,
reading.boxes/float64(reading.orders),
reading.volume/float64(reading.orders)/1e6,
reading.fill/float64(reading.orders)*100,
reading.unfit,
reading.atBound*100/reading.orders,
(reading.elapsed / time.Duration(reading.orders)).Round(time.Microsecond))
}
}
}
func measureWay(
t *testing.T, options []boxpacker3.Option, build func() []*boxpacker3.Box, orders int,
) analysisReading {
t.Helper()
packer := boxpacker3.NewPacker(options...)
reading := analysisReading{
boxes: 0, volume: 0, fill: 0, unfit: 0, gap: 0, elapsed: 0, atBound: 0, orders: orders,
}
for seed := range orders {
random := rand.New(rand.NewSource(int64(seed))) //nolint:gosec
items := randomOrderOf(random)
started := time.Now()
result, err := packer.Pack(t.Context(), build(), items)
require.NoError(t, err)
reading.elapsed += time.Since(started)
reading.boxes += float64(len(result.Boxes))
reading.unfit += len(result.Unpacked)
reading.fill += result.Report.Fill
reading.gap += float64(result.Report.GapBoxes)
if result.Report.GapBoxes == 0 && len(result.Unpacked) == 0 {
reading.atBound++
}
for _, box := range result.Boxes {
reading.volume += box.Volume()
}
}
return reading
}
func TestMeasure_DistanceToTheBound(t *testing.T) {
t.Parallel()
const orders = 200
for _, shelf := range analysisShelves() {
t.Logf("=== %s", shelf.name)
for _, rule := range boxpacker3.EveryRuleSettings() {
packer := boxpacker3.NewPacker(
boxpacker3.WithAlgorithm(boxpacker3.NewGreedy(rule.Order, rule.Selection)))
atBound, gap, worst := 0, 0, 0
for seed := range orders {
random := rand.New(rand.NewSource(int64(seed))) //nolint:gosec
result, err := packer.Pack(t.Context(), shelf.build(), randomOrderOf(random))
require.NoError(t, err)
if len(result.Unpacked) > 0 {
continue
}
gap += result.Report.GapBoxes
worst = max(worst, result.Report.GapBoxes)
if result.Report.GapBoxes == 0 {
atBound++
}
}
t.Logf("%-28s meets the bound on %3d%% of orders, %.2f boxes above it on average, %d at worst",
rule.Name(), atBound*100/orders, float64(gap)/float64(orders), worst)
}
}
}
func TestMeasure_GoalsAgainstTheirRules(t *testing.T) {
t.Parallel()
const orders = 100
measures := map[string]func(*boxpacker3.Result) float64{
nameFewestBoxes: func(r *boxpacker3.Result) float64 { return float64(len(r.Boxes)) },
nameLeastVolume: func(r *boxpacker3.Result) float64 { return boxpacker3.MetricsOf(r).BoxVolume },
nameMostItems: func(r *boxpacker3.Result) float64 { return float64(len(r.Unpacked)) },
nameHighestFill: func(r *boxpacker3.Result) float64 { return -boxpacker3.MetricsOf(r).Fill },
nameBalancedWeight: func(r *boxpacker3.Result) float64 { return boxpacker3.MetricsOf(r).WeightSpread },
}
for _, shelf := range analysisShelves() {
t.Logf("=== %s", shelf.name)
for _, goal := range boxpacker3.Goals() {
answer, ok := boxpacker3.BestForName(goal.Name())
require.True(t, ok)
measure := measures[goal.Name()]
beaten, level, better := 0, 0, 0
for seed := range orders {
random := rand.New(rand.NewSource(int64(seed))) //nolint:gosec
items := randomOrderOf(random)
goalResult, err := boxpacker3.NewPacker(boxpacker3.WithAlgorithm(answer)).
Pack(t.Context(), shelf.build(), items)
require.NoError(t, err)
best := math.Inf(1)
for _, rule := range boxpacker3.EveryRuleSettings() {
ruleResult, err := boxpacker3.NewPacker(
boxpacker3.WithAlgorithm(boxpacker3.NewGreedy(rule.Order, rule.Selection))).
Pack(t.Context(), shelf.build(), items)
require.NoError(t, err)
if len(ruleResult.Unpacked) == len(goalResult.Unpacked) {
best = math.Min(best, measure(ruleResult))
}
}
switch {
case measure(goalResult) > best+1e-9:
beaten++
case measure(goalResult) < best-1e-9:
better++
default:
level++
}
}
t.Logf("%-18s beaten by a rule on %3d orders, level on %3d, better on %3d",
goal.Name(), beaten, level, better)
}
}
}