Question: Informed Search Part 1 : Problem representation In this part, you have to define ( represent ) the problem, that is to tell

Informed Search
Part 1: Problem representation
In this part, you have to define \(represent\) the problem, that is to tell the computer what the problem look like. For example, your representaion should be able to express the location of the nodes, and the travel cost between connected nodes. \(Also, don't forget to define the grid world, you may need it for the heuristic.\)
# Define the graph
graph ={
# Your code here.
# hint: 'A': [('B',3),('C',8),('D',5)],...
}
positions ={
# Your code here.
# hint: 'A': (4,1),...
}
Part 2: Heuristic
In this part, define a admissible heuristic function for this problem.
# Your code here
def heuristic(node, goal, positions):
# Define a heuristic function that estimates the cost from node to goal
# Your code here.
pass
Part 3: Greedy Best First Search
In this part, implement a function doing the Greedy best first search to find the solution for the problem. (To reduce the difficulty, I have given you most code, you only need to fill-in a couple of lines to make this function working.\)
from queue import PriorityQueue
def greedy_best_first_search(graph, positions, start, goal):
visited = set()
pq = PriorityQueue()
pq.put((0, start))# (heuristic value, node)
parent ={}# stores the parent node of each visited node
while not pq.empty():
_, current_node = pq.get()
visited.add(current_node)
if current_node == goal:
break
for neighbor, weight in graph[current_node]:
if neighbor not in visited:
# Your code here.
pass
# Reconstruct the path from the goal to the start
path =[]
node = goal
while node != start:
path.append(node)
node = parent[node]
path.append(start)
# Reverse the path to get it from start to goal
path.reverse()
return path
Part 4: A* search
In this part, implement the A* function to solve the problem. (hint: you can seek inspiration from the greedy best first search implementation\)
def astar_search(graph, positions, start, goal):
# Your code here.
pass
Part 5: Main function
This part is the main function to run your search functions.
def print_path(path):
# Print the resulting path
if path:
print("Path found:", '->'.join(path))
else:
print("No path found.")
def main():
# Run the Greedy Best-First Search algorithm
start_node ='A'
goal_node ='I'
path_gbf = greedy_best_first_search(graph, positions, start_node, goal_node)
path_astar = astar_search(graph, positions, start_node, goal_node)
# Print the resulting path
print("Greedy Best First Search: ")
print_path(path_gbf)
print("A* Search: ")
print_path(path_astar)
main()
Please help me with this; follow the instructions carefully, please!
Thank you!!
Informed Search Part 1 : Problem representation

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 Programming Questions!