def check_model_1_constraints_GAC(stu_model): score = 0 print("---starting check_model_1_constraints_GAC---") try: board = [['0', '.', '0', '.', '0', '.', '0', '.', '0','.', '0', '.', '0'],['0', '.', '0', '.', '0', '.', '0', '.', '0','.', '0', '.', '0'],['0', '.', '0', '.', '0', '.', '0', '.', '0','.', '0', '.', '0'],['0', '.', '0', '.', '0', '.', '0', '.', '0','.', '0', '.', '0'],['0', '.', '0', '.', '0', '.', '0', '.', '0','.', '0', '.', '0'],['0', '.', '0', '.', '0', '.', '0', '.', '0','.', '0', '.', '0'],['0', '.', '0', '.', '0', '.', '0', '.', '0','.', '0', '.', '0']] #board = [[3,'.',0,'.',0,'<',0],[0,'.',0,'.',0,'.',0],[0,'.',0,'<',0,'.',0],[0,'.',0,'>',0,'.',1]]; #answer = [[3], [1, 2, 4], [1, 2], [2, 4], [1, 2, 4], [1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4], [1, 2, 4], [1, 2, 3], [2, 3, 4], [2, 3, 4], [2, 4], [3, 4], [2, 3], [1]] csp, var_array = stu_model.futoshiki_csp_model_1(board) lister = [] soln_propagators.prop_GAC(csp) for i in range(4): for j in range(4): lister.append(var_array[i][j].cur_domain()) #if lister != answer: # print("FAILED\nExpected Output: %r\nOutput Received: %r" % (answer,lister)) #else: # print("PASS") # score = 2 except Exception: print("Error occurred: %r" % traceback.print_exc()) print("---finished check_model_1_constraints_GAC---\n") return score
def check_model_2_constraints_GAC(stu_model): score = 0 print("---starting check_model_2_constraints_GAC---") try: board = [[3, '.', 0, '.', 0, '<', 0], [0, '.', 0, '.', 0, '.', 0], [0, '.', 0, '<', 0, '.', 0], [0, '.', 0, '>', 0, '.', 1]] answer = [[3], [1, 2, 4], [1, 2], [2, 4], [1, 2, 4], [1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4], [1, 2, 4], [1, 2, 3], [2, 3, 4], [2, 3, 4], [2, 4], [3, 4], [2, 3], [1]] csp, var_array = stu_model.futoshiki_csp_model_2(board) lister = [] soln_propagators.prop_GAC(csp) for i in range(4): for j in range(4): lister.append(var_array[i][j].cur_domain()) if lister != answer: print("FAILED\nExpected Output: %r\nOutput Received: %r" % (answer, lister)) else: print("PASS") score = 2 except Exception: print("Error occurred: %r" % traceback.print_exc()) print("---finished check_model_2_constraints_GAC---\n") return score
def three_queen_GAC(): score = 0 try: queens = nQueens(8) curr_vars = queens.get_all_vars() curr_vars[0].assign(4) curr_vars[2].assign(1) curr_vars[7].assign(5) prop_GAC(queens) answer = [[4], [6, 7, 8], [1], [3, 8], [6, 7], [2, 8], [2, 3, 7, 8], [5]] var_vals = [x.cur_domain() for x in curr_vars] print(var_vals) print(answer) if var_vals != answer: details = "Failed three queens GAC test: variable domains don't match expected results" else: score = 1 details = "" except Exception: details = "One or more runtime errors occurred while testing GAC with three queens: %r" % traceback.format_exc( ) return score, details
def check_model_2_constraints(stu_model): score = 0 print("---starting check_model_2_constraints---") try: board = [[0, 0, 8, 7, 0, 0, 0, 5, 0], [0, 3, 0, 0, 1, 0, 0, 9, 0], [0, 0, 0, 5, 0, 0, 1, 0, 0], [4, 0, 3, 0, 0, 7, 0, 0, 0], [9, 7, 0, 0, 0, 0, 0, 1, 8], [0, 0, 0, 8, 0, 0, 3, 0, 9], [0, 0, 6, 0, 0, 4, 0, 0, 0], [0, 9, 0, 0, 8, 0, 0, 2, 0], [0, 5, 0, 0, 0, 1, 8, 0, 0]] answer = [[1], [4], [8], [7], [2], [9], [6], [5], [3], [6], [3], [5], [4], [1], [8], [2], [9], [7], [7], [2], [9], [5], [6], [3], [1], [8], [4], [4], [8], [3], [1], [9], [7], [5], [6], [2], [9], [7], [2], [3], [5], [6], [4], [1], [8], [5], [6], [1], [8], [4], [2], [3], [7], [9], [8], [1], [6], [2], [7], [4], [9], [3], [5], [3], [9], [4], [6], [8], [5], [7], [2], [1], [2], [5], [7], [9], [3], [1], [8], [4], [6]] csp, var_array = stu_model.sudoku_csp_model_2(board) lister = [] soln_propagators.prop_GAC(csp) for i in range(9): for j in range(9): lister.append(var_array[i][j].cur_domain()) if lister != answer: print("FAILED\nExpected Output: %r\nOutput Received: %r" % (answer, lister)) else: print("PASS") score = 2 except Exception: print("Error occurred: %r" % traceback.print_exc()) print("---finished check_model_2_constraints---\n") return score
def test_simple_GAC(): did_fail = False score = 0 try: queens = nQueens(8) curr_vars = queens.get_all_vars() curr_vars[0].assign(1) prop_GAC(queens, newVar=curr_vars[0]) answer = [[1], [3, 4, 5, 6, 7, 8], [2, 4, 5, 6, 7, 8], [2, 3, 5, 6, 7, 8], [2, 3, 4, 6, 7, 8], [2, 3, 4, 5, 7, 8], [2, 3, 4, 5, 6, 8], [2, 3, 4, 5, 6, 7]] var_domain = [x.cur_domain() for x in curr_vars] print(var_domain) print(answer) for i in range(len(curr_vars)): if var_domain[i] != answer[i]: details = "Failed simple GAC test: variable domains don't match expected results." did_fail = True break if not did_fail: score = 1 details = "" except Exception: details = "One or more runtime errors occurred while testing simple GAC: %r" % traceback.format_exc( ) return score, details
def test_simple_GAC(self): queens = nQueens(8) curr_vars = queens.get_all_vars() curr_vars[0].assign(1) propagators.prop_GAC(queens,newVar=curr_vars[0]) answer = [[1],[3,4,5,6,7,8],[2,4,5,6,7,8],[2,3,5,6,7,8],[2,3,4,6,7,8],[2,3,4,5,7,8],[2,3,4,5,6,8],[2,3,4,5,6,7]] var_domain = [x.cur_domain() for x in curr_vars] for i in range(len(curr_vars)): self.assertEqual(var_domain[i], answer[i], "Failed simple GAC test: variable domains don't match expected results")
def test_DWO_GAC(self): queens = nQueens(6) cur_var = queens.get_all_vars() cur_var[0].assign(2) pruned = propagators.prop_GAC(queens,newVar=cur_var[0]) self.assertTrue(pruned[0], "Failed a FC test: should have resulted in a DWO") cur_var[1].assign(5) pruned = propagators.prop_GAC(queens,newVar=cur_var[1]) self.assertFalse(pruned[0], "Failed a FC test: should a resulted in a DWO")
def val_lcv(csp, var): domains = var.cur_domain().copy() affected = {} for d in domains: var.assign(d) flag, pruned = prop_GAC(csp, var) if flag: affected[d] = len(pruned) for v, d in pruned: v.unprune_value(d) var.unassign() return sorted(affected, key=affected.__getitem__, reverse=True)
def check_model_1_constraints(stu_model): score = 0 print("---starting check_model_1_constraints---") try: board = [[0,0,8,7,0,0,0,5,0],[0,3,0,0,1,0,0,9,0],[0,0,0,5,0,0,1,0,0],[4,0,3,0,0,7,0,0,0],[9,7,0,0,0,0,0,1,8],[0,0,0,8,0,0,3,0,9],[0,0,6,0,0,4,0,0,0],[0,9,0,0,8,0,0,2,0],[0,5,0,0,0,1,8,0,0]] answer = [[1, 2, 6], [1, 2, 4, 6], [8], [7], [2, 3, 4, 6, 9], [2, 3, 6, 9], [2, 4, 6], [5], [2, 3, 4, 6], [2, 5, 6, 7], [3], [2, 4, 5, 7], [2, 4, 6], [1], [2, 6, 8], [2, 4, 6, 7], [9], [2, 4, 6, 7], [2, 6, 7], [2, 4, 6], [2, 4, 7, 9], [5], [2, 3, 4, 6, 9], [2, 3, 6, 8, 9], [1], [3, 4, 7, 8], [2, 3, 4, 6, 7], [4], [1, 2, 8], [3], [1, 2, 9], [2, 5, 9], [7], [2, 5], [6], [2, 5], [9], [7], [2, 5], [2, 3, 4, 6], [2, 3, 4, 5, 6], [2, 3, 5, 6], [2, 4, 5], [1], [8], [1, 2, 5, 6], [1, 2, 6], [1, 2, 5], [8], [2, 4, 5, 6], [2, 5, 6], [3], [4, 7], [9], [1, 2, 3, 7, 8], [1, 2, 8], [6], [2, 3, 9], [2, 3, 5, 7, 9], [4], [5, 7, 9], [3, 7], [1, 3, 5, 7], [1, 3, 7], [9], [1, 4, 7], [3, 6], [8], [3, 5, 6], [4, 5, 6, 7], [2], [1, 3, 4, 5, 6, 7], [2, 3, 7], [5], [2, 4, 7], [2, 3, 6, 9], [2, 3, 6, 7, 9], [1], [8], [3, 4, 7], [3, 4, 6, 7]] csp, var_array = stu_model.sudoku_csp_model_1(board) lister = [] soln_propagators.prop_GAC(csp) for i in range(9): for j in range(9): lister.append(var_array[i][j].cur_domain()) if lister != answer: print("FAILED\nExpected Output: %r\nOutput Received: %r" % (answer,lister)) else: print("PASS") score = 2 except Exception: print("Error occurred: %r" % traceback.print_exc()) print("---finished check_model_1_constraints---\n") return score
def check_model_2_constraints4(stu_model): score = 0 print("---starting check_scheduler_constraints4---") try: current_list = ["Non-real", "fake_course"] answer = [['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'],\ ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'],\ ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'],\ ['NOCOURSE'], ['NOCOURSE'],\ ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'],\ ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'],\ ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'],\ ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'],\ ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'],\ ['NOCOURSE'],\ ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'],\ ['NOCOURSE'], ['NOCOURSE'],\ ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'], ['NOCOURSE'],\ ] csp, var_array = stu_model.weekly_planner_csp(current_list) lister = [] soln_propagators.prop_GAC(csp) for i in range(5): for j in range(24): lister.append(var_array[i][j].cur_domain()) if lister != answer: print("FAILED\nExpected Output: %r\nOutput Received: %r" % (answer, lister)) else: print("PASS") score = 2 except Exception: print("Error occurred: %r" % traceback.print_exc()) print("---finished check_scheduler_constraints4---\n") return score