-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuniform_instance_gen.py
292 lines (260 loc) · 8.95 KB
/
uniform_instance_gen.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
from dataclasses import dataclass
from random import choice, randint
import numpy as np
@dataclass(frozen=True)
class DatasetConfig:
num_of_jobs: int
num_of_machines: int
highest_num_of_operations_per_job: int
lowest_num_of_operations_per_job: int
num_of_alternative_bounds: 'tuple[int, int]'
num_of_operations_to_num_of_jobs: 'dict[int, int]'
duration_bounds: 'tuple[int, int]'
def get_total_num_of_operations(self):
sum = 0
for o, j in self.num_of_operations_to_num_of_jobs.items():
sum += o * j
return sum
datasetConfigs = {
"MK01": DatasetConfig(
num_of_jobs=10,
num_of_machines=6,
duration_bounds=(1, 6),
num_of_alternative_bounds=(1, 3),
lowest_num_of_operations_per_job=5,
highest_num_of_operations_per_job=6,
num_of_operations_to_num_of_jobs={
5: 5,
6: 5
}),
"MK02": DatasetConfig(
num_of_jobs=10,
num_of_machines=6,
duration_bounds=(1, 6),
num_of_alternative_bounds=(1, 6),
lowest_num_of_operations_per_job=5,
highest_num_of_operations_per_job=6,
num_of_operations_to_num_of_jobs={
6: 8,
5: 2
}),
"MK03": DatasetConfig(
num_of_jobs=15,
num_of_machines=8,
duration_bounds=(1, 19),
num_of_alternative_bounds=(1, 5),
lowest_num_of_operations_per_job=10,
highest_num_of_operations_per_job=10,
num_of_operations_to_num_of_jobs={
10: 15
}),
"MK04": DatasetConfig(
num_of_jobs=15,
num_of_machines=8,
duration_bounds=(1, 9),
num_of_alternative_bounds=(1, 3),
lowest_num_of_operations_per_job=3,
highest_num_of_operations_per_job=9,
num_of_operations_to_num_of_jobs={
9: 2,
8: 1,
7: 2,
6: 4,
5: 3,
4: 2,
3: 1,
}),
"MK05": DatasetConfig(
num_of_jobs=15,
num_of_machines=4,
duration_bounds=(5, 9),
num_of_alternative_bounds=(1, 2),
lowest_num_of_operations_per_job=5,
highest_num_of_operations_per_job=9,
num_of_operations_to_num_of_jobs={
9: 3,
8: 2,
7: 5,
6: 3,
5: 2
}),
"MK06": DatasetConfig(
num_of_jobs=10,
num_of_machines=15,
duration_bounds=(1, 9),
num_of_alternative_bounds=(1, 5),
lowest_num_of_operations_per_job=15,
highest_num_of_operations_per_job=15,
num_of_operations_to_num_of_jobs={
15: 10
}),
"MK07": DatasetConfig(
num_of_jobs=20,
num_of_machines=5,
duration_bounds=(1, 19),
num_of_alternative_bounds=(1, 5),
lowest_num_of_operations_per_job=5,
highest_num_of_operations_per_job=5,
num_of_operations_to_num_of_jobs={
5: 15
}),
"MK08": DatasetConfig(
num_of_jobs=20,
num_of_machines=10,
duration_bounds=(5, 19),
num_of_alternative_bounds=(1, 2),
lowest_num_of_operations_per_job=10,
highest_num_of_operations_per_job=14,
num_of_operations_to_num_of_jobs={
10: 6,
11: 8,
12: 3,
13: 1,
14: 2
}),
"MK09": DatasetConfig(
num_of_jobs=20,
num_of_machines=10,
duration_bounds=(5, 19),
num_of_alternative_bounds=(1, 5),
lowest_num_of_operations_per_job=10,
highest_num_of_operations_per_job=14,
num_of_operations_to_num_of_jobs={
10: 2,
11: 7,
12: 3,
13: 5,
14: 3
}),
"MK10": DatasetConfig(
num_of_jobs=20,
num_of_machines=15,
duration_bounds=(5, 19),
num_of_alternative_bounds=(1, 5),
lowest_num_of_operations_per_job=10,
highest_num_of_operations_per_job=14,
num_of_operations_to_num_of_jobs={
10: 2,
11: 7,
12: 3,
13: 5,
14: 3,
}),
}
def uniform_instance_gen(
num_of_jobs,
num_of_machines,
lowest_num_of_operation_per_job,
highest_num_of_operation_per_job,
lowest_num_of_alternatives_per_op,
highest_num_of_alternatives_per_op,
duration_lb,
duration_ub
):
jobs = [
[
[0 for _ in range(num_of_machines)]
for _ in range(highest_num_of_operation_per_job)
] for _ in range(num_of_jobs)
]
for i in range(num_of_jobs):
num_of_operation = randint(lowest_num_of_operation_per_job, highest_num_of_operation_per_job)
for j in range(num_of_operation):
num_of_alternatives = np.random.randint(lowest_num_of_alternatives_per_op, highest_num_of_alternatives_per_op)
machine_ids = list(range(num_of_machines))
for _ in range(num_of_alternatives):
col_idx = choice(machine_ids)
machine_ids.remove(col_idx)
duration = randint(duration_lb, duration_ub)
jobs[i][j][col_idx] = duration
return np.array(jobs, dtype=np.int32)
def uniform_instance_gen_with_fixed_num_of_operations(
num_of_jobs,
num_of_machines,
highest_num_of_operation_per_job,
num_of_alternatives_bounds: 'tuple[int, int]',
num_of_operations_to_num_of_jobs: 'dict[int, int]',
durations_bounds: 'tuple[int, int]',
):
jobs = [
[
[0 for _ in range(num_of_machines)]
for _ in range(highest_num_of_operation_per_job)
] for _ in range(num_of_jobs)
]
# eg: [(5, [0, 1, 2]), (6, [3, 4, 5])]
# means Job 0, 1, 2 has duration 5 operations
num_of_operations_to_job_indexes: 'dict[int, list[int]]' = {
j: [] for j in num_of_operations_to_num_of_jobs.keys()
}
indexes = list(range(num_of_jobs))
# Assign job indexes for each number of operations
'''
1. For each number of operations, get the number of jobs
2. For the number of jobs,
'''
for num_of_ops, num_of_jobs in num_of_operations_to_num_of_jobs.items():
for _ in range(num_of_jobs):
num = choice(indexes)
indexes.remove(num)
num_of_operations_to_job_indexes[num_of_ops].append(num)
# Assign durations to the job array
'''
1. For each job, get it's number of operations
2. For number of operations, assign a random duration, and a random machine
'''
for i, v in enumerate(num_of_operations_to_job_indexes):
for job_index in num_of_operations_to_job_indexes[v]:
num_of_operations = v
for j in range(num_of_operations):
num_of_alternatives = np.random.randint(num_of_alternatives_bounds[0], num_of_alternatives_bounds[1])
machine_ids = list(range(num_of_machines))
for _ in range(num_of_alternatives):
col_idx = choice(machine_ids)
machine_ids.remove(col_idx)
duration = randint(durations_bounds[0], durations_bounds[1])
jobs[job_index][j][col_idx] = duration
return np.array(jobs, dtype=np.int32)
def write_dataset_in_brandimarte_format(
jobs_array,
lowest_num_of_alternatives,
highest_num_of_alternatives,
file_name
):
f = open(file_name, 'w')
lines = []
# for first line
num_of_jobs = jobs_array.shape[0]
num_of_machines = jobs_array.shape[2]
avg_num_of_alternatives = (lowest_num_of_alternatives + highest_num_of_alternatives) / 2
lines.append(f'{num_of_jobs} {num_of_machines} {int(avg_num_of_alternatives)}\n')
# for subsequent lines
for job in jobs_array:
num_of_ops = job.shape[0] - len(np.where(~job.any(axis=1))[0])
line = f'{num_of_ops}'
for row in job:
num_of_alternatives = np.count_nonzero(row)
line += f' {num_of_alternatives}'
indexes = np.nonzero(row)[0]
for i in indexes: line += f' {i+1} {row[i]}'
line += '\n'
lines.append(line)
f.writelines(lines)
f.close()
# generate dataset
if __name__ == '__main__':
for size, config in datasetConfigs.items():
problems = [
uniform_instance_gen(
num_of_jobs=config.num_of_jobs,
num_of_machines=config.num_of_machines,
lowest_num_of_operation_per_job=config.lowest_num_of_operations_per_job,
highest_num_of_operation_per_job=config.highest_num_of_operations_per_job,
highest_num_of_alternatives_per_op=config.num_of_alternative_bounds[1],
lowest_num_of_alternatives_per_op=config.num_of_alternative_bounds[0],
duration_ub=config.duration_bounds[1],
duration_lb=config.duration_bounds[0],
) for _ in range(12)
]
problems = np.array(problems, dtype=np.int32)
np.save(f'./validation/{size}_validation_set_4', problems)