this assignment you will use the given code to solve the Map Coloring, and the n-Queens For this assignment, we will be using a library called simpleai You have been given a file called backtracking.py. Study the code carefully as this code demonstrates how to call the backtracking search algorithm that uses the AC-3 algorithm for arc consistency and the MRV (Most Constrained Variable) and LCV (Least Constraining Value) heuristics to solve a given CSP problem. A simple example is already given to you that shows how to solve a three variable CSP problem. Please see the code for the details. Note carefully how variables, domain, and the constraints are defined in the problem. Also, note that constraints need to be defined as explicit constraints. In the example, three different constraints are defined that are to be applied on the three variables. As mentioned above, you will need to define explicit constraints in this library for solving your problems. You will edit this code file and write your assignment solution. The lines of the code that you should not change are: a) The top two import statements. b) The last three lines that demonstrate how to call the backtracking search with all heuristics to solve the problem and print the CSP solution.
In this assignment you will use the given code to solve the Map Coloring, and the n-Queens
For this assignment, we will be using a library called simpleai
You have been given a file called backtracking.py. Study the code carefully as this code demonstrates how to call the backtracking search
A simple example is already given to you that shows how to solve a three variable CSP problem. Please see the code for the details. Note carefully how variables, domain, and the constraints are defined in the problem. Also, note that constraints need to be defined as explicit constraints. In the example, three different constraints are defined that are to be applied on the three variables.
As mentioned above, you will need to define explicit constraints in this library for solving your problems. You will edit this code file and write your assignment solution. The lines of the code that you should not change are: a) The top two import statements.
b) The last three lines that demonstrate how to call the backtracking search with all heuristics to solve the problem and print the CSP solution.
backtracking.py; from simpleai.search import CspProblem variables = ('A', 'B', 'C') domains = { # a constraint that expects different variables to have different values # a constraint that expects one variable to be bigger than other # a constraint thet expects two variables to be one odd and the other even, constraints = [ my_problem = CspProblem(variables, domains, constraints) result = backtrack(my_problem, variable_heuristic='MOST_CONSTRAINED_VARIABLE', value_heuristic='LEAST_CONSTRAINING_VALUE', inference=True) print(result) |
Step by step
Solved in 2 steps