lovehate = dict((x.split('\t')[0].lower(), [q.strip() for q in x.split('\t')[1:]]) for x in open('table')) people = list(x.strip().lower() for x in open('people')) used = 0 for person in people: try: assert(len(lovehate[person]) == 2) used += 1 except KeyError: print("unable to match up", person) except AssertionError: print("wrong lovehate", person, lovehate[person]) raise assert(used == len(lovehate.keys())) import yaml conf = yaml.load(open('tasks', 'r')) config = conf['config'] tasks_dict = conf['tasks'] # print(tasks_dict) tasks = list(tasks_dict.keys()) tasksi = dict(((v,k) for k,v in enumerate(tasks))) peoplei = dict(((v,k) for k,v in enumerate(people))) tasksdesc = { "schoonmaken": ['schoonmaken'], "afwassen": ['afwassen'], "koken": ['koken', 'kookhulp'], "snackdealen": ['hapjes'], "ignore": ['(vega)', '(liever niet dezelfde maaltijd als het kan)', ''], "superkok": ['superkok'], "fotograferen": ['fotograferen', 'fototroep'] } tasksdesci = {} import re for t,a in tasksdesc.items(): for q in a: tasksdesci[q] = t loves_matrix = [[0] * len(tasks) for person in people] hates_matrix = [[0] * len(tasks) for person in people] capab_matrix = [[0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1] for person in people] def set_capab(person, task): capab_matrix[peoplei[person]][tasksi[task]] = 1 cake_penalty = ["linda", "nanette", "alphacentauri", "obfusk (felix)", "tessa", "joshua"] for person,(loves, hates) in lovehate.items(): loves = [tasksdesci[x] for x in re.split('[ ,/]+', loves) if tasksdesci[x] != 'ignore'] hates = [tasksdesci[x] for x in re.split('[ ,/]+', hates) if tasksdesci[x] != 'ignore'] for l in loves: loves_matrix[peoplei[person]][tasksi[l]] = 1 set_capab(person, l) for h in hates: hates_matrix[peoplei[person]][tasksi[h]] = 1 for person in cake_penalty: set_capab(person, "baktaarten") for person in ["macgyver", "colin", "petervdv"]: set_capab(person, "halen") set_capab("weasel", "hotemetoten") set_capab("yorick", "hotemetoten") set_capab("linda", "hotemetoten") #capab_matrix[peoplei["avel"]] = [0]*len(tasks) def col_all_zeros(ls, i, al=0): return all(x[i] == al for x in ls) def row_all_zeros(ls, i, al=0): return all(x == al for x in ls[i]) def format_matrix(name, ls, default=0): nonzero_cols = [i+1 for i in range(len(ls[0])) if not col_all_zeros(ls, i, default)] nonzero_rows = [i+1 for i in range(len(ls)) if not row_all_zeros(ls, i, default)] print("param", name, ":", " ".join("{:2d}".format(x) for x in nonzero_cols), ":=") for r in nonzero_rows: print("{:9d}".format(r), end='') for c in nonzero_cols: print(" {:2d}".format(ls[r-1][c-1]), end='') print(';' if r == nonzero_rows[-1] else '') def format_dict(name, thing, default=None): print("param {} := {};".format(name, ", ".join(["{} {}".format(k, v) for k,v in thing.items() if v != default]))) def format_param(name, val): print("param {} := {};".format(name, val)) req_matrix = [[tasks_dict[t]['req'][d] for t in tasks] for d in range(config['days'])] from collections import defaultdict import sys if sys.argv[1] == 'in': format_matrix("L", loves_matrix) format_matrix("H", hates_matrix) format_matrix("C", capab_matrix, 1) format_matrix("R", req_matrix, None) # hardcode: superkoks, linda hotemetoot print("param Q := 5,1,1,1 25,5,1,1 18,5,2,1 11,5,3,1;") format_dict("Wl", {tasksi[t]+1: tasks_dict[t]['workload'] for t in tasks}) format_param("D_count", config['days']) format_param("P_count", len(people)) format_param("J_count", len(tasks)) format_param("WL", config['weights']['likes']) format_param("WH", config['weights']['hates']) elif sys.argv[1] == 'out': flag=False ln = [] for l in open('output'): if l == "<<<<\n": flag = False if flag: ln.append(map(int, l.strip().split())) if l == ">>>>\n": flag=True assigned = defaultdict(lambda: [[], [], []]) # lambda: [("", 1, 0), ("", 1, 0), ("", 1, 0)]) for [p, d, j, W, l] in ln: assigned[people[p-1]][d-1].append((tasks[j-1], W, l)) for p, (a, b, c) in assigned.items(): q = lambda w: ",".join([x[0] + " <3" if x[1] else x[0] for x in w]) v = lambda w: sum([x[1] for x in w]) if len(w) else 1 l = lambda w: sum([x[2] for x in w]) print("| {} || {} || {} || {} || {}||{}".format(p, q(a), q(b), q(c), v(a)+v(b)+v(c), l(a)+l(b)+l(c))) print("|-") else: print(sys.argv[0], "in|out")