Labset 4
Labset 4
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.
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
# 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()
In [ ]: