added penalty for pc pools and changed clingo output to json

This commit is contained in:
Anja Reusch 2019-06-10 23:04:56 +02:00
parent 3b33fcaad7
commit 407164f92d
2 changed files with 21 additions and 15 deletions

View file

@ -18,7 +18,7 @@ class WorkGroup:
self.track = track if track is not None else 'none' # haben alle aks einen track?
self.projector = 'true' if projector else 'false'
self.reso = 'true' if reso else 'false'
self.length = floor(length / SLOT_LEN) + 1
self.length = floor((length-1) / SLOT_LEN) + 1
if self.length > 2:
print(f"Warning, length of workgroup {id} greater than 2.")
self.length = 2
@ -53,13 +53,17 @@ class WorkGroup:
class Room:
def __init__(self, id, n_places, projector): # TODO internet, whiteboard & co.
def __init__(self, id, n_places, projector, is_pool): # TODO internet, whiteboard & co.
self.id = id # must be int
self.n_places = n_places
self.projector = 'true' if projector else 'false'
self.is_pool = is_pool
def __str__(self):
return f'room({self.id}, {self.n_places}, {self.projector}).'
pool_rule = ''
if self.is_pool:
pool_rule = f'\npool({self.id}).'
return f'room({self.id}, {self.n_places}, {self.projector}).{pool_rule}'
class Timeslot:
@ -110,7 +114,7 @@ def parse_rooms(data):
rooms = []
id_to_room = {}
for i, room in enumerate(data['rooms']):
rooms.append(Room(room['id'], room['places'], room['projector'])) # TODO internet, whiteboard & co.
rooms.append(Room(room['id'], room['places'], room['projector'], room['pool'])) # TODO internet, whiteboard & co.
id_to_room[room['id']] = i
return rooms, id_to_room
@ -145,10 +149,13 @@ LENGTH = { schedule(AK,TIMESLOT,ROOM): timeslot(TIMESLOT,_,_,_,_), room(ROOM,_,_
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) .
"""
lengths = defaultdict(int)
for workgroup in workgroups:
lengths[workgroup.length] += 1
rules += str(workgroup) + '\n'
print(lengths, lengths[1] + lengths[2]*2)
for room in rooms:
rules += str(room) + '\n'
@ -209,7 +216,10 @@ for workgroup in workgroups:
f':- schedule({workgroup.id}, TIMESLOT, _), timeslot(TIMESLOT, ORDER, _, _, _), ORDER >= {first_timeslot}, ORDER <= {last_timeslot}.')
rules += f':- schedule({workgroup.id}, TIMESLOT, _), timeslot(TIMESLOT, ORDER, _, _, _), ORDER >= {first_timeslot}, ORDER <= {last_timeslot}.\n'
rules += "#show schedule/3."
rules += "pool_schedule(ROOM) :- schedule(_,_,ROOM), pool(ROOM).\n"\
"poolpen(T) :- T = {pool_schedule(_)}.\n"\
"#minimize { T : poolpen(T) }.\n" \
"#show schedule/3."
#######################
# GENERATE SCHEDULE #
@ -220,7 +230,7 @@ output_file_name = "generated_rules.pl"
open(output_file_name, 'w').write(rules)
# create subprocess definition
process = ['clingo', output_file_name]
process = ['clingo', output_file_name, '--outf=2']
# run subprocess, not sure, if busy-waiting..
completed_process = subprocess.run(process, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
@ -237,16 +247,12 @@ else:
print('SATISFIABLE, outputting schedule...')
import re
try:
match = re.match('(.|\n)*Answer: (.+)\n(.*)\nSATISFIABLE', output)
schedules_string = match.group(3)
except Exception as e:
print(e)
print(output)
output = json.loads(output)
schedules = []
workgroup_to_schedule = defaultdict(list)
for i, schedule in enumerate(schedules_string.split('schedule')[1:]):
match = re.match('\((.+),"(.+)#(.+)#.+",(.+)\)', schedule)
for i, schedule in enumerate(output['Call'][0]['Witnesses'][-1]['Value']):
match = re.match('schedule\((.+),"(.+)#(.+)#.+",(.+)\)', schedule)
try:
workgroup_id = id_to_workgroup[int(match.group(1))]
room_id = id_to_room[int(match.group(4))]

0
temp Normal file
View file