def check_model_2_constraints_FC(stu_model): score = 0 print("---starting check_model_2_constraints_FC---") try: board = [[3, '.', 0, '.', 0, '<', 0], [0, '.', 0, '.', 0, '.', 0], [0, '.', 0, '<', 0, '.', 0], [0, '.', 0, '>', 0, '.', 1]] answer = [[3], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1]] csp, var_array = stu_model.futoshiki_csp_model_2(board) lister = [] soln_propagators.prop_FC(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_FC---\n") return score
def three_queen_FC(): 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_FC(queens) answer = [[4], [6, 7, 8], [1], [3, 6, 8], [6, 7], [2, 6, 8], [2, 3, 7, 8], [5]] var_vals = [x.cur_domain() for x in curr_vars] if var_vals != answer: details = "Failed three queens FC test: variable domains don't match expected results" else: score = 1 details = "" except Exception: details = "One or more runtime errors occurred while testing FC with three queens: %r" % traceback.format_exc( ) return score, details
def check_model_1_constraints_FC(stu_model): score = 0 print("---starting check_model_1_constraints_FC---") try: board = [[3,'.',0,'.',0,'<',0], [0,'.',0,'.',0,'.',0], [0,'.',0,'<',0,'.',0], [0,'.',0,'>',0,'.',1]]; answer = [[3], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1]] csp, var_array = stu_model.futoshiki_csp_model_1(board) lister = [] soln_propagators.prop_FC(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_FC---\n") return score
def test_simple_FC(): did_fail = False score = 0 try: queens = nQueens(8) curr_vars = queens.get_all_vars() curr_vars[0].assign(1) prop_FC(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)): if var_domain[i] != answer[i]: details = "Failed simple FC 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 FC: %r" % traceback.format_exc( ) return score, details
def test_simple_FC(self): queens = nQueens(8) curr_vars = queens.get_all_vars() curr_vars[0].assign(1) propagators.prop_FC(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 FC test: variable domains don't match expected results")
def test_PMOK2_FC( self): #tests the incorrect solution of a 4 queens problem queens = nQueens(4) cur_var = queens.get_all_vars() cur_var[0].assign(4) pruned = propagators.prop_FC(queens, newVar=cur_var[0]) self.assertTrue(pruned[0], "Failed a FC test: returned DWO too early.1") cur_var[1].assign(2) pruned = propagators.prop_FC(queens, newVar=cur_var[1]) self.assertFalse(pruned[0], "Failed a FC test: did not return DWO.2")
def test_DWO_FC(self): queens = nQueens(6) cur_var = queens.get_all_vars() cur_var[0].assign(2) pruned = propagators.prop_FC(queens,newVar=cur_var[0]) self.assertTrue(pruned[0], "Failed a FC test: returned DWO too early.") cur_var[1].assign(5) pruned = propagators.prop_FC(queens,newVar=cur_var[1]) self.assertTrue(pruned[0], "Failed a FC test: returned DWO too early.") cur_var[4].assign(1) pruned = propagators.prop_FC(queens,newVar=cur_var[4]) self.assertFalse(pruned[0], "Failed a FC test: should have resulted in a DWO")
def test_PMOK_FC(self): #tests the correct solution of a 4 queens problem queens = nQueens(4) cur_var = queens.get_all_vars() cur_var[0].assign(2) pruned = propagators.prop_FC(queens, newVar=cur_var[0]) self.assertTrue(pruned[0], "Failed a FC test: returned DWO too early.") cur_var[1].assign(4) pruned = propagators.prop_FC(queens, newVar=cur_var[1]) self.assertTrue(pruned[0], "Failed a FC test: returned DWO too early.") cur_var[2].assign(1) pruned = propagators.prop_FC(queens, newVar=cur_var[2]) self.assertTrue(pruned[0], "Failed a FC test: returned DWO too early.") cur_var[3].assign(3) pruned = propagators.prop_FC(queens, newVar=cur_var[3]) self.assertTrue(pruned[0], "Failed a FC test: returned DWO too early.")
def val_lcv(csp, var): ''' Assign a value (from the variable's domain) to the given variable then leverage FC propagation to determine how many values are being pruned due to the assignment. Return a sorted (according to the number of pruned values) list of values. ''' domain = var.cur_domain() returnList = [] dictionary = {} for value in domain: var.assign(value) garbage, prunedList = propagators.prop_FC(csp, var) dictionary[value] = len(prunedList) #reset for variable, val in prunedList: variable.unprune_value(val) var.unassign() #sort the dictionary according to the number of the pruned values dictionarySorted = sorted(dictionary.items(), key=lambda x: x[1]) for key in dictionarySorted: returnList.append(int(key[0])) return returnList
def val_lcv(csp, var): val_lcv = [] for val in var.cur_domain(): var.assign(val) status, pruned = prop_FC(csp, var) var.unassign() for var_p, val_p in pruned: var_p.unprune_value(val_p) if status: val_lcv.append((val, len(pruned))) val_lcv.sort(key=lambda v: v[1]) return [v[0] for v in val_lcv]