summaryrefslogtreecommitdiff
path: root/ipl/gprogs/bpack.icn
blob: 1a01764bceaa5524f95332bdbbd660850a1c5777 (plain)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
############################################################################
#
#	File:     bpack.icn
#
#	Subject:  Program to demonstrate some bin packing algorithms
#
#	Author:   Gregg M. Townsend
#
#	Date:     August 7, 1998
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  Usage:  bpack [window options]
#
#  Bpack illustrates several approximation algorithms for solving the
#  one-dimensional bin packing problem.
#
#  For a discussion of this program, see
#	http://www.cs.arizona.edu/icon/oddsends/bpack/bpack.htm
#
#  For references, see the "about" screen.
#
############################################################################
#
#  Requires:  Version 9 graphics
#
############################################################################
#
#  Links: numbers, graphics, random, vsetup
#
############################################################################



link numbers
link graphics
link random
link vsetup

$define Version "Binpack Lite (November, 1997)"

$define FULL 61261200	# value representing a full bin
			# (least common multiple of {1 to 18, 20, and 25})

$define PieceWidth 6	# width of one piece
$define BinWidth 7	# width of one bin



# pieces
global pieces		# list of piece sizes

# current output parameters
global xll, yll		# display origin
global bin		# list of current bin sizes
global nfilled		# number of bins (partially) filled

# colors
global color		# array of GCs of different colors
global cscale		# conversion from piece size to color index

# display regions
global shelf1		# input segments
global shelf2		# packed bins



#########################  main program  #########################

procedure main(args)
   local v, r, c, gc

   randomize()				# set irreproducible mode

   v := ui(args)			# open window, set up vib-built vidgets
   r := v["root"]
   shelf1 := v["shelf1"]
   shelf2 := v["shelf2"]
   if shelf1.uw ~= shelf2.uw | shelf1.uw ~= shelf2.uw then
      runerr(500, "inconsistent layout")

   # make a set of colors for different bin heights
   # note that exactly half are reds/yellows and half are blues & darker
   color := []
   every c := Blend(
         "black", 1, "deep purple-magenta", 10, "cyan-blue",
         1, "reddish-yellow", 11, "orange-red") do {
      gc := Clone(&window)
      Fg(gc, c)
      put(color, gc)
      }
   color := copy(color)			# ensure contiguous array
   cscale := *color / real(FULL + 1)

   reload()				# initialize random bins
   GetEvents(r)				# enter event loop
end



#########################  bin packing primitives  #########################


#  prepare(v) -- prepare shelf v for placing pieces

procedure prepare(v)
   xll := v.ux
   yll := v.uy + v.uh
   bin := list(*pieces, 0)
   nfilled := 0
   EraseArea(v.ux, v.uy, v.uw, v.uh)
   return
end


#  place(p, b) -- add a piece of size p to bin b

procedure place(p, b)
   local o, t, x, y0, y1
   static m
   initial m := shelf1.uh / real(FULL)

   o := bin[b] | fail
   if (t := o + p) > FULL then
      fail
   bin[b] := t
   nfilled <:= b

   x := xll + (b - 1) * (PieceWidth + 1)
   y0 := integer(yll - m * o)
   y1 := integer(yll - m * t) + 1
   FillRectangle(color[cscale * p + 1], x, y1, PieceWidth, 0 < (y0 - y1))
   return
end


#  status(s) -- write string s and shelf population at end of output shelf

procedure status(s)
   local x

   x := xll + nfilled * BinWidth + 4
   x >:= xll + shelf1.uw - TextWidth("000 ")
   GotoXY(x, yll - WAttrib("leading") - WAttrib("descent"))
   WWrites(s)
   GotoXY(x, yll - WAttrib("descent"))
   WWrites(nfilled)
   return
end



#########################  source set manipulation  #########################


#  reload() -- reload first shelf with random-sized pieces.

procedure reload()
   local i, j, z, p

   pieces := list(shelf1.uw / BinWidth)
   prepare(shelf1)
   every place(pieces[i := 1 to *pieces] := ?FULL, i)
   status("")
   return
end



#  mix() -- randomly reorder the first shelf.

procedure mix()
   local i

   every i := *pieces to 2 by -1 do
      pieces[?i] :=: pieces[i]

   prepare(shelf1)
   every place(pieces[i := 1 to *pieces], i)
   status("")
   return
end



#  regular() -- place equally-spaced using golden ratio

procedure regular()
   local i, n, p

   n := integer(*pieces / &phi + 1)
   while gcd(*pieces, n) > 1 do
      n -:= 1
   i := 0
   every p := !sort(pieces) do {
      i := (i + n) % *pieces
      pieces[i + 1] := p
      }

   prepare(shelf1)
   every place(pieces[i := 1 to *pieces], i)
   status("")
   return
end



#  ascending() -- sort the first shelf in ascending order

procedure ascending()
   local i

   pieces := sort(pieces)
   prepare(shelf1)
   every place(pieces[i := 1 to *pieces], i)
   status("")
   return
end



#  descending() -- sort the first shelf in descending order

