AI Module
AI Module
ENVIRONMENTS
MODULE 3
❖ Artificial Intelligence (AI) is revolutionizing how we solve complex problems and make decisions.
❖ One crucial aspect of AI is local search algorithms, which play a significant role in finding optimal
solutions in various domains.
WHAT IS LOCAL SEARCH IN AI?
❖ Local search in AI refers to a family of optimization algorithms that are used to find the best
possible solution within a given search space.
❖ Unlike global search methods that explore the entire solution space, local search algorithms focus on
making incremental changes to improve a current solution until they reach a locally optimal or
satisfactory solution.
❖ This approach is useful in situations where the solution space is vast, making an exhaustive search
impractical.
❖ The local search algorithm in artificial intelligence is a family of optimization algorithms used to find
the best possible solution to a problem by iteratively making small changes to an initial solution.
❖ These algorithms are used to solve optimization problems in which an exact solution is either
impossible to find or computationally expensive.
❖ Local search algorithms operate using a single current node and generally move only to neighbors
of that node.
❖ Local search algorithms ease up on completeness and optimality in the interest of improving time
and space complexity?
❖ Although local search algorithms are not systematic, they have two key advantages:
1. They use very little memory (usually a constant amount), and
2. They can often find reasonable solutions in large or infinite (continuous) state spaces. •
❖ In addition to finding goals, local search algorithms are useful for solving pure optimization
problems, in which the aim is to find the best state according to an objective function.
1. In optimization problems, the path to goal is irrelevant and the goal state itself is the
solution.
2. In some optimization problems, the goal is not known and the aim is to find the best state.
Local search algorithms are useful for solving pure optimization problems. In pure
optimization problems main aim is to find the best state according to required objective
function.
Local search algorithm make use of concept called as state space landscape. This landscape
has two structures -
1) Location (defined by the state)
2) Elevation (defined by the value of the heuristic cost function or objective function).
• If elevation corresponds to the cost, then the aim is to find the lowest valley - (a global
minimum).
• If elevation corresponds to the objective function, then aim is to find the highest peak - (a
global maximum).
• Local search algorithms explore the landscape.
What is Objective Function?
It focuses on decision-making based on constraints. It is a real-valued function that is either to be
maximized or minimized depending upon the constraints. Some terminologies include:
•Constraints: They are basically the conditional equations that govern the Linear function
•Decision Variables: The variables whose values are to be found out. The equations are solved so as to
get the optimal value of these variables.
•Feasible Region: It is the region in the graph where the constraints are satisfied and the decision
variables are found at the corners of the region.
•Optimum Solution: The best possible solution that satisfies all constraints and achieves the highest or
lowest objective.
•Infeasible Solution: A solution that violates one or more constraints and cannot be implemented or
executed.
Working of a Local Search Algorithm
The basic working principle of a local search algorithm involves the following steps:
❖ Initialization:
Start with an initial solution, which can be generated randomly or through some heuristic method.
❖ Evaluation:
Evaluate the quality of the initial solution using an objective function or a fitness measure. This
function quantifies how close the solution is to the desired outcome.
❖ Neighbor Generation:
Generate a set of neighboring solutions by making minor changes to the current solution. These
changes are typically referred to as "moves."
❖ Selection:
Choose one of the neighboring solutions based on a criterion, such as the improvement in the
objective function value. This step determines the direction in which the search proceeds.
❖ Termination:
Continue the process iteratively, moving to the selected neighboring solution, and repeating
steps 2 to 4 until a termination condition is met. This condition could be a maximum number of
iterations, reaching a predefined threshold, or finding a satisfactory solution.
❖ Solution:
The final solution is the best solution found during the search process.
Optimization Problems
❖ Optimization problems are central to artificial intelligence (AI) and machine learning, focusing
on selecting the best element from some set of available alternatives based on specific
criteria.
❖ These problems are characterized by an objective function that needs to be maximized or
minimized, often subject to a set of constraints.
❖ In the context of machine learning, optimization algorithms are used to minimize a loss
function, which measures the difference between the algorithm's predictions and the actual
data.
❖ The solutions to these problems are vital for training models, feature selection, resource
allocation, scheduling tasks, and much more, making them integral to both the development of
AI technologies and their application in various domains.
HILL CLIMBING ALGORITHM IN ARTIFICIAL INTELLIGENCE
❖ Hill climbing algorithm is a local search algorithm which continuously moves in the direction
of increasing elevation/value to find the peak of the mountain or best solution to the
problem. It terminates when it reaches a peak value where no neighbor has a higher value.
❖ Hill climbing algorithm is a technique which is used for optimizing the mathematical
problems. One of the widely discussed examples of Hill climbing algorithm is Traveling-
salesman Problem in which we need to minimize the distance traveled by the salesman.
❖ It is also called greedy local search as it only looks to its good immediate neighbor state
❖ A node of hill climbing algorithm has two components which are state and value.
❖ In the Hill Climbing algorithm, the process begins with an initial solution, which is then
❖ These changes are evaluated by a heuristic function to determine the quality of the solution.
❖ The algorithm continues to make these adjustments until it reaches a local maximum—a point
where no further improvement can be made with the current set of moves.
Basic Concepts of Hill Climbing Algorithms
2.Neighboring States: Identify neighboring states of the current solution by making small
adjustments (mutations or tweaks).
3.Move to Neighbor: If one of the neighboring states offers a better solution (according to some
evaluation function), move to this new state.
4.Termination: Repeat this process until no neighboring state is better than the current one. At this
point, you’ve reached a local maximum or minimum (depending on whether you’re maximizing or
minimizing).
Advantages of Hill climb algorithm:
1.Ideal for complex problems.
2.It uses much less RAM for the current problem state and the solutions located around it
3.When it comes to the acceleration of the hill up, most of the time it brings a closure in the
local maximum straight away. This is the route if having quickly getting a solution, outshining
acquiring a global maximum, is an incentive.
•X-axis: Represents the state space, which includes all the possible states or configurations that
the algorithm can reach.
•Y-axis: Represents the values of the objective function corresponding to each state.
If the function on Y-axis is cost then, the goal of search is to find the global minimum and local
minimum. If the function of Y-axis is Objective function, then the goal of the search is to find the
❖ Steepest-Ascent hill-climbing:
❖ Simple hill climbing is the simplest way to implement a hill climbing algorithm.
❖ It only evaluates the neighbor node state at a time and selects the first one which
❖ It only checks it's one successor state, and if it finds better than the current state, then move
•Step 1: Evaluate the initial state, if it is goal state then return success and Stop.
•Step 2: Loop Until a solution is found or there is no new operator left to apply.
• Else if it is better than the current state then assign new state as a current state.
• Else if not better than the current state, then return to step2.
•Step 5: Exit.
2. Steepest-Ascent Hill Climbing
❖ Instead of moving to the first neighboring node that improves the state, it evaluates all
neighbors and moves to the one offering the highest improvement (steepest ascent).
Algorithm for Steepest-Ascent Hill Climbing
3.Repeat until the solution is found or the current state remains unchanged:
• Select a new state that hasn’t been applied to the current state.
• If the best state improves upon the current state, make it the new current state and
repeat.
random neighboring node and decides whether to move based on its improvement over
3.Repeat until a solution is found or the current state does not change:
• Apply the successor function to the current state and generate all neighboring states.
• If the chosen state is better than the current state, make it the new current state.
The SA algorithm is based on the annealing process used in metallurgy, where a metal is heated to
a high temperature quickly and then gradually cooled. At high temperatures, the atoms move fast.
Moreover, when the temperature is reduced, their kinetic energy decreases as well. At the end of
the annealing process, the atoms fall into a more ordered state. Additionally, the material becomes
Similarly, in SA, a search process starts with a high-energy state (an initial solution) and
gradually lowers the temperature (a control parameter) until it reaches a state of minimum
iteratively improves the current solution by randomly perturbing it and accepting the
The probability of accepting a worse solution is initially high. However, as the number of
Therefore, the accuracy of the solution depends on the number of iterations SA performs.
•Initialization: Begin with an initial solution Sο and an initial temperature Tο. he temperature
controls how likely the algorithm is to accept worse solutions as it explores the search space.
•Neighborhood Search: At each step, a new solution S′ is generated by making a small change (or
perturbation) to the current solution S.
•Objective Function Evaluation: The new solution S' is evaluated using the objective function. If S'
provides a better solution than S, it is accepted as the new solution.
•Acceptance Probability: If S' is worse than S, it may still be accepted with a probability based on
the temperature and the difference in objective function values. The acceptance probability is
given by:
•Cooling Schedule: After each iteration, the temperature is decreased according to a predefined
cooling schedule, which determines how quickly the algorithm converges. Common cooling schedules
•Termination: The algorithm continues until the system reaches a low temperature (i.e., no more
The cooling schedule plays a crucial role in the performance of Simulated Annealing. If the
temperature decreases too quickly, the algorithm might converge prematurely to a suboptimal solution
(local optimum). On the other hand, if the cooling is too slow, the algorithm may take an excessively
long time to find the optimal solution. Hence, finding the right balance between exploration (high
•Ability to Escape Local Minima: One of the most significant advantages of Simulated
Annealing is its ability to escape local minima. The probabilistic acceptance of worse solutions
allows the algorithm to explore a broader solution space.
•Simple Implementation: The algorithm is relatively easy to implement and can be adapted to a
wide range of optimization problems.
•Global Optimization: Simulated Annealing can approach a global optimum, especially when
paired with a well-designed cooling schedule.
•Flexibility: The algorithm is flexible and can be applied to both continuous and discrete
optimization problems.
Disadvantages of Simulated Annealing
•Slow Convergence: The convergence rate is generally slower than more deterministic methods
• As such, it uses mechanisms that are typically associated with biological evolution, such as
solutions are eliminated while stronger, more viable options are retained and re-evaluated in the
next evolution—with the goal being to arrive at optimal actions to achieve the desired outcomes.
• With this approach, candidate solutions to an optimization problem are randomly generated
and act as individuals interacting with a larger population.
• A fitness function determines the quality of the solutions the candidates find as they move
about in each iteration.
• The “best fit” individuals are then chosen for reproduction in the next iteration.
• This generational process is repeated until the algorithm has evolved to find the optimal
solution to the problem.
How do Fitness Functions Work in Evolutionary Algorithms?
The heart of these algorithms is the fitness function, which is an objective function used to
summarize, as a single figure of merit (attractiveness), how close the current agent (solution) is to
achieving the algorithm’s programmed aims. There are many variations of the exact function that can
include velocity/position like in swarm intelligence, or employ any other time or frequency variables.
Regardless of specific function, the evolution process works involves the same steps:
1. Efficiency
One of the main benefits of evolutionary algorithms is their efficiency. Unlike traditional optimization
algorithms, which can get stuck in local optima, evolutionary algorithms explore a wide range of
solutions and can find the global optimum. This is because they use a population of solutions instead of
a single solution and apply operators such as mutation and crossover to generate new solutions. As a
result, evolutionary algorithms are often faster and more accurate than traditional optimization
algorithms.
For example, evolutionary algorithms have been used to optimize the design of aircraft wings, where
the goal is to minimize weight while maintaining structural integrity. By using evolutionary algorithms,
researchers were able to find a design that was 20% lighter than the previous design, while still
meeting all the structural requirements.
2. Robustness
Another benefit of evolutionary algorithms is their robustness. Evolutionary algorithms are able to
adapt to changing environments and complex problems, making them ideal for real world applications.
This is because they use a stochastic search process that can explore a wide range of solutions and
adapt to new situations.
For example, evolutionary algorithms have been used to optimize the routing of emergency vehicles
in urban areas. In this scenario, the goal is to minimize response time while taking into account
traffic congestion and road closures. By using evolutionary algorithms, researchers were able to find
a routing strategy that was robust to changes in traffic patterns and road closures, resulting in
faster response time and better emergency services.
Flexibility
Evolutionary Algorithms are highly flexible and can be customized to suit different
applications. This is because they can be adapted to handle different types of data and
search spaces, making them ideal for a wide range of applications.
For example, evolutionary algorithms have been used to optimize the placement of wind
turbines in offshore wind farms. In this scenario, the goal is to maximize energy
production while minimizing the impact on marine life and the environment. By using
evolutionary algorithms, researchers were able to find an optimal placement strategy that
took into account a wide range of factors, resulting in a more efficient and sustainable energy
farm.
OPTIMAL DECISIONS IN GAMES
Humans’ intellectual capacities have been engaged by games for as long as civilization has existed,
sometimes to an alarming degree. Games are an intriguing subject for AI researchers because of their
abstract character. A game’s state is simple to depict, and actors are usually limited to a small number
of actions with predetermined results. Physical games, such as croquet and ice hockey, contain
significantly more intricate descriptions, a much wider variety of possible actions, and rather
ambiguous regulations defining the legality of activities. With the exception of robot soccer, these
physical games have not piqued the AI community’s interest.
Games are usually intriguing because they are difficult to solve. Chess, for example, has an average
branching factor of around 35, and games frequently stretch to 50 moves per player, therefore the
search tree has roughly 35100 or 10154 nodes (despite the search graph having “only” about 1040
unique nodes). As a result, games, like the real world, necessitate the ability to make some sort of
decision even when calculating the best option is impossible.
Optimal Decision Making in Games
Let us start with games with two players, whom we’ll refer to as MAX and MIN for obvious reasons.
MAX is the first to move, and then they take turns until the game is finished. At the conclusion of the
game, the victorious player receives points, while the loser receives penalties. A game can be formalized
as a type of search problem that has the following elements:
• S0: The initial state of the game, which describes how it is set up at the start.
• Terminal-Test (s): A terminal test that returns true if the game is over but false otherwise.
Terminal states are those in which the game has come to a conclusion.
• Utility (s, p): A utility function (also known as a payout function or objective function )
determines the final numeric value for a game that concludes in the terminal state s for
player p. The result in chess is a win, a loss, or a draw, with values of +1, 0, or 1/2.
Backgammon’s payoffs range from 0 to +192, but certain games have a greater range of
possible outcomes. A zero-sum game is defined (confusingly) as one in which the total reward
to all players is the same for each game instance. Chess is a zero-sum game because each game
has a payoff of 0 + 1, 1 + 0, or 1/2 + 1/2. “Constant-sum” would have been a preferable name,
22 but zero-sum is the usual term and makes sense if each participant is charged 1.
Adversarial Search
• Adversarial search is a search, where we examine the problem which arises when we try to
plan ahead of the world and other agents are planning against us.
• There might be some situations where more than one agent is searching for the solution in the
same search space, and this situation usually occurs in game playing.
• The environment with more than one agent is termed as multi-agent environment, in which each
agent is an opponent of other agent and playing against each other. Each agent needs to consider
the action of other agent and effect of that action on their performance.
• So, Searches in which two or more players with conflicting goals are trying to explore the
same search space for the solution, are called adversarial searches, often known as Games.
• Games are modeled as a Search problem and heuristic evaluation function, and these are the two
main factors which help to model and solve games in AI.
MINI-MAX ALGORITHM IN ARTIFICIAL INTELLIGENCE
game theory. It provides an optimal move for the player assuming that opponent is also playing
optimally.
• Min-Max algorithm is mostly used for game playing in AI. Such as Chess, Checkers, tic-tac-toe, go,
and various tow-players game. This Algorithm computes the minimax decision for the current state.
• In this algorithm two players play the game, one is called MAX and other is called MIN.
• Both the players fight it as the opponent player gets the minimum benefit while they get the
maximum benefit.
• Both Players of the game are opponent of each other, where MAX will select the maximized
• The minimax algorithm performs a depth-first search algorithm for the exploration of the
• The minimax algorithm proceeds all the way down to the terminal node of the tree, then
Players Involved
• Chooses the move that leads to the highest possible utility value, assuming the opponent will play
optimally.
• The interplay between these two players is central to the Min-Max algorithm, as each player
attempts to outthink and counter the other's strategies.
• The working of the minimax algorithm can be easily described using an example. Below we have
taken an example of game-tree which is representing the two-player game.
• In this example, there are two players one is called Maximizer and other is called Minimizer.
• Maximizer will try to get the Maximum possible score, and Minimizer will try to get the minimum
possible score.
• This algorithm applies DFS, so in this game-tree, we have to go all the way through the leaves to
reach the terminal nodes.
• At the terminal node, the terminal values are given so we will compare those value and backtrack
the tree until the initial state occurs. Following are the main steps involved in solving the two-player
game tree:
Step-1: In the first step, the algorithm generates the entire game-tree and apply the utility
function to get the utility values for the terminal states. In the below tree diagram, let's take A is
the initial state of the tree. Suppose maximizer takes first turn which has worst-case initial value
=- infinity, and minimizer will take next turn which has worst-case initial value = +infinity.
Step 2: Now, first we find the utilities value for the Maximizer, its initial value is -∞, so we will
compare each value in terminal state with initial value of Maximizer and determines the higher
nodes values. It will find the maximum among the all.
• For node D max(-1,- -∞) => max(-1,4)= 4
• For Node E max(2, -∞) => max(2, 6)= 6
• For Node F max(-3, -∞) => max(-3,-5) = -3
• For node G max(0, -∞) = max(0, 7) = 7
Step 3: In the next step, it's a turn for minimizer, so it will compare all nodes value with +∞, and
will find the 3rd layer node values.
• For node B= min(4,6) = 4
• For node C= min (-3, 7) = -3
Step 4: Now it's a turn for Maximizer, and it will again choose the maximum of all nodes value and
find the maximum value for the root node. In this game tree, there are only 4 layers, hence we
reach immediately to the root node, but in real games, there will be more than 4 layers.
• For node A max(4, -3)= 4
That was the complete workflow of the minimax two player game.
Properties of Mini-Max algorithm:
• Complete- Min-Max algorithm is Complete. It will definitely find a solution (if exist), in the finite
search tree.
• Optimal- Min-Max algorithm is optimal if both opponents are playing optimally.
• Time complexity- As it performs DFS for the game-tree, so the time complexity of Min-Max
algorithm is O(bm), where b is branching factor of the game-tree, and m is the maximum depth of
the tree.
• Space Complexity- Space complexity of Mini-max algorithm is also similar to DFS which
is O(bm).
• Order the nodes in the tree such that the best nodes are checked first.
• Use domain knowledge while finding the best move. Ex: for Chess, try order: captures first, then
• We can bookkeep the states, as there is a possibility that states may repeat.