Ai Tu Cse 3
Ai Tu Cse 3
ARTIFICIAL INTELLIGENCE
By
Dr B Nandini,
Associate Professor, Dept of CSE,
Telangana University
• Problem solving is an important aspect of Artificial Intelligence.
• A problem can be considered to consist of a goal and a set of
actions that can be taken to lead to the goal.
• At any given time, we consider the state of the search space to
represent where we have reached as a result of the actions we
have applied so far.
• Search is a method that can be used by computers to examine a
problem space in order to find a goal.
• Often, we want to find the goal as quickly as possible or without
using too many resources.
• A problem space can be considered to be a search space
because in order to solve the problem, we will search the space
for a goal state.
• We will continue to use the term search space to describe this
concept. In this chapter, we will look at a number of methods for
examining a search space. These methods are called search
methods.
• The Importance of Search in AI
– It has already become clear that many of the tasks underlying AI can be
phrased in terms of a search for the solution to the problem at hand.
– Many goal based agents are essentially problem solving agents which must
decide what to do by searching for a sequence of actions that lead to their
solutions.
– For production systems, we have seen the need to search for a sequence of
rule applications that lead to the required fact or action.
– For neural network systems, we need to search for the set of connection
weights that will result in the required input to output mapping.
• Which search algorithm one should use will generally depend on the
problem domain? There are four important factors to consider:
– Completeness – Is a solution guaranteed to be found if at least one solution
exists?
– Optimality – Is the solution found guaranteed to be the best (or lowest cost)
solution if there exists more than one solution?
– Time Complexity – The upper bound on the time required to find a solution, as
a function of the complexity of the problem.
– Space Complexity – The upper bound on the storage space (memory) required
at any point during the search, as a function of the complexity of the problem.
• Routes through State Space:
• Our general aim is to search for a route, or sequence of transitions,
through the state space graph from our initial state to a goal state.
• Sometimes there will be more than one possible goal state. We
define a goal test to determine if a goal state has been achieved.
• The solution can be represented as a sequence of link labels (or
transitions) on the state space graph. Note that the labels depend
on the direction moved along the link.
• Sometimes there may be more than one path to a goal state, and
we may want to find the optimal (best possible) path. We can
define link costs and path costs for measuring the cost of going
along a particular path, e.g. the path cost may just equal the
number of links, or could be the sum of individual link costs.
• For most realistic problems, the state space graph will be too large
for us to hold all of it explicitly in memory at any one time.
Search Trees:
• It is helpful to think of the search process as
building up a search tree of routes through the
state space graph. The root of the search tree is
the search node corresponding to the initial state.
• The leaf nodes correspond either to states that
have not yet been expanded, or to states that
generated no further nodes when expanded.
• At each step, the search algorithm chooses a new
unexpanded leaf node to expand. The different
search strategies essentially correspond to the
different algorithms one can use to select which
is the next node to be expanded at each stage.
Breadth-First Search
(BFS):
• This method explores all the neighboring
nodes at a given level systematically
before moving on to the next level. It's
like checking every door on one floor of a
building before moving up to the next
floor. BFS is guaranteed to find a solution
if one exists, but it can be slow for
expansive search spaces.
Data Structures:
• Queue (Q): Used to store the nodes to be explored. It follows a First-In-First-Out (FIFO) principle.
• Visited: This can be an array or hash table to keep track of visited nodes and avoid revisiting them.
Algorithm:
• Initialization:
– Enqueue the starting node in the queue (Q).
– Mark the starting node as visited.
• Exploration Loop:
– While the queue is not empty:
• Dequeue a node (v) from the queue.
• Process the node (v) (e.g., print its value).
• For each unvisited neighbor (w) of node (v):
Explanation:
• We start by adding the starting node to the queue and marking it visited.
• The exploration loop continues as long as there are nodes to explore (queue is not empty).
• In each iteration, we remove the first node (v) from the queue and process it (e.g., print its value).
• We then check all the neighbors of the dequeued node (v). If a neighbor (w) is unvisited, we add it to the queue
and mark it visited.
• This process repeats until all reachable nodes from the starting node are explored.
Benefits of BFS:
• Guaranteed to find a path to the goal node (if it exists) because it explores all neighboring nodes systematically.
• Useful for finding the shortest path in unweighted graphs.
Drawbacks of BFS:
• Can be slow for expansive search spaces due to exploring all nodes level by level.
• Might not be optimal for finding the shortest path in weighted graphs
• Let's consider a scenario where
you're searching for your friend Alice
in a social network. You can model
the social network as a graph, where
people are nodes and friendships are
edges. BFS can help you find Alice
efficiently.
Here's how BFS would work to find Alice, starting your search from John:
• Initialization: Add John (the starting node) to the queue and mark him as visited.
• Exploration Loop:
– Iteration 1:
• Dequeue John from the queue (since he's the only one there).
• Process John (e.g., you already know John and he isn't Alice).
• Check John's neighbors: Sarah and David.
• Add Sarah and David to the queue (since they haven't been visited).
• Mark Sarah and David as visited.
– Iteration 2:
• Dequeue Sarah from the queue.
• Process Sarah (still not Alice).
• Check Sarah's neighbors: Emily and Mike.
• Add Emily and Mike to the queue (unvisited).
• Mark Emily and Mike as visited.
– Iteration 3:
• Dequeue David from the queue.
• Process David (still not Alice).
• Check David's neighbor: Charles.
• Add Charles to the queue (unvisited).
• Mark Charles as visited.
– Iteration 4:
• Dequeue Emily from the queue.
• Process Emily (still not Alice).
• Emily has no unvisited neighbors (both Sarah and Mike have been visited).
– Iteration 5: (Similar to iteration 4 for Mike)
– Iteration 6:
• Dequeue Charles from the queue.
• Process Charles (still not Alice).
• Check Charles' neighbors: Alice and Bob.
• Add Alice to the queue (unvisited - bingo!).
• Mark Alice as visited.
• Bob is not added since we're looking for Alice.
End of Exploration: We've found Alice! BFS systematically explored all John's friends (level 1), then friends of
friends (level 2), until it reached Alice on level 2. This demonstrates how BFS searches outward level by level.
Applications:
Breadth-First Search (BFS) has a variety of applications across computer science and beyond, due to its efficient
exploration of neighboring nodes. Here are some examples:
Web Crawling:
• Search engines like Google use BFS to crawl the web. Starting from a seed webpage, BFS follows all the links on that
page, then all the links on those linked pages, and so on. This systematic exploration helps search engines discover
new webpages and build their index.
Social Networks:
• Social media platforms can leverage BFS to find people within a certain distance (e.g., friends of friends) of a
particular user. By starting from the user and exploring their connections level by level, BFS can efficiently identify
people within the specified social network radius.
Routing and Navigation:
• BFS can be used in GPS navigation systems to find the shortest path between two locations on a map (if the map is
unweighted). It explores neighboring roads systematically, ensuring it finds the quickest route with the same
number of turns for each road.
Circuit Design:
• In circuit board design, BFS can be employed to check for electrical connectivity between components. Starting from
a specific point, BFS traverses the conductive paths to verify if there's a complete circuit connection.
Image Processing:
• BFS plays a role in image processing tasks like flood fill. Given a starting point and a new color, BFS can efficiently
fill a connected region in an image with the new color, ensuring it only changes pixels that are adjacent to the
starting point and have the original color.
Game Development:
• BFS can be used in pathfinding algorithms for game characters. By exploring neighboring tiles or spaces level by
level, BFS helps characters find the shortest path to reach a goal location within the game world.
Computer Networks:
• Peer-to-peer file-sharing networks like BitTorrent utilize BFS to discover neighboring devices on the network. Starting
from a seed node, BFS searches for peers who have the desired file, enabling efficient file sharing and distribution.
• These are just a few examples, and the applications of BFS continue to grow in various fields that involve exploring
interconnected structures and finding optimal solutions.
• Social Network Analysis: BFS goes beyond just finding friends of friends. It can be used to analyze
the spread of information or influence within a social network. By starting from a source node (e.g.,
someone who starts a rumor), BFS can efficiently track how the information propagates to other
users based on their connections, revealing patterns of influence and information dissemination.
• Load Balancing: In distributed computing systems, BFS can be used for load balancing. It helps
distribute tasks or workloads evenly across multiple servers or resources. By starting from a central
task queue and assigning tasks to available servers level by level (using BFS approach), the system
ensures no server gets overloaded while others remain idle.
• Minimum Spanning Tree Algorithms: Algorithms like Prim's Minimum Spanning Tree (MST) utilize
BFS principles. They find the minimum cost network connecting all nodes in a graph. BFS helps
explore neighboring nodes systematically, ensuring the chosen edges contribute to the minimum
overall cost for the spanning tree.
• Broadcast in Wireless Networks: In wireless networks, BFS can be used for efficient message
broadcasting. A node receiving a message can forward it to all its neighbors within its transmission
range. By using BFS, the message propagates outwards level by level, ensuring all reachable nodes
receive the broadcast efficiently.
• Video Game Level Design: Game developers can leverage BFS to create open-world game levels.
BFS helps procedurally generate accessible and interesting level layouts. It starts from a central point
and progressively expands outwards, placing obstacles, rooms, and points of interest while ensuring
all areas are reachable from the starting point.
• Image and Video Compression: Some image and video compression techniques employ BFS. By
analyzing pixel color or feature similarities, BFS can identify redundant regions within an image or
video. These redundant areas can then be compressed efficiently, reducing file size without
significant quality loss.
• Spam Filtering: Email spam filters can utilize BFS to analyze the origin and sender reputation of
emails. Starting from a suspicious email, BFS can explore the email's sender and recipient network to
identify connections to known spam sources. This helps flag suspicious emails more effectively.
• Protein-Protein Interaction Networks: In bioinformatics, BFS can be used to analyze protein-
protein interaction networks. By modeling proteins as nodes and interactions as edges, BFS can
identify groups of interacting proteins that might be involved in specific biological processes. This
aids in understanding cellular functions and protein signaling pathways.
Depth-First Search (DFS):
• DFS dives as deep as possible along
a single branch before backtracking
and exploring alternative branches.
Imagine going down a long corridor,
then backtracking to try other
corridors if you reach a dead end.
DFS can be faster than BFS for
certain problems, but it might miss
the optimal solution if the goal is
located on a shallower branch.
• Data Structures:
• Stack (S): Used to remember the path taken during exploration. It follows a Last-In-First-Out (LIFO) principle.
• Visited: Similar to BFS, this can be an array or hash table to keep track of visited nodes and avoid revisiting
them.
• Algorithm:
• Initialization:
– Push the starting node onto the stack (S).
– Mark the starting node as visited.
• Exploration Loop:
– While the stack is not empty:
• Pop a node (v) from the stack (S).
• Process the node (v) (e.g., print its value).
• For each unvisited neighbor (w) of node (v):
– Push the neighbor (w) onto the stack (S).
– Mark the neighbor (w) as visited.
• Explanation:
• We start by pushing the starting node onto the stack and marking it visited.
• The exploration loop follows a depth-first approach.
• In each iteration, we pop a node (v) from the stack (since it follows LIFO, it explores the deepest path first).
• We process the popped node (v).
• We then check all the unvisited neighbors of node (v). If a neighbor (w) is unvisited, we push it onto the stack
for further exploration and mark it visited.
• This process continues until all reachable paths from the starting node are explored. Since the stack prioritizes
the most recent path, DFS explores one branch as deep as possible before backtracking and trying other
branches.
• Benefits of DFS:
• Can be faster than BFS for certain problems, especially when the goal node is located on a deeper branch in
the search space.
• Useful for finding paths in graphs that might have cycles, as BFS would keep looping infinitely in cycles.
• Drawbacks of DFS:
• Not guaranteed to find the optimal solution (shortest path) compared to BFS.
• Might explore unnecessary paths before reaching the goal, especially for expansive search spaces.
• Here's how DFS would work to find Alice, starting your search from John:
• Initialization: Push John (the starting node) onto the stack and mark him as visited.
• Exploration Loop:
– Iteration 1:
• Pop John from the stack.
• Process John.
• Check John's neighbors: Sarah and David. Since DFS goes deep first, explore Sarah first.
• Push Sarah onto the stack and mark her visited.
– Iteration 2:
• Pop Sarah from the stack.
• Process Sarah.
• Check Sarah's neighbors: Emily and Mike. Push Emily first (DFS follows LIFO).
• Push Emily onto the stack and mark her visited.
– Iteration 3:
• Pop Emily from the stack.
• Process Emily.
• Emily has no unvisited neighbors (both Sarah and Mike have been visited). Since there are no more unvisited neighbors on this path, DFS backtracks.
– Backtracking: DFS pops Sarah again (revisit) since there are unexplored neighbors (David).
– Iteration 4:
• Pop Sarah from the stack (again). (We already processed her).
• Now explore David (John's other neighbor).
• Push David onto the stack and mark him visited.
– Iteration 5:
• Pop David from the stack.
• Process David.
• Check David's neighbor: Charles. Push Charles onto the stack.
– Iteration 6:
• Pop Charles from the stack.
• Process Charles.
• Check Charles' neighbors: Alice and Bob. Push Alice first (DFS prioritizes it).
– Iteration 7: (similar to iteration 3, DFS backtracks as Emily has no unvisited neighbors)
– Backtracking: Pop Charles again (revisit) to explore Bob (since Alice is not found yet).
– Iteration 8:
• Pop Charles from the stack (again). (We already processed him).
• Explore Bob (David's neighbor). Since Bob is not Alice, DFS backtracks again.
• End of Exploration (Backtracking): We haven't found Alice after exploring John-Sarah-Emily and John-David-Charles paths.
Backtracking all the way to John, DFS would realize there are no more unvisited neighbors to explore. While BFS would
systematically check all friends and friends of friends, DFS might miss the shortest path if the goal (Alice) is on a less explored
branch.
• In this case, DFS didn't find Alice as quickly as BFS, but it demonstrates how DFS explores a single path as deeply as possible
until it reaches the goal or a dead end, then backtracks to try other branches.
Applications:
• Depth-First Search (DFS) proves valuable in various computer science scenarios due to its efficient
exploration of paths within a graph structure. Here are some key applications of DFS:
• Maze Solving: DFS is a fundamental algorithm for solving maze problems. It starts at the entry
point and explores one path at a time until it reaches the exit. If it hits a dead end, it backtracks
and tries another path. This systematic exploration makes DFS well-suited for finding a solution in
mazes.
• File System Navigation: DFS is handy for navigating directory structures in file systems. It can
efficiently list all files and folders within a directory, starting from a root directory and traversing
all subdirectories one by one until it reaches the end of each branch.
• Topological Sorting: This technique sorts a directed acyclic graph (DAG) where there are no
cycles. DFS helps achieve this by following directed edges and ensuring all predecessors of a node
are visited before the node itself. This ordering is useful in various applications, like resolving
dependencies when compiling code or scheduling tasks without conflicts.
• Cycle Detection in Graphs: DFS can effectively detect cycles in graphs. As it explores a path, it
keeps track of visited nodes. If it encounters a node that has already been visited while following a
path, it signifies a cycle exists in the graph.
• Undirected Graph Connectivity: DFS can be used to determine if an undirected graph is
connected. By starting from a node and using DFS to explore all reachable nodes, we can identify if
all nodes in the graph are connected or if there are isolated components.
• Strongly Connected Components: An extension of DFS can be used to find strongly connected
components (SCCs) in a directed graph. SCCs are groups of nodes where there's a path between
any two nodes within the group. DFS helps identify these SCCs efficiently.
• Network Routing Protocols: Some routing protocols, like distance-vector routing, can leverage
DFS principles to discover neighboring routers and exchange routing information. By exploring
paths outward from a router, DFS helps establish connections and build routing tables.
• These are just a few examples, and DFS continues to be a versatile tool for various tasks involving
graph exploration, pathfinding, and dependency management in computer science and beyond.
• Game AI: In games with artificial intelligence (AI) controlled enemies, DFS can be used
to explore the game world and chase the player. The AI can start from its current location
and use DFS to explore reachable areas, prioritizing paths that might lead to the player's
location.
• Natural Language Processing (NLP): DFS can be used in some NLP tasks for text
parsing and understanding sentence structure. By following the grammatical
dependencies between words in a sentence, DFS can help identify the relationships
between clauses and phrases, aiding in tasks like sentiment analysis or machine
translation.
• Computer Vision: In image segmentation problems, DFS can be employed to group
connected pixels that belong to the same object in an image. Starting from a seed pixel,
DFS can traverse neighboring pixels with similar color or features, effectively segmenting
the foreground object from the background.
• Logic Puzzles and Constraint Satisfaction Problems (CSPs): DFS can be a valuable
tool for solving logic puzzles like Sudoku or solving CSPs. It can systematically explore
different configurations (assignments of values to variables) and backtrack when
encountering contradictions, eventually finding a solution that satisfies all constraints.
• Circuit Analysis: Some circuit analysis algorithms utilize DFS to identify loops or paths
within a circuit. This can be helpful in tasks like finding short circuits or analyzing signal
flow within the circuit.
• Model Checking: In software verification, DFS can be used for model checking, a
technique to ensure a system meets specific requirements. It can explore all possible
states of the system using DFS and verify if any state violates the desired properties.
• Bioinformatics: In analyzing biological sequences like DNA or proteins, DFS can be used
to identify patterns or motifs within the sequence. It can explore different paths through
the sequence and compare them to known patterns, aiding in gene identification or
protein structure analysis.
Complexity
Both Breadth-First Search (BFS) and Depth-First Search (DFS) have a time complexity of O(V + E), where V represents
the number of vertices (nodes) and E represents the number of edges (connections) in the graph.
Here's why:
• Iterating over the Graph: Both BFS and DFS traverse the entire graph in the worst case scenario. BFS explores all
nodes level by level, while DFS explores all branches until it reaches a dead end or the goal. This traversal ensures
they visit (and potentially process) all V vertices and their corresponding edges (E).
• Linear Relationship: The time complexity grows linearly with the increase in the number of vertices and edges.
This means the execution time roughly doubles if the graph size doubles.
In conclusion:
• Both BFS and DFS offer efficient algorithms for graph traversal with a time complexity of O(V + E). Their space
complexity can differ based on the data structure used and the graph's properties. BFS uses more space in the worst
case due to the queue, while DFS uses more space in the worst case for deep graphs due to the stack.
Heuristic searches:
Heuristic searches are a type of search algorithm used in artificial intelligence
(AI) to find optimal or satisfactory solutions to problems within a vast search
space. Unlike uninformed search algorithms like Breadth-First Search (BFS)
and Depth-First Search (DFS) that explore the search space exhaustively,
heuristic searches leverage additional knowledge to guide their exploration
towards promising areas.
Here's how heuristic searches work:
• Problem Representation: The problem is modeled as a search space,
where each state represents a possible configuration, and actions lead to
transitions between states. The goal state represents the desired solution.
• Heuristic Function: A heuristic function, often denoted by h(x), estimates
the cost or distance from a current state (x) to the goal state. This function is
crucial for guiding the search towards more promising paths. However, it's
important to note that the heuristic function doesn't guarantee finding the
optimal solution, only an estimate.
• Evaluation Function: An evaluation function, often denoted by f(x),
combines the cost of reaching the current state (g(x)) with the heuristic
estimate (h(x)). Common evaluation functions include:
– f(x) = g(x) + h(x) (This is often used for A* search)
• Search Algorithm: The chosen search algorithm leverages the evaluation
function to prioritize exploring states that seem closer to the goal based on
the heuristic estimate.
Benefits of Heuristic Search:
• Reduced Exploration Time: By focusing on promising areas based on the heuristic, heuristic
searches can significantly reduce exploration time compared to uninformed searches, especially
for large search spaces.
• Finding Good Solutions: Even if the heuristic isn't perfect, it can still guide the search towards
good solutions, making them valuable in scenarios where finding the absolute optimal solution
might not be critical.
Think of it like this: Imagine you're trying to find the combination to a lock. You
might:
• Generate possible combinations (e.g., trying numbers one by one).
• Test each combination on the lock to see if it opens.
Properties:
• Guaranteed Solution (if systematic): If there exists a solution and you
systematically generate and test all possibilities, you're guaranteed to find it
eventually.
• Exhaustive Search: In its most basic form, it's essentially an exhaustive
search, checking every single option. This can be slow for problems with vast
solution spaces.
• Limited Search Space: It becomes more powerful when combined with
constraints or heuristics that limit the generation process to a more focused
search space, making it more efficient.
Here are some contexts where generate-and-test is used:
• Constraint Satisfaction Problems (CSPs): Here, the
algorithm finds assignments that satisfy all the constraints of
the problem. For example, fitting together a jigsaw puzzle can
be seen as a CSP.
• Finding all solutions: If you need to find all possible valid
solutions, not just the first one, generate-and-test can be
adapted to keep track of all working solutions.
Limitations:
• Scalability: For problems with massive solution spaces,
exhaustive generate-and-test becomes impractical.
• Random vs. Systematic: Random generation might miss good
solutions, while systematic generation can be slow.
• Overall, the generate-and-test algorithm is a versatile
tool. It's a good starting point for understanding
problem-solving techniques and can be a foundation for
more advanced algorithms that incorporate heuristics
and optimizations.
Example:
A jigsaw puzzle is a tiling puzzle that requires the assembly of
often irregularly shaped interlocking and mosaicked pieces,
each of which typically has a portion of a picture. When
assembled, the puzzle pieces produce a complete picture.
• Jigsaw puzzles come in a variety of shapes and sizes. They
can be enjoyed by people of all ages and can provide a
variety of benefits, including:
• Improved problem-solving skills
• Enhanced critical thinking
• Increased patience and concentration
• Reduced stress and anxiety
• Boosted cognitive function
• Jigsaw puzzles can be a fun and relaxing activity for
individuals or groups. They can also be used as a challenge
or to promote teamwork.
Plan - Generate and Test:
• DENDRAL is an early example of successful
AI Program based on Heuristic plan-
generate-and test.
• In planning process it used Constraint –
satisfaction techniques to create lists of
recommended and constraint substructures.
• Next these fairly limited set of structures are
going to be used in Generate – and – Test
procedure.
• Constrained in this way, Generate and Test
procedure has proved highly effective.
Dendral was a pioneering AI program developed in the 1960s at Stanford University
https://en.wikipedia.org/wiki/Dendral. It's considered a landmark achievement for being
one of the first successful expert systems.
Impact of Dendral:
• Problem-Solving: Dendral paved the way for other expert systems in various domains,
demonstrating the power of AI in replicating human expertise for problem-solving.
• Drug Discovery: It had a significant impact on the field of drug discovery by automating
tasks and speeding up the process of identifying new drug candidates.
• Foundation for Future AI: The techniques and knowledge gained from Dendral laid the
groundwork for advancements in knowledge representation, reasoning, and search
algorithms, all crucial aspects of modern AI.
Limitations:
• Similar to the previous example, here too, the algorithm can get stuck in local minima (a shorter route
might exist but not explored). The quality of the solution significantly depends on the initial random route.
• This example demonstrates how hill climbing can be applied in routing problems to find (hopefully) the
most efficient path despite the possibility of local optima.
steepest ascent hill climbing
Steepest ascent hill climbing is a variant of the simple hill climbing algorithm
used in Artificial Intelligence (AI) for optimization problems. Here's how it differs
from regular hill climbing:
Regular Hill Climbing:
• Evaluates only one neighboring state at a time from the current state.
• Selects the first neighbor that improves the objective function value.
Steepest Ascent Hill Climbing:
• Evaluates all neighboring states of the current state.
• Selects the neighbor with the highest value for a maximization problem (or
the lowest value for minimization) according to the objective function.
• In essence, steepest ascent takes a more comprehensive approach by
considering all possibilities in the immediate vicinity before making a move.
Advantages:
• Potentially faster convergence towards a local optimum compared to simple
hill climbing, especially in scenarios with many neighbors.
• Might be more likely to escape shallow local optima by taking a bigger jump in
the direction of steeper improvement.
Disadvantages:
• Can be computationally expensive, especially for problems with a large
number of neighbors.
• Still susceptible to getting trapped in local optima, just like regular hill
climbing.
Here's an analogy:
• Imagine you're lost in a foggy forest trying to find the highest
peak (maximum value).
• Regular hill climbing: You check the visibility in one direction
(neighbor) at a time and move towards the first uphill path you
find.
• Steepest ascent hill climbing: You climb a tree (if possible) to
get a better view of the surroundings (evaluate all neighbors).
Then, you descend towards the direction with the steepest incline
(highest value neighbor).
Disadvantages:
• Slower Convergence: Compared to deterministic methods,
stochastic hill climbing might take longer to converge towards a
local optimum because it doesn't strictly follow the steepest
ascent path.
• Finding Suboptimal Solutions: The random element can also
lead the algorithm towards less than ideal solutions, especially if
the chosen "random improvement" neighbor doesn't lead to a
very promising area of the search space.
Applications:
• Stochastic hill climbing is a good choice for problems with:
• Rough landscapes: When the objective function has many ups
and downs, randomness can help explore different areas and
potentially find better solutions.
• Risk of getting stuck: If the problem is prone to local optima,
the random element can increase the chance of escaping them.
Here's an analogy:
• Imagine you're again lost in a foggy forest but this time with a
compass (objective function) to guide you uphill.
• Regular/Steepest Ascent Hill Climbing: You meticulously
follow the steepest uphill path indicated by the compass.
• Stochastic Hill Climbing: You trust the compass generally but
occasionally take slightly random detours (within a reasonable
range) hoping to stumble upon a better path out of the fog.
In conclusion, stochastic hill climbing offers a balance between
deterministic improvement and exploration, making it a valuable
tool for navigating complex search spaces in AI.
Example : Local Maxima and Global
Maxima
Let's revisit the scenario of finding the highest point on a hill
(maximization problem) to illustrate local and global maxima:
Imagine a landscape with two hills:
• Hill A: Tall and isolated, with a single peak (global maximum) at its
center.
• Hill B: Smaller with multiple bumps. It has a central peak (local
maximum) that's not the highest point on the entire landscape
(global maximum lies on Hill A).
Local Maximum:
• The peak on Hill B is a local maximum. Here's why:
• It's the highest point in its immediate vicinity.
• If you start climbing from any point on Hill B, you'll eventually
reach this central peak.
• However, this peak is not the highest point on the entire landscape
(both hills combined).
Global Maximum:
• The global maximum refers to the absolute highest point across
the entire landscape (both hills). In this case, it's the peak on Hill A.
• Key Points:
• A local maximum is only the highest point in its local area.
• The global maximum is the highest point overall.
• Finding Local vs. Global Maxima:
• Hill climbing algorithms like the ones we discussed earlier often find
local maxima. They might get stuck there if the surrounding area
doesn't offer a clear path towards a higher point.
• There are advanced optimization techniques that can improve the
chances of finding the global maximum, but they often come with
increased complexity.
• Visualizing Local and Global Maxima:
• Imagine a graph where the x-axis represents different locations on
the landscape and the y-axis represents the height at each location.
The peaks on the graph would correspond to the maxima.
• A local maximum would appear as a bump on the graph, higher than
its immediate surroundings but lower than some other points on the
graph (global maximum).
• The global maximum would be the highest point on the entire graph.
Diagrammatic
Representation:
• Plateau and ridge are terms used in AI, specifically in optimization algorithms like hill
climbing, to describe challenging terrains encountered during the search for an optimal
solution.
• Plateau:
• A plateau refers to a flat region in the search space where all neighboring states
(possible solutions) have the same value according to the objective function.
• Imagine yourself on a flat tableland while climbing a hill. No matter which direction you
move, the elevation remains constant.
• For a hill climbing algorithm, a plateau presents a dilemma. The objective function
doesn't provide any clear direction for improvement (either uphill or downhill) since
neighboring states have the same value.
• Ridge:
• A ridge is a more subtle challenge. It's a long, narrow area in the search space that's
generally higher than its surroundings but has a slight downward slope in all directions.
• Imagine yourself on the crest of a long, narrow mountain ridge. While you're at a higher
elevation compared to the valleys on either side, there's no clear peak to reach.
• For a hill climbing algorithm, a ridge can be misleading. Since all immediate neighbors
have a lower value, the algorithm might terminate here, mistaking it for a local
maximum (peak). The true optimum might lie further along the ridge.
• Challenges Posed by Plateaus and Ridges:
• Both plateaus and ridges can hinder the progress of a hill climbing algorithm in finding
the optimal solution.
• Plateaus cause the algorithm to stagnate as there's no clear direction for improvement.
• Ridges can lead to premature termination as the algorithm gets stuck at a seemingly
high point (but not the highest).
Techniques to overcome local maxima, plateau and ridge
Hill climbing algorithms are great for finding optimal solutions, but they can get stuck in less-
than-ideal situations like local maxima, plateaus, and ridges. Here are some techniques to
overcome these challenges:
Local Maxima:
• Simulated Annealing: This method introduces a random element, allowing the algorithm
to escape local maxima with a certain probability. It works by accepting worse solutions
(downhill moves) with a decreasing probability as the search progresses, mimicking the
cooling process of metal.
• Multiple Restarts: Run the algorithm multiple times with different starting points. This
increases the chance of finding the global optimum by exploring various regions of the
search space.
• Genetic Algorithms: These algorithms work with a population of solutions and mimic
biological evolution. They can explore diverse areas of the search space and potentially
escape local maxima through crossover and mutation operations.
Plateau:
• Random Restarts: Similar to local maxima, restarting the algorithm from a new random
point can help it escape the flat region and explore areas with more promising solutions.
• Larger Step Sizes: Instead of evaluating only immediate neighbors, the algorithm can
consider states further away. This might allow it to "jump" over the plateau and find a
better direction for improvement.
• Change of Objective Function: If applicable, consider using a secondary objective
function alongside the primary one. This can break ties in cases where multiple neighboring
states have the same value in the primary function.
Ridge:
• Random Restarts: Similar to plateaus and local maxima, random restarts can be helpful.
• Variable Step Sizes: Instead of a fixed step size for exploring neighbors, the algorithm
can dynamically adjust it. This allows for larger jumps along the ridge while still exploring
nearby areas for improvement.
• Multiple Evaluation Criteria: Similar to plateaus, using additional objective functions can
help the algorithm navigate a ridge. These secondary functions might provide a way to
differentiate between points along the ridge and guide the search towards a true peak.
Overall:
• While these techniques can improve the performance of hill climbing algorithms, it's
important to remember that there's no guaranteed escape from local optima, plateaus, or
ridges. The choice of technique depends on the problem and the desired balance between
efficiency and finding the best possible solution.
Best-First Search
• In BFS and DFS, when we are at a node, we can
consider any of the adjacent as next node.
• So both BFS and DFS blindly explore paths
without considering any cost function.
• The idea of Best First Search is to use an
evaluation function to decide which adjacent is
most promising and then explore.
• Best First Search falls under the category of
Heuristic Search or Informed Search.
• We use a priority queue to store costs of nodes.
So the implementation is a variation of BFS, we
just need to change Queue to PriorityQueue.
OR Graphs
• We start from source "S" and search for goal "I"
using given costs and Best First search.
• pq initially contains S We remove s from and
process unvisited neighbors of S to pq.
• pq now contains {A, C, B} (C is put before B
because C has lesser cost)
• We remove A from pq and process unvisited
neighbors of A to pq. pq now contains {C, B, E, D}
• We remove C from pq and process unvisited
neighbors of C to pq. pq now contains {B, H, E, D}
• We remove B from pq and process unvisited
neighbors of B to pq. pq now contains {H, E, D, F,
G}
• We remove H from pq. Since our goal "I" is a
neighbor of H, we return.
Analysis :
• The worst case time complexity for Best
First Search is O(n * Log n) where n is
number of nodes. In worst case, we may
have to visit all nodes before we reach goal.
Note that priority queue is implemented
using Min(or Max) Heap, and insert and
remove operations take O(log n) time.
• Performance of the algorithm depends on
how well the cost or evaluation function is
designed.
Best-first search is a type of search algorithm used in artificial
intelligence (AI) for pathfinding and problem-solving. It belongs
to the category of informed search algorithms, meaning it
utilizes additional knowledge about the problem domain to
guide its exploration.
– Here's a breakdown of how it works:
• Core Idea:
– Unlike blind search algorithms (BFS & DFS) that explore all
possibilities equally, best-first search prioritizes exploring what
seems to be the most promising path based on a specific evaluation
function.
• Key Components:
– Search Space: The set of all possible states or configurations the
search can explore.
– Evaluation Function: A function that estimates how "good" a
particular state is relative to the goal. This function is crucial for
guiding the search towards the optimal solution.
– Priority Queue: A data structure that prioritizes elements based on
their evaluation function scores. The lower the score (indicating a
more promising state), the higher the priority for exploration.
• Steps:
– Start State: Begin with an initial state representing the starting point of the
search.
– Evaluation: Calculate the evaluation function score for the starting state.
– Expand State: Add the starting state to the priority queue.
• Loop: While not finished searching:
– Dequeue: Remove the state with the highest priority (supposedly closest to the
goal) from the queue.
– Goal Check: If the dequeued state is the goal state, terminate the search and
return the solution path.
– Generate Successors: Identify all possible next states reachable from the current
state.
– Evaluate Successors: Calculate the evaluation function score for each successor
state.
– Enqueue Successors: Add the successor states along with their scores to the
priority queue.
• No Solution Found: If the loop finishes without finding the goal state, it
might indicate either no solution exists or the evaluation function needs
refinement.