forked from zcaicaros/L2D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permissibleLS.py
164 lines (144 loc) · 6.7 KB
/
permissibleLS.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
from Params import configs
import numpy as np
def permissibleLeftShift(a, durMat, mchMat, mchsStartTimes, opIDsOnMchs):
jobRdyTime_a, mchRdyTime_a = calJobAndMchRdyTimeOfa(a, mchMat, durMat, mchsStartTimes, opIDsOnMchs)
dur_a = np.take(durMat, a)
mch_a = np.take(mchMat, a) - 1
startTimesForMchOfa = mchsStartTimes[mch_a]
opsIDsForMchOfa = opIDsOnMchs[mch_a]
flag = False
possiblePos = np.where(jobRdyTime_a < startTimesForMchOfa)[0]
# print('possiblePos:', possiblePos)
if len(possiblePos) == 0:
startTime_a = putInTheEnd(a, jobRdyTime_a, mchRdyTime_a, startTimesForMchOfa, opsIDsForMchOfa)
else:
idxLegalPos, legalPos, endTimesForPossiblePos = calLegalPos(dur_a, jobRdyTime_a, durMat, possiblePos, startTimesForMchOfa, opsIDsForMchOfa)
# print('legalPos:', legalPos)
if len(legalPos) == 0:
startTime_a = putInTheEnd(a, jobRdyTime_a, mchRdyTime_a, startTimesForMchOfa, opsIDsForMchOfa)
else:
flag = True
startTime_a = putInBetween(a, idxLegalPos, legalPos, endTimesForPossiblePos, startTimesForMchOfa, opsIDsForMchOfa)
return startTime_a, flag
def putInTheEnd(a, jobRdyTime_a, mchRdyTime_a, startTimesForMchOfa, opsIDsForMchOfa):
# index = first position of -config.high in startTimesForMchOfa
# print('Yes!OK!')
index = np.where(startTimesForMchOfa == -configs.high)[0][0]
startTime_a = max(jobRdyTime_a, mchRdyTime_a)
startTimesForMchOfa[index] = startTime_a
opsIDsForMchOfa[index] = a
return startTime_a
def calLegalPos(dur_a, jobRdyTime_a, durMat, possiblePos, startTimesForMchOfa, opsIDsForMchOfa):
startTimesOfPossiblePos = startTimesForMchOfa[possiblePos]
durOfPossiblePos = np.take(durMat, opsIDsForMchOfa[possiblePos])
startTimeEarlst = max(jobRdyTime_a, startTimesForMchOfa[possiblePos[0]-1] + np.take(durMat, [opsIDsForMchOfa[possiblePos[0]-1]]))
endTimesForPossiblePos = np.append(startTimeEarlst, (startTimesOfPossiblePos + durOfPossiblePos))[:-1]# end time for last ops don't care
possibleGaps = startTimesOfPossiblePos - endTimesForPossiblePos
idxLegalPos = np.where(dur_a <= possibleGaps)[0]
legalPos = np.take(possiblePos, idxLegalPos)
return idxLegalPos, legalPos, endTimesForPossiblePos
def putInBetween(a, idxLegalPos, legalPos, endTimesForPossiblePos, startTimesForMchOfa, opsIDsForMchOfa):
earlstIdx = idxLegalPos[0]
# print('idxLegalPos:', idxLegalPos)
earlstPos = legalPos[0]
startTime_a = endTimesForPossiblePos[earlstIdx]
# print('endTimesForPossiblePos:', endTimesForPossiblePos)
startTimesForMchOfa[:] = np.insert(startTimesForMchOfa, earlstPos, startTime_a)[:-1]
opsIDsForMchOfa[:] = np.insert(opsIDsForMchOfa, earlstPos, a)[:-1]
return startTime_a
def calJobAndMchRdyTimeOfa(a, mchMat, durMat, mchsStartTimes, opIDsOnMchs):
mch_a = np.take(mchMat, a) - 1
# cal jobRdyTime_a
jobPredecessor = a - 1 if a % mchMat.shape[1] != 0 else None
if jobPredecessor is not None:
durJobPredecessor = np.take(durMat, jobPredecessor)
mchJobPredecessor = np.take(mchMat, jobPredecessor) - 1
jobRdyTime_a = (mchsStartTimes[mchJobPredecessor][np.where(opIDsOnMchs[mchJobPredecessor] == jobPredecessor)] + durJobPredecessor).item()
else:
jobRdyTime_a = 0
# cal mchRdyTime_a
mchPredecessor = opIDsOnMchs[mch_a][np.where(opIDsOnMchs[mch_a] >= 0)][-1] if len(np.where(opIDsOnMchs[mch_a] >= 0)[0]) != 0 else None
if mchPredecessor is not None:
durMchPredecessor = np.take(durMat, mchPredecessor)
mchRdyTime_a = (mchsStartTimes[mch_a][np.where(mchsStartTimes[mch_a] >= 0)][-1] + durMchPredecessor).item()
else:
mchRdyTime_a = 0
return jobRdyTime_a, mchRdyTime_a
if __name__ == "__main__":
from JSSP_Env import SJSSP
from uniform_instance_gen import uni_instance_gen
import time
n_j = 3
n_m = 3
low = 1
high = 99
SEED = 10
np.random.seed(SEED)
env = SJSSP(n_j=n_j, n_m=n_m)
'''arr = np.ones(3)
idces = np.where(arr == -1)
print(len(idces[0]))'''
# rollout env random action
t1 = time.time()
data = uni_instance_gen(n_j=n_j, n_m=n_m, low=low, high=high)
print('Dur')
print(data[0])
print('Mach')
print(data[-1])
print()
# start time of operations on machines
mchsStartTimes = -configs.high * np.ones_like(data[0].transpose(), dtype=np.int32)
# Ops ID on machines
opIDsOnMchs = -n_j * np.ones_like(data[0].transpose(), dtype=np.int32)
# random rollout to test
# count = 0
_, _, omega, mask = env.reset(data)
rewards = []
flags = []
# ts = []
while True:
action = np.random.choice(omega[np.where(mask == 0)])
print(action)
mch_a = np.take(data[-1], action) - 1
# print(mch_a)
# print('action:', action)
# t3 = time.time()
adj, _, reward, done, omega, mask = env.step(action)
# t4 = time.time()
# ts.append(t4 - t3)
# jobRdyTime_a, mchRdyTime_a = calJobAndMchRdyTimeOfa(a=action, mchMat=data[-1], durMat=data[0], mchsStartTimes=mchsStartTimes, opIDsOnMchs=opIDsOnMchs)
# print('mchRdyTime_a:', mchRdyTime_a)
startTime_a, flag = permissibleLeftShift(a=action, durMat=data[0].astype(np.single), mchMat=data[-1], mchsStartTimes=mchsStartTimes, opIDsOnMchs=opIDsOnMchs)
flags.append(flag)
# print('startTime_a:', startTime_a)
# print('mchsStartTimes\n', mchsStartTimes)
# print('NOOOOOOOOOOOOO' if not np.array_equal(env.mchsStartTimes, mchsStartTimes) else '\n')
print('opIDsOnMchs\n', opIDsOnMchs)
# print('LBs\n', env.LBs)
rewards.append(reward)
# print('ET after action:\n', env.LBs)
print()
if env.done():
break
t2 = time.time()
print(t2 - t1)
# print(sum(ts))
# print(np.sum(opIDsOnMchs // n_m, axis=1))
# print(np.where(mchsStartTimes == mchsStartTimes.max()))
# print(opIDsOnMchs[np.where(mchsStartTimes == mchsStartTimes.max())])
print(mchsStartTimes.max() + np.take(data[0], opIDsOnMchs[np.where(mchsStartTimes == mchsStartTimes.max())]))
# np.save('sol', opIDsOnMchs // n_m)
# np.save('jobSequence', opIDsOnMchs)
# np.save('testData', data)
# print(mchsStartTimes)
durAlongMchs = np.take(data[0], opIDsOnMchs)
mchsEndTimes = mchsStartTimes + durAlongMchs
print(mchsStartTimes)
print(mchsEndTimes)
print()
print(env.opIDsOnMchs)
print(env.adj)
# print(sum(flags))
# data = np.load('data.npy')
# print(len(np.where(np.array(rewards) == 0)[0]))
# print(rewards)