fixed time comparison bug, fixed wrong abs symbol and wrong comparison in more-than-one-slot-workgroup-rule

This commit is contained in:
Anja Reusch 2019-06-11 16:02:59 +02:00
parent 407164f92d
commit ae6b2abf66

View file

@ -19,9 +19,9 @@ class WorkGroup:
self.projector = 'true' if projector else 'false' self.projector = 'true' if projector else 'false'
self.reso = 'true' if reso else 'false' self.reso = 'true' if reso else 'false'
self.length = floor((length-1) / SLOT_LEN) + 1 self.length = floor((length-1) / SLOT_LEN) + 1
if self.length > 2: # if self.length > 2:
print(f"Warning, length of workgroup {id} greater than 2.") # print(f"Warning, length of workgroup {id} greater than 2.")
self.length = 2 # self.length = 2
self.leaders = leaders self.leaders = leaders
self.constrainted_time = [{'day': 1, 'time': 840, 'type': 'before'}] if reso else [] self.constrainted_time = [{'day': 1, 'time': 840, 'type': 'before'}] if reso else []
self.not_on_day = [0, 1, 2] if len(list(filter(lambda c: c['type'] == 'OnlyOnDay', constraints))) else [] self.not_on_day = [0, 1, 2] if len(list(filter(lambda c: c['type'] == 'OnlyOnDay', constraints))) else []
@ -101,13 +101,13 @@ class Schedule:
def parse_workgroups(data): def parse_workgroups(data):
workgroups = [] workgroups = []
id_to_workgroup = {} id_to_index_workgroup = {}
for i, workgroup in enumerate(data['workGroups']): for i, workgroup in enumerate(data['workGroups']):
workgroups.append( workgroups.append(
WorkGroup(workgroup['id'], workgroup['interested'], workgroup['track'], workgroup['projector'], WorkGroup(workgroup['id'], workgroup['interested'], workgroup['track'], workgroup['projector'],
workgroup['resolution'], workgroup['length'], workgroup['leader'], workgroup['constraints'])) workgroup['resolution'], workgroup['length'], workgroup['leader'], workgroup['constraints']))
id_to_workgroup[workgroup['id']] = i id_to_index_workgroup[workgroup['id']] = i
return workgroups, id_to_workgroup return workgroups, id_to_index_workgroup
def parse_rooms(data): def parse_rooms(data):
@ -126,8 +126,8 @@ def parse_rooms(data):
data = json.load(open('backup.json')) data = json.load(open('backup.json'))
workgroups, id_to_workgroup = parse_workgroups(data) workgroups, id_to_index_workgroup = parse_workgroups(data)
rooms, id_to_room = parse_rooms(data) rooms, id_to_index_room = parse_rooms(data)
rules = """ rules = """
%LENGTH = { schedule(AK,TIMESLOT,ROOM): timeslot(TIMESLOT,_,_,_,_), room(ROOM,_,_) } :- ak(AK,_,_,_,_,LENGTH). %LENGTH = { schedule(AK,TIMESLOT,ROOM): timeslot(TIMESLOT,_,_,_,_), room(ROOM,_,_) } :- ak(AK,_,_,_,_,LENGTH).
@ -145,16 +145,20 @@ LENGTH = { schedule(AK,TIMESLOT,ROOM): timeslot(TIMESLOT,_,_,_,_), room(ROOM,_,_
% forbid two AKs with same leader at same timestep % forbid two AKs with same leader at same timestep
:- schedule(AK1,TIMESTEP,_), schedule(AK2,TIMESTEP,_), leader(AK1,LEADER), leader(AK2,LEADER), AK1 != AK2. :- schedule(AK1,TIMESTEP,_), schedule(AK2,TIMESTEP,_), leader(AK1,LEADER), leader(AK2,LEADER), AK1 != AK2.
% schedules of longer aks should be in the same room and in consecutive timeslots % schedules of longer aks should be in the same room and in consecutive timeslots, on the same day
schedule(AK,TIMESLOT2,ROOM) :- ak(AK,_,_,_,_,2), schedule(AK,TIMESLOT1,ROOM), timeslot(TIMESLOT1,ORDER1,_,_,DAY), timeslot(TIMESLOT2,ORDER2,_,_,DAY), timeslot(TIMESLOT3,ORDER3,_,_,_), ORDER2 != ORDER1+1, ORDER1 = ORDER3+1, not schedule(AK,TIMESLOT3,ROOM) . %schedule(AK,TIMESLOT2,ROOM) :- ak(AK,_,_,_,_,LENGTH), LENGTH>1, schedule(AK,TIMESLOT1,ROOM), timeslot(TIMESLOT1,ORDER1,_,_,DAY), timeslot(TIMESLOT2,ORDER2,_,_,DAY), timeslot(TIMESLOT3,ORDER3,_,_,_), ORDER2 != ORDER1+1, ORDER1 = ORDER3+1, not schedule(AK,TIMESLOT3,ROOM) .
:- schedule(AK, TIMESLOT1, _), schedule(AK, TIMESLOT2, _), timeslot(TIMESLOT1,ORDER1,_,_,_), timeslot(TIMESLOT2,ORDER2,_,_,_), ak(AK,_,_,_,_,LENGTH), |ORDER2-ORDER1| >= LENGTH.
%same room
:- schedule(AK, _, ROOM1), schedule(AK, _, ROOM2), ROOM1 != ROOM2.
%same day
:- schedule(AK, TIMESLOT1, _), schedule(AK, TIMESLOT2, _), timeslot(TIMESLOT1, _,_,_, DAY1), timeslot(TIMESLOT2, _,_,_, DAY2), DAY1 != DAY2.
""" """
lengths = defaultdict(int) lengths = defaultdict(int)
for workgroup in workgroups: for workgroup in workgroups:
lengths[workgroup.length] += 1 lengths[workgroup.length] += 1
rules += str(workgroup) + '\n' rules += str(workgroup) + '\n'
print(lengths, lengths[1] + lengths[2]*2) print(lengths)
for room in rooms: for room in rooms:
rules += str(room) + '\n' rules += str(room) + '\n'
@ -236,6 +240,8 @@ process = ['clingo', output_file_name, '--outf=2']
completed_process = subprocess.run(process, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) completed_process = subprocess.run(process, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
output = completed_process.stdout output = completed_process.stdout
open('solver_output.json', 'w').write(output)
####################### #######################
# OUTPUT SCHEDULE # # OUTPUT SCHEDULE #
####################### #######################
@ -254,10 +260,10 @@ else:
for i, schedule in enumerate(output['Call'][0]['Witnesses'][-1]['Value']): for i, schedule in enumerate(output['Call'][0]['Witnesses'][-1]['Value']):
match = re.match('schedule\((.+),"(.+)#(.+)#.+",(.+)\)', schedule) match = re.match('schedule\((.+),"(.+)#(.+)#.+",(.+)\)', schedule)
try: try:
workgroup_id = id_to_workgroup[int(match.group(1))] workgroup_index = id_to_index_workgroup[int(match.group(1))]
room_id = id_to_room[int(match.group(4))] room_id = id_to_index_room[int(match.group(4))]
schedules.append( schedules.append(
Schedule(i, data['workGroups'][workgroup_id], match.group(2), match.group(3), data['rooms'][room_id])) Schedule(i, data['workGroups'][workgroup_index], match.group(2), match.group(3), data['rooms'][room_id]))
# { # {
# 'id': i, # 'id': i,
# 'workGroup': data['workGroups'][workgroup_id], # 'workGroup': data['workGroups'][workgroup_id],
@ -267,14 +273,14 @@ else:
# 'lockRoom': False, # 'lockRoom': False,
# 'lockTime': False, # 'lockTime': False,
# }) # })
workgroup_to_schedule[workgroup_id].append(i) workgroup_to_schedule[workgroup_index].append(i)
except Exception as e: except Exception as e:
print(e) print(e)
schedules_filtered = [] schedules_filtered = []
for workgroup_id in workgroup_to_schedule: for workgroup_index in workgroup_to_schedule:
schedules_for_workgroup = workgroup_to_schedule[workgroup_id] schedules_for_workgroup = workgroup_to_schedule[workgroup_index]
if len(schedules_for_workgroup) == 1: if len(schedules_for_workgroup) == 1:
schedules_filtered.append(schedules[schedules_for_workgroup[0]]) schedules_filtered.append(schedules[schedules_for_workgroup[0]])
else: else:
@ -282,7 +288,11 @@ else:
# find out which one is the first one and remove the second workgroup: # find out which one is the first one and remove the second workgroup:
schedules_for_workgroup_0 = schedules[schedules_for_workgroup[0]] schedules_for_workgroup_0 = schedules[schedules_for_workgroup[0]]
schedules_for_workgroup_1 = schedules[schedules_for_workgroup[1]] schedules_for_workgroup_1 = schedules[schedules_for_workgroup[1]]
if schedules_for_workgroup_0.time < schedules_for_workgroup_1.time: print(schedules_for_workgroup_0.time < schedules_for_workgroup_1.time)
print(f'{schedules_for_workgroup_0.time} < {schedules_for_workgroup_1.time}')
print(type(schedules_for_workgroup_0.time), type(schedules_for_workgroup_1.time))
print('-------')
if int(schedules_for_workgroup_0.time) < int(schedules_for_workgroup_1.time):
schedules_filtered.append(schedules_for_workgroup_0) schedules_filtered.append(schedules_for_workgroup_0)
else: else:
schedules_filtered.append(schedules_for_workgroup_1) schedules_filtered.append(schedules_for_workgroup_1)