Repository navigation
Expand file tree
/
Copy pathpacker_audit_goals_test.go
More file actions
152 lines (119 loc) · 2.84 KB
/
Copy pathpacker_audit_goals_test.go
File metadata and controls
152 lines (119 loc) · 2.84 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
package boxpacker3_test
import (
"math"
"testing"
"github.com/bavix/boxpacker3/v2"
)
type criterionCheck struct {
metric func(*boxpacker3.Result) float64
moreWins bool
}
func unfitCount(result *boxpacker3.Result) float64 {
return float64(len(result.Unpacked))
}
func usedBoxCount(result *boxpacker3.Result) float64 {
return float64(usedBoxCountOf(result))
}
func averageFill(result *boxpacker3.Result) float64 {
total, count := 0.0, 0
for _, box := range result.Boxes {
items := box.Items
if len(items) == 0 {
continue
}
used := 0.0
for _, item := range items {
d := item.Dimension
used += d[0] * d[1] * d[2]
}
total += used / (box.Box.Width() * box.Box.Height() * box.Box.Depth())
count++
}
if count == 0 {
return 0
}
return total / float64(count)
}
func packedVolume(result *boxpacker3.Result) float64 {
total := 0.0
for _, box := range result.Boxes {
for _, item := range box.Items {
d := item.Dimension
total += d[0] * d[1] * d[2]
}
}
return total
}
func beats(rival, winner *boxpacker3.Result, criteria []criterionCheck) bool {
for _, c := range criteria {
a, b := c.metric(rival), c.metric(winner)
if math.Abs(a-b) < 1e-9 {
continue
}
if c.moreWins {
return a > b
}
return a < b
}
return false
}
func parallelOver(t *testing.T, goalName string,
boxes []*boxpacker3.Box, items []*boxpacker3.Item,
) *boxpacker3.Result {
t.Helper()
goal, ok := boxpacker3.GoalByName(goalName)
if !ok {
t.Fatalf("no goal named %s", goalName)
}
algorithms := make([]boxpacker3.Algorithm, 0, 6)
for _, rule := range boxpacker3.BoxSelections() {
algorithms = append(algorithms, boxpacker3.NewGreedy(boxpacker3.OrderDecreasing, rule))
}
result, err := boxpacker3.NewPacker(boxpacker3.WithAlgorithm(
boxpacker3.NewPortfolio(goal, algorithms...))).Pack(t.Context(), boxes, items)
if err != nil {
t.Fatal(err)
}
return result
}
func TestAudit_ParallelGoalPicksTheBest(t *testing.T) {
t.Parallel()
goals := map[string][]criterionCheck{
"FewestBoxes": {
{unfitCount, false},
{usedBoxCount, false},
},
"MostItems": {
{unfitCount, false},
},
"HighestFill": {
{unfitCount, false},
{averageFill, true},
},
"LeastVolume": {
{unfitCount, false},
{packedVolume, true},
},
}
losses := 0
for seed := range 80 {
boxes, items := randomOrder(int64(seed))
for name, criteria := range goals {
winner := parallelOver(t, name, boxes, items)
for _, rule := range boxpacker3.BoxSelections() {
rival, err := rulePacker(boxpacker3.OrderDecreasing, rule).
Pack(t.Context(), boxes, items)
if err != nil {
t.Fatal(err)
}
if beats(rival, winner, criteria) {
losses++
if losses <= 5 {
t.Errorf("seed %d goal %s: %s beat the chosen packing", seed, name, rule)
}
}
}
}
}
t.Logf("goals beaten by a lone rule: %d", losses)
}