0% found this document useful (0 votes)
6 views2 pages

Labset 4

This lab manual outlines the implementation of the A* algorithm for designing a navigation system to find the shortest and fastest route for a delivery truck in a city. It details the steps to set up a city grid, define cost functions, implement heuristic functions, and execute the A* algorithm to visualize the optimal path. The manual includes code snippets for each step, guiding users through the process of simulating varying traffic and distance conditions.

Uploaded by

televisionanu123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Labset 4

This lab manual outlines the implementation of the A* algorithm for designing a navigation system to find the shortest and fastest route for a delivery truck in a city. It details the steps to set up a city grid, define cost functions, implement heuristic functions, and execute the A* algorithm to visualize the optimal path. The manual includes code snippets for each step, guiding users through the process of simulating varying traffic and distance conditions.

Uploaded by

televisionanu123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ARTIFICIAL INTELLIGENCE

LAB MANUAL
LABSET 4 - HEURISTIC SEARCH

Author:

Dr. Chandrika M, Associate Professor, Department of Data Analytics & Mathematical Sciences, School of sciences, Jain University.

Use Case: Design a navigation system that finds the shortest and fastest route for a delivery truck in a city.
Objective: Implement the A* algorithm to find the optimal path from a starting location to a destination, considering both distance and traffic conditions.
Heuristics: Use the Manhattan or Euclidean distance for evaluation.

Step 1: Import Libraries


Start by loading essential libraries:

In [1]: # import necessary libraries


import numpy as np
import matplotlib.pyplot as plt
import heapq # For priority queue implementation

Step 2: Define the City Grid and Cost Function


Represent the city as a 2D grid, where each cell represents a road segment.
Define two matrices: one for distances and another for traffic congestion. For simplicity, let’s use random values to simulate varying traffic and distance values.

In [2]: # Define the city grid and cost function


grid_size = (10, 10) # Adjust grid size as needed
distance_matrix = np.random.randint(1, 10, size=grid_size)
traffic_matrix = np.random.randint(1, 5, size=grid_size)

Step 3: Define Heuristic Functions


Implement Manhattan and Euclidean heuristics

In [3]: # Define heuristic functions


def manhattan_distance(start, end):
return abs(start[0] - end[0]) + abs(start[1] - end[1])

def euclidean_distance(start, end):


return np.sqrt((start[0] - end[0])**2 + (start[1] - end[1])**2)

Step 4: Implement the A* Algorithm


Create the A* algorithm that calculates the cost of reaching a neighboring cell based on distance and traffic matrices. Use a priority queue to prioritize nodes with the lowest
estimated cost.

In [4]: # Implement A* Algorithm


def a_star_algorithm(start, end, distance_matrix, traffic_matrix, heuristic_func):
rows, cols = distance_matrix.shape
open_set = []
heapq.heappush(open_set, (0, start))
came_from = {}
cost_so_far = {start: 0}

while open_set:
current_cost, current = heapq.heappop(open_set)

if current == end:
break

for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: # Possible moves
neighbor = (current[0] + dx, current[1] + dy)
if 0 <= neighbor[0] < rows and 0 <= neighbor[1] < cols:
traffic_penalty = traffic_matrix[neighbor]
new_cost = cost_so_far[current] + distance_matrix[neighbor] + traffic_penalty

if neighbor not in cost_so_far or new_cost < cost_so_far[neighbor]:


cost_so_far[neighbor] = new_cost
priority = new_cost + heuristic_func(neighbor, end)
heapq.heappush(open_set, (priority, neighbor))
came_from[neighbor] = current

return came_from, cost_so_far

Step 5: Reconstruct and Visualize the Path


Once the optimal path is found, reconstruct and plot it on the grid for visualization.
In [5]: # Reconstruct and visualize the path
def reconstruct_path(came_from, start, end):
path = []
current = end
while current != start:
path.append(current)
current = came_from.get(current)
path.append(start)
path.reverse()
return path

# Visualization
def visualize_grid(distance_matrix, path):
plt.imshow(distance_matrix, cmap="YlGn", origin="upper")
path_x, path_y = zip(*path)
plt.plot(path_y, path_x, marker="o", color="red")
plt.title("Optimal Path")
plt.colorbar(label="Distance")
plt.show()

Step 6: Run the Experiment


Define a start and end location, choose a heuristic, and run the A* algorithm to find and visualize the optimal path.

In [6]: # Run the experiment


start = (0, 0) # Starting point
end = (9, 9) # Destination

# Choose heuristic: Manhattan or Euclidean


heuristic = manhattan_distance

came_from, cost_so_far = a_star_algorithm(start, end, distance_matrix, traffic_matrix, heuristic)


path = reconstruct_path(came_from, start, end)

# Visualize the path on the grid


visualize_grid(distance_matrix, path)

In [ ]:

You might also like