-
Notifications
You must be signed in to change notification settings - Fork 4
/
algo.js
739 lines (670 loc) · 19.5 KB
/
algo.js
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
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3
// Copyright (C) 2020-2021 Eklavya Sharma. Licensed under GNU GPLv3.
'use strict';
//==[ Random-number generation ]================================================
// From https://stackoverflow.com/a/47593316/10294000
var seeds = [];
function xmur3(str) {
let h = 1779033703 ^ str.length;
for(let i = 0; i < str.length; i++) {
h = Math.imul(h ^ str.charCodeAt(i), 3432918353);
h = h << 13 | h >>> 19;
}
return function() {
h = Math.imul(h ^ h >>> 16, 2246822507);
h = Math.imul(h ^ h >>> 13, 3266489909);
return (h ^= h >>> 16) >>> 0;
}
}
function mulberry32(a) {
return function() {
let t = a += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
}
function getRandomSeed() {
const seed = 'r' + Math.random().toString().substr(2, 12);
console.debug('getRandomSeed: ' + seed);
seeds.push(seed);
return seed;
}
function getRandGen(seed) {
if(seed === null) {
seed = getRandomSeed();
}
return mulberry32(xmur3(seed)());
}
//==[ Packing util ]============================================================
var rawSimplePackers = new Map();
var packers = new Map();
function fillEmptySlotsWithNulls(a) {
for(let i=0; i < a.length; ++i) {
if(a[i] === undefined) {
a[i] = null;
}
}
}
function getStripDims(items, stripPackSol) {
let xLen = 0, yLen = 0;
for(let i=0; i < items.length; ++i) {
xLen = Math.max(xLen, stripPackSol[items[i].id][0] + items[i].xLen);
yLen = Math.max(yLen, stripPackSol[items[i].id][1] + items[i].yLen);
}
return [xLen, yLen];
}
function countUsedBinsAndPackedItems(bpSol) {
let ind = [];
let nPacked = 0;
for(let i=0; i < bpSol.length; ++i) {
if(bpSol[i] !== null) {
ind[bpSol[i][0]] = 1;
nPacked += 1;
}
}
let nBins = 0;
for(let j=0; j < ind.length; ++j) {
if(ind[j] !== undefined) {
nBins++;
}
}
return [nBins, nPacked];
}
function rotateAllItems(items) {
for(let i=0; i < items.length; ++i) {
let yLen = items[i].yLen;
items[i].yLen = items[i].xLen;
items[i].xLen = yLen;
}
}
function mirrorAlgo(gbpAlgo) {
function mirrorBinPack(items, binXLen, binYLen) {
rotateAllItems(items);
let output = gbpAlgo(items, binYLen, binXLen);
rotateAllItems(items);
for(let i=0; i < output.length; ++i) {
if(output[i] !== null) {
let y = output[i][2];
output[i][2] = output[i][1];
output[i][1] = y;
}
}
return output;
}
return mirrorBinPack;
}
function createRawMirrors(algoNames) {
for(let algoName of algoNames) {
rawSimplePackers.set(algoName + '-mirror', mirrorAlgo(rawSimplePackers.get(algoName)));
}
}
function simplePackerWrap(packAlgo) {
function simplePacker(items, binXLen, binYLen, succHook, failHook=null, logger=null) {
let packing = packAlgo(items, binXLen, binYLen);
let packObj = packing;
if(Array.isArray(packing)) {
packObj = {'deterministic': true, 'packings': new Map([['0', packing]])};
}
succHook(packObj);
return null;
}
simplePacker.packerType = 'simple';
return simplePacker;
}
function cookSimplePackers() {
for(let [algoName, rawAlgo] of rawSimplePackers) {
packers.set(algoName, simplePackerWrap(rawAlgo));
}
}
//==[ Shelf-based packing algorithms ]==========================================
class Shelf {
constructor(id, xLen) {
this.id = id;
this.free = xLen;
this.items = [];
this.size = 0;
}
add(item) {
if(item.xLen <= this.free) {
this.size = Math.max(this.size, item.yLen);
this.free -= item.xLen;
this.items.push(item);
return true;
}
else {
return false;
}
}
}
function nextFitShelfPack(items, xLen) {
if(items.length === 0) {
return [];
}
let shelfId = 0;
let shelf = new Shelf(0, xLen);
let shelves = [shelf];
for(let i=0; i < items.length; ++i) {
let item = items[i];
if(!shelf.add(item)) {
shelf = new Shelf(++shelfId, xLen);
shelves.push(shelf);
shelf.add(item);
}
}
return shelves;
}
function firstFitShelfPack(items, xLen) {
if(items.length === 0) {
return [];
}
let shelves = [];
for(let i=0; i < items.length; ++i) {
let packed = false;
const item = items[i];
for(let j=0; j < shelves.length; ++j) {
if(shelves[j].add(item)) {
packed = true;
break;
}
}
if(!packed) {
let shelf = new Shelf(shelves.length, xLen);
shelves.push(shelf);
shelf.add(item);
}
}
return shelves;
}
function RectComparator(item1, item2) {
if(item1.yLen < item2.yLen) {
return 1;
}
else if(item1.yLen > item2.yLen) {
return -1;
}
else if(item1.xLen < item2.xLen) {
return 1;
}
else if(item1.xLen > item2.xLen) {
return -1;
}
else {
return 0;
}
}
function nfdhShelfPack(items, xLen) {
let sortedItems = [...items].sort(RectComparator);
return nextFitShelfPack(sortedItems, xLen);
}
function ffdhShelfPack(items, xLen) {
let sortedItems = [...items].sort(RectComparator);
return firstFitShelfPack(sortedItems, xLen);
}
function packShelvesIntoStrip(shelves, stripXLen) {
let yAgg=0;
let output = [];
for(let i=0; i < shelves.length; ++i) {
let items = shelves[i].items;
let xAgg = 0;
for(let j=0; j < items.length; ++j) {
output[items[j].id] = [xAgg, yAgg];
xAgg += items[j].xLen;
}
yAgg += shelves[i].size;
}
fillEmptySlotsWithNulls(output);
return output;
}
function nfdhStripPack(items, stripXLen) {
let shelves = nfdhShelfPack(items, stripXLen);
return packShelvesIntoStrip(shelves, stripXLen);
}
function ffdhStripPack(items, stripXLen) {
let shelves = ffdhShelfPack(items, stripXLen);
return packShelvesIntoStrip(shelves, stripXLen);
}
function nextFit1D(items, binSize) {
let used = 0;
let binId = 0;
let output = [];
for(let i=0; i < items.length; ++i) {
if(used + items[i].size <= binSize) {
output[items[i].id] = [binId, used];
used += items[i].size;
}
else {
output[items[i].id] = [++binId, 0];
used = items[i].size;
}
}
fillEmptySlotsWithNulls(output);
return output;
}
function firstFit1D(items, binSize) {
let usage = [];
let output = [];
for(let i=0; i < items.length; ++i) {
let itemSize = items[i].size;
let packed = false;
for(let j=0; j < usage.length; ++j) {
if(usage[j] + itemSize <= binSize) {
output[items[i].id] = [j, usage[j]];
usage[j] += itemSize;
packed = true;
break;
}
}
if(!packed) {
output[items[i].id] = [usage.length, 0];
usage.push(itemSize);
}
}
fillEmptySlotsWithNulls(output);
return output;
}
function shelfBinPack(items, shelfAlgo, bpAlgo, binXLen, binYLen) {
let shelves = shelfAlgo(items, binXLen);
let shelfBPOutput = bpAlgo(shelves, binYLen);
let output = [];
for(let i=0; i < shelves.length; ++i) {
let items = shelves[i].items;
let xAgg = 0;
let [binId, y] = shelfBPOutput[i];
for(let j=0; j < items.length; ++j) {
output[items[j].id] = [binId, xAgg, y];
xAgg += items[j].xLen;
}
}
fillEmptySlotsWithNulls(output);
return output;
}
function ffdhFfBinPack(items, binXLen, binYLen) {
return shelfBinPack(items, ffdhShelfPack, firstFit1D, binXLen, binYLen);
}
rawSimplePackers.set('ffdh-ff', ffdhFfBinPack);
function nfdhBinPack(items, binXLen, binYLen) {
return shelfBinPack(items, nfdhShelfPack, nextFit1D, binXLen, binYLen);
}
rawSimplePackers.set('nfdh', nfdhBinPack);
function ffdhNfBinPack(items, binXLen, binYLen) {
return shelfBinPack(items, ffdhShelfPack, nextFit1D, binXLen, binYLen);
}
rawSimplePackers.set('ffdh-nf', ffdhNfBinPack);
//==[ Packing lower-bounds ]====================================================
function dff1(x, xMax) {
// identity function
return x / xMax;
}
function dff2(x, xMax) {
// step at 0.5
const x2 = x * 2;
if(x2 === xMax) {
return 0.5;
}
else if(x2 < xMax) {
return 0;
}
else {
return 1;
}
}
function dff3a(x, xMax) {
// step at 1/3 and 2/3
let x3 = x * 3;
if(x3 <= xMax) {
return 0;
}
else if(x3 < 2 * xMax) {
return 0.5;
}
else {
return 1;
}
}
function dff3b(x, xMax) {
// step at 1/3 and 2/3
let xRel = x / xMax;
let x3 = x * 3;
if(x3 <= xMax) {
return 0;
}
else if(x3 < 2 * xMax) {
return 3 * xRel - 1;
}
else {
return 1;
}
}
function dffkGen(k) {
function dffk(x, xMax) {
// step at 1/k, 2/k, ..., (k-1)/k.
const kx = k * x;
for(let i=1; i < k; ++i) {
if(kx <= i * xMax) {
return (i-1) / (k-1);
}
}
return 1;
}
return dffk;
}
var dffMap = new Map([
['id', dff1],
['f2', dff2],
['f3a', dff3a],
['f3b', dff3b],
['f4', dffkGen(4)],
['f5', dffkGen(5)],
]);
function getDffAgg(items, binXLen, binYLen, rotation) {
let names = [];
for(let name1 of dffMap.keys()) {
if(rotation) {
names.push('dffpair(' + name1 + ', ' + name1 + ')');
}
else {
for(let name2 of dffMap.keys()) {
names.push('dffpair(' + name1 + ', ' + name2 + ')');
}
}
}
let cumAreas = new Array(names.length).fill(0);
for(let i=0; i < items.length; ++i) {
let [xLen, yLen] = [items[i].xLen, items[i].yLen];
let xs = [], ys = [];
for(let f of dffMap.values()) {
xs.push(f(xLen, binXLen));
ys.push(f(yLen, binYLen));
}
let areas = [];
if(rotation) {
for(let j=0; j < xs.length; ++j) {
areas.push(xs[j] * ys[j]);
}
}
else {
for(let x of xs) {
for(let y of ys) {
areas.push(x * y);
}
}
}
console.assert(areas.length === cumAreas.length, 'areas.length != cumAreas.length');
for(let j=0; j < cumAreas.length; ++j) {
cumAreas[j] += areas[j];
}
}
let pairs = [];
for(let j=0; j < names.length; ++j) {
pairs.push([names[j], cumAreas[j]]);
}
return new Map(pairs);
}
function bpLowerBound(items, binXLen, binYLen, rotation) {
const delta = 0.00000001;
let cumAreas = getDffAgg(items, binXLen, binYLen, rotation);
let lb = 0;
let reason = null;
for(let [name, cumArea] of cumAreas.entries()) {
let lbj = Math.ceil(cumArea - delta);
if(lbj > lb) {
lb = lbj;
reason = name;
}
}
return [lb, reason];
}
//==[ Brute-force guillotine-packing ]==========================================
var gTreeCollCache = new Map();
let MaskType = BigInt;
let maskConst0 = MaskType(0);
let maskConst1 = MaskType(1);
function setBigIntAsMaskType(yes=true) {
if(yes) {
MaskType = BigInt;
maskConst0 = 0n;
maskConst1 = 1n;
}
else {
MaskType = Number;
maskConst0 = 0;
maskConst1 = 1;
}
}
class GTree {
/* A guillotine tree of d-dimensional cuboids.
* mask: MaskType representing the set of items in the tree
* lens: lengths of the bounding box of the packing.
* cutDim: the dimension perpendicular to which we cut. null for leaves.
* children: the subtrees obtained by cutting. [] for leaves.
* itemIndex: index of the item at a leaf node. null for non-leaves.
*/
constructor(mask, lens, cutDim, children, depth, itemIndex) {
this.mask = mask;
this.lens = lens;
this.cutDim = cutDim;
this.children = children;
this.depth = depth;
this.itemIndex = itemIndex;
}
hash() {
return this.mask.toString(36) + ';' + this.lens.join(',');
}
}
function listAllLE(l1, l2) {
// returns true iff for all i, l1[i] <= l2[i]
for(let i=0; i < l1.length && i < l2.length; ++i) {
if(l1[i] > l2[i]) {
return false;
}
}
return true;
}
function gTreeFromItem(itemIndex, itemLens) {
return new GTree(maskConst1 << MaskType(itemIndex), itemLens, null, [], 0, itemIndex);
}
function concatGTrees(gTrees, cutDim) {
if(gTrees.length <= 1) {
console.warn('concatGTrees called with only ' + gTrees.length + ' trees.');
}
const d = gTrees[0].lens.length;
let newMask = maskConst0;
let maxDepth = 0;
let lens = new Array(d).fill(0);
for(let gTree of gTrees) {
newMask |= gTree.mask;
for(let i=0; i<d; ++i) {
if(i != cutDim) {
lens[i] = Math.max(lens[i], gTree.lens[i]);
}
}
maxDepth = Math.max(maxDepth, gTree.depth);
lens[cutDim] += gTree.lens[cutDim];
}
return new GTree(newMask, lens, cutDim, gTrees, 1 + maxDepth, null);
}
function isWeakInferior(gTree1, gTree2) {
// returns true iff gTree1 and gTree2 have the same items but
// gTree1 takes more space than gTree2.
if(gTree1.mask !== gTree2.mask) {
return false;
}
return listAllLE(gTree2.lens, gTree1.lens);
}
function isStrongInferior(gTree1, gTree2) {
// returns true iff gTree1 has a subset of gTree2's items but takes more space than gTree2.
if((gTree1.mask & gTree2.mask) !== gTree1.mask) {
return false;
}
return listAllLE(gTree2.lens, gTree1.lens);
}
class GTreeCollection {
constructor(inferiority=null) {
this.inferiority = inferiority;
this.map = new Map();
}
size() {
return this.map.size;
}
add(gTree) {
let hash = gTree.hash();
if(this.map.has(hash)) {
return false;
}
if(this.inferiority !== null) {
let keysToDelete = new Set();
for(let [hash2, gTree2] of this.map) {
if(this.inferiority(gTree, gTree2)) {
return false;
}
else if(this.inferiority(gTree2, gTree)) {
keysToDelete.add(hash2);
}
}
for(const key of keysToDelete) {
this.map.delete(key);
}
}
this.map.set(hash, gTree);
return true;
}
asArray() {
return [...this.map.values()];
}
getTreesWithBestMask(f, minVal=0) {
let output = [];
let maxVal = minVal;
for(let gTree of this.map.values()) {
const val = f(gTree.mask);
if(val > maxVal) {
maxVal = val;
output = [gTree];
}
else if(val === maxVal) {
output.push(gTree);
}
}
return [maxVal, output];
}
*genUPairs() {
for(let [hash1, gTree1] of this.map.entries()) {
for(let [hash2, gTree2] of this.map.entries()) {
if(hash1 < hash2) {
yield [gTree1, gTree2];
}
}
}
}
}
function getInitialColl(itemLensList, binLens) {
let gTreeColl = new GTreeCollection(isWeakInferior);
// let gTreeColl = new GTreeCollection(isStrongInferior);
for(let i=0; i < itemLensList.length; ++i) {
if(listAllLE(itemLensList[i], binLens)) {
let gTree = gTreeFromItem(i, itemLensList[i]);
gTreeColl.add(gTree, false);
}
}
return gTreeColl;
}
function improveColl(gTreeColl, binLens, cutDim) {
let newGTrees = [];
for(let [gTree1, gTree2] of gTreeColl.genUPairs()) {
const intersection = gTree1.mask & gTree2.mask;
const fits = (gTree1.lens[cutDim] + gTree2.lens[cutDim] <= binLens[cutDim]);
if(intersection === maskConst0 && fits) {
// if(intersection !== gTree1.mask && intersection !== gTree2.mask && fits) {
newGTrees.push(concatGTrees([gTree1, gTree2], cutDim));
}
}
let improvement = false;
for(let gTree of newGTrees) {
if(gTreeColl.add(gTree)) {
improvement = true;
}
}
return improvement;
}
function enumGTrees(itemLensList, binLens) {
setBigIntAsMaskType(itemLensList.length > 30);
let gTreeColl = getInitialColl(itemLensList, binLens);
if(gTreeColl.size() === 0) {
return gTreeColl;
}
const d = itemLensList[0].length;
let cutDim = 0;
let failCount = 0;
while(failCount < d) {
const improvement = improveColl(gTreeColl, binLens, cutDim);
if(improvement) {failCount = 0;}
else {failCount += 1;}
cutDim = (cutDim + 1) % d;
}
return gTreeColl;
}
function enumGTreesWithCaching(itemLensList, binLens) {
const key = JSON.stringify([itemLensList, binLens]);
let gTreeColl = gTreeCollCache.get(key);
if(gTreeColl === undefined) {
gTreeColl = enumGTrees(itemLensList, binLens);
gTreeCollCache.set(key, gTreeColl);
}
return gTreeColl;
}
function gTreeToPackingHelper(gTree, items, binId, position, output) {
if(gTree.itemIndex !== null) {
output[items[gTree.itemIndex].id] = [binId].concat(position);
}
else {
let position2 = position.slice();
for(let child of gTree.children) {
gTreeToPackingHelper(child, items, binId, position2, output);
position2[gTree.cutDim] += child.lens[gTree.cutDim];
}
}
}
function gTreeToPacking(gTree, items, binId) {
let output = [];
gTreeToPackingHelper(gTree, items, binId, [0, 0], output);
fillEmptySlotsWithNulls(output);
return output;
}
function _enumGuillKSTrees(items, binXLen, binYLen) {
let itemLensList = [];
let totalProfit = 0;
for(let item of items) {
itemLensList.push([item.xLen, item.yLen]);
totalProfit += item.profit;
}
if(totalProfit <= 0) {
return [0, []];
}
let gTreeColl = enumGTreesWithCaching(itemLensList, [binXLen, binYLen]);
function maskToProfit(mask, output=0) {
for(let i=0; mask; ++i) {
if(mask & maskConst1) {
output += items[i].profit;
}
mask >>= maskConst1;
}
return output;
}
let [maxProfit, gTrees] = gTreeColl.getTreesWithBestMask(maskToProfit);
return [maxProfit, gTrees];
}
function enumGuillKSSols(items, binXLen, binYLen) {
let [maxProfit, gTrees] = _enumGuillKSTrees(items, binXLen, binYLen);
let packings = new Map();
for(let gTree of gTrees) {
let packing = gTreeToPacking(gTree, items, 0);
let key = gTree.mask.toString(16);
packings.set(key, packing);
}
return {'maxProfit': maxProfit, 'deterministic': true, 'packings': packings};
}
//==[ Main ]====================================================================
createRawMirrors(['ffdh-ff', 'nfdh', 'ffdh-nf']);
rawSimplePackers.set('opt-guill-ks', enumGuillKSSols);
cookSimplePackers();
// @license-end