-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinpack2-bench2.lisp
338 lines (320 loc) · 11.4 KB
/
binpack2-bench2.lisp
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
(in-package #:binpack/2)
;; some of binpack2-bench rewritten to use full API so packer is selectable
#++
(defun run1 (wh &key sort)
(let* ((w1 wh)
(h1 wh)
(hole (init-hole w1 h1))
(a 0)
(c 0)
(tx (gen-test w1 h1))
(r (list (list :init w1 h1))))
(time
(progn
(time
(when sort
(setf tx (sort tx (if (eql sort t) #'xy> sort)))))
(loop with x = nil
with y = nil
for (w h) in tx
do (setf (values hole x y)
(place hole w h #+:heuristic1 'random-placement
#+:heuristic1 'minimize-contact
:heuristic1 'smallest-hole
:heuristic2 'maximize-contact))
(push (list :show w h) r)
when x
do (incf a (* w h))
(incf c)
(push (list :place x y w h) r)
while hole)))
#++ (sb-ext:atomic-push (nreverse r) binpack2-vis::*replays*)
(format t "placed ~s/~s @ ~s%~%"
c (length tx) (* 100.0 (/ a (* w1 h1))))))
#++
(defun run2 (w1 h1 tx &key sort shape)
(let* ((hole (init-hole w1 h1))
(a 0)
(c 0)
(r (make-array 32 :adjustable t :fill-pointer 0))
(mx 0)
(my 0))
(time
(progn
(time
(when sort
(setf tx (sort tx (if (eql sort t) #'xy> sort)))))
(loop with x = nil
with y = nil
for (w h) in tx
do (setf (values hole x y)
(place hole w h #+:heuristic1 'random-placement
#+:heuristic1 'minimize-contact
;:heuristic1 'smallest-hole
;:heuristic2 'maximize-contact
:shaping shape))
when x
do (incf a (* w h))
(incf c)
(vector-push-extend (list x y w h) r)
(setf mx (max mx (+ x w)))
(setf my (max my (+ y h)))
while hole)))
#++(sb-ext:atomic-push r binpack2-vis2::*replays*)
(format t "placed ~s/~s @ ~s%~%"
c (length tx) (* 100.0 (/ a (* w1 h1))))
(format t " final size = ~sx~s @ ~s%~%"
mx my (* 100.0 (/ a (* mx my))))))
(defun bench-file2 (fn &key (algorithm :chazelle))
(alexandria:with-input-from-file (f fn)
(let ((w (read f)) (h (read f))
(start)
(pack nil)
(placed 0)
(unplaced 0)
(i 0))
(format t "initializing bin to size ~s x ~s~%" w h)
(setf start (get-internal-real-time))
(setf pack (start-pack w h :packer algorithm))
(loop for rw = (read f nil nil)
for rh = (read f nil nil)
while (and rw rh)
do (let ((r (pack-1/@ (rect* rw rh) pack 0)))
(if (x r)
(incf placed)
(incf unplaced))
(incf i)
(when (zerop (mod i 100))
(let ((t2 (get-internal-real-time)))
(format t "~s = ~sms~%"
i
(floor (- t2 start)
(/ internal-time-units-per-second
1000)))))))
(let ((t2 (get-internal-real-time)))
(format t "~s done @ ~sms~%"
i
(floor (- t2 start)
(/ internal-time-units-per-second
1000))))
(format t "packed ~s rects, ~s unpacked~%" placed unplaced))))
(defun bench-file/2a (fn &key (algorithm :chazelle))
(alexandria:with-input-from-file (f fn)
(let* ((w (* 2 (read f)))
(h (* 2 (read f)))
(start)
(pack)
(placed 0)
(unplaced 0)
(placed-area 0)
(i 0)
(mx 0)
(my 0)
(shape (make-instance 'shaping-quantized
:w (floor w 2) :h (floor h 2)
:dx 1 :dy 1)))
(format t "initializing bin to size ~s x ~s~%" w h)
(setf start (get-internal-real-time))
(setf pack (start-pack w h :packer algorithm
:growth-policy shape))
(loop for rw = (read f nil nil)
for rh = (read f nil nil)
while (and rw rh)
do (let ((r (pack-1/@ (rect* rw rh) pack 0)))
(setf mx (max mx (+ (x r) rw)))
(setf my (max my (+ (y r) rh)))
(if (x r)
(progn
(incf placed)
(incf placed-area (* rw rh)))
(incf unplaced))
(incf i)
(when (zerop (mod i 100))
(let ((t2 (get-internal-real-time)))
(format t "~s = ~sms~%"
i
(floor (- t2 start)
(/ internal-time-units-per-second
1000)))))))
(let ((t2 (get-internal-real-time)))
(format t "~s done @ ~sms~%"
i
(floor (- t2 start)
(/ internal-time-units-per-second
1000))))
(format t "packed ~s rects, ~s unpacked~%" placed unplaced)
(format t " final size = ~sx~s~%" mx my)
(format t " = ~s (placed = ~s)~%" (* mx my) placed-area))))
(defun bench-file/21 (fn &key (algorithm :chazelle))
(alexandria:with-input-from-file (f fn)
(let ((w (read f)) (h (read f))
(start)
(pack)
(placed 0)
(unplaced 0)
(i 0))
(format t "initializing bin to size ~s x ~s~%" w h)
(setf start (get-internal-real-time))
(setf pack (start-pack w h :packer algorithm))
(loop for rw = (read f nil nil)
for rh = (read f nil nil)
while (and rw rh)
do (let ((ok (x (pack-1/@ (rect nil 0 0 rw rh) pack 0))))
(if ok
(incf placed)
(incf unplaced))
(incf i)
(when (zerop (mod i 100))
(let ((t2 (get-internal-real-time)))
(format t "~s = ~sms~%"
i
(floor (- t2 start)
(/ internal-time-units-per-second
1000)))))))
(let ((t2 (get-internal-real-time)))
(format t "~s done @ ~sms~%"
i
(floor (- t2 start)
(/ internal-time-units-per-second
1000))))
(format t "packed ~s rects, ~s unpacked~%" placed unplaced))))
(defun bench-file/2s (fn &key (algorithm :chazelle))
(alexandria:with-input-from-file (f fn)
(let ((w (read f)) (h (read f))
(start)
(pack)
(placed 0)
(unplaced 0)
(i 0)
(v (make-array 1 :adjustable t :fill-pointer 0)))
(format t "initializing bin to size ~s x ~s~%" w h)
(setf start (get-internal-real-time))
(setf pack (start-pack w h :packer algorithm))
(loop for rw = (read f nil nil)
for rh = (read f nil nil)
while (and rw rh)
do (vector-push-extend (cons rw rh) v))
(setf v (sort v (lambda (a b)
#++(> (* (car a) (cdr a))
(* (car b) (cdr b)))
#++
(> (+ (car a) (cdr a))
(+ (car b) (cdr b)))
(if (= (car a) (car b))
(> (cdr a) (cdr b))
(> (car a) (car b)))
#++
(if (= (cdr a) (cdr b))
(> (car a) (car b))
(> (cdr a) (cdr b))))))
(format t "-> ~s~%" (subseq v 0 123))
(let ((t2 (get-internal-real-time)))
(format t "load+sort ~sms~%"
(floor (- t2 start)
(/ internal-time-units-per-second
1000))))
(loop for (rw . rh) across v
do (let ((r (pack-1/@ (rect* rw rh) pack 0)))
(if (x r)
(incf placed)
(incf unplaced))
(incf i)
(when (zerop (mod i 100))
(let ((t2 (get-internal-real-time)))
(format t "~s = ~sms~%"
i
(floor (- t2 start)
(/ internal-time-units-per-second
1000)))))))
(let ((t2 (get-internal-real-time)))
(format t "~s done @ ~sms~%"
i
(floor (- t2 start)
(/ internal-time-units-per-second
1000))))
(format t "packed ~s rects, ~s unpacked~%" placed unplaced))))
#++
(bench-file/2a "e:/tmp/binpackb6s.txt")
#++
(bench-file/2a "e:/tmp/binpackb6s.txt" :algorithm :maxrects2)
#++
(bench-file/21 "e:/tmp/binpackb5s2.txt")
#++
(bench-file/21 "e:/tmp/binpackb5s2.txt" :algorithm :maxrects2)
#++
(bench-file/21 "d:/tmp/rects.noto")
#++
(bench-file/21 "d:/tmp/rects.noto" :algorithm :maxrects2)
#++
(bench-file/2s "e:/tmp/binpackb3.txt")
#++
(bench-file/2s "e:/tmp/binpackb3.txt" :algorithm :maxrects2)
#++
(bench-file/2s "d:/tmp/rects.noto")
#++
(bench-file/2s "d:/tmp/rects.noto" :algorithm :maxrects2)
#++
(bench-file/2s "e:/tmp/binpackb3.txt" :algorithm :maxrects2)
(defun load-file (fn)
(alexandria:with-input-from-file (f fn)
(let ((w (read f))
(h (read f))
(v (make-array 1 :adjustable t :fill-pointer 0)))
(loop for rw = (read f nil nil)
for rh = (read f nil nil)
while (and rw rh)
do (vector-push-extend (list rw rh) v))
(list w h (coerce v 'list)))))
#++
(apply #'run2 (load-file "d:/tmp/rects.noto"))
#++
(run2 9096 9096
#++(gen-test 2048 2048)
(third (load-file "e:/tmp/binpackb9s.txt"))
;:sort 'xy>
:shape (make-instance
'shaping-quantized
:w 2048 :h 2048
:dx 4 :dy 1))
#++
(let* ((f1 "e:/tmp/binpackb9")
(w 2048)
(h 2048)
(r (gen-test2 w h)))
(alexandria:with-output-to-file (f (format nil "~a.txt" f1))
(format f "~s ~s~%" w h)
(loop for (x y) in r
do (format f "~s ~s~%" x y)))
(setf r (sort r 'xy>))
(alexandria:with-output-to-file (f (format nil "~as.txt" f1))
(format f "~s ~s~%" w h)
(loop for (x y) in r
do (format f "~s ~s~%" x y)))
(setf r (sort r 'xy<))
(alexandria:with-output-to-file (f (format nil "~as2.txt" f1))
(format f "~s ~s~%" w h)
(loop for (x y) in r
do (format f "~s ~s~%" x y))))
#++
(run 400 :sort #'xy>)
#++(run 800 :sort #'yx>)
#++(setf *exit* t)
#++
(progn
(setf *exit* nil)
(time
(loop for i from 0
until *exit*
do #++ (format t "~s~%" i)
#+sbcl (sb-ext:atomic-incf (car *tests*))
(run-binpack2-test))))
#++
(progn
(setf *exit* nil)
(loop until *exit*
for x = (car *tests*)
do (sleep 1)
(let ((y (car *tests*)))
(format t "~s (~s / sec)~%" y (- y x)))))
#++
(setf *break-on-signals* t)