-
Notifications
You must be signed in to change notification settings - Fork 1
/
01.py
611 lines (523 loc) · 20.5 KB
/
01.py
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
import numpy as np
import argparse
import pickle
import random
import time
import os
import copy
class Graph:
def __init__(self, num_con, num_var, edges, node_features, edge_features, obj_sense):
self.num_con = num_con
self.num_var = num_var
self.edges = edges
self.node_features = node_features
self.edge_features = edge_features
self.obj_sense = obj_sense
def generate_IS(N, M):
'''
Function Description:
Generate instances of the maximum independent set problem in a general graph.
Parameters:
- N: Number of vertices in the graph.
- M: Number of edges in the graph.
Return:
Relevant parameters of the generated maximum independent set problem.
'''
# n represents the number of decision variables, where each vertex in the graph corresponds to a decision variable.
# m represents the number of constraints, where each edge in the graph corresponds to a constraint.
# k[i] represents the number of decision variables in the i-th constraint.
n = N
m = M
k = []
# site[i][j] represents which decision variable the j-th decision variable corresponds to in the i-th constraint.
# value[i][j] represents the coefficient of the j-th decision variable in the i-th constraint.
# constraint[i] represents the right-hand side value of the i-th constraint.
# constraint_type[i] represents the type of the i-th constraint, where 1 represents <=, 2 represents >=, and 3 represents =.
# coefficient[i] represents the coefficient of the i-th decision variable in the objective function.
site = []
value = []
for i in range(m):
site.append([])
value.append([])
k.append(0)
constraint = np.zeros(m)
constraint_type = np.zeros(m)
coefficient = {}
# Add constraint: randomly generate an edge and impose a constraint that the vertices connected by the edge cannot be selected simultaneously.
for i in range(M):
x = random.randint(0, N - 1)
y = random.randint(0, N - 1)
while(x == y) :
x = random.randint(0, N - 1)
y = random.randint(0, N - 1)
site[i].append(x)
value[i].append(1)
site[i].append(y)
value[i].append(1)
constraint[i] = 1
constraint_type[i] = 1
k[i] = 2
# Set the coefficients of the objective function, where the coefficient value of each decision variable corresponding to a vertex is a random value.
for i in range(N):
coefficient[i] = random.random()
node_features = []
edges = []
edge_features = []
for i in range(m):
if constraint_type[i] == 1:
tmp = copy.deepcopy([0, 0, 0, 1, 0, 0, 0, 0])
elif constraint_type[i] == 2:
tmp = copy.deepcopy([0, 0, 0, 0, 0, 1, 0, 0])
else:
tmp = copy.deepcopy([0, 0, 0, 0, 1, 0, 0, 0])
tmp[6] = constraint[i]
node_features.append(tmp)
for i in range(n):
tmp = copy.deepcopy([0, 0, 1, 0, 0, 0, 0, 0])
tmp[7] = coefficient[i]
node_features.append(tmp)
for i in range(m):
for j in range(k[i]):
edges.append((i, site[i][j] + m))
edge_features.append(value[i][j])
obj_sense = 1
graph = Graph(num_con=m,
num_var=n,
edges=edges,
node_features=node_features,
edge_features=edge_features,
obj_sense=obj_sense)
return graph
def generate_MVC(N, M):
'''
Function Description:
Generate instances of the minimum vertex cover problem in a general graph.
Parameters:
- N: Number of vertices in the graph.
- M: Number of edges in the graph.
Return:
Relevant parameters of the generated minimum vertex cover problem.
'''
# n represents the number of decision variables, where each vertex in the graph corresponds to a decision variable.
# m represents the number of constraints, where each edge in the graph corresponds to a constraint.
# k[i] represents the number of decision variables in the i-th constraint.
n = N
m = M
k = []
# site[i][j] represents which decision variable the j-th decision variable corresponds to in the i-th constraint.
# value[i][j] represents the coefficient of the j-th decision variable in the i-th constraint.
# constraint[i] represents the right-hand side value of the i-th constraint.
# constraint_type[i] represents the type of the i-th constraint, where 1 represents <=, 2 represents >=, and 3 represents =.
# coefficient[i] represents the coefficient of the i-th decision variable in the objective function.
site = []
value = []
for i in range(m):
site.append([])
value.append([])
k.append(0)
constraint = np.zeros(m)
constraint_type = np.zeros(m)
coefficient = {}
# Add constraint: randomly generate an edge and impose a constraint that at least one of the vertices connected by the edge must be selected.
for i in range(M):
x = random.randint(0, N - 1)
y = random.randint(0, N - 1)
while(x == y) :
x = random.randint(0, N - 1)
y = random.randint(0, N - 1)
k[i] = 2
site[i].append(x)
value[i].append(1)
site[i].append(y)
value[i].append(1)
constraint[i] = 1
constraint_type[i] = 2
# Set the coefficients of the objective function, where the coefficient value of each decision variable corresponding to a vertex is a random value.
for i in range(N):
coefficient[i] = random.random()
node_features = []
edges = []
edge_features = []
for i in range(m):
if constraint_type[i] == 1:
tmp = copy.deepcopy([0, 0, 0, 1, 0, 0, 0, 0])
elif constraint_type[i] == 2:
tmp = copy.deepcopy([0, 0, 0, 0, 0, 1, 0, 0])
else:
tmp = copy.deepcopy([0, 0, 0, 0, 1, 0, 0, 0])
tmp[6] = constraint[i]
node_features.append(tmp)
for i in range(n):
tmp = copy.deepcopy([0, 0, 1, 0, 0, 0, 0, 0])
tmp[7] = coefficient[i]
node_features.append(tmp)
for i in range(m):
for j in range(k[i]):
edges.append((i, site[i][j] + m))
edge_features.append(value[i][j])
obj_sense = 0
graph = Graph(num_con=m,
num_var=n,
edges=edges,
node_features=node_features,
edge_features=edge_features,
obj_sense=obj_sense)
return graph
def generate_SC(N, M):
'''
Function Description:
Generate instances of the set cover problem, where each item is guaranteed to appear in exactly 3 sets.
Parameters:
- N: Number of sets.
- M: Number of items.
Return:
Relevant parameters of the generated set cover problem.
'''
# n represents the number of decision variables, where each set corresponds to a decision variable.
# m represents the number of constraints, where each item corresponds to a constraint.
# k[i] represents the number of decision variables in the i-th constraint.
n = N
m = M
k = []
# site[i][j] represents which decision variable the j-th decision variable corresponds to in the i-th constraint.
# value[i][j] represents the coefficient of the j-th decision variable in the i-th constraint.
# constraint[i] represents the right-hand side value of the i-th constraint.
# constraint_type[i] represents the type of the i-th constraint, where 1 represents <=, 2 represents >=, and 3 represents =.
# coefficient[i] represents the coefficient of the i-th decision variable in the objective function.
site = []
value = []
for i in range(m):
site.append([])
value.append([])
k.append(0)
constraint = np.zeros(m)
constraint_type = np.zeros(m)
coefficient = {}
# Add constraint: At least one of the four sets in which each item appears must be selected.
for i in range(M):
vis = {}
for j in range(3):
now = random.randint(0, N - 1)
while(now in vis.keys()):
now = random.randint(0, N - 1)
vis[now] = 1
site[i].append(now)
value[i].append(1)
k[i] = 3
for i in range(M):
constraint[i] = 1
constraint_type[i] = 2
# Set the coefficients of the objective function, where the coefficient value of each decision variable corresponding to a set is a random value.
for i in range(N):
coefficient[i] = random.random()
node_features = []
edges = []
edge_features = []
for i in range(m):
if constraint_type[i] == 1:
tmp = copy.deepcopy([0, 0, 0, 1, 0, 0, 0, 0])
elif constraint_type[i] == 2:
tmp = copy.deepcopy([0, 0, 0, 0, 0, 1, 0, 0])
else:
tmp = copy.deepcopy([0, 0, 0, 0, 1, 0, 0, 0])
tmp[6] = constraint[i]
node_features.append(tmp)
for i in range(n):
tmp = copy.deepcopy([0, 0, 1, 0, 0, 0, 0, 0])
tmp[7] = coefficient[i]
node_features.append(tmp)
for i in range(m):
for j in range(k[i]):
edges.append((i, site[i][j] + m))
edge_features.append(value[i][j])
obj_sense = 0
graph = Graph(num_con=m,
num_var=n,
edges=edges,
node_features=node_features,
edge_features=edge_features,
obj_sense=obj_sense)
return graph
def generate_MAXCUT(N, M):
'''
函数说明:
生成一般图当中最大割的问题实例。
参数说明:
- N: 图的点数。
- M: 图的边数。
'''
n = N + N * N
m = 2 * M
k = []
#site[i][j]表示第i个约束的第j个决策变量是哪个决策变量
#value[i][j]表示第i个约束的第j个决策变量的系数
#constraint[i]表示第i个约束右侧的数
#constraint_type[i]表示第i个约束的类型,1表示<=,2表示>=
#coefficient[i]表示第i个决策变量在目标函数中的系数
site = []
value = []
for i in range(m):
site.append([])
value.append([])
k.append(0)
constraint = np.zeros(m)
constraint_type = np.zeros(m)
coefficient = {}
#先将问题转换为将点染成01,一条边连接的两个点若为一个0一个1,则这条边必须选取。
#添加约束,每次随机生成一条边,设定边的目标函数系数为随机,添加约束使得:
#1.一条边连接的两个点若为一个0一个1,则这条边必须选取(为1);
#2.一条边连接的两个点若为两个0,或两个1,则这条边必不选取(为0)。
for i in range(M):
x = random.randint(0, N - 1)
y = random.randint(0, N - 1)
while(x == y) :
x = random.randint(0, N - 1)
y = random.randint(0, N - 1)
site[i * 2].append(N + x * N + y)
value[i * 2].append(1)
site[i * 2].append(x)
value[i * 2].append(-1)
site[i * 2].append(y)
value[i * 2].append(-1)
constraint[i * 2] = 0
constraint_type[i * 2] = 1
k[i * 2] = 3
site[i * 2 + 1].append(N + x * N + y)
value[i * 2 + 1].append(1)
site[i * 2 + 1].append(x)
value[i * 2 + 1].append(1)
site[i * 2 + 1].append(y)
value[i * 2 + 1].append(1)
constraint[i * 2 + 1] = 2
constraint_type[i * 2 + 1] = 1
k[i * 2 + 1] = 3
if(not(N + x * N + y in coefficient)):
coefficient[N + x * N + y] = 0
coefficient[N + x * N + y] += random.random()
node_features = []
edges = []
edge_features = []
for i in range(m):
if constraint_type[i] == 1:
tmp = copy.deepcopy([0, 0, 0, 1, 0, 0, 0, 0])
elif constraint_type[i] == 2:
tmp = copy.deepcopy([0, 0, 0, 0, 0, 1, 0, 0])
else:
tmp = copy.deepcopy([0, 0, 0, 0, 1, 0, 0, 0])
tmp[6] = constraint[i]
node_features.append(tmp)
for i in range(n):
tmp = copy.deepcopy([0, 0, 1, 0, 0, 0, 0, 0])
if i in coefficient.keys():
tmp[7] = coefficient[i]
else:
tmp[7] = 0
node_features.append(tmp)
for i in range(m):
for j in range(k[i]):
edges.append((i, site[i][j] + m))
edge_features.append(value[i][j])
obj_sense = 1
graph = Graph(num_con=m,
num_var=n,
edges=edges,
node_features=node_features,
edge_features=edge_features,
obj_sense=obj_sense)
return graph
def generate_CAT(N, M):
'''
Function Description:
Generate instances of the set cover problem, where each item is guaranteed to appear in exactly 3 sets.
Parameters:
- N: Number of sets.
- M: Number of items.
Return:
Relevant parameters of the generated set cover problem.
'''
# n represents the number of decision variables, where each set corresponds to a decision variable.
# m represents the number of constraints, where each item corresponds to a constraint.
# k[i] represents the number of decision variables in the i-th constraint.
n = N
m = M
k = []
# site[i][j] represents which decision variable the j-th decision variable corresponds to in the i-th constraint.
# value[i][j] represents the coefficient of the j-th decision variable in the i-th constraint.
# constraint[i] represents the right-hand side value of the i-th constraint.
# constraint_type[i] represents the type of the i-th constraint, where 1 represents <=, 2 represents >=, and 3 represents =.
# coefficient[i] represents the coefficient of the i-th decision variable in the objective function.
site = []
value = []
for i in range(m):
site.append([])
value.append([])
k.append(0)
constraint = np.zeros(m)
constraint_type = np.zeros(m)
coefficient = {}
# Add constraints.
for i in range(M):
vis = {}
for j in range(3):
now = random.randint(0, N - 1)
while(now in vis.keys()):
now = random.randint(0, N - 1)
vis[now] = 1
site[i].append(now)
value[i].append(1)
k[i] = 3
for i in range(M):
constraint[i] = 1
constraint_type[i] = 1
# Set the coefficients of the objective function, where the coefficient value of each decision variable corresponding to a set is a random value.
for i in range(N):
coefficient[i] = random.random() * 1000
node_features = []
edges = []
edge_features = []
for i in range(m):
if constraint_type[i] == 1:
tmp = copy.deepcopy([0, 0, 0, 1, 0, 0, 0, 0])
elif constraint_type[i] == 2:
tmp = copy.deepcopy([0, 0, 0, 0, 0, 1, 0, 0])
else:
tmp = copy.deepcopy([0, 0, 0, 0, 1, 0, 0, 0])
tmp[6] = constraint[i]
node_features.append(tmp)
for i in range(n):
tmp = copy.deepcopy([0, 0, 1, 0, 0, 0, 0, 0])
tmp[7] = coefficient[i]
node_features.append(tmp)
for i in range(m):
for j in range(k[i]):
edges.append((i, site[i][j] + m))
edge_features.append(value[i][j])
obj_sense = 1
graph = Graph(num_con=m,
num_var=n,
edges=edges,
node_features=node_features,
edge_features=edge_features,
obj_sense=obj_sense)
return graph
def generate_samples(
problem_type : str,
difficulty_mode : str,
seed : int,
number : int,
output_dir : str
):
'''
Function Description:
Generate problem instances based on the provided parameters and package the output as data.pickle.
Parameters:
- problem_type: Available options are ['IS', 'MVC', 'MAXCUT', 'SC'], representing the maximum independent set problem, minimum vertex cover problem, maximum cut problem, minimum set cover problem, and Meituan flash sale problem, respectively.
- difficulty_mode: Available options are ['easy', 'medium', 'hard'], representing easy (small-scale), medium (medium-scale), and hard (large-scale) difficulties.
- seed: Integer value indicating the starting random seed used for problem generation.
- number: Integer value indicating the number of instances to generate.
Return:
The problem instances are generated and packaged as data.pickle. The function does not have a return value.
'''
# Set the random seed.
random.seed(seed)
# Check and create using the os module.
dir_name = 'example'
if not os.path.exists(dir_name):
os.mkdir(dir_name)
for i in range(number):
# Randomly generate instances of the maximum independent set problem and package the output.
if(problem_type == 'IS'):
if(difficulty_mode == 'tiny'):
N = 2500
M = 7500
elif(difficulty_mode == 'easy'):
N = 10000
M = 30000
elif(difficulty_mode == 'medium'):
N = 100000
M = 300000
else:
N = 1000000
M = 3000000
graph = generate_IS(N, M)
with open(output_dir + '/IS_' + str(i), 'wb') as f:
pickle.dump(graph, f)
# Randomly generate instances of the minimum vertex cover problem and package the output.
if(problem_type == 'MVC'):
if(difficulty_mode == 'tiny'):
N = 2500
M = 7500
elif(difficulty_mode == 'easy'):
N = 10000
M = 30000
elif(difficulty_mode == 'medium'):
N = 100000
M = 300000
else:
N = 1000000
M = 3000000
graph = generate_MVC(N, M)
with open(output_dir + '/MVC_' + str(i), 'wb') as f:
pickle.dump(graph, f)
# Randomly generate instances of the minimum set cover problem and package the output.
if(problem_type == 'SC'):
if(difficulty_mode == 'tiny'):
N = 2500
M = 7500
elif(difficulty_mode == 'easy'):
N = 10000
M = 30000
elif(difficulty_mode == 'medium'):
N = 100000
M = 300000
else:
N = 1000000
M = 3000000
graph = generate_SC(N, M)
with open(output_dir + '/SC_' + str(i), 'wb') as f:
pickle.dump(graph, f)
# Randomly generate instances of the combinatorial auction problem and package the output.
if(problem_type == 'CAT'):
if(difficulty_mode == 'tiny'):
N = 2500
M = 5000
elif(difficulty_mode == 'easy'):
N = 10000
M = 20000
elif(difficulty_mode == 'medium'):
N = 100000
M = 200000
else:
N = 1000000
M = 2000000
graph = generate_CAT(N, M)
with open(output_dir + '/CAT_' + str(i), 'wb') as f:
pickle.dump(graph, f)
if(problem_type == 'MAXCUT'):
if(difficulty_mode == 'tiny'):
N = 50
M = 750
elif(difficulty_mode == 'easy'):
N = 100
M = 1250
elif(difficulty_mode == 'medium'):
N = 250
M = 25000
else:
N = 600
M = 150000
graph = generate_MAXCUT(N, M)
with open(output_dir + '/MAXCUT_' + str(i), 'wb') as f:
pickle.dump(graph, f)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--problem_type", choices = ['IS', 'MVC', 'SC', 'CAT', 'MAXCUT'], default = 'SC', help = "Problem type selection")
parser.add_argument("--difficulty_mode", choices = ['tiny', 'easy', 'medium', 'hard'], default = 'easy', help = "Difficulty level.")
parser.add_argument('--seed', type = int, default = 0, help = 'Random generator seed.')
parser.add_argument("--number", type = int, default = 10, help = 'The number of instances.')
parser.add_argument("--output_dir", type = str, default = "bipartite_graph/4type_problem")
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
#print(vars(args))
generate_samples(**vars(args))