-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.py
executable file
·86 lines (65 loc) · 2.59 KB
/
data.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
"""Everything related to data. Read and generate case.
Author
--------
Adrien Pouyet
"""
import numpy as np
import sys
def build_array(tasks_l, nb_task, nb_machine):
"""Build an array : list of (task duration, task id, machine id)."""
tasks_a = []
for i in range(nb_machine):
for j in range(nb_task):
tasks_a.append((tasks_l[i][j], j, i))
return tasks_a
def build_dic(tasks_l, nb_tasks):
"""Build a dic : key is task id, value is task duration on each machine."""
tasks_d = {}
tasks = np.array(tasks_l)
for i in range(nb_tasks):
tasks_d[i] = tasks[:, i]
return tasks_d
def read_case():
"""Read a case form sys.stdin."""
n = int(sys.stdin.readline())
tasks_l = []
tasks_l.append(list(map(int, sys.stdin.readline().split(" ")))) # Read A
tasks_l.append(list(map(int, sys.stdin.readline().split(" ")))) # Read B
tasks_l.append(list(map(int, sys.stdin.readline().split(" ")))) # Read C
return build_array(tasks_l, n, 3), build_dic(tasks_l, n), n
def random_case(nb_machine, nb_task, min_length, max_length):
"""Generate a random case."""
tasks_l = np.random.randint(min_length, max_length+1, (nb_machine, nb_task))
return build_array(tasks_l, nb_task, nb_machine), build_dic(tasks_l, nb_task)
def generate_cases(n, nb_machine, nb_task, min_length, max_length):
"""Generate n random_cases."""
for i in range(n):
yield random_case(nb_machine, nb_task, min_length, max_length)
def random_case_correle(nb_machine, nb_task):
"""Generate a random case."""
tasks_l = []
for i in range(nb_task):
r_i = np.random.randint(0, 4+1)
lo = 20*r_i
hi = 20*r_i+20
tasks_l.append(np.random.randint(lo, hi, 3))
tasks_l = np.asarray(tasks_l).T
return build_array(tasks_l, nb_task, nb_machine), build_dic(tasks_l, nb_task)
def generate_cases_correle(n, nb_machine, nb_task):
"""Generate n random_cases."""
for i in range(n):
yield random_case_correle(nb_machine, nb_task)
def random_case_correle_onmachine(nb_machine, nb_task):
"""Generate a random case."""
tasks_l = []
for i in range(nb_task):
A = np.random.randint(1, 100)
B = np.random.randint(16, 116)
C = np.random.randint(31, 131)
tasks_l.append([A, B, C])
tasks_l = np.asarray(tasks_l).T
return build_array(tasks_l, nb_task, nb_machine), build_dic(tasks_l, nb_task)
def generate_cases_correle_onmachine(n, nb_machine, nb_task):
"""Generate n random_cases."""
for i in range(n):
yield random_case_correle_onmachine(nb_machine, nb_task)