-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3dbpp.cpp
More file actions
2633 lines (2340 loc) · 68.7 KB
/
Copy path3dbpp.cpp
File metadata and controls
2633 lines (2340 loc) · 68.7 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
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ======================================================================
3D BIN PACKING, Silvano Martello, David Pisinger, Daniele Vigo
1998, 2003, 2006
====================================================================== */
/* This code solves the three-dimensional bin-packing problem, which
* asks for an orthogonal packing of a given set of rectangular-shaped
* boxes into the minimum number of three-dimensional rectangular bins.
* Each box j=1,..,n is characterized by a width w_j, height h_j, and
* depth d_j. An unlimited number of indentical three-dimensional bins,
* having width W, height H and depth D is available. The boxes have fixed
* orientation, i.e., they may not be rotated.
*
* A description of this code is found in the following papers:
*
* S. Martello, D. Pisinger, D. Vigo, E. den Boef, J. Korst (2003)
* "Algorithms for General and Robot-packable Variants of the
* Three-Dimensional Bin Packing Problem"
* submitted TOMS.
*
* S.Martello, D.Pisinger, D.Vigo (2000)
* "The three-dimensional bin packing problem"
* Operations Research, 48, 256-267
*
* The present code is written in ANSI-C, and has been compiled with
* the GNU-C compiler using option "-ansi -pedantic" as well as the
* HP-UX C compiler using option "-Aa" (ansi standard).
*
* This file contains the callable routine binpack3d with prototype
*
* void binpack3d(int n, int W, int H, int D,
* int *w, int *h, int *d,
* int *x, int *y, int *z, int *bno,
* int *lb, int *ub,
* int nodelimit, int iterlimit, int timelimit,
* int *nodeused, int *iterused, int *timeused,
* int packingtype);
*
* the meaning of the parameters is the following:
* n Size of problem, i.e., number of boxes to be packed.
* This value must be smaller than MAXBOXES defined below.
* W,H,D Width, height and depth of every bin.
* w,h,d Integer arrays of length n, where w[j], h[j], d[j]
* are the dimensions of box j for j=0,..,n-1.
* x,y,z,bno Integer arrays of length n where the solution found
* is returned. For each box j=0,..,n-1, the bin number
* it is packed into is given by bno[j], and x[j], y[j], z[j]
* are the coordinates of it lower-left-backward corner.
* lb Lower bound on the solution value (returned by the procedure).
* ub Objective value of the solution found, i.e., number of bins
* used to pack the n boxes. (returned by the procedure).
* nodelimit maximum number of decision nodes to be explored in the
* main branching tree. If set to zero, the algorithm will
* run until an optimal solution is found (or timelimit or
* iterlimit is reached). Measured in thousands (see IUNIT).
* iterlimit maximum number of iterations in the ONEBIN algorithm
* which packs a single bin. If set to zero, the algorithm will
* run until an optimal solution is found (or timelimit or
* nodelimit is reached). Measured in thousands (see IUNIT).
* timelimit Time limit for solving the problem expressed in seconds.
* If set to zero, the algorithm will run until an optimal
* solution is found; otherwise it terminates after timelimit
* seconds with a heuristic solution.
* nodeused returns the number of branch-and-bound nodes investigated,
* measured in thousands (see IUNIT).
* iterused returns the number of iterations in ONEBIN algorithm,
* measured in thousands (see IUNIT).
* timeused returns the time used in miliseconds
* packingtype
* Desired packing type. If set to zero, the algorithm will
* search for an optimal general packing; if set to one, it
* will search for a robot packing.
*
* (c) Copyright 1998, 2003, 2005
*
* David Pisinger Silvano Martello, Daniele Vigo
* DIKU, University of Copenhagen DEIS, University of Bologna
* Universitetsparken 1 Viale Risorgimento 2
* Copenhagen, Denmark Bologna, Italy
*
* This code can be used free of charge for research and academic purposes
* only.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <malloc.h>
#include <time.h>
#include <limits.h>
#include <map>
#include "ext/xml_parser.h"
#include "ext/packlist.h"
#include "ext/response.h"
#include "3dbpp.h"
/* ======================================================================
macros
====================================================================== */
#define TRUE 1 /* logical variables */
#define FALSE 0
#define WDIM 0 /* rotations of boxes */
#define HDIM 1
#define DDIM 2
#define GENERAL 0 /* packing type */
#define ROBOT 1
#define LEFT 0 /* relative placements */
#define RIGHT 1
#define UNDER 2
#define ABOVE 3
#define FRONT 4
#define BEHIND 5
#define UNDEF 6
#define RELMAX 8
#define STACKDEPTH (MAXBOXES*MAXBOXES*RELMAX)
#define VOL(i) ((i)->w * (ptype) (i)->h * (i)->d)
#define MINIMUM(i,j) ((i) < (j) ? (i) : (j))
#define MAXIMUM(i,j) ((i) > (j) ? (i) : (j))
#define DIF(i,j) ((int) ((j) - (i) + 1))
#define SWAPINT(a,b) { register int t; t=*(a);*(a)=*(b);*(b)=t; }
#define SWAP(a,b) { register box t; t=*(a);*(a)=*(b);*(b)=t; }
#define SWAPI(a,b) { register itype t; t=(a);(a)=(b);(b)=t; }
#define SWAPP(a,b) { register point t; t=*(a);*(a)=*(b);*(b)=t; }
#define DF(a,b) ((r=(a).y-(b).y) != 0 ? r : (a).x-(b).x)
/* ======================================================================
type declarations
====================================================================== */
typedef short boolean; /* logical variable */
typedef short ntype; /* number of states,bins */
typedef short itype; /* can hold up to W,H,D */
typedef long stype; /* can hold up to W*H*D */
typedef long ptype; /* product multiplication */
/* box record */
typedef struct irec {
ntype no; /* box number */
itype w; /* box width (x-size) */
itype h; /* box height (y-size) */
itype d; /* box depth (z-size) */
itype x; /* optimal x-position */
itype y; /* optimal y-position */
itype z; /* optimal z-position */
ntype bno; /* bin number */
boolean k; /* is the box chosen? */
stype vol; /* volume of box */
struct irec *ref; /* reference to original box (if necessary) */
} box;
/* all problem information */
typedef struct {
itype W; /* x-size of bin */
itype H; /* y-size of bin */
itype D; /* z-size of bin */
stype BVOL; /* volume of a bin */
ntype n; /* number of boxes */
boolean packtype; /* packing type: GENERAL or ROBOT */
box *fbox; /* first box in problem */
box *lbox; /* last box in problem */
box *fsol; /* first box in current solution */
box *lsol; /* last box in current solution */
box *fopt; /* first box in optimal solution */
box *lopt; /* last box in optimal solution */
boolean *closed; /* for each bin indicator whether closed */
box *fclosed; /* first box in closed bins */
box *lclosed; /* last box in closed bins */
ntype noc; /* number of closed bins */
itype mindim; /* currently smallest box length */
itype maxdim; /* currently largest box length */
stype maxfill; /* the best filling found */
int mcut; /* how many siblings at each node in b&b */
/* different bounds */
ntype bound0; /* Bound L_0 at root node */
ntype bound1; /* Bound L_1 at root node */
ntype bound2; /* Bound L_2 at root node */
ntype lb; /* best of the above */
ntype z; /* currently best solution */
/* controle of 3d filler */
int maxiter; /* max iterations in onebin_robot */
int miss; /* number boxes not packed in onebin_robot */
/* debugging and controle information */
int nodes; /* nodes in branch-and-bound */
int iterat; /* iterations in onebin_decision */
int subnodes; /* nodes in branch-and-bound */
int subiterat; /* iterations in onebin_decision */
int exfill; /* number of calls to onebin_decision */
int iter3d; /* iterations in onebin_robot or general */
int zlayer; /* heuristic solution layer */
int zmcut; /* heuristic solution mcut */
double exacttopo; /* number of topological sorts */
double exacttopn; /* number of topological sorts */
int exactcall; /* number of calls to exact */
int exactn; /* largest problem for exact */
double genertime; /* time used in onebin_general */
double robottime; /* time used in onebin_robot */
double time; /* computing time */
double lhtime; /* layer heuristic computing time */
double mhtime; /* mcut heuristic computing time */
int didpush; /* did the lower bound push up bound */
int maxclose; /* max number of closed bins at any time */
int nodelimit; /* maximum number of nodes in main tree */
int iterlimit; /* maximum number of iterations in ONEBIN*/
int timelimit; /* maximum amount of time to be used */
} allinfo;
/* structure for greedy algorithm */
typedef struct {
int lno; /* layer number */
int d; /* depth of layer */
int bno; /* bin no assigned to layer */
int z; /* z level of layer within bin */
int b; /* temporary bin number */
} heurpair;
/* structure for extreme points in a single bin */
typedef struct {
itype x; /* x-coordinate */
itype y; /* y-coordinate */
itype z; /* z-coordinate */
} point;
/* structure for a domain pair in constraint programming */
typedef struct {
int i; /* index of box i */
int j; /* index of box j */
int relation; /* relation between the two boxes */
boolean domain; /* domain of the two boxes */
} domainpair;
/* set of domains */
typedef char domset[RELMAX];
typedef domset domline[MAXBOXES];
/* pointer to comparison function */
typedef int (*funcptr)(const void *, const void *);
/* ======================================================================
global variables
====================================================================== */
/* boolean variable to indicate time-out situation */
boolean stopped;
/* counter used to ensure that 1D BPP at most performs MAXBPP iterations */
int bpiterat;
/* boolean variables to indicate when 1D packing algorithm should terminate */
boolean feasible, terminate;
/* stack of domain pairs */
domainpair domstack[STACKDEPTH];
domainpair *dompos, *domend;
/* domain of each box */
domline domain[MAXBOXES];
/* current relation between two boxes */
char relation[MAXBOXES][MAXBOXES];
/* debug variable to see level in recursive packing algorithm */
int bblevel;
/* =======================================================================
error
======================================================================= */
void error(const char *str, ...) {
va_list args;
va_start(args, str);
vprintf(str, args);
printf("\n");
va_end(args);
printf("IRREGULAR PROGRAM TERMINATION\n");
exit(-1);
}
/* **********************************************************************
**********************************************************************
Timing routines
**********************************************************************
********************************************************************** */
/* This timing routine is based on the ANSI-C procedure "clock", which
* has a resolution of 1000000 ticks per second. This however implies
* that we pass the limit of a long integer after only 4295 seconds.
* The following routine attempts to correct such situations by adding
* the constant ULONG_MAX to the counter whenever wraparound can be
* detected. But the user is advised to use a timing routine like "times"
* (which however is not ANSI-C standard) for measuring longer time
* periods.
*/
void timer(double *time) {
static double tstart, tend, tprev;
if (time == NULL) {
clock(); /* one extra call to initialize clock */
tstart = tprev = clock();
} else {
tend = clock();
if (tend < tprev)
tstart -= ULONG_MAX; /* wraparound occured */
tprev = tend;
*time = (tend - tstart) / CLOCKS_PER_SEC; /* convert to seconds */
}
}
/* test for time limit */
void check_timelimit(long max) {
double t;
if (max == 0)
return;
timer(&t);
if (t >= max) {
if (!stopped)
printf("TIMELIMIT\n");
stopped = TRUE;
}
}
/* test for node limit */
void check_nodelimit(long nodes, long max) {
if (max == 0)
return;
if (nodes >= max) {
if (!stopped)
printf("NODELIMIT\n");
stopped = TRUE;
}
}
/* test for iteration limit */
void check_iterlimit(long iterations, long max) {
if (max == 0)
return;
if (iterations >= max) {
if (!stopped)
printf("ITERLIMIT\n");
stopped = TRUE;
}
}
/* **********************************************************************
**********************************************************************
Small procedures
**********************************************************************
********************************************************************** */
/* ======================================================================
simple comparisions
====================================================================== */
/* Comparisons used as argument to qsort. */
int dcomp(box *a, box *b) {
int r;
r = b->d - a->d;
if (r != 0)
return r;
else
return b->no - a->no;
}
int hcomp(box *a, box *b) {
int r;
r = b->h - a->h;
if (r != 0)
return r;
else
return b->no - a->no;
}
int vcomp(box *a, box *b) /* volume decr. */
{
int r;
r = b->vol - a->vol;
if (r != 0)
return r;
else
return b->no - a->no;
}
int xcomp(heurpair *a, heurpair *b) /* depth decr. */
{
int r;
r = b->d - a->d;
if (r != 0)
return r;
else
return b->lno - a->lno;
}
int lcomp(heurpair *a, heurpair *b) /* layer number decr. */
{
int r;
r = a->lno - b->lno;
if (r != 0)
return r;
else
return b->d - a->d;
}
/* ======================================================================
palloc
====================================================================== */
/* Memory allocation and freeing, with implicit check */
void *palloc(long sz, long no) {
long size;
void *p;
size = sz * no;
if (size == 0)
size = 1;
p = (void *) malloc(size);
if (p == NULL)
error("no memory size %ld", size);
return p;
}
void pfree(void *p) {
if (p == NULL)
error("freeing null");
free(p);
}
/* ======================================================================
checksol
====================================================================== */
/* Check correctnes of solution, i.e., no boxes overlap, no duplicated boxes.
* If the solution should be robot packable, it is checked that there
* exists an ordering of the boxes such that they can be removed one
* by one withouth having any other boxes behind/above/right of the
* current box.
*/
void checksol(allinfo *a, box *f, box *l) {
box *i, *j, *m;
boolean stillboxes, foundextreme, extreme;
int b;
for (i = f, m = l + 1; i != m; i++) {
if (!i->k)
continue; /* box currently not chosen */
for (j = f; j != m; j++) {
if (i == j)
continue;
if (i->no == j->no)
error("duplicated box %d\n", i->no);
if (!j->k)
continue;
if (i->bno != j->bno)
continue;
if ((i->x + i->w > j->x) && (j->x + j->w > i->x) && (i->y + i->h
> j->y) && (j->y + j->h > i->y) && (i->z + i->d > j->z)
&& (j->z + j->d > i->z)) {
error("overlap box %d,%d: [%d,%d,%d] [%d,%d,%d]", i->no, j->no,
i->w, i->h, i->d, j->w, j->h, j->d);
}
}
}
if (a->packtype != ROBOT)
return;
/* check if robot packable */
for (b = 1; b <= a->z; b++) {
for (;;) {
stillboxes = FALSE;
foundextreme = TRUE;
for (i = f; i <= m; i++) {
if ((i->bno == b) && (i->k == 1))
stillboxes = TRUE;
}
if (!stillboxes)
break;
foundextreme = FALSE;
for (i = f; i <= m; i++) {
if ((i->bno != b) || (i->k != 1))
continue;
extreme = TRUE;
for (j = f; j <= m; j++) {
if (j == i)
continue;
if ((j->bno != b) || (!j->k != 1))
continue;
if ((i->x < j->x + j->w) || (i->y < j->y + j->h) || (i->z
< j->z + j->d))
extreme = FALSE;
}
if (extreme) {
i->k = -1;
foundextreme = TRUE;
}
}
if (!foundextreme)
break;
}
if (!foundextreme) {
for (i = f; i <= m; i++) {
if (i->bno == b) {
printf("no %d (%d %d %d) (%d %d %d) bin %d k %d\n", i->no,
i->x, i->y, i->z, i->w, i->h, i->d, i->bno, i->k);
}
}
error("not robot packable");
}
/* restore k values */
for (i = f; i <= m; i++)
if (i->k == -1)
i->k = 1;
}
}
/* ======================================================================
savesol
====================================================================== */
/* save an updated solution, checking its validity */
void savesol(allinfo *a, box *f, box *l, ntype z) {
box *i, *k, *m;
/* first check validity */
if (z >= a->z)
error("not improved");
for (i = f, m = l + 1; i != m; i++) {
if ((1 <= i->bno) && (i->bno <= z))
continue;
error("illegal bin %d, box %d", i->bno, i->no);
}
/* now do the saving */
a->z = z;
for (i = f, k = a->fopt, m = l + 1; i != m; i++, k++)
*k = *i;
for (i = a->fclosed, m = a->lclosed + 1; i != m; i++, k++)
*k = *i;
for (i = a->fopt, m = a->lopt + 1; i != m; i++)
i->k = TRUE;
if (DIF(a->fopt,k-1) != a->n)
error("not correct amount of boxes");
checksol(a, a->fopt, a->lopt);
}
/* ======================================================================
isortincr
====================================================================== */
/* A specialized routine for sorting integers in increasing order. */
/* qsort could be used equally well, but this routine is faster. */
void isortincr(int *f, int *l) {
register int mi;
register int *i, *j, *m;
register int d;
d = l - f + 1;
if (d < 1)
error("negative interval in isortincr");
if (d == 1)
return;
m = f + d / 2;
if (*f > *m)
SWAPINT(f, m);
if (d > 2) {
if (*m > *l) {
SWAPINT(m, l);
if (*f > *m)
SWAPINT(f, m);
}
}
if (d <= 3)
return;
mi = *m;
i = f;
j = l;
for (;;) {
do
i++;
while (*i < mi);
do
j--;
while (*j > mi);
if (i > j)
break;
else
SWAPINT(i, j);
}
isortincr(f, i - 1);
isortincr(i, l);
}
/* ======================================================================
isortdecr
====================================================================== */
/* A specialized routine for sorting integers in decreasing order. */
/* qsort could be used equally well, but this routine is faster. */
void isortdecr(int *f, int *l) {
register int mi;
register int *i, *j, *m;
register int d;
d = l - f + 1;
if (d < 1)
error("negative interval in isortdecr");
if (d == 1)
return;
m = f + d / 2;
if (*f < *m)
SWAPINT(f, m);
if (d > 2) {
if (*m < *l) {
SWAPINT(m, l);
if (*f < *m)
SWAPINT(f, m);
}
}
if (d <= 3)
return;
mi = *m;
i = f;
j = l;
for (;;) {
do
i++;
while (*i > mi);
do
j--;
while (*j < mi);
if (i > j)
break;
else
SWAPINT(i, j);
}
isortdecr(f, i - 1);
isortdecr(i, l);
}
/* ======================================================================
psortdecr
====================================================================== */
/* A specialized routine for sorting extreme points according to decreasing */
/* y-coordinate (decreasing x-coordinate in case of ties) */
void psortdecr(point *f, point *l) {
register point mi;
register point *i, *j, *m;
register int d, r;
d = l - f + 1;
if (d <= 1)
return;
m = f + d / 2;
if (DF(*f,*m) < 0)
SWAPP(f,m);
if (d == 2)
return;
if (DF(*m,*l) < 0) {
SWAPP(m,l);
if (DF(*f,*m) < 0)
SWAPP(f,m);
}
if (d <= 3)
return;
mi = *m;
i = f;
j = l;
for (;;) {
do
i++;
while (DF(*i,mi) > 0);
do
j--;
while (DF(*j,mi) < 0);
if (i > j)
break;
else
SWAPP(i, j);
}
psortdecr(f, i - 1);
psortdecr(i, l);
}
/* **********************************************************************
**********************************************************************
Lower Bounds
**********************************************************************
********************************************************************** */
/* ======================================================================
bound_zero
====================================================================== */
/* The continuous bound L_0 */
int bound_zero(allinfo *a, box *f, box *l) {
box *i, *m;
stype vsum, lb;
vsum = 0;
for (i = f, m = l + 1; i != m; i++)
vsum += i->vol;
lb = (stype) ceil(vsum / (double) a->BVOL);
return lb;
}
/* ======================================================================
rotate_solution
====================================================================== */
/* rotates the solution. After 3 rotations we return to original problem */
void rotate_solution(allinfo *a, box *f, box *l) {
register box *i, *m;
register itype w, x;
for (i = f, m = l + 1; i != m; i++) {
w = i->w;
i->w = i->h;
i->h = i->d;
i->d = w;
x = i->x;
i->x = i->y;
i->y = i->z;
i->z = x;
}
}
/* ======================================================================
rotate_problem
====================================================================== */
/* rotates the dimensions by one step */
void rotate_problem(allinfo *a, box *f, box *l) {
register box *i, *m;
register itype w, x;
for (i = f, m = l + 1; i != m; i++) {
w = i->w;
i->w = i->h;
i->h = i->d;
i->d = w;
x = i->x;
i->x = i->y;
i->y = i->z;
i->z = x;
}
w = a->W;
a->W = a->H;
a->H = a->D;
a->D = w;
}
/* ======================================================================
choose_boxes
====================================================================== */
/* returns a set of boxes with w > W2 and d > D2. This set is used in */
/* bound_one */
void choose_boxes(allinfo *a, box *f, box *l, int W2, int D2, box *fbox,
box **lbox) {
box *i, *k, *m;
for (i = f, m = l + 1, k = fbox; i != m; i++) {
if ((i->w > W2) && (i->d > D2)) {
*k = *i;
k++;
}
}
*lbox = k - 1;
}
/* ======================================================================
find_plist
====================================================================== */
/* returns a zero-terimanted list of distinct dimensions */
void find_plist(box *fbox, box *lbox, itype M, int dim, int *pl) {
register box *i, *m;
register int *k, *j, *l;
i = fbox;
m = lbox + 1;
k = pl;
switch (dim) {
case WDIM:
for (; i != m; i++) {
if (i->w <= M) {
*k = i->w;
k++;
}
}
break;
case HDIM:
for (; i != m; i++) {
if (i->h <= M) {
*k = i->h;
k++;
}
}
break;
case DDIM:
for (; i != m; i++) {
if (i->d <= M) {
*k = i->d;
k++;
}
}
break;
}
if (k == pl) {
*k = 0;
return;
}
isortincr(pl, k - 1); /* sort the dimensions */
for (j = pl + 1, l = pl; j != k; j++) { /* remove duplicates */
if (*j != *l) {
l++;
*l = *j;
}
}
l++;
*l = 0;
}
/* ======================================================================
bound_one
====================================================================== */
/* Derive bound L_1 for a fixed dimension */
int bound_one_x(allinfo *a, box *f, box *l) {
register box *i, *m;
register itype H, H2, h;
register int p, j1, j2, j3, j2h, j2hp, j3h;
int *pp, lb, lb_one, alpha, beta;
box fbox[MAXBOXES], *lbox;
int plist[MAXBOXES];
if (l == f - 1)
return 0;
lb = 1;
H = a->H;
H2 = H / 2;
choose_boxes(a, f, l, a->W / 2, a->D / 2, fbox, &lbox);
if (lbox == fbox - 1) { /* empty */
return lb;
}
find_plist(fbox, lbox, H2, HDIM, plist);
for (pp = plist; *pp != 0; pp++) {
p = *pp;
j1 = j2 = j3 = j2h = j2hp = j3h = 0;
for (i = fbox, m = lbox + 1; i != m; i++) {
h = i->h;
if (h > H - p)
j1++;
if ((H - p >= h) && (h > H2)) {
j2++;
j2h += h;
j2hp += (H - h) / p;
}
if ((H2 >= h) && (h >= p)) {
j3++;
j3h += h;
}
}
alpha = (int) ceil((j3h - (j2 * H - j2h)) / (double) H);
beta = (int) ceil((j3 - j2hp) / (double) (H / p));
if (alpha < 0)
alpha = 0;
if (beta < 0)
beta = 0;
lb_one = j1 + j2 + MAXIMUM(alpha, beta);
if (lb_one > lb)
lb = lb_one;
}
return lb;
}
/* Derive bound L_1 as the best of all L_1 bounds for three rotations */
int bound_one(allinfo *a, box *f, box *l) {
int i, lb, lbx;
lb = 0;
for (i = WDIM; i <= DDIM; i++) {
lbx = bound_one_x(a, f, l);
if (lbx > lb)
lb = lbx;
rotate_problem(a, f, l);
}
return lb;
}
/* ======================================================================
bound_two
====================================================================== */
/* Derive bound L_2 for a fixed dimension */
int bound_two_x(allinfo *a, box *f, box *l) {
register box *i, *m;
register itype W, H, D, w, h, d, W2, D2;
register int p, q, k1h, k23v;
int hlb1, lb, lb1, lbx, fract;
int plist[MAXBOXES], qlist[MAXBOXES];
int *qq, *pp;
double WD, BVOL;
/* derive bound_one */
lb = lb1 = bound_one_x(a, f, l);
W = a->W;
H = a->H;
D = a->D;
hlb1 = H * lb1;
W2 = W / 2;
D2 = D / 2;
WD = W * (double) D;
BVOL = a->BVOL;
/* run through all values of p, q */
find_plist(f, l, W2, WDIM, plist);
find_plist(f, l, D2, DDIM, qlist);
for (pp = plist; *pp != 0; pp++) {
p = *pp;
for (qq = qlist; *qq != 0; qq++) {
q = *qq;
k1h = k23v = 0;
for (i = f, m = l + 1; i != m; i++) {
w = i->w;
h = i->h;
d = i->d;
if ((w > W - p) && (d > D - q)) {
k1h += h;
continue;
}
if ((w >= p) && (d >= q)) {
k23v += i->vol;
}
}
fract = (int) ceil((k23v - (hlb1 - k1h) * WD) / BVOL);
if (fract < 0)
fract = 0;
lbx = lb1 + fract;
if (lbx > lb)
lb = lbx;
}
}
return lb;
}
/* Derive bound L_2 as the best of all L_2 bounds for three rotations */
int bound_two(allinfo *a, box *f, box *l) {
int i, lb, lbx;
lb = 0;
for (i = WDIM; i <= DDIM; i++) {
lbx = bound_two_x(a, f, l);
if (lbx > lb)
lb = lbx;
rotate_problem(a, f, l);
}
return lb;
}
/* **********************************************************************
**********************************************************************
heuristic filling
**********************************************************************
********************************************************************** */
/* ======================================================================
onelayer
====================================================================== */
/* Fill a layer of depth f->d by arranging the boxes in a number of */