Question: In this assignment, you will tormulate the NQueens problem as a search problem and solve it using traditional search algorithms: Breadth - First Search (

In this assignment, you will tormulate the NQueens problem as a search problem and solve it using
traditional search algorithms:
Breadth-First Search (BFS),
Uniform Cost Search (UCS),
Depth First Search(DFS),
Depth Limited Search (DLS),
Iterative Deepening Search (IDS),
Greedy Search, and A* Search.
assume that you are allowed to move a single queen in its column at each step and
each move is of cost 1. In other words, each action will be in the form of "Move Queen i to row j", where
1i,jN.
3.1 Implementation
Use your solution to PA1 and do the following modifications.
You have to define NQueens class as a child of abstract SearchProblem (see
models.py).
There are 3 main functions (actions, is_goal, result) that you have to implement for uninformed
search algorithms, and one function (heuristic) for informed search algorithms. (In addition to
these functions, you may define internal helper functions if you need.)
__init_(self,N): Class constructor that initializes the attributes. Calls parent class constructor
with the initial state. The initial state can be randomly generated or initialized by the user as
before. You can define the list of all possible actions here (as in missioners example).
actions(self, state): Returns the list of possible actions from the state given as parameter.
result(self, state, action): Computes and returns the resulting new state when the given action is
performed from the given state.
is_goal(self, state): Returns whether the state given as parameter is a goal state. Note that a
goal state is a state on which the number of attacking pairs is 0.(Hint: You already implemented
this in PA1.)
heuristic (self, state): Returns the estimated solution cost from the given state to the goal state.
You can use the number of attacking pairs in the given state as a heuristic function.
A template class definition for NQueens is given in Figure 1.*** Uninformed Search Algorithms ***
**********************************
import random
#class definition for NQueens
class NQueens():
def __init__(self, N):
self.N = N
self._set_state()
def __str__(self):
return f"N: {self.N}, state: {self.state}"
def _set_state(self):
state_answer = input("Enter 1 for Manuel Entrance, 0 for Random State:")
if state_answer =="0":
self.state = self.generate_random_state()
elif state_answer =="1":
state_temp = input("enter state: ")
if self._is_valid(state_temp):
self.state = state_temp
else:
self.state = "wrong state"
else:
self.state="wrong entry"
def generate_random_state(self):
random_state =""
for i in range(self.N):
str_val = random.randint(1,self.N)
random_state+= str(str_val)
return random_state
def _is_valid(self,state):
if len(state)!= self.N :
print("The length of the state string is not equal to N.")
return False
for i in range(len(state)):
try:
if (int(state[i])=0) or (int(state[i])> self.N):
print("State string includes numbers greater than N or less than 1.")
return False
except:
return False
return True
def _count_attacking_pairs(self,
state):
if self._is_valid(self.state):
atacking_pairs =0
for index,state_indexed in enumerate(state):
for index1, state_indexed1 in enumerate(state):
if index index1:
coordinate =[index+1,int(state_indexed)]
coordinate_1=[index1+1,int(state_indexed1)]
if (abs(coordinate[0]-coordinate_1[0])== abs(coordinate[1]-coordinate_1[1])) or (coordinate[0]== coordinate_1[0]) or (coordinate[1]== coordinate_1[1]):
atacking_pairs+=1
return atacking_pairs
return "Error"
problem = NQueens(5) #create NQueens instance
print(problem) #print the description of the problem
print(problem._count_attacking_pairs(problem.state))
-----------
Output Should be like this:
Graph search? True
BFS
Resulting path:
[(None,'1432'),(('Move queen 1 to row 2',1,2),'2432'),(('Move
Resulting state: 2413
Total cost: 3
Viewer stats:
{'max_fringe_size': 120, 'visited_nodes': 87, 'iterations': 87
 In this assignment, you will tormulate the NQueens problem as a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!