added penalty for pc pools and changed clingo output to json
This commit is contained in:
parent
3b33fcaad7
commit
407164f92d
|
@ -18,7 +18,7 @@ class WorkGroup:
|
||||||
self.track = track if track is not None else 'none' # haben alle aks einen track?
|
self.track = track if track is not None else 'none' # haben alle aks einen track?
|
||||||
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 / 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
|
||||||
|
@ -53,13 +53,17 @@ class WorkGroup:
|
||||||
|
|
||||||
|
|
||||||
class Room:
|
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.id = id # must be int
|
||||||
self.n_places = n_places
|
self.n_places = n_places
|
||||||
self.projector = 'true' if projector else 'false'
|
self.projector = 'true' if projector else 'false'
|
||||||
|
self.is_pool = is_pool
|
||||||
|
|
||||||
def __str__(self):
|
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:
|
class Timeslot:
|
||||||
|
@ -110,7 +114,7 @@ def parse_rooms(data):
|
||||||
rooms = []
|
rooms = []
|
||||||
id_to_room = {}
|
id_to_room = {}
|
||||||
for i, room in enumerate(data['rooms']):
|
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
|
id_to_room[room['id']] = i
|
||||||
return rooms, id_to_room
|
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) .
|
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:
|
for workgroup in workgroups:
|
||||||
|
lengths[workgroup.length] += 1
|
||||||
rules += str(workgroup) + '\n'
|
rules += str(workgroup) + '\n'
|
||||||
|
|
||||||
|
print(lengths, lengths[1] + lengths[2]*2)
|
||||||
|
|
||||||
for room in rooms:
|
for room in rooms:
|
||||||
rules += str(room) + '\n'
|
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}.')
|
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 += 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 #
|
# GENERATE SCHEDULE #
|
||||||
|
@ -220,7 +230,7 @@ output_file_name = "generated_rules.pl"
|
||||||
open(output_file_name, 'w').write(rules)
|
open(output_file_name, 'w').write(rules)
|
||||||
|
|
||||||
# create subprocess definition
|
# create subprocess definition
|
||||||
process = ['clingo', output_file_name]
|
process = ['clingo', output_file_name, '--outf=2']
|
||||||
|
|
||||||
# run subprocess, not sure, if busy-waiting..
|
# run subprocess, not sure, if busy-waiting..
|
||||||
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)
|
||||||
|
@ -237,16 +247,12 @@ else:
|
||||||
print('SATISFIABLE, outputting schedule...')
|
print('SATISFIABLE, outputting schedule...')
|
||||||
import re
|
import re
|
||||||
|
|
||||||
try:
|
output = json.loads(output)
|
||||||
match = re.match('(.|\n)*Answer: (.+)\n(.*)\nSATISFIABLE', output)
|
|
||||||
schedules_string = match.group(3)
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
print(output)
|
|
||||||
schedules = []
|
schedules = []
|
||||||
workgroup_to_schedule = defaultdict(list)
|
workgroup_to_schedule = defaultdict(list)
|
||||||
for i, schedule in enumerate(schedules_string.split('schedule')[1:]):
|
for i, schedule in enumerate(output['Call'][0]['Witnesses'][-1]['Value']):
|
||||||
match = re.match('\((.+),"(.+)#(.+)#.+",(.+)\)', schedule)
|
match = re.match('schedule\((.+),"(.+)#(.+)#.+",(.+)\)', schedule)
|
||||||
try:
|
try:
|
||||||
workgroup_id = id_to_workgroup[int(match.group(1))]
|
workgroup_id = id_to_workgroup[int(match.group(1))]
|
||||||
room_id = id_to_room[int(match.group(4))]
|
room_id = id_to_room[int(match.group(4))]
|
||||||
|
|
Loading…
Reference in a new issue