Rauf

AI and ML

Search in AI

Search problems involve finding a solution from an initial state to a goal state. For instance, a navigator app finds the best route from your current location to your destination. In AI, these problems are modeled using agents (entities that perceive and act on their environment), states (configurations of the agent), and actions (choices made in each state). The state space consists of all possible states the agent can reach from its initial state, and the goal test determines when the agent has reached the goal.

Key Concepts in Search:

Solving Search Problems

To solve search problems, we use the frontier, a mechanism to manage the nodes in a search process. Nodes store information about the state, the parent node, the action taken to reach the node, and the path cost. The algorithm explores nodes by following specific strategies, such as Depth-First Search (DFS) or Breadth-First Search (BFS).

Search Algorithms

Depth-First Search (DFS):

def remove(self):
    if self.empty():
        raise Exception("empty frontier")
    else:
        node = self.frontier[-1]
        self.frontier = self.frontier[:-1]
        return node

Breadth-First Search (BFS):

def remove(self):
    if self.empty():
        raise Exception("empty frontier")
    else:
        node = self.frontier[0]
        self.frontier = self.frontier[1:]
        return node

Greedy Best-First Search:

A* Search:

Heuristics: Manhattan Distance

A common heuristic used in search algorithms, especially in grid-based pathfinding, is the Manhattan Distance. It measures the number of steps required to move from one point to another, ignoring obstacles and calculating based on vertical and horizontal movement.

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