-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcommon_utils.py
214 lines (166 loc) · 6.05 KB
/
common_utils.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
import json
import random
from torch.distributions.categorical import Categorical
import sys
import numpy as np
import torch
import copy
"""
agent utils
"""
def sample_action(p):
"""
sample an action by the distribution p
:param p: this distribution with the probability of choosing each action
:return: an action sampled by p
"""
dist = Categorical(p)
s = dist.sample() # index
return s, dist.log_prob(s)
def eval_actions(p, actions):
"""
:param p: the policy
:param actions: action sequences
:return: the log probability of actions and the entropy of p
"""
softmax_dist = Categorical(p.squeeze())
ret = softmax_dist.log_prob(actions).reshape(-1)
entropy = softmax_dist.entropy().mean()
return ret, entropy
def greedy_select_action(p):
_, index = torch.max(p, dim=1)
return index
def min_element_index(array):
"""
:param array: an array with numbers
:return: Index set corresponding to the minimum element of the array
"""
min_element = np.min(array)
candidate = np.where(array == min_element)
return candidate
def max_element_index(array):
"""
:param array: an array with numbers
:return: Index set corresponding to the maximum element of the array
"""
max_element = np.max(array)
candidate = np.where(array == max_element)
return candidate
def available_mch_list_for_job(chosen_job, env):
"""
:param chosen_job: the selected job
:param env: the production environment
:return: the machines which can immediately process the chosen job
"""
mch_state = ~env.candidate_process_relation[0, chosen_job]
available_mch_list = np.where(mch_state == True)[0]
mch_free_time = env.mch_free_time[0][available_mch_list]
job_free_time = env.candidate_free_time[0][chosen_job]
# case1 eg:
# JF: 50
# MchF: 55 60 65 70
if (job_free_time < mch_free_time).all():
chosen_mch_list = available_mch_list[min_element_index(mch_free_time)]
# case2 eg:
# JF: 50
# MchF: 35 40 55 60
else:
chosen_mch_list = available_mch_list[np.where(mch_free_time <= job_free_time)]
return chosen_mch_list
def heuristic_select_action(method, env):
"""
:param method: the name of heuristic method
:param env: the environment
:return: the action selected by the heuristic method
here are heuristic methods selected for comparison:
FIFO: First in first out
MOR(or MOPNR): Most operations remaining
SPT: Shortest processing time
MWKR: Most work remaining
"""
chosen_job = -1
chosen_mch = -1
job_state = (env.mask[0] == 0)
process_job_state = (env.candidate_free_time[0] <= env.next_schedule_time[0])
job_state = process_job_state & job_state
available_jobs = np.where(job_state == True)[0]
available_ops = env.candidate[0][available_jobs]
if method == 'FIFO':
# selecting the earliest ready candidate operation
candidate_free_time = env.candidate_free_time[0][available_jobs]
chosen_job_list = available_jobs[min_element_index(candidate_free_time)]
chosen_job = np.random.choice(chosen_job_list)
# select the earliest ready machine
mch_state = ~env.candidate_process_relation[0, chosen_job]
available_mchs = np.where(mch_state == True)[0]
mch_free_time = env.mch_free_time[0][available_mchs]
chosen_mch_list = available_mchs[min_element_index(mch_free_time)]
chosen_mch = np.random.choice(chosen_mch_list)
elif method == 'MOR':
remain_ops = env.op_match_job_left_op_nums[0][available_ops]
chosen_job_list = available_jobs[max_element_index(remain_ops)]
chosen_job = np.random.choice(chosen_job_list)
# select a machine which can immediately process the chosen job
chosen_mch_list = available_mch_list_for_job(chosen_job, env)
chosen_mch = np.random.choice(chosen_mch_list)
elif method == 'SPT':
temp_pt = copy.deepcopy(env.candidate_pt[0])
temp_pt[env.dynamic_pair_mask[0]] = float("inf")
pt_list = temp_pt.reshape(-1)
action_list = np.where(pt_list == np.min(pt_list))[0]
action = np.random.choice(action_list)
return action
elif method == 'MWKR':
job_remain_work_list = env.op_match_job_remain_work[0][available_ops]
chosen_job = available_jobs[np.random.choice(max_element_index(job_remain_work_list)[0])]
# select a machine which can immediately process the chosen job
chosen_mch_list = available_mch_list_for_job(chosen_job, env)
chosen_mch = np.random.choice(chosen_mch_list)
else:
print(f'Error From rule select: undefined method {method}')
sys.exit()
if chosen_job == -1 or chosen_mch == -1:
print(f'Error From choosing action: choose job {chosen_job}, mch {chosen_mch}')
sys.exit()
action = chosen_job * env.number_of_machines + chosen_mch
return action
"""
common utils
"""
def save_default_params(config):
"""
save parameters in the config
:param config: a package of parameters
:return:
"""
with open('./config_default.json', 'wt') as f:
json.dump(vars(config), f, indent=4)
print("successfully save default params")
def nonzero_averaging(x):
"""
remove zero vectors and then compute the mean of x
(The deleted nodes are represented by zero vectors)
:param x: feature vectors with shape [sz_b, node_num, d]
:return: the desired mean value with shape [sz_b, d]
"""
b = x.sum(dim=-2)
y = torch.count_nonzero(x, dim=-1)
z = (y != 0).sum(dim=-1, keepdim=True)
p = 1 / z
p[z == 0] = 0
return torch.mul(p, b)
def strToSuffix(str):
if str == '':
return str
else:
return '+' + str
def setup_seed(seed):
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
if __name__ == '__main__':
print('123')