procedure descending()
   local i

   pieces := sort(pieces)
   every i := 1 to *pieces / 2 do	# change from ascending to descending
      pieces[i] :=: pieces[-i]

   prepare(shelf1)
   every place(pieces[i := 1 to *pieces], i)
   status("")
   return
end



#########################  packing algorithms  #########################


#  nextfit(l) -- pack using next-fit algorithm

procedure nextfit(l)
   local p

   every p := !l do
      place(p, nfilled | nfilled + 1)
   return
end



#  firstfit(l) -- pack using first-fit algorithm

procedure firstfit(l)
   local p

   every p := !l do
      place(p, 1 to nfilled + 1)
   return
end


#  lastfit(l) -- pack using last-fit algorithm

procedure lastfit(l)
   local p

   every p := !l do
      place(p, (nfilled to 1 by -1) | (nfilled + 1))
   return
end


#  bestfit(l) -- pack using best-fit algorithm

procedure bestfit(l)
   local p, b, i, max, found

   every p := !l do {
      max := FULL - p			# fullest acceptable bin size
      found := 0			# size of best bin found so far
      b := nfilled + 1			# index of where found
      every i := 1 to nfilled do
         if found <:= (max >= bin[i]) then
            b := i
      place(p, b)			# place in best bin found
      }
   return
end


#  worstfit(l, n) -- pack using worst-fit algorithm

procedure worstfit(l, n)
   local p, b, i, found

   every p := !l do {
      found := FULL - p			# size of best bin found so far
      b := nfilled + 1			# index of where found
      every i := 1 to nfilled do
         if found >:= bin[i] then
            b := i
      place(p, b)			# place in best bin found
      }
   return
end


#  nearworst(l, n) -- pack using almost-worst-fit algorithm

procedure nearworst(l, n)
   local p, a, b, i, found

   every p := !l do {
      found := FULL - p			# size of best bin found so far
      a := b := &null
      every i := 1 to nfilled do
         if found >:= bin[i] then {
            a := b
            b := i
            }
      place(p, \a | \b | (nfilled + 1))	# place in second-best bin found
      }
   return
end



#########################  event handling  #########################



#  menu_cb(v, a) -- File and Reorder menu callback

procedure menu_cb(v, a)
   case a[1] of {
      "About":		about()
      "New":		reload()
      "Quit":		exit()
      "Random":		mix()
      "Regular":	regular()
      "Ascending":	ascending()
      "Descending":	descending()
      }
end



#  pack_cb(v, a) -- Pack menu callback

procedure pack_cb(v, a)
   local s, p

   a[1] ? {
      s := tab(upto(' '))		# get 2- or 3-letter name
      }

   prepare(shelf2)			# clear the shelf
   p := copy(pieces)
   case s of {
      "FF":	firstfit(p)
      "LF":	lastfit(p)
      "NF":	nextfit(p)
      "BF":	bestfit(p)
      "WF":	worstfit(p)
      "AWF":	nearworst(p)
   }

   status(s)
   return
end



#  about() -- handle "about" menu entry

procedure about(x, v)
   static text
   initial text := ["",
         Version,
         "by Gregg Townsend, The University of Arizona",
         "",
         "",
         "BF   Best Fit           picks the fullest possible bin",
         "WF   Worst Fit          picks the emptiest bin",
         "AWF  Almost Worst Fit   picks second-emptiest bin",
         "FF   First Fit          picks the oldest possible bin",
         "LF   Last Fit           picks the newest possible bin",
         "NF   Next Fit           tries only the current bin",
         "",
         "",
         "For more information about bin packing algorithms, see:",
         "",
         "   `Approximation Algorithms for Bin-Packing -- An Updated Survey'",
         "   by E.G. Coffman, Jr., M.R. Garey, and D.S. Johnson, in",
         "   Algorithm Design for Computer System Design, ed. by",
         "   Ausiello, Lucertini, and Serafini, Springer-Verlag, 1984",
         "",
         "   `Fast Algorithms for Bin Packing' by David S. Johnson,",
         "   Journal of Computer and System Sciences 8, 272-314 (1974)",
         ""]

   Notice ! text
   return
end



#===<<vib:begin>>===	modify using vib; do not remove this marker line
procedure ui_atts()
   return ["size=600,400", "bg=pale gray", "label=Bin Packer"]
end

procedure ui(win, cbk)
return vsetup(win, cbk,
   [":Sizer:::0,0,600,400:Bin Packer",],
   ["file:Menu:pull::0,0,36,21:File",menu_cb,
      ["About","New","Quit"]],
   ["line:Line:::0,22,599,22:",],
   ["pack:Menu:pull::93,0,36,21:Pack",pack_cb,
      ["FF   first fit","LF   last fit","NF   next fit","BF   best fit","WF   worst fit",
      "AWF  almost worst"]],
   ["reorder:Menu:pull::36,0,57,21:Reorder",menu_cb,
      ["Random","Regular","Ascending","Descending"]],
   ["shelf1:Rect:sunken::12,34,576,170:",],
   ["shelf2:Rect:sunken::12,217,576,170:",],
   )
end
#===<<vib:end>>===	end of section maintained by vib