-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
416 lines (355 loc) · 17 KB
/
environment.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
from ra import RA
from ba import BA
from collections import deque
import time
import threading
from utils import Constraint
import itertools
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class Environment():
def __init__(self, rules, random_explore):
self.random_explore = random_explore
self.lock = threading.RLock()
self.rules = rules
self.build_grid()
self.all_bas = []
self.ras = {"T": [], "SG": []}
for teacher in range(rules["nb_teachers"]):
self.ras["T"].append(RA("T", teacher, rules, self, self.random_explore))
self.all_bas.extend(self.ras["T"][-1].bas)
for st_group in range(rules["nb_st_groups"]):
self.ras["SG"].append(RA("SG", st_group, rules, self, self.random_explore))
self.all_bas.extend(self.ras["SG"][-1].bas)
# self.step_count = 0
self.all_ras = self.ras["T"] + self.ras["SG"]
def build_grid(self):
self.grid = []
for day in range(self.rules["nb_days"]):
self.grid.append([])
for room in range(self.rules["nb_rooms"]):
self.grid[-1].append([])
for time_slot in range(self.rules["nb_time_slots"]):
c = Cell(day, room, time_slot, self.rules)
self.grid[-1][-1].append(c)
def set_relax(self):
for ba in self.all_bas:
ba.relax = bool(self.rules["relax"])
def step(self):
time = 0
self.print_info(time)
while True:
for ba in self.all_bas:
ba.step()
# rez = ba.test_deadlock()
# if not rez:
# print("DEADLOCK")
# exit(0)
print("[Info] BA {} acted".format(ba.name))
self.perceive_cycle()
self.print_info(time)
time += 1
def perceive_cycle(self):
for i in range(2):
for baj in self.all_bas:
baj.perceive()
for ra in self.all_ras:
ra.perceive()
# def start(self):
# for ra in self.all_ras:
# ra.start()
# while(1):
# time.sleep(10)
# self.print_info()
# booked = 0
# for day in range(self.rules["nb_days"]):
# for room in range(self.rules["nb_rooms"]):
# for time_slot in range(self.rules["nb_time_slots"]):
# cell = self.get_cell([day, room, time_slot])
# if cell.reservation is not None and cell.reservation.partner:
# booked += 1
# if booked == self.rules["occupancy"]:
# exit(0)
def print_violated_constraint(self, v):
if v.type == "R":
text = "Constraint {} ".format(v.type)
else:
text = "Constraint {} owner: {} ".format(v.type, v.owner.name)
if v.type == "T":
text += "teacher: {} day: {} time_slot: {} ".format(v.teacher, v.day,
v.time_slot)
if v.type == "SG":
text += "teacher course availability {} ".format(v.tc_avail)
if v.type == "U" or v.type == "R":
text += "cell day: {}, room: {}, time_slot: {}".format(v.day, v.room, v.time_slot)
print(bcolors.OKBLUE + text + bcolors.ENDC)
def calc_occupancy(self):
occupancy = 0
for teacher in range(self.rules["nb_teachers"]):
occupancy += self.rules['ra_constraints']["T"][str(teacher)]["nb_courses"]
return occupancy
def print_violated_constraints(self, vc):
table_header = ">" * (5 + 2 + (53 + 2) * self.rules["nb_rooms"])
anchor = '^' * int((5 + 1 + (53 + 2) * self.rules["nb_rooms"] - 20) / 2 - 2)
print(('||' + anchor + " Violated Constraints" + anchor + '||').format(time))
table_footer = "<" * (5 + 2 + (53 + 2) * self.rules["nb_rooms"])
print(bcolors.BOLD + table_header + bcolors.ENDC)
for v in vc:
self.print_violated_constraint(v)
print(bcolors.BOLD + table_footer + bcolors.ENDC)
def print_pawns(self, time):
total_violated_constraints = []
double_line = '=' * (5 + 1 + (53 + 2) * self.rules["nb_rooms"])
anchor = ' ' * int((5 + 1 + (53 + 2) * self.rules["nb_rooms"] - 7) / 2 - 2)
print(double_line)
print(('||' + anchor + " TIME {} " + anchor + '||').format(time))
print(double_line)
time_slot_encoder = [" 8-10", "10-12", "14-16", "16–18"]
table_header = ">" * (5 + 2 + (53 + 2) * self.rules["nb_rooms"])
table_footer = "<" * (5 + 2 + (53 + 2) * self.rules["nb_rooms"])
print(bcolors.WARNING + table_header + bcolors.ENDC)
for day in range(self.rules["nb_days"]):
line = '-' * (5 + 2 + (53 + 2) * self.rules["nb_rooms"])
double_line = '=' * (5 + 2 + (53 + 2) * self.rules["nb_rooms"])
anchor = ' ' * int((5 + 2 + (53 + 2) * self.rules["nb_rooms"] - 7) / 2 - 2)
print(double_line)
print(('||' + anchor + " DAY {} " + anchor + '||').format(day))
print(double_line)
info_grid = []
info_rez = []
for time_slot in range(self.rules["nb_time_slots"]):
info_grid.append([])
info_rez.append([])
for room in range(self.rules["nb_rooms"]):
cell = self.get_cell([day, room, time_slot])
if len(cell.bas) == 0:
info_grid[-1].append(" ")
else:
info_bas = []
for ba in cell.bas:
ba_info = ba.name
ba_info += "("
if ba.partner:
ba_info += 't'
else:
ba_info += "f"
ba_info += "/"
if ba.reservation:
ba_info += 't'
else:
ba_info += "f"
ba_info += ")"
info_bas.append(ba_info)
info_grid[-1].append(", ".join(info_bas))
#info_grid[-1].append(", ".join([ba.name for ba in cell.bas]))
text = ""
violated_constraints = []
if cell.reservation:
one = cell.reservation.name
violated_constraints.append(BA.NC_cell(cell.reservation, cell))
if cell.reservation.partner:
two = cell.reservation.partnership.name
# info_rez[-1].append(cell.reservation.name + "/" + cell.reservation.partnership.name)
text += one + "/" + two
violated_constraints.append(BA.NC_ba(cell.reservation, cell.reservation.partnership))
else:
# info_rez[-1].append(cell.reservation.name + "/None")
text += one + "/None"
else:
# info_rez[-1].append("None/None")
text += "None/None"
violated_constraints = list(set(itertools.chain(*violated_constraints)))
violated_constraints = [v for v in violated_constraints if v.type != "I"]
total_violated_constraints.extend(violated_constraints)
vc = len(violated_constraints)
if vc == 0:
info_rez[-1].append(bcolors.OKBLUE + text + bcolors.ENDC)
else:
info_rez[-1].append(bcolors.FAIL + text + bcolors.ENDC)
header = "|T / R|"
for i in range(self.rules["nb_rooms"]):
header += "| {} |".format(i)
print(header)
print(line)
for time_slot, rooms in enumerate(info_grid):
row = "|{}|".format(time_slot_encoder[time_slot])
for t in rooms:
row += "|{}|".format(t.ljust(53))
print(row)
row_rez = bcolors.OKBLUE + "|{}|".format(time_slot_encoder[time_slot]) + bcolors.ENDC
for t in info_rez[time_slot]:
row_rez += "|{}|".format(t.ljust(53 + 9))
print(row_rez)
print(line)
# print("--- ROOM {} ---".format(room))
# for time_slot in range(self.rules["nb_time_slots"]):
# print("+ {} +".format(time_slot))
# cell = self.get_cell([day, room, time_slot])
# for ba in cell.bas:
# print(ba.name)
print(bcolors.WARNING + table_footer + bcolors.ENDC)
if len(total_violated_constraints) != 0:
self.print_violated_constraints(total_violated_constraints)
def print_board(self):
pass
def print_info(self, time):
self.print_pawns(time)
booked = 0
for day in range(self.rules["nb_days"]):
for room in range(self.rules["nb_rooms"]):
for time_slot in range(self.rules["nb_time_slots"]):
cell = self.get_cell([day, room, time_slot])
if cell.reservation is not None and cell.reservation.partner:
booked += 1
double_line = '=' * (5 + 1 + (53 + 2) * self.rules["nb_rooms"])
anchor = ' ' * int((5 + 1 + (53 + 2) * self.rules["nb_rooms"] - 7) / 2 - 2)
print(bcolors.FAIL + double_line + bcolors.ENDC)
print(bcolors.FAIL + ('||' + anchor + " BOOK {} " + anchor + '||').format(booked) + bcolors.ENDC)
print(bcolors.FAIL + double_line + bcolors.ENDC)
if booked == self.calc_occupancy():
violated_constraints = self.print_solution()
return violated_constraints
return -1
# exit(0)
# self.print_board()
def get_cell(self, directions):
return self.grid[directions[0]][directions[1]][directions[2]]
def print_solution(self):
total_violated_constraints = 0
double_line = '=' * (5 + 1 + (53 + 2) * self.rules["nb_rooms"])
anchor = ' ' * int((5 + 1 + (53 + 2) * self.rules["nb_rooms"] - 7) / 2 - 2)
print(double_line)
print(('||' + anchor + " SOLUTION " + anchor + '||'))
print(double_line)
time_slot_encoder = [" 8-10", "10-12", "14-16", "16–18"]
table_header = ">" * (5 + 2 + (53 + 2) * self.rules["nb_rooms"])
table_footer = "<" * (5 + 2 + (53 + 2) * self.rules["nb_rooms"])
print(bcolors.OKGREEN + table_header + bcolors.ENDC)
for day in range(self.rules["nb_days"]):
line = '-' * (5 + 2 + (53 + 2) * self.rules["nb_rooms"])
double_line = '=' * (5 + 2 + (53 + 2) * self.rules["nb_rooms"])
anchor = ' ' * int((5 + 2 + (53 + 2) * self.rules["nb_rooms"] - 7) / 2 - 2)
print(double_line)
print(('||' + anchor + " DAY {} " + anchor + '||').format(day))
print(double_line)
info_grid = []
info_rez = []
for time_slot in range(self.rules["nb_time_slots"]):
info_grid.append([])
info_rez.append([])
for room in range(self.rules["nb_rooms"]):
violated_constraints = []
cell = self.get_cell([day, room, time_slot])
text = ""
if cell.reservation:
violated_constraints.append(BA.NC_cell(cell.reservation, cell))
one = cell.reservation.type + str(cell.reservation.ra_id + 1)
# violated_constraints += len(BA.NC_cell(cell.reservation, cell))
if cell.reservation.partner:
two = cell.reservation.partnership.type + str(cell.reservation.partnership.ra_id + 1)
# info_rez[-1].append(one + "/" + two)
text += one + "/" + two
violated_constraints.append((BA.NC_ba(cell.reservation, cell.reservation.partnership)))
else:
# info_rez[-1].append(one + "/None")
text += one + "/None"
else:
# info_rez[-1].append("None/None")
text += "None/None"
violated_constraints = list(set(itertools.chain(*violated_constraints)))
violated_constraints = [v for v in violated_constraints if v.type != "I"]
vc = len(violated_constraints)
if vc == 0:
info_rez[-1].append(bcolors.OKGREEN + text + bcolors.ENDC)
else:
info_rez[-1].append(bcolors.FAIL + text + "({})".format(vc) + bcolors.ENDC)
total_violated_constraints += vc
header = "|T / R|"
for i in range(self.rules["nb_rooms"]):
header += "| {} |".format(i)
print(header)
print(line)
for time_slot, rooms in enumerate(info_grid):
row_rez = "|{}|".format(time_slot_encoder[time_slot])
for t in info_rez[time_slot]:
row_rez += "|{}|".format(t.ljust(53))
print(row_rez)
print(line)
# print("--- ROOM {} ---".format(room))
# for time_slot in range(self.rules["nb_time_slots"]):
# print("+ {} +".format(time_slot))
# cell = self.get_cell([day, room, time_slot])
# for ba in cell.bas:
# print(ba.name)
print(bcolors.OKGREEN + table_footer + bcolors.ENDC)
return total_violated_constraints
def remove_cell_unavailability_constraint(self, con):
for day in range(self.rules["nb_days"]):
for room in range(self.rules["nb_rooms"]):
for time_slot in range(self.rules["nb_time_slots"]):
if con["day"] == day and con["time_slot"] == time_slot and con["room"] == room:
cell = self.get_cell([day, room, time_slot])
cell.remove_cell_unavailability_constraint()
def add_cell_constraint(self, con):
for day in range(self.rules["nb_days"]):
for room in range(self.rules["nb_rooms"]):
for time_slot in range(self.rules["nb_time_slots"]):
if con["day"] == day and con["time_slot"] == time_slot and con["room"] == room:
cell = self.get_cell([day, room, time_slot])
cell.add_cell_unavailability_constraint(con)
class Cell():
lock = threading.RLock()
def __init__(self, day, room, time_slot, rules):
self.day = day
self.room = room
self.time_slot = time_slot
self.rules = rules
self.C = [Constraint("R", day=day, time_slot=time_slot, room=room)]
self.bas = []
self.reservation = None
self.set_unavailability_constraints()
self.set_tools_constraints()
self.f = open("messages.txt", "a")
def set_reservation(self, new_value):
with self.lock:
self.reservation = new_value
def move_ba_here(self, ba):
with self.lock:
self.bas.append(ba)
def remove_ba_from_here(self, ba):
with self.lock:
self.bas.remove(ba)
def get_bas(self):
return self.bas
def set_unavailability_constraints(self):
for constr in self.rules["cell_constraints"]["unavailability"]:
if constr["day"] == self.day and \
constr["time_slot"] == self.time_slot and \
constr["room"] == self.room:
c = Constraint("U", day=constr["day"],
time_slot=constr["time_slot"],
teacher=constr["room"], weight=1)
self.C.append(c)
def set_tools_constraints(self):
for constr in self.rules["cell_constraints"]["tools_availability"]:
if self.room == constr["room"]:
c = Constraint("A", day=self.day,
time_slot=self.time_slot,
room=constr["room"], tools=constr["tools"], weight=1)
self.C.append(c)
def remove_cell_unavailability_constraint(self):
for c in self.C:
if c.type == "U":
self.C.remove(c)
def add_cell_unavailability_constraint(self, constr):
c = Constraint("U", day=constr["day"],
time_slot=constr["time_slot"],
teacher=constr["room"], weight=1)
self.C.append(c)