Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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: BreadthFirst Search BFS Uniform Cost Search UCS Depth First SearchDFS 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 In other words, each action will be in the form of "Move Queen to row where Implementation Use your solution to PA and do the following modifications. You have to define NQueens class as a child of abstract SearchProblem see models.py There are main functions actions isgoal, 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. initself: 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 actionsself state: Returns the list of possible actions from the state given as parameter. resultself state, action: Computes and returns the resulting new state when the given action is performed from the given state. isgoalself 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 Hint: You already implemented this in PA 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 Uninformed Search Algorithms import random #class definition for NQueens class NQueens: def initself N: self.N N self.setstate def strself: return fN: selfN state: selfstate def setstateself: stateanswer inputEnter for Manuel Entrance, for Random State:" if stateanswer : self.state self.generaterandomstate elif stateanswer : statetemp inputenter state: if self.isvalidstatetemp: self.state statetemp else: self.state "wrong state" else: self.state"wrong entry" def generaterandomstateself: randomstate for i in rangeselfN: strval random.randintself.N randomstate strstrval return randomstate def isvalidselfstate: if lenstate self.N : printThe length of the state string is not equal to N return False for i in rangelenstate: try: if intstatei or intstatei self.N: printState string includes numbers greater than N or less than return False except: return False return True def countattackingpairsself state: if self.isvalidselfstate: atackingpairs for index,stateindexed in enumeratestate: for index stateindexed in enumeratestate: if index index: coordinate indexintstateindexed coordinateindexintstateindexed if abscoordinatecoordinate abscoordinatecoordinate or coordinate coordinate or coordinate coordinate: atackingpairs return atackingpairs return "Error" problem NQueens #create NQueens instance printproblem #print the description of the problem printproblemcountattackingpairsproblemstate Output Should be like this: Graph search? True BFS Resulting path: NoneMove queen to row Move Resulting state: Total cost: Viewer stats: maxfringesize': 'visitednodes': 'iterations':
In this assignment, you will tormulate the NQueens problem as a search problem and solve it using
traditional search algorithms:
BreadthFirst Search BFS
Uniform Cost Search UCS
Depth First SearchDFS
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 In other words, each action will be in the form of "Move Queen to row where
Implementation
Use your solution to PA and do the following modifications.
You have to define NQueens class as a child of abstract SearchProblem see
models.py
There are main functions actions isgoal, 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.
initself: 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
actionsself state: Returns the list of possible actions from the state given as parameter.
resultself state, action: Computes and returns the resulting new state when the given action is
performed from the given state.
isgoalself 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 Hint: You already implemented
this in PA
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 Uninformed Search Algorithms
import random
#class definition for NQueens
class NQueens:
def initself N:
self.N N
self.setstate
def strself:
return fN: selfN state: selfstate
def setstateself:
stateanswer inputEnter for Manuel Entrance, for Random State:"
if stateanswer :
self.state self.generaterandomstate
elif stateanswer :
statetemp inputenter state:
if self.isvalidstatetemp:
self.state statetemp
else:
self.state "wrong state"
else:
self.state"wrong entry"
def generaterandomstateself:
randomstate
for i in rangeselfN:
strval random.randintself.N
randomstate strstrval
return randomstate
def isvalidselfstate:
if lenstate self.N :
printThe length of the state string is not equal to N
return False
for i in rangelenstate:
try:
if intstatei or intstatei self.N:
printState string includes numbers greater than N or less than
return False
except:
return False
return True
def countattackingpairsself
state:
if self.isvalidselfstate:
atackingpairs
for index,stateindexed in enumeratestate:
for index stateindexed in enumeratestate:
if index index:
coordinate indexintstateindexed
coordinateindexintstateindexed
if abscoordinatecoordinate abscoordinatecoordinate or coordinate coordinate or coordinate coordinate:
atackingpairs
return atackingpairs
return "Error"
problem NQueens #create NQueens instance
printproblem #print the description of the problem
printproblemcountattackingpairsproblemstate
Output Should be like this:
Graph search? True
BFS
Resulting path:
NoneMove queen to row Move
Resulting state:
Total cost:
Viewer stats:
maxfringesize': 'visitednodes': 'iterations':
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started