-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathins_generation.py
More file actions
57 lines (41 loc) · 1.68 KB
/
ins_generation.py
File metadata and controls
57 lines (41 loc) · 1.68 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
import pyscipopt as scip
import numpy as np
import random
import os
def gen_bin_packing_ins(capacity=100,largeRatio=0.3,largeLB=0.8,largeUB=1.0,smallLB=0.1,smallUB=0.3,nItems=10):
largeItemTypes = list(range(int(largeLB*capacity)+1,int(largeUB*capacity)+1))
smallItemTypes = list(range(int(smallLB*capacity)+1,int(smallUB*capacity)+1))
nBins = nItems
nLargeItem = int(nItems*largeRatio)
largeItems = np.random.choice(largeItemTypes,size=nLargeItem,replace=False)
smallItems = np.random.choice(smallItemTypes,size=nItems - nLargeItem,replace=False)
items = np.concatenate([largeItems,smallItems],axis=0)
np.random.shuffle(items)
m = scip.Model()
# set variables
x = np.zeros((nItems,nBins)).astype(object)
y = np.zeros(nBins).astype(object)
for j in range(nBins):
y[j] = m.addVar(f'y_{j}','B')
for i in range(nItems):
x[i,j] = m.addVar(f'x_{i}_{j}','B')
# add constraints
for i in range(nItems):
m.addCons( x[i,:].sum() == 1 )
for j in range(nBins):
m.addCons( (x[:,j]*items).sum() <= capacity * y[j] )
# set objective
m.setObjective(y.sum())
return m
if __name__ == '__main__':
insDir = './data/BPP/train/instances'
nIns = 500
os.makedirs(insDir,exist_ok=True)
for i in range(nIns):
m = gen_bin_packing_ins(capacity=100,largeRatio=0.3,largeLB=0.8,largeUB=1.0,smallLB=0.1,smallUB=0.3,nItems=20)
savepath = os.path.join(insDir,f'bin_packing_{i}.lp')
m.writeProblem(savepath)
# compress
os.system(f'gzip {savepath}')
print(f'processed {i + 1}/{nIns}')
print('done')