-
Notifications
You must be signed in to change notification settings - Fork 2
/
minknap.c
688 lines (576 loc) · 17.7 KB
/
minknap.c
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/* ======================================================================
MINKNAP.C, David Pisinger march 1993, revised feb. 1998
MODIFIED by Marius Posta in 2012
* split the original MINKNAP.C into minknap.h and minknap.c
====================================================================== */
/* This is the C-code corresponding to the paper:
*
* D. Pisinger
* A minimal algorithm for the 0-1 knapsack problem
* Operations Research, 45, 758-767 (1997).
*
* Further details on the project can also be found in
*
* D. Pisinger
* Algorithms for Knapsack Problems
* Report 95/1, DIKU, University of Copenhagen
* Universitetsparken 1
* DK-2100 Copenhagen
*
* The algorithm may be used for academic, non-commercial purposes
* only.
* -------------------------------------------------------------------
* The present code is a callable routine which solves a 0-1 Knapsack
* Problem:
*
* maximize \sum_{j=1}^{n} p_{j} x_{j}
* subject to \sum_{j=1}^{n} w_{j} x_{j} \leq c
* x_{j} \in \{0,1\}, j = 1,\ldots,n
*
* The minknap algorithm is called as
*
* z = minknap(n, p, w, x, c)
*
* where p[], w[], x[] are arrays of integers. The optimal objective
* value is returned in z, and x[] gives the solution vector.
* If you need a different interface for your algorithm, minknap
* may easily be adapted to your own datastructures since all tables
* are copied to the internal representation.
*
* Since the minknap algorithm is based on dynamic programming, you
* must give an upper limit MAXSTATES on the number of states. The
* constant is defined below. Different types should be defined as
* follows:
*
* itype should be sufficiently large to hold a profit or weight
* stype should be sufficient to hold sum of profits/weights
* ptype should hold the product of an stype and itype
*
* The code has been tested on a hp9000/735, and conforms with the
* ANSI-C standard.
*
* Errors and questions are refered to:
*
* David Pisinger, associate professor
* DIKU, University of Copenhagen,
* Universitetsparken 1,
* DK-2100 Copenhagen.
* e-mail: [email protected]
* fax: +45 35 32 14 01
*/
/* ======================================================================
definitions
====================================================================== */
#include "minknap.h"
/* ======================================================================
errorx
====================================================================== */
void errorx(char *str, ...)
{
va_list args;
va_start(args, str);
vprintf(str, args); printf("\n");
va_end(args);
printf("Program is terminated !!!\n\n");
exit(-1);
}
/* ======================================================================
findvect
====================================================================== */
state *findvect(stype ws, state *f, state *l)
{
/* find vector i, so that i->wsum <= ws < (i+1)->wsum */
register state *m;
/* a set should always have at least one vector */
if (f > l) errorx((char*) "findvect: empty set");
if (f->wsum > ws) return NULL;
if (l->wsum <= ws) return l;
while (l - f > SYNC) {
m = f + (l - f) / 2;
if (m->wsum > ws) { l = m-1; } else { f = m; }
}
while (l->wsum > ws) l--;
return l;
}
/* ======================================================================
push/pop
====================================================================== */
void push(allinfo *a, int side, item *f, item *l)
{
interval *pos = NULL;
switch (side) {
case LEFT : pos = a->intv1; (a->intv1)++; break;
case RIGHT: pos = a->intv2; (a->intv2)--; break;
}
if (a->intv1 == a->intv2) errorx((char*) "interval stack full");
pos->f = f; pos->l = l;
}
void pop(allinfo *a, int side, item **f, item **l)
{
interval *pos = NULL;
switch (side) {
case LEFT : if (a->intv1 == a->intv1b) errorx((char*) "pop left");
(a->intv1)--; pos = a->intv1; break;
case RIGHT: if (a->intv2 == a->intv2b) errorx((char*) "pop right");
(a->intv2)++; pos = a->intv2; break;
}
*f = pos->f; *l = pos->l;
}
/* ======================================================================
improvesolution
====================================================================== */
void improvesolution(allinfo *a, state *v)
{
if (v->wsum > a->c) errorx((char*) "wrong improvesoluton");
if (v->psum <= a->z) errorx((char*) "not improved solution");
a->z = v->psum;
a->zwsum = v->wsum;
a->ovect = v->vect;
memcpy(a->ovitem, a->vitem, sizeof(item *) * MAXV);
}
/* ======================================================================
definesolution
====================================================================== */
void definesolution(allinfo *a)
{
register item *f, *l, *i;
register stype psum, wsum;
register btype j, k;
if (a->firsttime) {
a->zstar = a->z;
a->firsttime = FALSE;
}
psum = a->z;
wsum = a->zwsum;
f = a->fsort - 1;
l = a->lsort + 1;
for (j = 0; j < MAXV; j++) {
k = a->ovect & ((btype) 1 << j);
i = a->ovitem[j]; if (i == NULL) continue;
if (*(i->x) == 1) {
if (i > f) f = i;
if (k) { psum += i->p; wsum += i->w; *(i->x) = 0; }
} else {
if (i < l) l = i;
if (k) { psum -= i->p; wsum -= i->w; *(i->x) = 1; }
}
}
a->welldef = (psum == a->psumb) && (wsum == a->wsumb);
/* prepare for next round */
if (!a->welldef) {
a->fsort = f + 1;
a->lsort = l - 1;
a->intv1 = a->intv1b;
a->intv2 = a->intv2b;
a->c = wsum;
a->z = psum - 1;
a->ub = psum;
a->maxstates = 0;
}
}
/* ======================================================================
median
====================================================================== */
item *median(item *f1, item *l1, ntype s)
{
/* Find median r of items [f1, f1+s, f1+2s, ... l1], */
/* and ensure the ordering f1 >= r >= l1. */
register ptype mp, mw;
register item *i, *j;
register item *f, *l, *k, *m, *q;
ntype n, d;
static item r;
n = (l1 - f1) / s; /* number of values */
f = f1; /* calculated first item */
l = f1 + s * n; /* calculated last item */
k = l; /* saved last item */
q = f + s * (n / 2); /* middle value */
for (;;) {
d = (l - f + s) / s;
m = f + s * (d / 2);
if (d > 1) {
if (DET(f->p, f->w, m->p, m->w) < 0) SWAP(f, m);
if (d > 2) {
if (DET(m->p, m->w, l->p, l->w) < 0) {
SWAP(m, l);
if (DET(f->p, f->w, m->p, m->w) < 0) SWAP(f, m);
}
}
}
if (d <= 3) { r = *q; break; }
r.p = mp = m->p; r.w = mw = m->w; i = f; j = l;
for (;;) {
do { i += s; } while (DET(i->p, i->w, mp, mw) > 0);
do { j -= s; } while (DET(j->p, j->w, mp, mw) < 0);
if (i > j) break;
SWAP(i, j);
}
if ((j <= q) && (q <= i)) break;
if (i > q) l = j; else f = i;
}
SWAP(k, l1);
return &r;
}
/* ======================================================================
partsort
====================================================================== */
void partsort(allinfo *a, item *f, item *l, stype ws, int what)
{
register ptype mp, mw;
register item *i, *j, *m;
register stype wi;
register int d;
d = l - f + 1;
if (d < 1) errorx((char*) "negative interval in partsort");
if (d > MINMED) {
m = median(f, l, (int) sqrt(d));
} else {
if (d > 1) {
m = f + d / 2;
if (DET(f->p, f->w, m->p, m->w) < 0) SWAP(f, m);
if (d > 2) {
if (DET(m->p, m->w, l->p, l->w) < 0) {
SWAP(m, l);
if (DET(f->p, f->w, m->p, m->w) < 0) SWAP(f, m);
}
}
}
}
if (d > 3) {
mp = m->p; mw = m->w; i = f; j = l; wi = ws;
for (;;) {
do { wi += i->w; i++; } while (DET(i->p, i->w, mp, mw) > 0);
do { j--; } while (DET(j->p, j->w, mp, mw) < 0);
if (i > j) break;
SWAP(i, j);
}
if (wi <= a->cstar) {
if (what == SORTALL) partsort(a, f, i-1, ws, what);
if (what == PARTIATE) push(a, LEFT, f, i-1);
partsort(a, i, l, wi, what);
} else {
if (what == SORTALL) partsort(a, i, l, wi, what);
if (what == PARTIATE) push(a, RIGHT, i, l);
partsort(a, f, i-1, ws, what);
}
}
if ((d <= 3) || (what == SORTALL)) {
a->fpart = f; a->lpart = l; a->wfpart = ws;
}
}
/* ======================================================================
haschance
====================================================================== */
boolean haschance(allinfo *a, item *i, int side)
{
register state *j, *m;
register ptype p, w, r;
stype pp, ww;
if (a->d.size == 0) return FALSE;
if (side == RIGHT) {
if (a->d.fset->wsum <= a->c - i->w) return TRUE;
p = a->ps; w = a->ws; a->pitested++;
pp = i->p - a->z - 1; ww = i->w - a->c;
r = -DET(pp, ww, p, w);
for (j = a->d.fset, m = a->d.lset + 1; j != m; j++) {
if (DET(j->psum, j->wsum, p, w) >= r) return TRUE;
}
} else {
if (a->d.lset->wsum > a->c + i->w) return TRUE;
p = a->pt; w = a->wt; a->pitested++;
pp = -i->p - a->z - 1; ww = -i->w - a->c;
r = -DET(pp, ww, p, w);
for (j = a->d.lset, m = a->d.fset - 1; j != m; j--) {
if (DET(j->psum, j->wsum, p, w) >= r) return TRUE;
}
}
a->pireduced++;
return FALSE;
}
/* ======================================================================
multiply
====================================================================== */
void multiply(allinfo *a, item *h, int side)
{
register state *i, *j, *k, *m;
register itype p, w;
register btype mask0, mask1;
state *r1, *rm;
if (a->d.size == 0) return;
if (side == RIGHT) { p = h->p; w = h->w; } else { p = -h->p; w = -h->w; }
if (2*a->d.size + 2 > MAXSTATES) errorx((char*) "no space in multiply");
/* keep track on solution vector */
a->vno++;
if (a->vno == MAXV) a->vno = 0;
mask1 = ((btype) 1 << a->vno);
mask0 = ~mask1;
a->vitem[a->vno] = h;
/* initialize limits */
r1 = a->d.fset; rm = a->d.lset; k = a->d.set1; m = rm + 1;
k->psum = -1;
k->wsum = r1->wsum + abs(p) + 1;
m->wsum = rm->wsum + abs(w) + 1;
for (i = r1, j = r1; (i != m) || (j != m); ) {
if (i->wsum <= j->wsum + w) {
if (i->psum > k->psum) {
if (i->wsum > k->wsum) k++;
k->psum = i->psum; k->wsum = i->wsum;
k->vect = i->vect & mask0;
}
i++;
} else {
if (j->psum + p > k->psum) {
if (j->wsum + w > k->wsum) k++;
k->psum = j->psum + p; k->wsum = j->wsum + w;
k->vect = j->vect | mask1;
}
j++;
}
}
a->d.fset = a->d.set1;
a->d.lset = k;
a->d.size = a->d.lset - a->d.fset + 1;
a->coresize++;
if (a->d.size > a->maxstates) a->maxstates = a->d.size;
}
/* =========================================================================
simpreduce
========================================================================= */
void simpreduce(int side, item **f, item **l, allinfo *a)
{
register item *i, *j, *k;
register ptype pb, wb;
register ptype q, r;
register int redu;
if (a->d.size == 0) { *f = *l+1; return; }
if (*l < *f) return;
pb = a->b->p; wb = a->b->w;
q = DET(a->z+1-a->psumb, a->c-a->wsumb, pb, wb);
r = -DET(a->z+1-a->psumb, a->c-a->wsumb, pb, wb);
i = *f; j = *l;
redu = 0;
if (side == LEFT) {
k = a->fsort - 1;
while (i <= j) {
if (DET(j->p, j->w, pb, wb) > r) {
SWAP(i, j); i++; redu++; /* not feasible */
} else {
SWAP(j, k); j--; k--; /* feasible */
}
}
*l = a->fsort - 1; *f = k + 1;
} else {
k = a->lsort + 1;
while (i <= j) {
if (DET(i->p, i->w, pb, wb) < q) {
SWAP(i, j); j--; redu++; /* not feasible */
} else {
SWAP(i, k); i++; k++; /* feasible */
}
}
*f = a->lsort + 1; *l = k - 1;
}
a->simpreduced += redu;
}
/* ======================================================================
reduceset
====================================================================== */
void reduceset(allinfo *a)
{
register state *i, *m, *k;
register ptype ps, ws, pt, wt, r;
stype z, c;
state *r1, *rm, *v;
item *f, *l;
if (a->d.size == 0) return;
/* initialize limits */
r1 = a->d.fset; rm = a->d.lset;
v = findvect(a->c, r1, rm);
if (v == NULL) v = r1 - 1; /* all states infeasible */
else { if (v->psum > a->z) improvesolution(a, v); }
c = a->c; z = a->z + 1; k = a->d.setm;
/* expand core, and choose ps, ws */
if (a->s < a->fsort) {
if (a->intv1 == a->intv1b) {
ps = PMAX; ws = WMAX;
} else {
pop(a, LEFT, &f, &l);
if (f < a->ftouch) a->ftouch = f;
ps = f->p; ws = f->w; /* default: pick first item */
simpreduce(LEFT, &f, &l, a);
if (f <= l) {
partsort(a, f, l, 0, SORTALL); a->fsort = f;
ps = a->s->p; ws = a->s->w;
}
}
} else {
ps = a->s->p; ws = a->s->w;
}
/* expand core, and choose pt, wt */
if (a->t > a->lsort) {
if (a->intv2 == a->intv2b) {
pt = PMIN; wt = WMIN;
} else {
pop(a, RIGHT, &f, &l);
if (l > a->ltouch) a->ltouch = l;
pt = l->p; wt = l->w; /* default: pick first item */
simpreduce(RIGHT, &f, &l, a);
if (f <= l) {
partsort(a, f, l, 0, SORTALL); a->lsort = l;
pt = a->t->p; wt = a->t->w;
}
}
} else {
pt = a->t->p; wt = a->t->w;
}
/* now do the reduction */
r = DET(z, c, ps, ws);
for (i = rm, m = v; i != m; i--) {
if (DET(i->psum, i->wsum, ps, ws) >= r) {
k--; *k = *i;
}
}
r = DET(z, c, pt, wt);
for (i = v, m = r1 - 1; i != m; i--) {
if (DET(i->psum, i->wsum, pt, wt) >= r) {
k--; *k = *i;
}
}
a->ps = ps; a->ws = ws;
a->pt = pt; a->wt = wt;
a->d.fset = k;
a->d.lset = a->d.setm - 1; /* reserve one record for multiplication */
a->d.size = a->d.lset - a->d.fset + 1;
}
/* ======================================================================
initfirst
====================================================================== */
void initfirst(allinfo *a, stype ps, stype ws)
{
register state *k;
a->d.size = 1;
a->d.setm = a->d.set1 + MAXSTATES - 1;
a->d.fset = a->d.set1;
a->d.lset = a->d.set1;
k = a->d.fset;
k->psum = ps;
k->wsum = ws;
k->vect = 0;
}
/* ======================================================================
initvect
====================================================================== */
void initvect(allinfo *a)
{
register btype i;
for (i = 0; i < MAXV; i++) a->vitem[i] = NULL;
a->vno = MAXV-1;
}
/* ======================================================================
copyproblem
====================================================================== */
void copyproblem(item *f, item *l, int *p, int *w, int *x)
{
register item *i, *m;
register int *pp, *ww, *xx;
for (i = f, m = l+1, pp = p, ww = w, xx = x; i != m; i++, pp++, ww++, xx++) {
i->p = *pp; i->w = *ww; i->x = xx;
}
}
/* ======================================================================
findbreak
====================================================================== */
void findbreak(allinfo *a)
{
register item *i, *m;
register stype psum, wsum, c, r;
psum = 0; wsum = 0; c = a->cstar;
for (i = a->fitem; wsum <= c; i++) {
*(i->x) = 1; psum += i->p; wsum += i->w;
}
i--; psum -= i->p; wsum -= i->w; /* we went one item too far */
a->fsort = a->fpart;
a->lsort = a->lpart;
a->ftouch = a->fpart;
a->ltouch = a->lpart;
a->b = i;
a->psumb = psum;
a->wsumb = wsum;
a->dantzig = psum + ((c - wsum) * (ptype) i->p) / i->w;
/* find greedy solution */
r = c - wsum;
for (i = a->b, m = a->litem; i <= m; i++) {
*(i->x) = 0; if (i->w <= r) { psum += i->p; r -= i->w; }
}
a->z = psum - 1;
a->zstar = 0;
a->c = a->cstar;
}
/* ======================================================================
minknap
====================================================================== */
minknap_t* minknap_prepare(int maxn)
{
minknap_t* data = (minknap_t*) calloc(1, sizeof(minknap_t));
data->tab = (item *) calloc(maxn, sizeof(item));
data->inttab = (interval*) calloc(SORTSTACK, sizeof(interval));
data->a.d.set1 = (state*) calloc(MAXSTATES, sizeof(state));
return data;
}
void minknap_free(minknap_t* data)
{
free(data->tab);
free(data->inttab);
free(data->a.d.set1);
free(data);
}
stype minknap(minknap_t* data, int n, int *p, int *w, int *x, int c)
{
allinfo a = data->a;
item *tab = data->tab;
interval *inttab = data->inttab;
/* allocate space for internal representation */
a.fitem = &tab[0]; a.litem = &tab[n-1];
copyproblem(a.fitem, a.litem, p, w, x);
a.n = n;
a.cstar = c;
a.iterates = 0;
a.simpreduced = 0;
a.pireduced = 0;
a.pitested = 0;
a.maxstates = 0;
a.coresize = 0;
a.intv1 = a.intv1b = &inttab[0];
a.intv2 = a.intv2b = &inttab[SORTSTACK - 1];
a.fsort = a.litem; a.lsort = a.fitem;
partsort(&a, a.fitem, a.litem, 0, PARTIATE);
findbreak(&a);
a.ub = a.dantzig;
a.firsttime = TRUE;
for (;;) {
a.iterates++;
a.s = a.b-1;
a.t = a.b;
initfirst(&a, a.psumb, a.wsumb);
initvect(&a);
reduceset(&a);
while ((a.d.size > 0) && (a.z < a.ub)) {
if (a.t <= a.lsort) {
if (haschance(&a, a.t, RIGHT)) multiply(&a, a.t, RIGHT);
(a.t)++;
}
reduceset(&a);
if (a.s >= a.fsort) {
if (haschance(&a, a.s, LEFT)) multiply(&a, a.s, LEFT);
(a.s)--;
}
reduceset(&a);
}
definesolution(&a);
if (a.welldef) break;
}
return a.zstar;
}
/* ======================================================================
end
====================================================================== */