Note 4g
Note 4g
INTRODUCTION TO AI
Historically, researchers have pursued several different versions of AI. Some have defined
intelligence in terms of fidelity to human performance, while others prefer an abstract, formal definition
of intelligence called rationality—loosely speaking, doing the “right thing.” The subject matter itself
also varies: some consider intelligence to be a property of internal thought processes and reasoning, while
others focus on intelligent behavior, an external characterization.
From these two dimensions—human vs. rational and thought vs. behavior—there are four
possible combinations, and there have been adherents and research programs for all four. The methods
used are necessarily different: the pursuit of human-like intelligence must be in part an empirical science
related to psychology, involving observations and hypotheses about actual human behavior and thought
processes; a rationalist approach, on the other hand, involves a combination of mathematics and
engineering, and connects to statistics, control theory, and economics. The various groups have both
disparaged and helped each other.
DEFINITION
Artificial Intelligence (AI) is a branch of science that deals with helping machines finds solutions
to complex problems in a more human-like fashion.
• Artificial Intelligence is the branch of computer science concerned with making computers behave
like humans.
• Coined and defined as “The science and engineering of making intelligent machines, especially
intelligent computer programs” by John McCarthy in 1956.
The definitions on top are concerned with thought processes and reasoning, whereas the ones on
the bottom address behavior.
Following are some sectors which have the application of Artificial Intelligence:
1. AI in Astronomy
Artificial Intelligence can be very useful to solve complex universe problems. AI technology can be
helpful for understanding the universe such as how it works, origin, etc.
2. AI in Healthcare
In the last, five to ten years, AI becoming more advantageous for the healthcare industry and going to
have a significant impact on this industry.
Healthcare Industries are applying AI to make a better and faster diagnosis than humans. AI can help
doctors with diagnoses and can inform when patients are worsening so that medical help can reach to the
patient before hospitalization.
3. AI in Gaming
AI can be used for gaming purpose. The AI machines can play strategic games like chess, where the
machine needs to think of a large number of possible places.
4. AI in Finance
AI and finance industries are the best matches for each other. The finance industry is implementing
automation, chatbot, adaptive intelligence, algorithm trading, and machine learning into financial
processes.
5. AI in Data Security
The security of data is crucial for every company and cyber-attacks are growing very rapidly in the
digital world. AI can be used to make your data more safe and secure. Some examples such as AEG bot,
AI2 Platform,are used to determine software bug and cyber-attacks in a better way.
6. AI in Social Media
Social Media sites such as Facebook, Twitter, and Snapchat contain billions of user profiles, which need
to be stored and managed in a very efficient way. AI can organize and manage massive amounts of data.
AI can analyze lots of data to identify the latest trends, hashtag, and requirement of different users.
7. AI in Travel & Transport
AI is becoming highly demanding for travel industries. AI is capable of doing various travel related
works such as from making travel arrangement to suggesting the hotels, flights, and best routes to the
customers. Travel industries are using AI-powered chatbots which can make human-like interaction with
customers for better and fast response.
8. AI in Automotive Industry
Some Automotive industries are using AI to provide virtual assistant to their user for better performance.
Such as Tesla has introduced TeslaBot, an intelligent virtual assistant.
Various Industries are currently working for developing self-driven cars which can make your journey
more safe and secure.
9. AI in Robotics
Artificial Intelligence has a remarkable role in Robotics. Usually, general robots are programmed such
that they can perform some repetitive task, but with the help of AI, we can create intelligent robots which
can perform tasks with their own experiences without pre-programmed.
Humanoid Robots are best examples for AI in robotics, recently the intelligent Humanoid robot named as
Erica and Sophia has been developed which can talk and behave like humans.
10. AI in Entertainment
We are currently using some AI based applications in our daily life with some entertainment services
such as Netflix or Amazon. With the help of ML/AI algorithms, these services show the
recommendations for programs or shows.
11. AI in Agriculture
Agriculture is an area which requires various resources, labor, money, and time for best result. Now a
day's agriculture is becoming digital, and AI is emerging in this field. Agriculture is applying AI as
agriculture robotics, solid and crop monitoring, predictive analysis. AI in agriculture can be very helpful
for farmers.
12. AI in E-commerce
AI is providing a competitive edge to the e-commerce industry, and it is becoming more demanding in
the e-commerce business. AI is helping shoppers to discover associated products with recommended size,
color, or even brand.
13. AI in education
AI can automate grading so that the tutor can have more time to teach. AI chatbot can communicate with
students as a teaching assistant.
AI in the future can be work as a personal virtual tutor for students, which will be accessible easily at any
time and any place.
Example Problem:
Imagine an agent enjoying a touring vacation in Romania. The agent wants to take in the sights,
improve its Romanian, enjoy the nightlife, avoid hangovers, and so on. The decision problem is a
complex one. Now, suppose the agent is currently in the city of Arad and has a nonrefundable ticket to
fly out of Bucharest the following day. The agent observes street signs and sees that there are three roads
leading out of Arad: one toward Sibiu, one to Timisoara, and one to Zerind. None of these are the goal,
so unless the agent is familiar with the geography of Romania, it will not know which road to follow.
If the agent has no additional information—that is, if the environment is unknown—then the
agent can do no better than to execute one of the actions at random. In this chapter, we will assume our
agents always have access to information about the world, such as the map in Figure 3.1 . With that
information, the agent can follow this four-phase problem-solving process:
GOAL FORMULATION: The agent adopts the goal of reaching Bucharest. Goals organize behavior by
limiting the objectives and hence the actions to be considered.
PROBLEM FORMULATION: The agent devises a description of the states and actions necessary to
reach the goal—an abstract model of the relevant part of the world. For our agent, one good model is to
consider the actions of traveling from one city to an adjacent city, and therefore the only fact about the
state of the world that will change due to an action is the current city.
SEARCH: Before taking any action in the real world, the agent simulates sequences of actions in its
model, searching until it finds a sequence of actions that reaches the goal. Such a sequence is called a
solution. The agent might have to simulate multiple sequences that do not reach the goal, but eventually it
will find a solution (such as going from Arad to Sibiu to Fagaras to Bucharest), or it will find that no
solution is possible.
EXECUTION: The agent can now execute the actions in the solution, one at a time.
It is an important property that in a fully observable, deterministic, known environment, the
solution to any problem is a fixed sequence of actions: drive to Sibiu, then Fagaras, then Bucharest. If the
model is correct, then once the agent has found a solution, it can ignore its percepts while it is executing
the actions—closing its eyes, so to speak—because the solution is guaranteed to lead to the goal. Control
theorists call this an open-loop system: ignoring the percepts breaks the loop between agent and
environment. If there is a chance that the model is incorrect, or the environment is nondeterministic, then
the agent would be safer using a closed-loop approach that monitors the percepts.
In partially observable or nondeterministic environments, a solution would be a branching
strategy that recommends different future actions depending on what percepts arrive. For example, the
agent might plan to drive from Arad to Sibiu but might need a contingency plan in case it arrives in
Zerind by accident or finds a sign saying “Drum Închis” (Road Closed).
Formulating problems
Our formulation of the problem of getting to Bucharest is a model—an abstract mathematical
description—and not the real thing. Compare the simple atomic state description Arad to an actual cross-
country trip, where the state of the world includes so many things: the traveling companions, the current
radio program, the scenery out of the window, the proximity of law enforcement officers, the distance to
the next rest stop, the condition of the road, the weather, the traffic, and so on. All these considerations
are left out of our model because they are irrelevant to the problem of finding a route to Bucharest.
The process of removing detail from a representation is called abstraction. A good problem
formulation has the right level of detail. If the actions were at the level of “move the right foot forward a
centimeter” or “turn the steering wheel one degree left,” the agent would probably never find its way out
of the parking lot, let alone to Bucharest.
Can we be more precise about the appropriate level of abstraction? Think of the abstract states
and actions we have chosen as corresponding to large sets of detailed world states and detailed action
sequences. Now consider a solution to the abstract problem: for example, the path from Arad to Sibiu to
Rimnicu Vilcea to Pitesti to Bucharest. This abstract solution corresponds to a large number of more
detailed paths. For example, we could drive with the radio on between Sibiu and Rimnicu Vilcea, and
then switch it off for the rest of the trip.
The abstraction is valid if we can elaborate any abstract solution into a solution in the more
detailed world; a sufficient condition is that for every detailed state that is “in Arad,” there is a detailed
path to some state that is “in Sibiu,” and so on. The abstraction is useful if carrying out each of the
actions in the solution is easier than the original problem; in our case, the action “drive from Arad to
Sibiu” can be carried out without further search or planning by a driver with average skill. The choice of
a good abstraction thus involves removing as much detail as possible while retaining validity and
ensuring that the abstract actions are easy to carry out. Were it not for the ability to construct useful
abstractions, intelligent agents would be completely swamped by the real world.
EXAMPLE PROBLEMS
The problem-solving approach has been applied to a vast array of task environments. We list some of
the best known here, distinguishing between standardized and real-world problems.
A standardized problem is intended to illustrate or exercise various problemsolving methods. It
can be given a concise, exact description and hence is suitable as a benchmark for researchers to
compare the performance of algorithms.
A real-world problem, such as robot navigation, is one whose solutions people actually use, and
whose formulation is idiosyncratic, not standardized, because, for example, each robot has
different sensors that produce different data.
States: It describes the location of each numbered tiles and the blank tile.
Initial State: We can start from any state as the initial state.
Actions: Here, actions of the blank space is defined, i.e., either left, right, up or down
Transition Model: It returns the resulting state as per the given state and actions.
Goal test: It identifies whether we have reached the correct goal-state.
Path cost: The path cost is the number of steps in the path where the cost of each step is 1
2.Complete-state formulation: It starts with all the 8-queens on the chessboard and moves them around,
saving from the attacks.
Following steps are involved in this formulation
States: Arrangement of all the 8 queens one per column with no queen attacking the other queen.
Actions: Move the queen at the location where it is safe from the attacks.
The root node is expanded first, then all the successors of the root node are expanded next, then
their successors, and so on.
BFS Algorithm:
Set a variable NODE to the initial state, i.e., the root node.
Set a variable GOAL which contains the value of the goal state.
Loop each node by traversing level by level until the goal state is not found.
While performing the looping, start removing the elements from the queue in FIFO order.
If the goal state is found, return goal state otherwise continue the search.
Example:
In the below tree structure, we have shown the traversing of the tree using BFS algorithm from
the root node S to goal node K. BFS search algorithm traverse in layers, so it will follow the path which
is shown by the dotted arrow, and the traversed path will be:
S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
An exponential complexity bound such as O(bd) is scary. Figure 3.13 shows why. It lists, for
various values of the solution depth d, the time and memory required for a breadth first search with
branching factor b = 10. The table assumes that 1 million nodes can be generated per second and that a
node requires 1000 bytes of storage. Many search problems fit roughly within these assumptions (give or
take a factor of 100) when run on a modern personal computer.
Advantages:
BFS will provide a solution if any solution exists.
If there are more than one solution for a given problem, then BFS will provide the minimal
solution which requires the least number of steps.
Disadvantages of BFS
The biggest disadvantage of BFS is that it requires a lot of memory space, therefore it is a
memory bounded strategy.
BFS is time taking search strategy because it expands the nodes breadthwise.
2. UNIFORM-COST SEARCH
Uniform-cost search is a searching algorithm used for traversing a weighted tree or graph. This
algorithm comes into play when a different cost is available for each edge.
The primary goal of the uniform-cost search is to find a path to the goal node which has the lowest
cumulative cost.
A uniform-cost search algorithm is implemented by the priority queue. It gives maximum priority to
the lowest cumulative cost. Uniform cost search is equivalent to BFS algorithm if the path cost of all
edges is the same.
This search explores nodes based on their path cost from the root node.
It expands a node n having the lowest path cost g(n), where g(n) is the total cost from a root node to
node n.
Uniform-cost search is significantly different from the breadth-first search because of the following
two reasons:
First, the goal test is applied to a node only when it is selected for expansion not when it is first
generated because the first goal node which is generated may be on a suboptimal path.
Secondly, a goal test is added to a node, only when a better/optimal path is found.
Thus, uniform-cost search expands nodes in a sequence of their optimal path cost because before
exploring any node, it searches the optimal path. Also, the step cost is positive so, paths never get shorter
when a new node is added in the search.
The successors of Sibiu are RimnicuVilcea and Fagaras, with costs 80 and 99, respectively. The
least-cost node, RimnicuVilcea, is expanded next, adding Pitesti with cost 80 + 97 =177. The least-cost
node is now Fagaras, so it is expanded, adding Bucharest with cost 99 + 211 =310.
Uniform-cost search Algorithm
Set a variable NODE to the initial state, i.e., the root node and expand it.
After expanding the root node, select one node having the lowest path cost and expand it further.
Remember, the selection of the node should give an optimal path cost.
If the goal node is searched with optimal value, return goal state, else carry on the search.
Example:
In the above figure, it is seen that the goal-state is F and start/ initial state is A. There are three
paths available to reach the goal node. We need to select an optimal path which may give the lowest total
cost g(n). Therefore, A->B->E->F gives the optimal path cost i.e., 0+1+3+4=8.
3. DEPTH-FIRST SEARCH
This search strategy explores the deepest node first, then backtracks to explore other nodes.
It uses LIFO (Last in First Out) order, which is based on the stack, in order to expand the unexpanded
nodes in the search tree. The search proceeds to the deepest level of the tree where it has no successors.
This search expands nodes till infinity, i.e., the depth of the tree.
DFS Algorithm:
Set a variable NODE to the initial state, i.e., the root node.
Set a variable GOAL which contains the value of the goal state.
Loop each node by traversing deeply in one direction/path in search of the goal node.
While performing the looping, start removing the elements from the stack in LIFO order.
If the goal state is found, return goal state otherwise backtrack to expand nodes in other direction.
DFS SEARCH TREE
Depth-first search isa recursive algorithm for traversing a tree or graph data structure.
This search strategy explores the deepest node first, then backtracks to explore other nodes. It
uses LIFO (Last in First Out) order, which is based on the stack, in order to expand the
unexpanded nodes in the search tree. The search proceeds to the deepest level of the tree
where it has no successors. This search expands nodes till infinity, i.e.,the depth of the tree.
The process of the DFS algorithm is similar to the BFS algorithm.
DFS Algorithm
Set a variable NODE to the initial state, i.e., the root node.
Set a variable GOAL which contains the value of the goal state.
Loop each node by traversing deeply in one direction/path in search of the goal node.
While performing the looping, start removing the elements from the stack in LIFO order.
If the goal state is found, return goal state otherwise backtrack to expand nodes in other direction.
In the above figure, DFS works starting from the initial node A (root node) and traversing in one
direction deeply till node I and then backtrack to B and so on. Therefore, the sequence will be A->B->D-
>I->E->C->F->G.
4. DEPTH-LIMITED SEARCH
A depth-limited search algorithm is similar to depth-first search with a predetermined limit. Depth-
limited search can solve the drawback of the infinite path in the Depth-first search. In this algorithm, the
node at the depth limit will treat as it has no successor nodes further.
This search strategy is similar to DFS with a little difference. The difference is that in depth-limited
search, we limit the search by imposing a depth limit l to the depth of the search tree. It does not need to
explore till infinity. As a result, the depth-first search is a special case of depth-limited search. when the
limit l is infinite.
Depth-limited search can be terminated with two Conditions of failure:
o Standard failure value: It indicates that problem does not have any solution.
o Cutoff failure value: It defines no solution for the problem within a given depth limit.
In the above tree, the depth-limit is 1. So, only level 0 and 1 get expanded in A->B->C DFS
sequence, starting from the root node A till node B. It is not giving satisfactory result because we could
not reach the goal node I.
Depth-limited search Algorithm:
Set a variable NODE to the initial state, i.e., the root node.
Set a variable GOAL which contains the value of the goal state.
Set a variable LIMIT which carries a depth-limit value.
Loop each node by traversing in DFS manner till the depth-limit value.
While performing the looping, start removing the elements from the stack in LIFO order.
If the goal state is found, return goal state. Else terminate the search.
Performance measure of Depth-limited search:
Completeness: Depth-limited search does not guarantee to reach the goal node.
Optimality: It does not give an optimal solution as it expands the nodes till the depth-limit.
Space Complexity: The space complexity of the depth-limited search is O(bl).
Time Complexity: The time complexity of the depth-limited search is O(bl).
Advantages:
Depth-limited search is Memory efficient.
Disadvantages of Depth-limited search:
This search strategy is not complete.
It does not provide an optimal solution.
Note: Depth-limit search terminates with two kinds of failures: the standard failure value indicates “no
solution,” and cut-off value, which indicates “no solution within the depth-limit.”
In the above figure, the goal node is H and initial depth-limit =[0-1]. So, it will expand level 0 and 1
and will terminate with A->B->C sequence.
Further, change the depth-limit =[0-3], it will again expand the nodes from level 0 till level 3 and the
search terminate with A->B->D->F->E->H sequence where H is the desired goal node.
Iterative Deepening Search Algorithm:
Explore the nodes in DFS order.
Set a LIMIT variable with a limit value.
Loop each node up to the limit value and further increase the limit value accordingly.
Terminate the search when the goal state is found.
Performance measure of Iterative deepening search:
Completeness: Iterative deepening search may or may not reach the goal state.
Optimality: It does not give an optimal solution always.
Space Complexity: It has the same space complexity as BFS, i.e., O(bd).
Time Complexity: It has O(d) time complexity.
Advantage:
It combines the benefits of BFS and DFS search algorithm in terms of fast search and memory
efficiency.
Disadvantages of Iterative deepening search:
The drawback of iterative deepening search is that it seems wasteful because it generates states
multiple times.
6. BIDIRECTIONAL SEARCH
Bidirectional search algorithm runs two simultaneous searches, one form initial state called as
forward-search and other from goal node called as backward-search, to find the goal node. Hoping that
the two searches meet in the middle.
As soon as the two searches intersect one another, the bidirectional search terminates with the
goal node.
Example:
In the below search tree, bidirectional search algorithm is applied. This algorithm divides one graph/tree
into two sub-graphs. It starts traversing from node 1 in the forward direction and starts from goal node 16
in the backward direction.
Advantages:
Bidirectional search is fast.
Bidirectional search requires less memory
Disadvantages:
Implementation of the bidirectional search tree is difficult.
In bidirectional search, one should know the goal state in advance.
Requires a lot of memory space.
Solution:
1. First node to be expanded is Sibiu, since it is close to Bucharest than Timisoara and Zerind.
2. Then we should expand Fargas which is close to the goal node.
3. But here Fargas generates Bucharest instead of expanding.
4. Since SLD is used, the search cost is minimal.
Hint : Expand the node that is near to the goal node.
This is not an optimal solution since goal can be reached with less cost through Rimnicu and
Pitesti.
• GBFTS is incomplete even in finite state space.
Example: Find the shortest route from Iasi to Fagaras.
Heuristic suggest that Neamt should be expanded but it is dead end.
The solution is to go through Vaslui – Urziceni-Bucharest-Fargas.
GBFTS wont find solution for this problem and put Iasi into an infinte loot.
Greedy best-first graph search is complete in finite state spaces, but not in infinite ones. The
worst-case time and space complexity is O(|V|). With a good heuristic function, however, the complexity
can be reduced substantially, on certain problems reaching O(bm).
A* Search:
A* search is the most widely used informed search algorithm where a node n is evaluated by
combining values of the functions g(n)and h(n). The function g(n) is the path cost from the start/initial
node to a node n and h(n) is the estimated cost of the shortest path from node n to the goal node.
Therefore, we have
f(n)=g(n)+h(n)
where f(n) = estimated cost of the best path that continues from n to a goal.
So, in order to find the cheapest solution, try to find the lowest values of f(n).
Conditions for optimality:
• h(n) should be an admissible heuristic that never over estimates the cost to reach the goal.
• Admissible heuristic are optimal since they think that the cost of solving the problem is less than
it actually is.
• Example problem: Route to reach Bucharest from Arad using hSLD.
A* search is complete. Whether A* is cost-optimal depends on certain properties of the heuristic.
A key property is admissibility: an admissible heuristic is one that never overestimates the cost to reach
a goal. (An admissible heuristic is therefore optimistic.) With an admissible heuristic, A* is cost-optimal,
which we can show with a proof by contradiction. Suppose the optimal path has cost C* , but the
algorithm returns a path with cost C > C*.Then there must be some node n which is on the optimal path
and is unexpanded (because if all the nodes on the optimal path had been expanded, then we would have
returned that optimal solution). So then, using the notation g*(n) to mean the cost of the optimal path
from the start to n and h*(n) to mean the cost of the optimal path from n to the nearest goal, we have:
The first and last lines form a contradiction, so the supposition that the algorithm could return a
suboptimal path must be wrong—it must be that A* returns only cost-optima paths.
A slightly stronger property is called consistency. A heuristic h(n) is consistent if, for every node and
every successor of generated by an action we have:
h(n) ≤ c(n, a, n′) + h(n′).
This is a form of the triangle inequality, which stipulates that a side of a triangle cannot be longer
than the sum of the other two sides (see Figure). An example of a consistent heuristic is the straight-line
distance hSLD that we used in getting to Bucharest.
Every consistent heuristic is admissible (but not vice versa), so with a consistent heuristic, A* is
cost-optimal. In addition, with a consistent heuristic, the first time we reach a state it will be on an
optimal path, so we never have to re-add a state to the frontier, and never have to change an entry in
reached. But with an inconsistent heuristic, we may end up with multiple paths reaching the same state,
and if each new path has a lower path cost than the previous one, then we will end up with multiple nodes
for that state in the frontier, costing us both time and space. Because of that, some implementations of A*
take care to only enter a state into the frontier once, and if a better path to the state is found, all the
successors of the state are updated (which requires that nodes have child pointers as well as parent
pointers).
We say that A* with a consistent heuristic is optimally efficient in the sense that any algorithm
that extends search paths from the initial state, and uses the same heuristic information, must expand all
nodes that are surely expanded by A* (because any one of them could have been part of an optimal
solution).
A* is efficient because it prunes away search tree nodes that are not necessary for finding an
optimal solution.
Memory-bounded search
The main issue with A* is its use of memory. In this section we’ll cover some implementation
tricks that save space, and then some entirely new algorithms that take better advantage of the available
space.
Memory is split between the frontier and the reached states. In our implementation of best first
search, a state that is on the frontier is stored in two places: as a node in the frontier (so we can decide
what to expand next) and as an entry in the table of reached states (so we now if we have visited the state
before). For many problems (such as exploring a grid), this duplication is not a concern, because the size
of frontier is much smaller than reached, so duplicating the states in the frontier requires a comparatively
trivial amount of memory. But some implementations keep a state in only one of the two places, saving a
bit of space at the cost of complicating (and perhaps slowing down) the algorithm. Another possibility is
to remove states from reached when we can prove that they are no longer needed.
For other problems, we can keep reference counts of the number of times a state has been
reached, and remove it from the reached table when there are no more ways to reach the state. For
example, on a grid world where each state can be reached only from its four neighbors, once we have
reached a state four times, we can remove it from the table.
Now let’s consider new algorithms that are designed to conserve memory usage.
Beam search
Beam search limits the size of the frontier. The easiest approach is to keep only the nodes with the
best -scores, discarding any other expanded nodes. This of course makes the search incomplete and
suboptimal, but we can choose to make good use of available memory, and the algorithm executes fast
because it expands fewer nodes. For many problems it can find good near-optimal solutions. You can
think of uniform-cost or A* search as spreading out everywhere in concentric contours, and think of
beam search as exploring only a focused portion of those contours, the portion that contains the best
candidates.
An alternative version of beam search doesn’t keep a strict limit on the size of the frontier but
instead keeps every node whose -score is within of the best -score. That way, when there are a few
strong-scoring nodes only a few will be kept, but if there are no strong nodes then more will be kept until
a strong one emerges.
Iterative-deepening A* search (IDA*)
• Like IDDFS uses depth as cutoff value in IDA* f-cost (g+h) is used as cutoff rather than depth.
• Reduces memory requirements, incurred in A*, thereby putting bound on memory hence it is
called as memory bounded algorithm
• IDA* suffers from real value costs of the problem.
Two memory bounded algorithm:
1) Recursive Best First Search
2) MA* (Memory Bounded A*)
3. Ridges: A ridge is a special form of the local maximum. It has an area which is higher than its
surrounding areas, but itself has a slope, and cannot be reached in a single move.
• Solution: With the use of bidirectional search, or by moving in different directions, we can
improve this problem.
It is an iterative algorithm that starts with an arbitrary solution to a problem and attempts to find a
better solution by changing a single element of the solution incrementally. If the change produces a better
solution, an incremental change is taken as a new solution. This process is repeated until there are no
further improvements.
function Hill-Climbing (problem), returns a state that is a local maximum.
inputs: problem, a problem
local variables: current, a node
neighbor, a node
current <-Make_Node(Initial-State[problem])
loop
do neighbor <- a highest_valued successor of current
if Value[neighbor] ≤ Value[current] then
return State[current]
current <- neighbor
end
Types of hill climbing:
1. Stochastic hill climbing chooses at random from among the uphill moves. The probability of
selection can vary with the steepness of the uphill, it finds better solutions.
2. First-choice hill climbing implements stochastic hill climbing by generating successors
randomly until one is generated that is better than the current state. This is a good strategy when a
state has many successors.
3. Random-restart hill climbing - “If at first you don’t succeed, try, try again.” It conducts a series
of hill-climbing searches from randomly generated initial state 1, until a goal is found.
4. Steepest ascent hill climbing: differs from basic hill climbing algorithm by choosing best
successor rather than the first successor that is better.
Simulated annealing:
• A hill-climbing algorithm that never makes “downhill” moves toward states with lower value is
guaranteed to be incomplete.
• In contrast, a purely random walk—that is, moving to a successor chosen uniformly at random
from the set of successors - is complete but extremely inefficient.
• Therefore, it seems reasonable to try to combine hill climbing with a random walk in some way
that yields both efficiency and completeness.
• Simulated annealing is such an algorithm.
The simulated annealing algorithm, a version of stochastic hill climbing where some downhill
moves are allowed. The schedule input determines the value of the “temperature” T as a function
of time.
• In metallurgy, SIMULATED ANNEALING is the process used to temper or harden metals and
glass by heating them to a high temperature and then gradually cooling them, thus allowing the
material to reach a low energy crystalline state.
• The innermost loop of the simulated-annealing algorithm is quite similar to hill climbing. Instead
of picking the best move, however, it picks a random move. If the move improves the situation, it
is always accepted. Otherwise, the algorithm accepts the move with some probability less than 1.
Local beam search:
• Keeping just one node in memory might seem to be an extreme reaction to the problem of
memory limitations.
• Keeps track of k states rather than just one. It begins with k randomly generated states.
• At each step, all the successors of all k states are generated. If any one is a goal, the algorithm
halts. Otherwise, it selects the k best successors from the complete list and repeats.
• In a local beam search, useful information is passed among the parallel search threads.
• The algorithm quickly abandons unfruitful searches and moves its resources to where the most
progress is being made.
• Local beam search can suffer from a lack of diversity among the k states making the search
expensive than hill climbing.
• Stochastic beam search chooses k successors at random, with the probability of choosing a given
successor being an increasing function of its value.
• In this algorithm, it holds k number of states at any given time. At the start, these states are
generated randomly. The successors of these k states are computed with the help of objective
function. If any of these successors is the maximum value of the objective function, then the
algorithm stops.
• Otherwise the (initial k states and k number of successors of the states = 2k) states are placed in a
pool. The pool is then sorted numerically. The highest k states are selected as new initial states.
This process continues until a maximum value is reached.
Evolutionary algorithms
Evolutionary algorithms can be seen as variants of stochastic beam search that are explicitly
motivated by the metaphor of natural selection in biology: there is a population of individuals (states), in
which the fittest (highest value) individuals produce offspring (successor states) that populate the next
generation, a process called recombination. There are endless forms of evolutionary algorithms, varying
in the following ways:
The size of the population.
The representation of each individual. In genetic algorithms, each individual is a string over a
finite alphabet (often a Boolean string), just as DNA is a string over the alphabet ACGT. In
evolution strategies, an individual is a sequence of real numbers, and in genetic programming
an individual is a computer program.
The mixing number, , which is the number of parents that come together to form offspring. The
most common case is ρ = 2: two parents combine their “genes” (parts of their representation) to
form offspring. When ρ = 1 we have stochastic beam search (which can be seen as asexual
reproduction). It is possible to have ρ > 2, which occurs only rarely in nature but is easy enough to
simulate on computers.
The selection process for selecting the individuals who will become the parents of the next
generation: one possibility is to select from all individuals with probability proportional to their
fitness score. Another possibility is to randomly select individuals , and then select the most fit
ones as parents.
The recombination procedure. One common approach (assuming ρ =2 ), is to randomly select a
crossover point to split each of the parent strings, and recombine the parts to form two children,
one with the first part of parent 1 and the second part of parent 2; the other with the second part of
parent 1 and the first part of parent 2.
The mutation rate, which determines how often offspring have random mutations to their
representation. Once an offspring has been generated, every bit in its composition is flipped with
probability equal to the mutation rate.
The makeup of the next generation. This can be just the newly formed offspring, or it can include
a few top-scoring parents from the previous generation (a practice called elitism, which
guarantees that overall fitness will never decrease over time). The practice of culling, in which all
individuals below a given threshold are discarded, can lead to a speedup.
Figure 4.6(a) shows a population of four 8-digit strings, each representing a state of the 8- queens
puzzle: the -th digit represents the row number of the queen in column . In (b), each state is rated by the
fitness function. Higher fitness values are better, so for the 8- queens problem we use the number of
nonattacking pairs of queens, which has a value of 8 × 7/2 = 28 for a solution. The values of the four
states in (b) are 24, 23, 20, and 11. The fitness scores are then normalized to probabilities, and the
resulting values are shown next to the fitness values in (b).
In (c), two pairs of parents are selected, in accordance with the probabilities in (b). Notice that one
individual is selected twice and one not at all. For each selected pair, a crossover point (dotted line) is
chosen randomly. In (d), we cross over the parent strings at the crossover points, yielding new offspring.
For example, the first child of the first pair gets the first three digits (327) from the first parent and the
remaining digits (48552) from the second parent. The 8-queens states involved in this recombination step
are shown in Figure 4.7.
Finally, in (e), each location in each string is subject to random mutation with a small independent
probability. One digit was mutated in the first, third, and fourth offspring. In the 8-queens problem, this
corresponds to choosing a queen at random and moving it to a random square in its column. It is often the
case that the population is diverse early on in the process, so crossover frequently takes large steps in the
state space early in the search process (as in simulated annealing). After many generations of selection
towards higher fitness, the population becomes less diverse, and smaller steps are typical. Figure 4.8
describes an algorithm that implements all these steps.
A game can be formally defined as a kind of search problem with the following elements:
Initial state – Empty squares
Players – X O
Action – X moved one spot 3rd row, 2nd colum
Result – result of each move – x is placed in 3rd
Terminal result – final result of the game – O won
Utility – final numeric value for a game that ends in terminal state – O 1, X 0
• The initial state, action function, and result function define the game tree for the game - a tree
where the nodes are game states and the edges are moves.
Mini-max algorithm
o Mini-max algorithm is a recursive or backtracking algorithm which is used in decision-making
and game theory. It provides an optimal move for the player assuming that opponent is also
playing optimally.
o Mini-Max algorithm uses recursion to search through the game-tree.
o 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.
o In this algorithm two players play the game, one is called MAX and other is called MIN.
o Both the players fight it as the opponent player gets the minimum benefit while they get the
maximum benefit.
o Both Players of the game are opponent of each other, where MAX will select the maximized
value and MIN will select the minimized value.
o The minimax algorithm performs a depth-first search algorithm for the exploration of the
complete game tree.
o The minimax algorithm proceeds all the way down to the terminal node of the tree, then backtrack
the tree as the recursion.
Working of Mini-max algorithm:
o 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.
o In this example, there are two players one is called Maximizer and other is called Minimizer.
o Maximizer will try to get the Maximum possible score, and Minimizer will try to get the
minimum possible score.
o This algorithm applies DFS, so in this game-tree, we have to go all the way through the leaves to
reach the terminal nodes.
o 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.
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.
The main drawback of the minimax algorithm is that it gets really slow for complex games such
as Chess, go, etc. This type of games has a huge branching factor, and the player has lots of choices to
decide. This limitation of the minimax algorithm can be improved from alpha-beta pruning which we
have discussed in the next topic.
Step 2: At Node D, the value of α will be calculated as its turn for Max. The value of α is compared with
firstly 2 and then 3, and the max (2, 3) = 3 will be the value of α at node D and node value will also 3.
Step 3: Now algorithm backtrack to node B, where the value of β will change as this is a turn of Min,
Now β= +∞, will compare with the available subsequent nodes value, i.e. min (∞, 3) = 3, hence at node B
now α= -∞, and β= 3
In the next step, algorithm traverse the next successor of Node B which is node E, and the values of α= -
∞, and β= 3 will also be passed.
Step 4: At node E, Max will take its turn, and the value of alpha will change. The current value of alpha
will be compared with 5, so max (-∞, 5) = 5, hence at node E α= 5 and β= 3, where α>=β, so the right
successor of E will be pruned, and algorithm will not traverse it, and the value at node E will be 5.
Step 5: At next step, algorithm again backtrack the tree, from node B to node A. At node A, the value of
alpha will be changed the maximum available value is 3 as max (-∞, 3)= 3, and β= +∞, these two values
now passes to right successor of A which is Node C.
At node C, α=3 and β= +∞, and the same values will be passed on to node F.
Step 6: At node F, again the value of α will be compared with left child which is 0, and max(3,0)= 3, and
then compared with right child which is 1, and max(3,1)= 3 still α remains 3, but the node value of F will
become 1.
Step 7: Node F returns the node value 1 to node C, at C α= 3 and β= +∞, here the value of beta will be
changed, it will compare with 1 so min (∞, 1) = 1. Now at C, α=3 and β= 1, and again it satisfies the
condition α>=β, so the next child of C which is G will be pruned, and the algorithm will not compute the
entire sub-tree G.
Step 8: C now returns the value of 1 to A here the best value for A is max (3, 1) = 3. Following is the
final game tree which is the showing the nodes which are computed and nodes which has never
computed. Hence the optimal value for the maximizer is 3 for this example.
Some special types of solution algorithms are used to solve the following types of constraints:
Linear Constraints: These type of constraints are commonly used in linear programming where each
variable containing an integer value exists in linear form only.
Non-linear Constraints: These type of constraints are used in non-linear programming where each
variable (an integer value) exists in a non-linear form.
Constrain Propagation
• Although forward checking detects many inconsistencies, it does not detect all of them.
• In local state-spaces, the choice is only one, i.e., to search for a solution. But in CSP, we have two
choices either:
• We can search for a solution or
• We can perform a special type of inference called constraint propagation.
• Constraint propagation is a special type of inference that helps in reducing the legal number of
values for the variables. The idea behind constraint propagation is local consistency.
• In local consistency, variables are treated as nodes, and each binary constraint is treated as an arc
in the given problem.
CSP Problems:
Graph coloring
Sudoku Playing
n-queen problem
Crossword
Cryptarithmetic Problem
Cryptarithmetic Problem
• This problem has one most important constraint that is, we cannot assign a different digit to the
same character. All digits should contain a unique alphabet.
• Cryptarithmetic Problem is a type of constraint satisfaction problem where the game is about
digits and its unique replacement either with alphabets or other symbols.
• In cryptarithmetic problem, the digits (0-9) get substituted by some possible alphabets or
symbols. The task in cryptarithmetic problem is to substitute each digit with an alphabet to get the
result arithmetically correct.
• We can perform all the arithmetic operations on a given cryptarithmetic problem.
The rules or constraints on a cryptarithmetic problem are as follows:
• There should be a unique digit to be replaced with a unique alphabet.
• The result should satisfy the predefined arithmetic rules, i.e., 2+2 =4, nothing else.
• Digits should be from 0-9 only.
• There should be only one carry forward, while performing the addition operation on a problem.
• The problem can be solved from both sides, i.e., lefthand side (L.H.S), or righthand side
(R.H.S)
Let’s understand the cryptarithmetic problem as well its constraints better with the help of an
example:
Given a cryptarithmetic problem, i.e., S E N D + M O R E = M O N E Y
• In this example, add both terms S E N D and M O R E to bring M O N E Y as a result.
Follow the below steps to understand the given problem by breaking it into its subparts:
• Starting from the left hand side (L.H.S) , the terms are S and M. Assign a digit which could give a
satisfactory result. Let’s assign S->9 and M->1.
• Hence, we get a satisfactory result by adding up the terms and got an assignment for O as O->0 as
well.
• Now, move ahead to the next terms E and O to get N as its output.
• Adding E and O, which means 5+0=0, which is not possible because according to
cryptarithmetic constraints, we cannot assign the same digit to two letters. So, we need to think
more and assign some other value.
• Note: When we will solve further, we will get one carry, so after applying it, the answer will be
satisfied.
• Further, adding the next two terms N and R we get,
• But, we have already assigned E->5. Thus, the above result does not satisfy the values
• because we are getting a different value for E. So, we need to think more.
• Again, after solving the whole problem, we will get a carryover on this term, so our answer
will be satisfied.