def main(): faculty_var_arrays = [get_prof(VISITORS) for prof in PROFESSORS] model = Model() model.add(get_visitor_consistency_constraints(faculty_var_arrays)) model.add(get_no_repeat_constraints(faculty_var_arrays)) # read faculty availability information busyfile = open(sys.argv[2], 'rb') busyreader = csv.reader(busyfile) for r in busyreader: if r[0] in PROFESSORS: prof_index = PROFESSORS.index(r[0]) for i in range(1,NUM_SLOTS + 1): if r[i] == "NO": model.add(get_busy_constraint(i-1, prof_index, faculty_var_arrays)) else: print "professor not found in list" busyfile.close() requestfile = open(sys.argv[1], 'rb') reader = csv.reader(requestfile) for r in reader: row = [name for name in r if not (name == "," or name == "" or name == " ")] if row[0] in VISITORS: for i in range(1,len(row)): if not (row[i] in PROFESSORS): print "professor name mismatch: " + str(row[i]) else: model.add(get_meeting_constraint(VISITORS.index(row[0]), PROFESSORS.index(row[i]), faculty_var_arrays)) elif row[0] in PROFESSORS: for i in range(1,len(row)): if not (row[i] in VISITORS): print "visitor name mismatch in row " + str(row) else: model.add(get_meeting_constraint(VISITORS.index(row[i]), PROFESSORS.index(row[0]), faculty_var_arrays)) else: print "requestor name mismatch in row " + str(row) solver = Solver(model, [p[s] for p in faculty_var_arrays for s in range(NUM_SLOTS)]) solver.setVerbosity(2) solver.solve() # print(solver.solveAndRestart()) print "PROFESSOR SCHEDULES" print "-------------------" for i in range(len(PROFESSORS)): print "---------------" print "schedule for " + PROFESSORS[i] for slot in faculty_var_arrays[i]: if slot.get_value(): print VISITORS[slot.get_value()] # print(solver) requestfile.close()
def testOr_2(self): var1 = Variable(0, 2) dur1 = 1 var2 = Variable(0, 4) dur2 = 2 var3 = Variable(0, 6) dur3 = 3 model = NativeModel() #model.add( NoOverlap( (var1, var2), (dur1, dur2) ) ) #model.add( NoOverlap( (var1, var3), (dur1, dur3) ) ) #model.add( NoOverlap( (var2, var3), (dur2, dur3) ) ) ur = UnaryResource ( [ NoOverlap( (var1, var2), (dur1, dur2) ), NoOverlap( (var1, var3), (dur1, dur3) ), NoOverlap( (var2, var3), (dur2, dur3) ) ] ) model.add( ur ) solver = Solver(model) assert(solver.solve()) assert ( (var1.get_value() + dur1 < var2.get_value ) or ( var2.get_value() + dur2 < var1.get_value ) )
def testOr_2(self): var1 = Variable(0, 2) dur1 = 1 var2 = Variable(0, 4) dur2 = 2 var3 = Variable(0, 6) dur3 = 3 model = NativeModel() #model.add( NoOverlap( (var1, var2), (dur1, dur2) ) ) #model.add( NoOverlap( (var1, var3), (dur1, dur3) ) ) #model.add( NoOverlap( (var2, var3), (dur2, dur3) ) ) ur = UnaryResource([ NoOverlap((var1, var2), (dur1, dur2)), NoOverlap((var1, var3), (dur1, dur3)), NoOverlap((var2, var3), (dur2, dur3)) ]) model.add(ur) solver = Solver(model) assert (solver.solve()) assert ((var1.get_value() + dur1 < var2.get_value) or (var2.get_value() + dur2 < var1.get_value))
def testSumConstant(self): obj1 = Variable(list(range(0,6))) obj2 = Variable(list(range(0,8))) obj3 = Variable(list(range(0,11))) capacity = Variable(list(range(34,35))) volumes = [7, 5, 3] model = NativeModel() vol_sum = Sum(((obj1, obj2, obj3), volumes)) model.add_variable((obj1, obj2, obj3, vol_sum, capacity)) model.add_constraint(Leq((vol_sum, capacity))) solver = Solver(model) ''' obj1.print_domain() obj2.print_domain() obj3.print_domain() vol_sum.print_domain() capacity.print_domain() ''' assert( solver.solve() ) '''
def testSumConstant(self): obj1 = Variable(list(range(0, 6))) obj2 = Variable(list(range(0, 8))) obj3 = Variable(list(range(0, 11))) capacity = Variable(list(range(34, 35))) volumes = [7, 5, 3] model = NativeModel() vol_sum = Sum(((obj1, obj2, obj3), volumes)) model.add_variable((obj1, obj2, obj3, vol_sum, capacity)) model.add_constraint(Leq((vol_sum, capacity))) solver = Solver(model) ''' obj1.print_domain() obj2.print_domain() obj3.print_domain() vol_sum.print_domain() capacity.print_domain() ''' assert (solver.solve()) '''
def testAtLeastUNSAT_2(self): model = NativeModel() var1, var2 = (Variable(0,5), Variable(0,5)) model << (AtLeast(([var1, var2], 0, 3))) solver = Solver(model) assert(not solver.solve())
def testOperatorOVerloading(self): var1 = Variable(list(range(0, 4))) var2 = Variable(list(range(0, 4))) model = NativeModel() model.add_constraint(var1 != var2) solver = Solver(model) assert (solver.solve())
def testExactlyUNSAT_2(self): model = NativeModel() var1, var2 = (Variable(0,5), Variable(0,5)) model << (Exactly(((var1, var2), 2, 3))) solver = Solver(model) assert(not solver.solve())
def testTimesOverNativeModelOp(self): var1, var2 = (Variable(list(range(1,4))), Variable(list(range(0,4)))) model = NativeModel() model << (var1 == var2 * 2) solver = Solver(model) assert(solver.solve())
def testOperatorOVerloading(self): var1 = Variable(list(range(0,4))) var2 = Variable(list(range(0,4))) model = NativeModel() model.add_constraint(var1 != var2) solver = Solver(model) assert(solver.solve())
def testMinusOver(self): var1, var2 = (Variable(list(range(0,4))), Variable(list(range(0,4)))) model = NativeModel() model.add_constraint(var1 == var2 - 3) solver = Solver(model) assert(solver.solve())
def testExactlyUNSAT_2(self): model = NativeModel() var1, var2 = (Variable(0, 5), Variable(0, 5)) model << (Exactly(((var1, var2), 2, 3))) solver = Solver(model) assert (not solver.solve())
def testAtMostUNSAT_2(self): model = NativeModel() var1, var2 = (Variable(2, 2), Variable(0, 1)) model << (AtMost(([var1, var2], 2, 2))) solver = Solver(model) assert (solver.solve())
def testTimesOverNativeModelOp(self): var1, var2 = (Variable(list(range(1, 4))), Variable(list(range(0, 4)))) model = NativeModel() model << (var1 == var2 * 2) solver = Solver(model) assert (solver.solve())
def testMinusOverNativeModelOp(self): var1, var2 = (Variable(range(0,4)), Variable(range(0,4))) model = NativeModel() model << (var1 == var2 - 3) solver = Solver(model) assert(solver.solve())
def testTimesOver(self): var1, var2 = (Variable(range(1,4)), Variable(range(0,4))) model = NativeModel() model.add_constraint(var1 == var2 * 2) solver = Solver(model) assert(solver.solve())
def testMinusOver(self): var1, var2 = (Variable(list(range(0, 4))), Variable(list(range(0, 4)))) model = NativeModel() model.add_constraint(var1 == var2 - 3) solver = Solver(model) assert (solver.solve())
def testAtMostUNSAT_2(self): model = NativeModel() var1, var2 = (Variable(2,2), Variable(0,1)) model << (AtMost(([var1, var2], 2, 2))) solver = Solver(model) assert(solver.solve())
def testAtLeastUNSAT_2(self): model = NativeModel() var1, var2 = (Variable(0, 5), Variable(0, 5)) model << (AtLeast(([var1, var2], 0, 3))) solver = Solver(model) assert (not solver.solve())
def testMinus(self): var1, var2 = (Variable(list(range(0,4))), Variable(list(range(0,4)))) model = NativeModel() model.add_variable((var1, var2)) model.add_constraint(Equal((var1, Minus(var2, 3)))) solver = Solver(model) assert(solver.solve())
def testExactlySAT_2(self): model = NativeModel() var1, var2 = (Variable(0, 5), Variable(0, 5)) model << (Exactly(((var1, var2), 2, 1))) solver = Solver(model) assert (solver.solve()) assert ((var1.get_value() is 2 or var2.get_value() is 2))
def testGTOver(self): var1, var2, var3 = (Variable(list(range(0,3))) for x in range(0,3)) model = NativeModel() model.add_constraint(var1 < var2) model.add_constraint(var2 < var3) model.add_constraint(var1 < var3) solver = Solver(model) assert(solver.solve())
def testEqual(self): var1 = Variable([0]) var2 = Variable(list(range(0, 1))) model = NativeModel() model.add_variable((var1, var2)) solver = Solver(model) assert (solver.solve())
def testGTOver(self): var1, var2, var3 = (Variable(list(range(0, 3))) for x in range(0, 3)) model = NativeModel() model.add_constraint(var1 < var2) model.add_constraint(var2 < var3) model.add_constraint(var1 < var3) solver = Solver(model) assert (solver.solve())
def testMinus(self): var1, var2 = (Variable(list(range(0, 4))), Variable(list(range(0, 4)))) model = NativeModel() model.add_variable((var1, var2)) model.add_constraint(Equal((var1, Minus(var2, 3)))) solver = Solver(model) assert (solver.solve())
def testEqualOver(self): var1 = Variable([0]) var2 = Variable(list(range(0, 1))) model = NativeModel() model.add_constraint(var1 == var2) solver = Solver(model) assert (solver.solve())
def testExactlySAT_2(self): model = NativeModel() var1, var2 = (Variable(0,5), Variable(0,5)) model << (Exactly(((var1, var2), 2, 1))) solver = Solver(model) assert(solver.solve()) assert((var1.get_value() is 2 or var2.get_value() is 2))
def testEqualOver(self): var1 = Variable([0]) var2 = Variable(list(range(0,1))) model = NativeModel() model.add_constraint(var1 == var2) solver = Solver(model) assert(solver.solve())
def testEqual(self): var1 = Variable([0]) var2 = Variable(list(range(0,1))) model = NativeModel() model.add_variable((var1, var2)) solver = Solver(model) assert(solver.solve())
def solveSchedule(): # Parse the data in a way that seems easy at 2am profTimes = dict(map( lambda x: (x[0], x[1:]), map( lambda x: map(str, x.replace('"','').strip().split(",")), open(sys.argv[1]).readlines()))) students = dict(map(lambda z: (z[0], z[1:]), map( lambda y: filter(lambda x: len(x) > 0, y), map(lambda x: x.strip().split(","), open(sys.argv[2]).readlines())))) model = Model() # Set up the professors as lists of variables profResources = {} for prof in visit_day_scheduler.PROFESSORS: profResources[prof] = [Variable(i, i) for i in range(len(profTimes[prof])) if profTimes[prof][i].find('NO') == 0] # Now add meetings studentMeetings = {} obj = [] for s in visit_day_scheduler.VISITORS: # 4 is a magic number which seems to make things SAT so mistral stays happy studentMeetings[s] = [Variable(0, 4*visit_day_scheduler.NUM_SLOTS) for i in students[s]] for i in range(len(students[s])): if profResources.has_key(students[s][i]): profResources[ students[s][i] ].append(studentMeetings[s][i]) if len(studentMeetings[s]) > 1: model.add(AllDiff(studentMeetings[s])) # try to put meetings in actual slots, not fake ones obj_test = [i > visit_day_scheduler.NUM_SLOTS - 1 for i in studentMeetings[s]] obj.extend(obj_test) model.add([AllDiff(x) for x in profResources.values() if len(x) > 1]) model.add(Minimize(Sum(obj))) solver = Solver(model) solver.setVerbosity(2) solver.setTimeLimit(10) if solver.solve() or True: outfile = open("out.csv", "wb") outwriter = csv.writer(outfile) outwriter.writerow(header) for s in visit_day_scheduler.VISITORS: outRow = [s] + ["" for i in range(visit_day_scheduler.NUM_SLOTS)] for i in range(len(studentMeetings[s])): if profResources.has_key(students[s][i]): meeting = int(str(studentMeetings[s][i])) if meeting < visit_day_scheduler.NUM_SLOTS: # only output real meetings outRow[meeting + 1] = str(students[s][i]) outwriter.writerow(outRow)
def testStupid_not_eqOver(self): var1 = Variable(list(range(0, 3))) var2 = Variable(list(range(0, 3))) model = NativeModel() model.add_constraint(var1 != var2) solver = Solver(model) assert (solver.solve()) assert (var1.get_value() != var2.get_value())
def testNegDomains(self): var1 = Variable(-3, -1) var2 = Variable(-2, 0) model = NativeModel() model << (var1 * 2 <= var2) solver = Solver(model) assert (solver.solve())
def testGT(self): var1, var2, var3 = (Variable(range(0,3)) for x in range(0,3)) model = NativeModel() model.add_variable((var1, var2, var3)) model.add_constraint(Gt((var1, var2))) model.add_constraint(Gt((var2, var3))) model.add_constraint(Gt((var1, var3))) solver = Solver(model) assert(solver.solve())
def testAtLeastSAT(self): model = NativeModel() var1, var2 = (Variable(0, 5), Variable(0, 5)) model << (AtLeast(([var1, var2], 2, 2))) solver = Solver(model) assert (solver.solve()) assert (var1.get_value() is 2) assert (var2.get_value() is 2)
def testNotUnsat(self): var1 = Variable(0, 1) var2 = Variable(2, 3) model = NativeModel() model << (Not(var1 != var2)) solver = Solver(model) assert(not solver.solve())
def testAtLeastSAT(self): model = NativeModel() var1, var2 = (Variable(0,5), Variable(0,5)) model << (AtLeast(([var1, var2], 2, 2))) solver = Solver(model) assert(solver.solve()) assert(var1.get_value() is 2) assert(var2.get_value() is 2)
def testNotUnsat(self): var1 = Variable(0, 1) var2 = Variable(2, 3) model = NativeModel() model << (Not(var1 != var2)) solver = Solver(model) assert (not solver.solve())
def testStupid_not_eqOver(self): var1 = Variable(list(range(0,3))) var2 = Variable(list(range(0,3))) model = NativeModel() model.add_constraint(var1 != var2) solver = Solver(model) assert(solver.solve()) assert(var1.get_value() != var2.get_value())
def testNegDomains(self): var1 = Variable(-3, -1) var2 = Variable(-2, 0) model = NativeModel() model << (var1*2 <= var2) solver = Solver(model) assert(solver.solve())
def testAnd_3(self): var1 = Variable(1, 1) var2 = Variable(2, 2) var3 = Variable(1, 1) model = NativeModel() model << (And((var1 == var2, var1 == var3))) solver = Solver(model) assert (not solver.solve())
def testNAnd_1(self): var1 = Variable(0, 1) var2 = Variable(1, 2) var3 = Variable(0, 1) model = NativeModel() model << (Nand((var1 != var2, var2 != var3))) solver = Solver(model) assert (solver.solve())
def testOr2(self): var1 = Variable(0, 1) var2 = Variable(1, 2) var3 = Variable(0, 1) model = NativeModel() model << (Or((var1 == var2, var2 != var3))) solver = Solver(model) assert ( solver.solve() )
def testOr2(self): var1 = Variable(0, 1) var2 = Variable(1, 2) var3 = Variable(0, 1) model = NativeModel() model << (Or((var1 == var2, var2 != var3))) solver = Solver(model) assert (solver.solve())
def testOr3(self): var1 = Variable(0, 0) var2 = Variable(0, 0) var3 = Variable(0, 0) model = NativeModel() model << (And((var1 != var2, var2 != var3))) solver = Solver(model) assert (not solver.solve())
def testOr3(self): var1 = Variable(0, 0) var2 = Variable(0, 0) var3 = Variable(0, 0) model = NativeModel() model << (And((var1 != var2, var2 != var3))) solver = Solver(model) assert ( not solver.solve() )
def testNAnd_1(self): var1 = Variable(0, 1) var2 = Variable(1, 2) var3 = Variable(0, 1) model = NativeModel() model << (Nand((var1 != var2, var2 != var3))) solver = Solver(model) assert ( solver.solve() )
def testAnd_3(self): var1 = Variable(1, 1) var2 = Variable(2, 2) var3 = Variable(1, 1) model = NativeModel() model << (And((var1 == var2, var1 == var3))) solver = Solver(model) assert ( not solver.solve() )
def testVarExtractionWSum(self): var1 = Variable(list(range(2, 3))) var2 = Variable(list(range(2, 3))) var3 = Variable(list(range(3, 10))) model = NativeModel() model.add_constraint(Equal((Sum(((var1, var2), (2, 1))), var3))) solver = Solver(model) #svar.print_domain() assert (solver.solve())
def testVarExtractionWSum(self): var1 = Variable(list(range(2,3))) var2 = Variable(list(range(2,3))) var3 = Variable(list(range(3,10))) model = NativeModel() model.add_constraint(Equal((Sum(((var1, var2), (2,1))), var3))) solver = Solver(model) #svar.print_domain() assert( solver.solve() )
def testFeasTuple_1(self): var1 = Variable(0, 0) var2 = Variable(0, 0) tups = ((0,0),) model = NativeModel() model.add(Table((var1, var2), tups)) solver = Solver(model) assert(solver.solve())
def testFeasTuple_1(self): var1 = Variable(0, 0) var2 = Variable(0, 0) tups = ((0, 0), ) model = NativeModel() model.add(Table((var1, var2), tups)) solver = Solver(model) assert (solver.solve())
def testLEQ(self): var1, var2, var3 = (Variable(list(range(0, 3))) for x in range(0, 3)) model = NativeModel() model.add_variable((var1, var2, var3)) model.add_constraint(Leq((var1, var2))) model.add_constraint(Leq((var2, var3))) model.add_constraint(Leq((var1, var3))) model.add_constraint(Equal((var1, var2))) model.add_constraint(NotEqual((var2, var3))) solver = Solver(model) assert (solver.solve())
def taestElementSAT_1(self): model = NativeModel() i = Variable(0, 5) k = Variable(1, 5) coef = [0, 1, 2, 3, 4, 5] # Make sure that coef[k] = i model << (Element((i, k, coef))) solver = Solver(model) assert (solver.solve()) assert (i.get_value() is coef[k.get_value()])
def testMinusVars(self): var1 = Variable(0, 3) var2 = Variable(0, 1) var3 = Variable(0, 0) model = NativeModel() minVar = var1 - var2 model << (minVar == var3) solver = Solver(model) assert (solver.solve())
def taestElementSAT_1(self): model = NativeModel() i = Variable(0,5) k = Variable(1,5) coef = [0,1,2,3,4,5] # Make sure that coef[k] = i model << (Element((i, k, coef))) solver = Solver(model) assert(solver.solve()) assert(i.get_value() is coef[k.get_value()])
def testDecVars(self): var1 = Variable(0, 3) var2 = Variable(0, 5) var3 = Variable(1, 2) model = NativeModel() model.add(var1 == var2) model.add(var1 != var3) solver = Solver(model, [var1, var2]) assert (solver.solve()) assert (not var3.get_is_instantiated())
def testLEQ(self): var1, var2, var3 = (Variable(list(range(0,3))) for x in range(0,3)) model = NativeModel() model.add_variable((var1, var2, var3)) model.add_constraint(Leq((var1, var2))) model.add_constraint(Leq((var2, var3))) model.add_constraint(Leq((var1, var3))) model.add_constraint(Equal((var1, var2))) model.add_constraint(NotEqual((var2, var3))) solver = Solver(model) assert(solver.solve())
def testNand_2(self): var1 = Variable(0, 0) var2 = Variable(0, 0) var3 = Variable(0, 0) model = NativeModel() model << (Nand((var1 == var2, var2 == var3))) solver = Solver(model) solved = solver.solve() assert (not solved)
def testHeurSel(self): var1 = Variable(0, 3) var2 = Variable(0, 5) var3 = Variable(1, 2) model = NativeModel() model.add(var1 == var2) model.add(var1 != var3) solver = Solver(model) solver.setHeuristic("RandomVar", "RandomVal") assert (solver.solve())
def testSum(self): var1 = Variable(list(range(0, 3))) var2 = Variable(list(range(0, 3))) var3 = Variable(list(range(3, 5))) model = NativeModel() svar = Sum((var1, var2)) model.add_variable((var1, var2, var3, svar)) model.add_constraint(Equal((svar, var3))) solver = Solver(model) assert (solver.solve())