-
Notifications
You must be signed in to change notification settings - Fork 5
/
scheduler.py
340 lines (310 loc) · 12.7 KB
/
scheduler.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
import numpy as np
import pandas as pd
from tabulate import tabulate
from state_generate import Jobs
from render_image import Render
import copy
import gym
def cut_matrix(matrix, mr, mt):
for n in range(mt):
if matrix[n : n+mr].all() == 0:
time = n
return matrix[:n * mr]
def zero_df(rows, columns):
""" returns dataframe filled with zeros and size rows x columns """
df = pd.DataFrame(np.zeros((rows, columns)))
return df
def get_empty_job(rows, columns):
""" generates an empty job of size rows x columns """
matrix = zero_df(rows, columns)
return matrix, matrix
def isempty_job(m1, m2):
""" checks if the job having m1, m2 requirement matrix is empty """
return ((not m1.any().any()) and (not m2.any().any()))
def get_index(s):
""" returns the index of first zero in the pd.Series """
return len(s) - len(s[s==0])
def get_d(df1, df2):
"""returns the index of first zero in a row """
s1, s2 = df1.iloc[0], df2.iloc[0]
return ((len(s1)-len(s1[s1==0])), (len(s2)-len(s2[s2==0])))
def make_job(time, rows, columns):
"""create a new job of time: time and rows x columns representation"""
r1, r2 = np.random.randint(0, columns, 2)
if r1==0 and r2==0: #Can't make both zero
if np.random.randint(0, 2):
r1 = 1
else:
r2 = 1
matrix1 = zero_df(rows, columns)
matrix1.iloc[0:time, 0:r1] = time
matrix2 = zero_df(rows, columns)
matrix2.iloc[0:time, 0:r2] = time
return matrix1, matrix2
class Scheduler(gym.Env):
def __init__(self, n, mt, mr):
"""
Simulate a job scheduling environment without preemption and two job
instance types with variable amount of resource instances, job numbers
and max job time.
@param n number of jobs
@param mt max burst time
@param mr max resource instance requirement
There are two resources
"""
self.state = 'INVALID' #Call make() to make an environment
self.action_sequence = [] #No actions taken yet
self.invalid_action_reward = -100
self.valid_action_reward = 0 #Try changing this
self.n = n
self.mt = mt
self.mr = mr
self.action_space = [i for i in range(1, mt+1)]
self.action_space_n = len(self.action_space)
self.observation_space = self.get_observation_space()
self.symbol = [str(i) for i in range(1, mt+1)]
self.rows = n * mt
self.columns = mr
self.reset()
def sample(self):
"""return a sample action"""
return np.random.choice(self.action_space)
def reset(self):
"""reset the environment"""
self.done = False
self.cr = 0
self.state = self.generate_start_state()
self.reward = 0
return self.collect_matrices()
def setPosition(self, state):
"""sets current state to some saved state"""
self.state = state
self.reward = 0
def generate_start_state(self):
"""generates a new start state"""
jObject = Jobs(self.n, self.mt, self.mr)
self.state = jObject.getState()
self.BACKLOG = self.state['backlog'].copy()
return(self.state)
def load_from_backlog(self, time):
"""load a job from the backlog"""
if not self.state['backlog'][time]:
return False #No job in backlog
self.state['backlog'][time] -= 1
instance = self.BACKLOG[time] - self.state['backlog'][time]
symbol = f"{time}.{instance}"
self.symbol[time-1] = symbol
m1, m2 = make_job(time, self.rows, self.columns)
self.state[f'job{time}'] = dict(zip(['r1', 'r2'], [m1, m2]))
# print("SYMBOLS: ", symbol,'\n',self.BACKLOG,'\n', self.state['backlog'])
return True
def sched_job(self, time):
"""schedule the job at time: @param time"""
assert self.state != "INVALID", "Invalid state, reset the environment"
m1, m2 = self.state[f'job{time}']['r1'], self.state[f'job{time}']['r2']
if isempty_job(m1, m2):
self.state = 'INVALID'
self.done = True
return self.invalid_action_reward
else:
self.state[f'job{time}']['r1'], self.state[f'job{time}']['r2'] = \
get_empty_job(self.rows, self.columns)
self.state['resource1'], self.state['resource2'] = \
self.allocate_job(time, m1, m2)
reward = self.get_reward()
self.load_from_backlog(time)
return reward
def allocate_job(self, time, m1, m2):
"""allocate the job at time: time"""
r1, r2 = self.state['resource1'], self.state['resource2']
d1, d2 = get_d(m1, m2)
r1, r2 = self.fill_matrix(r1, r2, time, d1, d2)
return r1, r2
def fill_matrix(self, r1, r2, time, d1, d2):
"""
fills the resource matrices r1 and r2 with the job having time: time
and resource requirement d1 and d2
"""
symbol = self.symbol[time-1]
l1 = [] #l1 is the list containing the number of empty boxes in each row
l2 = [] #l2 is the list containing the number of empty boxes in each row
update_cr = False
for row in r1.itertuples(index=False):
l1 += [list(row).count(0)]
for row in r2.itertuples(index=False):
l2 += [list(row).count(0)]
row = self.cr
if l1[row] < d1 or l2[row] < d2:
update_cr = True
while(row<self.rows and time>0):
if l1[row] >= d1 and l2[row] >= d2:
if update_cr:
update_cr = False
self.cr = row
zeroIndex1 = get_index(r1.iloc[row])
zeroIndex2 = get_index(r2.iloc[row])
if d1:
r1.iloc[row, zeroIndex1:zeroIndex1+d1] = symbol
if d2:
r2.iloc[row, zeroIndex2:zeroIndex2+d2] = symbol
time -= 1
row += 1
return r1, r2
def sjf(self, save_state = True):
if save_state:
saved_state = copy.deepcopy(self.state)
done = False
reward = 0
while not done:
for time in range(1, self.mt + 1):
m1, m2 = self.state[f'job{time}']['r1'], self.state[f'job{time}']['r2']
if isempty_job(m1, m2):
continue
s, r, done, info = self.step(time)
reward += r
if save_state:
self.state = copy.deepcopy(saved_state)
self.done = False
return reward
def all_done(self):
""""returns true if all jobs have been scheduled"""
state = self.state
for time in range(1, self.mt+1):
key = f"job{time}"
# print("PRINTING STATE\n\n")
# print(state)
m1, m2 = state[key]['r1'], state[key]['r2']
if not isempty_job(m1, m2):
return False
if not all(v == 0 for v in state['backlog'].values()):
return False
self.done = True
return True
def isrunning(self):
"""returns true if final state not reached"""
if self.done:
return False
return True
def fetch_reward(self, m):
"""calculate reward helper function"""
l = [] #Contains symbols of all the jobs for which calculations are done
rewards = []
for row in range(0, self.rows):
waiting_time = row
for jname in m.iloc[row].unique():
#example jname are 1.11, 10.11, 2, 1, 1.1
if jname not in l and int(str(jname).split('.')[0]):
l.append(jname)
burst_time = int(str(jname)[0])
rewards.append((waiting_time + burst_time) / burst_time)
return sum(rewards)
def calculate_reward(self):
"""calculate reward helper function"""
if type(self.state) is str:
return -1
m1 = self.state['resource1']
m2 = self.state['resource2']
r1, r2 = self.fetch_reward(m1), self.fetch_reward(m2)
return max(r1, r2)
def get_reward(self):
"""Get reward from state"""
state = self.state
if state == "INVALID":
return self.invalid_action_reward
if self.all_done():
return -self.calculate_reward() #Negative of waited burst time
else:
return self.valid_action_reward
def step(self, action):
"""takes the "action" step in the environment
@param action is the action to be taken example 1 means schedule job
with time 1 units
returns :
@environment: state
@reward: reward for the state, action pair
@self.done: true if end of episode
@infor: sequence of actions taken till now
"""
assert action in self.action_space, "Invalid action"
self.action_sequence.append(action)
reward = self.sched_job(action)
environment = self.state
info = self.action_sequence
flat_state = self.collect_matrices()
return flat_state, reward, self.done, info
def render(self, terminal=True):
"""
@terminal if True then prints the state on terminal else,
displays the current state as colored image file 'state.png'
"""
if terminal:
self.render_terminal()
elif self.state == "INVALID":
print("invalid action")
print("DONE")
Render(self.state, self.n).render_invalid()
return
else:
Render(self.state, self.n).render()
def get_observation_space(self):
some_state = self.generate_start_state()
matrices = np.array([])
for key, value in some_state.items():
if key in ['resource1', 'resource2']:
# matrices += np.array(value).reshape(-1)
matrices = np.append(matrices, np.array(value).reshape(-1))
if key[0:-1] == 'job':
m1 = cut_matrix(np.array(value['r1']).reshape(-1), self.mr, self.mt)
m2 = cut_matrix(np.array(value['r2']).reshape(-1), self.mr, self.mt)
# matrices = np.append(matrices, np.array(value['r1']).reshape(-1))
# matrices = np.append(matrices, np.array(value['r2']).reshape(-1))
matrices = np.append(matrices, m1)
matrices = np.append(matrices, m2)
if key == 'backlog':
matrices = np.append(matrices, np.array([list(value.values())]))
# matrices += np.array([list(value.values())])
return matrices.size
def collect_matrices(self):
"""collect the matrices and send them to flaten"""
matrices = np.array([])
if self.state == "INVALID":
return -1 * np.ones(self.observation_space)
for key, value in self.state.items():
if key in ['resource1', 'resource2']:
# matrices += np.array(value).reshape(-1)
matrices = np.append(matrices, np.array(value).reshape(-1))
if key[0:-1] == 'job':
m1 = cut_matrix(np.array(value['r1']).reshape(-1), self.mr, self.mt)
m2 = cut_matrix(np.array(value['r2']).reshape(-1), self.mr, self.mt)
# matrices = np.append(matrices, np.array(value['r1']).reshape(-1))
# matrices = np.append(matrices, np.array(value['r2']).reshape(-1))
matrices = np.append(matrices, m1)
matrices = np.append(matrices, m2)
if key == 'backlog':
matrices = np.append(matrices, np.array([list(value.values())]))
# matrices += np.array([list(value.values())])
self.observation_space = matrices.size
return matrices.astype(float)
def render_terminal(self):
"""Displays the current state in ASCII on terminal"""
if self.state == "INVALID":
print("invalid action")
print("DONE")
return
for key, value in self.state.items():
print(key)
if key in ['resource1', 'resource2']:
print(tabulate(value, headers='keys', tablefmt='psql'))
if key[0:-1] == 'job':
print(tabulate(value['r1'], headers='keys', tablefmt='psql'))
print(tabulate(value['r2'], headers='keys', tablefmt='psql'))
if key == 'backlog':
print(tabulate(
[list(value.values())],
headers=list(value.keys()),
tablefmt='psql')
)
if self.done:
print("DONE")
else:
print("NOT DONE")