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()
Пример #2
0
 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  ) )
Пример #3
0
    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))
Пример #4
0
 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() )
      
      '''
Пример #5
0
    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())
        '''
Пример #6
0
 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())
Пример #7
0
    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())
Пример #8
0
 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())
Пример #9
0
 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())
Пример #10
0
 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())
Пример #11
0
 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())
Пример #12
0
    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())
Пример #13
0
    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())
Пример #14
0
    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())
Пример #15
0
 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())
Пример #16
0
 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())
Пример #17
0
    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())
Пример #18
0
 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())
Пример #19
0
    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())
Пример #20
0
 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())
Пример #21
0
    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))
Пример #22
0
 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())
Пример #23
0
    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())
Пример #24
0
    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())
Пример #25
0
    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())
Пример #26
0
    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())
Пример #27
0
 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))
Пример #28
0
 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())
Пример #29
0
 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)
Пример #31
0
    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())
Пример #32
0
    def testNegDomains(self):
        var1 = Variable(-3, -1)
        var2 = Variable(-2, 0)

        model = NativeModel()

        model << (var1 * 2 <= var2)

        solver = Solver(model)

        assert (solver.solve())
Пример #33
0
 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())
Пример #34
0
    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)
Пример #35
0
   def testNotUnsat(self):
       var1 = Variable(0, 1)
       var2 = Variable(2, 3)
 
       model = NativeModel()
       
       model << (Not(var1 != var2))
       
       solver = Solver(model)
       
       assert(not solver.solve())
Пример #36
0
 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)
Пример #37
0
    def testNotUnsat(self):
        var1 = Variable(0, 1)
        var2 = Variable(2, 3)

        model = NativeModel()

        model << (Not(var1 != var2))

        solver = Solver(model)

        assert (not solver.solve())
Пример #38
0
 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())
Пример #39
0
    def testNegDomains(self):
        var1 = Variable(-3, -1)
        var2 = Variable(-2, 0)
        
        model = NativeModel()

        model << (var1*2 <= var2)
        
        solver = Solver(model)
        
        assert(solver.solve())
Пример #40
0
    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())
Пример #41
0
    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())
Пример #42
0
 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() )
Пример #43
0
    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())
Пример #44
0
    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())
Пример #45
0
 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() )
Пример #46
0
 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() )
Пример #47
0
 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() )
Пример #48
0
    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())
Пример #49
0
 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() )
Пример #50
0
 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())
Пример #51
0
    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())
Пример #52
0
    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())
Пример #53
0
    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()])
Пример #54
0
    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())
Пример #55
0
 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()])
Пример #56
0
    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())
Пример #57
0
 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())
Пример #58
0
    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)
Пример #59
0
    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())
Пример #60
0
    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())