0% found this document useful (0 votes)
25 views52 pages

AI Notes Unit1&2

Uploaded by

Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views52 pages

AI Notes Unit1&2

Uploaded by

Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 52

UNIT I INTRODUCTION

Introduction–Definition - Future of Artificial Intelligence – Characteristics of Intelligent Agents–


Typical Intelligent Agents – Problem Solving Approach to Typical AI problems.

Introduction

1. INTELLIGENCE
❖ The capacity to learn and solve problems.
❖ In particular,
● the ability to solve novel problems (i.e solve new problems)
● the ability to act rationally (i.e act based on reason)
● the ability to act like humans

What is involved in intelligence?


• Ability to interact with the real world
– to perceive, understand, and act
– e.g., speech recognition and understanding and synthesis
– e.g., image understanding
– e.g., ability to take actions, have an effect

• Reasoning and Planning


– modeling the external world, given input
– solving new problems, planning, and making decisions
– ability to deal with unexpected problems, uncertainties

• Learning and Adaptation


– we are continuously learning and adapting
– our internal models are always being ―updated‖
• e.g., a baby learning to categorize and recognize animals
HISTORY OF AI
• 1943: early beginnings
– McCulloch & Pitts: Boolean circuit model of brain
• 1950: Turing
– Turing's "Computing Machinery and Intelligence―
• 1956: birth of AI
– Dartmouth meeting: "Artificial Intelligence―name adopted
• 1950s: initial promise
– Early AI programs, including
– Samuel's checkers program
– Newell & Simon's Logic Theorist
• 1955-65: ―great enthusiasm‖
– Newell and Simon: GPS, general problem solver
– Gelertner: Geometry Theorem Prover
– McCarthy: invention of LISP
• 1966—73: Reality dawns
– Realization that many AI problems are intractable
– Limitations of existing neural network methods identified
• Neural network research almost disappears
• 1969—85: Adding domain knowledge
– Development of knowledge-based systems
– Success of rule-based expert systems,
• E.g., DENDRAL, MYCIN
• But were brittle and did not scale well in practice
• 1986-- Rise of machine learning
– Neural networks return to popularity
– Major advances in machine learning algorithms and applications
• 1990-- Role of uncertainty
– Bayesian networks as a knowledge representation framework
• 1995--AI as Science
– Integration of learning, reasoning, knowledge representation
– AI methods used in vision, language, data mining, etc
Definition

DEFINITIONS OF AI
● Building systems that think like humans
―The exciting new effort to make computers think … machines with minds, in
the full and literal sense‖ -- Haugeland, 1985
―The automation of activities that we associate with human thinking, … such as
decision-making, problem solving, learning, …‖ -- Bellman, 1978

● Building systems that act like humans


―The art of creating machines that perform functions that require
intelligence when performed by people‖ -- Kurzweil, 1990

―The study of how to make computers do things at which, at the moment, people
are better‖ -- Rich and Knight, 1991

● Building systems that think rationally


―The study of mental faculties through the use of computational models‖
-- Charniak and McDermott, 1985

―The study of the computations that make it possible to perceive, reason, and act‖
-Winston, 1992

● Building systems that act rationally


―A field of study that seeks to explain and emulate intelligent behavior in terms
of computational processes‖ -- Schalkoff, 1990
―The branch of computer science that is concerned with the automation of
intelligent behavior‖ -- Luger and Stubblefield, 1993
Acting Humanly: The Turing Test Approach
❖ Test proposed by Alan Turing in 1950
❖ The computer is asked questions by a human interrogator.
The computer passes the test if a human interrogator, after posing some written questions, cannot
tell whether the written responses come from a person or not. Programming a computer to pass,
the computer need to possess the following capabilities:
❖ Natural language processing to enable it to communicate successfully in English.
❖ Knowledge representation to store what it knows or hears
❖ Automated reasoning to use the stored information to answer questions and to draw new
conclusions.
❖ Machine learning to adapt to new circumstances and to detect and extrapolate
patterns. To pass the complete Turing Test, the computer will need
❖ Computer vision to perceive the objects, and
❖ Robotics to manipulate objects and move about.
Thinking humanly: The cognitive modeling approach
We need to get inside actual working of the human mind:
(a) Through introspection – trying to capture our own thoughts as they go by;
(b) Through psychological experiments
Allen Newell and Herbert Simon, who developed GPS, the ―General Problem Solver‖
tried to trace the reasoning steps to traces of human subjects solving the same problems.
The interdisciplinary field of cognitive science brings together computer models from AI
and experimental techniques from psychology to try to construct precise and testable
theories of the workings of the human mind
Thinking rationally : The “laws of thought approach”
The Greek philosopher Aristotle was one of the first to attempt to codify ―right thinking
that is irrefutable (ie. Impossible to deny) reasoning processes. His syllogism provided
patterns for argument structures that always yielded correct conclusions when given
correct premises—for example, Socrates is a man; all men are mortal; therefore Socrates
is mortal.‖. These laws of thought were supposed to govern the operation of the mind;
their study initiated a field called logic.
Acting rationally : The rational agent approach
An agent is something that acts. Computer agents are not mere programs, but they are
expected to have the following attributes also: (a) operating under autonomous control, (b)
perceiving their environment, (c) persisting over a prolonged time period, (e) adapting to
change. A rational agent is one that acts so as to achieve the best outcome.
Future of Artificial Intelligence

● Transportation: Although it could take a decade or more to perfect them, autonomous cars will one day
ferry us from place to place.
● Manufacturing: AI powered robots work alongside humans to perform a limited range of tasks like
assembly and stacking, and predictive analysis sensors keep equipment running smoothly.
● Healthcare: In the comparatively AI-nascent field of healthcare, diseases are more quickly and accurately
diagnosed, drug discovery is sped up and streamlined, virtual nursing assistants monitor patients and big
data analysis helps to create a more personalized patient experience.
● Education: Textbooks are digitized with the help of AI, early-stage virtual tutors assist human instructors
and facial analysis gauges the emotions of students to help determine who’s struggling or bored and better
tailor the experience to their individual needs.
● Media: Journalism is harnessing AI, too, and will continue to benefit from it. Bloomberg uses Cyborg
technology to help make quick sense of complex financial reports. The Associated Press employs the natural
language abilities of Automated Insights to produce 3,700 earning reports stories per year — nearly four
times more than in the recent past.
● Customer Service: Last but hardly least, Google is working on an AI assistant that can place human-like
calls to make appointments at, say, your neighborhood hair salon. In addition to words, the system
understands context and nuance.
Characteristics of Intelligent Agents

1. Autonomy (empowerment)
Agent takes initiative, exercises control over its actions. They are Goal-oriented,
Collaborative, Flexible, Self-starting
2. Operates in the background
3. Communication (interactivity)
4. Automates repetitive tasks
5. Proactive (persistence)
6. Temporal continuity
7. Personality
8. Mobile agents
9. Intelligence and learning.

Agents and its types

An agent can be anything that perceiveits environment through sensors and act upon that environment
through actuators. An Agent runs in the cycle of perceiving, thinking, and acting. An agent can be:

o Human-Agent: A human agent has eyes, ears, and other organs which work for sensors and hand,
legs, vocal tract work for actuators.
o Robotic Agent: A robotic agent can have cameras, infrared range finder, NLP for sensors and
various motors for actuators.
o Software Agent: Software agent can have keystrokes, file contents as sensory input and act on those
inputs and display output on the screen.

Hence the world around us is full of agents such as thermostat, cellphone, camera, and even we are also
agents.

Before moving forward, we should first know about sensors, effectors, and actuators.

Sensor: Sensor is a device which detects the change in the environment and sends the information to other
electronic devices. An agent observes its environment through sensors.

Actuators: Actuators are the component of machines that converts energy into motion. The actuators are
only responsible for moving and controlling a system. An actuator can be an electric motor, gears, rails, etc.

Effectors: Effectors are the devices which affect the environment. Effectors can be legs, wheels, arms,
fingers, wings, fins, and display screen.

Agents can be grouped into four classes based on their degree of perceived intelligence and capability :
● Simple Reflex Agents
● Model-Based Reflex Agents
● Goal-Based Agents
● Utility-Based Agents
● Learning Agent

Simple reflex agents

Simple reflex agents ignore the rest of the percept history and act only on the basis of the current percept.
Percept history is the history of all that an agent has perceived till date. The agent function is based on
the condition-action rule. A condition-action rule is a rule that maps a state i.e, condition to an action. If the
condition is true, then the action is taken, else not. This agent function only succeeds when the environment
is fully observable. For simple reflex agents operating in partially observable environments, infinite loops
are often unavoidable. It may be possible to escape from infinite loops if the agent can randomize its actions.
Problems with Simple reflex agents are :
● Very limited intelligence.
● No knowledge of non-perceptual parts of state.
● Usually too big to generate and store.
● If there occurs any change in the environment, then the collection of rules need to be updated.
Model-based reflex agents

It works by finding a rule whose condition matches the current situation. A model-based agent can
handle partially observable environments by use of model about the world. The agent has to keep track
of internal state which is adjusted by each percept and that depends on the percept history. The current state
is stored inside the agent which maintains some kind of structure describing the part of the world which
cannot be seen. Updating the state requires information about :
● how the world evolves in-dependently from the agent, and
● how the agent actions affects the world.

Goal-based agents

These kind of agents take decision based on how far they are currently from their goal(description of
desirable situations). Their every action is intended to reduce its distance from the goal. This allows the
agent a way to choose among multiple possibilities, selecting the one which reaches a goal state. The
knowledge that supports its decisions is represented explicitly and can be modified, which makes these
agents more flexible. They usually require search and planning. The goal-based agent’s behavior can easily
be changed.
Utility-based agents

The agents which are developed having their end uses as building blocks are called utility based agents.
When there are multiple possible alternatives, then to decide which one is best, utility-based agents are
used.They choose actions based on a preference (utility) for each state. Sometimes achieving the desired
goal is not enough. We may look for a quicker, safer, cheaper trip to reach a destination. Agent happiness
should be taken into consideration. Utility describes how “happy” the agent is. Because of the uncertainty
in the world, a utility agent chooses the action that maximizes the expected utility. A utility function maps a
state onto a real number which describes the associated degree of happiness.

Learning Agent
A learning agent in AI is the type of agent which can learn from its past experiences or it has learning
capabilities.
It starts to act with basic knowledge and then able to act and adapt automatically through learning.
A learning agent has mainly four conceptual components, which are:
1. Learning element :It is responsible for making improvements by learning from the environment
2. Critic: Learning element takes feedback from critic which describes how well the agent is doing with
respect to a fixed performance standard.
3. Performance element: It is responsile for selecting external action
4. Problem Generator: This component is responsible for suggesting actions that will lead to new and
informative experiences.

Problem Solving Approach to Typical AI problems

Problem Solving : Introduction


● Problem Solving in games such as “Sudoku” can be an example. It can be done by building an
artificially intelligent system to solve that particular problem. To do this, one needs to define the
problem statements first and then generating the solution by keeping the conditions in mind.
● Some of the most popularly used problem solving with the help of artificial intelligence are:
1. Chess.
2. Travelling Salesman Problem.
3. Tower of Hanoi Problem.
4. Water-Jug Problem.
5. N-Queen Problem.

Problem Searching
● In general, searching refers to as finding information one needs.
● Searching is the most commonly used technique of problem solving in artificial intelligence.
● The searching algorithm helps us to search for solution of particular problem.

Problem
● Problems are the issues which comes across any system. A solution is needed to solve that particular
problem.

Steps : Solve Problem Using Artificial Intelligence


● The process of solving a problem consists of five steps. These are:
Problem Solving in Artificial Intelligence

1. Defining The Problem: The definition of the problem must be included precisely. It should contain
the possible initial as well as final situations which should result in acceptable solution.
2. Analyzing The Problem: Analyzing the problem and its requirement must be done as few features
can have immense impact on the resulting solution.
3. Identification Of Solutions: This phase generates reasonable amount of solutions to the given
problem in a particular range.
4. Choosing a Solution: From all the identified solutions, the best solution is chosen basis on the
results produced by respective solutions.
5. Implementation: After choosing the best solution, its implementation is done.

AI Technique
AI technique is a method that exploits knowledge that should be represented in such a way
that:
• The knowledge captures generalizations. In other words, it is not necessary to represent
separately each individual situation. Instead, situations that share important properties are
grouped together. If knowledge does not have this property, inordinate amounts of memory and
updating will be required. So we usually call something without this property "data" rather than
knowledge.
• It can be understood by people who must provide it. Although for many programs, the bulk
of the data can be acquired automatically (for example, by taking readings from a variety of
instruments), in many AI domains, most of the knowledge a program has must ultimately be
provided by people in terms they understand.
• It can easily be modified to correct errors and to reflect changes in the world and in our
world view.
• It can be used in a great many situations even if it is not totally accurate or complete.
• It can be used to help overcome its own sheer bulk by helping to narrow the range of
possibilities that must usually be considered.
Although AI techniques must be designed in keeping with these constraints imposed by AI
problems, there is some degree of independence between problems and problem-solving
techniques. It is possible to solve AI problems without using AI techniques (although, as we
suggested above, those solutions are not likely to be very good).
Tic-Tac-Toe
Solution 1
⮚ Data Structure:

⮚ Elements of vector:
✔ 0 : Empty
✔ 1:X
✔ 2: O
⮚ The vector is a ternary number
⮚ Store inside the program a move-table (lookup table):
⮚ #Elements in the table: 19683 (=39)
⮚ Element = A vector which describes the most suitable move from the current game-board

Algorithm
1. View the vector as a ternary number. Convert it to a decimal number.
2. Use the computed number as an index into Move-Table and access the vector stored there.
3. Set the new board to that vector.
Comments
1. A lot of space to store the Move-Table.
2. A lot of work to specify all the entries in the Move-Table.
3. Difficult to extend.
Solution 2 Data
Structure
⮚ Use vector, called board, as Solution 1
⮚ However, elements of the vector:
✔ : Empty
✔ :X
✔ :O
⮚ Turn of move: indexed by integer
✔ 1,2,3, etc.
Function Library:
1. Make2:
⮚ Return a location on a game-board.
IF (board[5] = 2)
RETURN 5; //the center cell.
ELSE
RETURN any cell that is not at the board‘s corner;
// (cell: 2,4,6,8)

⮚ Let P represent for X or O


⮚ can_win(P) :
✔ P has filled already at least two cells on a straight line (horizontal, vertical, or
diagonal)
⮚ cannot_win(P) = NOT(can_win(P))
2. Posswin(P):
IF (cannot_win(P))
RETURN 0;
ELSE
RETURN index to the empty cell on the line of
can_win(P)
⮚ Let odd numbers are turns of X
⮚ Let even numbers are turns of O
3. Go(n): make a move.
IF odd(turn) THEN // for X
Board[n] = 3
ELSE // for O
Board[n] = 5
turn = turn + 1
Algorithm:
1. Turn = 1: (X moves)
Go(1) //make a move at the left-top cell
2. Turn = 2: (O moves)
IF board[5] is empty THEN
Go(5)
ELSE
Go(1)
3. Turn = 3: (X moves)
IF board[9] is empty THEN
Go(9)
ELSE
Go(3).
4. Turn = 4: (O moves)
IF Posswin(X) <> 0 THEN
Go(Posswin(X))
//Prevent the opponent to win
ELSE Go(Make2)
5. Turn = 5: (X moves)
IF Posswin(X) <> 0 THEN
Go(Posswin(X))
//Win for X.
ELSE IF Posswin(O) <> THEN
Go(Posswin(O))
//Prevent the opponent to win
ELSE IF board[7] is empty THEN
Go(7)
ELSE Go(3).
Comments:
1. Not efficient in time, as it has to check several conditions before making each move.
2. Easier to understand the program‘s strategy.
3. Hard to generalize.
4. Checking for a possible win is quicker.
5. Human finds the row-scan approach easier, while computer finds the number-counting
approach more efficient.
Solution 3
Data Structure
1. Game-board: Use vector as described for the above program
2. List:
✔ Contains possible game-boards generated from the current game-board
✔ Each the game-board is augmented a score indicating the possibility of victory of the
current turn
Algorithm:
1. If it is a win, give it the highest rating.
2. Otherwise, consider all the moves the opponent could make next. Assume the opponent will
make the move that is worst for us. Assign the rating of that move to the current node.
3. The best node is then the one with the highest rating.
Comments:
1. Require much more time to consider all possible moves.
2. Could be extended to handle more complicated games.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
UNIT II PROBLEM SOLVING METHODS

Problem solving Methods - Search Strategies- Uninformed - Informed - Heuristics - Local Search
Algorithms and Optimization Problems - Searching with Partial Observations - Constraint
Satisfaction Problems – Constraint Propagation - Backtracking Search - Game Playing - Optimal
Decisions in Games – Alpha - Beta Pruning - Stochastic Games

Problem solving Methods

A problem can be defined formally by four components:


1. Initial state
2. Successor function
3. Goal test
4. Path cost
1. Initial State
The starting state which agent knows itself.
2. Successor Function
● A description of the possible actions available to the agent.
● State x, successor – FN (x) returns a set of < action, successor> ordered pairs, where
each action is a legal action in a state x and each successor is a state that can be
reached from x by applying that action.
State Space
The set of all possible states reachable from the initial state by any sequence of
actions. The initial state and successor function defines the state space. The state
space forms a graph in which nodes are state and axis between the nodes are action.
Path
A path in the state space is a sequence of state connected by a sequence of actions.
3. Goal Test
Test to determine whether the given state is the goal state. If there is an explicit set of possible
goal states, then we can whether any one of the goal state is reached or not.
Example : In chess, the goal is to reach a state called ―checkmate‖ where the opponent‘s
king is under attack and can‘t escape.
4. Path cost
A function that assigns a numeric cost to each path. The cost of a path can be described as the
sum of the costs of the individual actions along that path.
Step cost of taking an action ‗a‘ to go from one state ‗x‘ to state ‗y‘ is denoted by
C(x,a,y) C-Cost , x,y- states , Action , Step costs are non-negative
These 4 elements are gathered into a data structure that is given as input to problem solving
algorithm. A solution quality is measured by path cost function. An optimal solution has lowest
path cost among all solutions.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
Total cost = Path cost + Search
cost Example: Route finding
problem

Initial State: In (Coimbatore)


Successor Function: {< Go (Pollachi), In (Pollachi)>
< Go (Erode), In (Erode)>
< Go (Palladam), In (Palladam)>
< Go (Mettupalayam), In (Mettupalayam)>}
Goal Test: In (Chennai)
Path Cost: {(In (Coimbatore),}
{Go (Erode),} = 100 [kilometers]
{In (Erode)}
Path cost = 100 + 66 + 200 + 140 = 506
TOY PROBLEM
Example-1 : Vacuum World Problem
– States. 2 x 22 = 8 states
– Formula n2n states
• Initial State
– Any one of 8 states
• Successor Function
– Legal states that result from three actions (Left, Right, Absorb)
• Goal Test
– All squares are clean
• Path Cost
– Number of steps (each step costs a value of 1)

KLNCE/BE/CSE/VI SEM/AI/2019-2020
Fig 1.2 Vacuum World

Fig: 1.3 State Space for the


Vacuum World State Space for the Vacuum World
Labels on Arcs denote L: Left, R: Right, S: Suck

Example 2: Playing chess


Initial State: Described as an 8 X 8 array where each positions contains a symbol
standing for the appropriate piece in the official chess position.
Successor function: The legal states that results from set of rules.
They can be described easily by as a set of rules consisting of two parts: a left side
that serves as a pattern to be matched against the current board position and a right
side that describes the changes to be made to the board position to reflect the move.
An example is shown in the following figure.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
Fig 1.4:The legal states that results from set of rules
However if we write rules like the one above, we have to write a very large number
of them since there has to be a separate set of rule for each of them roughly 10 120
possible board positions.
Practical difficulties to implement large number of rules,
1. It will take too long to implement large number of rules and could not be
done without mistakes.
2. No program could easily handle all those rules and storing it possess
serious difficulties.
In order to minimize such problems, we have to write rules describing the legal moves in as a
general way as possible. The following is the way to describe the chess moves.

Current Position
While pawn at square (e, 2), AND Square (e, 3) is empty, AND Square (e , 4 ) is empty.
Changing Board Position
Move pawn from Square (e, 2) to Square ( e , 4 ) .
Some of the problems that fall within the scope of AI and the kinds of techniques will
be useful to solve these problems.
Goal Test
Any position in which the opponent does not have a legal move and his or her
king is under attack.
Example: 3 Water Jug Problem
A Water Jug Problem: You are given two jugs, a 4-gallon one and a 3-gallon one, a
pump which has unlimited water which you can use to fill the jug, and the ground on
which water may be poured. Neither jug has any measuring markings on it. How can
you get exactly 2 gallons of water in the 4-gallon jug?
State: (x, y) x= 0, 1, 2, 3, or 4 y= 0, 1, 2, 3
KLNCE/BE/CSE/VI SEM/AI/2019-2020
x represents quantity of water in 4-gallon jug and y represents quantity of water in 3-gallon
jug.
• Start state: (0, 0).
• Goal state: (2, n) for any n. Attempting to end up in a goal state.( since the problem
doesn‘t specify the quantity of water in 3-gallon jug)

1. (x, y) →(4, y) Fill the 4-gallon jug


If x <4
2. (x, y) →(x, 3) Fill the 3-gallon jug
If y <3
3. (x, y) →(x −d, y) Pour some water
out of the
If x >0 4-gallon jug
4. (x, y) →(x, y −d) Pour some water
out of the
If y >0 3-gallon jug
5. (x, y) →(0, y) Empty the 4-gallon jug on
the
If x >0 ground
6. (x, y) →(x, 0) Empty the 3-gallon jug on
the
If y >0 ground
7. (x, y) →(4, y −(4 Pour water from the 3-gallon
−x)) jug
If x +y ≥4,y into the 4-gallon jug until the
>0
4-gallon jug is full
8. (x, y) →(x −(3 −y), Pour water from the 4-gallon
3) jug
If x +y ≥3,x into the 3-gallon jug
>0 until the 3-gallon jug
is full
9. (x, y) →(x +y, 0) Pour all the water from the
3-gallon
If x +y ≤4,y jug into the 4-gallon jug
>0
10. (x, y) →(0, x +y) Pour all the water from the

KLNCE/BE/CSE/VI SEM/AI/2019-2020
4-gallon
If x +y ≤3,x jug into the 3-gallon jug
>0
11. (0, 2) →(2, 0) Pour the 2 gallons from the
3-gallon
Jug into the 4-gallon jug
12. (2, y) →(0, y) Empty the 2 gallons in the 4-
gallon
Jug on the ground
Production rules for the water jug problem
Trace of steps involved in solving the water jug problem First solution
Number of Steps Rules applied 4-g jug 3-g jug

1 Initial state 0 0
2 R2 {Fill 3-g jug} 0 3
3 R7 {Pour all water from 3 to 4-g jug} 3 0
4 R2 {Fill 3-g jug} 3 3
5 R5 {Pour from 3 to 4-g jug until it is full} 4 2
6 R3 {Empty 4-gallon jug} 0 2
7 R7 {Pour all water from 3 to 4-g jug} 2 0
Goal
State
Second Solution
Number of Steps Rules applied 4-g jug 3-g jug

1 Initial state 0 0
2 R1 {Fill 4-gallon jug} 4 0
3 R6 {Pour from 4 to 3-g jug until it is full} 1 3
4 R4 {Empty 3-gallon jug} 1 0
5 R8 {Pour all water from 4 to 3-gallon jug} 0 1
6 R1 {Fill 4-gallon jug} 4 1
7 R6 {Pour from 4 to 3-g jug until it is full} 2 3
8 R4 {Empty 3-gallon jug} 2 0
Goal
State

Example - 5 8-puzzle Problem

KLNCE/BE/CSE/VI SEM/AI/2019-2020
The 8-puzzle problem consists of a 3 x 3 board with eight numbered tiles and a blank
space. A tile adjacent to the blank space can slide into the space. The object is to
reach a specified goal state.
States: A state description specifies the location of each of the eight tiles and the
blank in one of the nine squares.
Initial state: Any state can be designated as the initial state.
Successor function: This generates the legal states that result from trying the four
actions (blank moves Left, Right, Up, or Down).
Goal test: This checks whether the state matches the goal configuration (Other goal
configurations are possible.)
Path cost: Each step costs 1, so the path cost is the number of steps in the path.

Fig 1.5 8 Puzzle Problem


Search Strategies

Uninformed Search Algorithms

Uninformed search is a class of general-purpose search algorithms which operates in brute force-
way. Uninformed search algorithms do not have additional information about state or search
space other than how to traverse the tree, so it is also called blind search.
Following are the various types of uninformed search algorithms:
1. Breadth-first Search
2. Depth-first Search
3. Depth-limited Search
4. Iterative deepening depth-first search
5. Uniform cost search
6. Bidirectional Search

1. Breadth-first Search:

o Breadth-first search is the most common search strategy for traversing a tree or graph.
This algorithm searches breadthwise in a tree or graph, so it is called breadth-first search.
o BFS algorithm starts searching from the root node of the tree and expands all successor
node at the current level before moving to nodes of next level.
KLNCE/BE/CSE/VI SEM/AI/2019-2020
o The breadth-first search algorithm is an example of a general-graph search algorithm.
o Breadth-first search implemented using FIFO queue data structure.

Advantages:
o BFS will provide a solution if any solution exists.
o If there are more than one solutions for a given problem, then BFS will provide the
minimal solution which requires the least number of steps.

Disadvantages:
o It requires lots of memory since each level of the tree must be saved into memory to
expand the next level.
o BFS needs lots of time if the solution is far away from the root node.

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:
1. S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K

Time Complexity: Time Complexity of BFS algorithm can be obtained by the number of nodes
traversed in BFS until the shallowest Node. Where the d= depth of shallowest solution and b is a
node at every state.
T (b) = 1+b2+b3+.......+ bd= O (bd)

KLNCE/BE/CSE/VI SEM/AI/2019-2020
Space Complexity: Space complexity of BFS algorithm is given by the Memory size of frontier
which is O(bd).
Completeness: BFS is complete, which means if the shallowest goal node is at some finite
depth, then BFS will find a solution.
Optimality: BFS is optimal if path cost is a non-decreasing function of the depth of the node.

2. Depth-first Search

o Depth-first search is a recursive algorithm for traversing a tree or graph data structure.
o It is called the depth-first search because it starts from the root node and follows each
path to its greatest depth node before moving to the next path.
o DFS uses a stack data structure for its implementation.
o The process of the DFS algorithm is similar to the BFS algorithm.

Note: Backtracking is an algorithm technique for finding all possible solutions using recursion.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
Advantage:
o DFS requires very less memory as it only needs to store a stack of the nodes on the path
from root node to the current node.
o It takes less time to reach to the goal node than BFS algorithm (if it traverses in the right
path).

Disadvantage:
o There is the possibility that many states keep re-occurring, and there is no guarantee of
finding the solution.
o DFS algorithm goes for deep down searching and sometime it may go to the infinite loop.

Example:
In the below search tree, we have shown the flow of depth-first search, and it will follow the
order as:
Root node--->Left node ----> right node.
It will start searching from root node S, and traverse A, then B, then D and E, after traversing E,
it will backtrack the tree as E has no other successor and still goal node is not found. After
backtracking it will traverse node C and then G, and here it will terminate as it found goal node.

Completeness: DFS search algorithm is complete within finite state space as it will expand
every node within a limited search tree.
Time Complexity: Time complexity of DFS will be equivalent to the node traversed by the
algorithm. It is given by:
T(n)= 1+ n2+ n3 +.........+ nm=O(nm)
Where, m= maximum depth of any node and this can be much larger than d (Shallowest solution
depth)
KLNCE/BE/CSE/VI SEM/AI/2019-2020
Space Complexity: DFS algorithm needs to store only single path from the root node, hence
space complexity of DFS is equivalent to the size of the fringe set, which is O(bm).
Optimal: DFS search algorithm is non-optimal, as it may generate a large number of steps or
high cost to reach to the goal node.

3. Depth-Limited Search Algorithm:

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.
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.
Advantages:
Depth-limited search is Memory efficient.
Disadvantages:
o Depth-limited search also has a disadvantage of incompleteness.
o It may not be optimal if the problem has more than one solution.
Example:

Completeness: DLS search algorithm is complete if the solution is above the depth-limit.
Time Complexity: Time complexity of DLS algorithm is O(bℓ).
Space Complexity: Space complexity of DLS algorithm is O(b×ℓ).
KLNCE/BE/CSE/VI SEM/AI/2019-2020
Optimal: Depth-limited search can be viewed as a special case of DFS, and it is also not optimal
even if ℓ>d.
4. Uniform-cost Search Algorithm:
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.
Uniform-cost search expands nodes according to their path costs form the root node. It can be
used to solve any graph/tree where the optimal cost is in demand. 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.
Advantages:
o Uniform cost search is optimal because at every state the path with the least cost is
chosen.
Disadvantages:
o It does not care about the number of steps involve in searching and only concerned about
path cost. Due to which this algorithm may be stuck in an infinite loop.

Example:

KLNCE/BE/CSE/VI SEM/AI/2019-2020
Completeness:
Uniform-cost search is complete, such as if there is a solution, UCS will find it.
Time Complexity:
Let C* is Cost of the optimal solution, and ε is each step to get closer to the goal node. Then the
number of steps is = C*/ε+1. Here we have taken +1, as we start from state 0 and end to C*/ε.
Hence, the worst-case time complexity of Uniform-cost search isO(b1 + [C*/ε])/.
Space Complexity:
The same logic is for space complexity so, the worst-case space complexity of Uniform-cost
search is O(b1 + [C*/ε]).
Optimal:
Uniform-cost search is always optimal as it only selects a path with the lowest path cost.

5. Iterative deepening depth-first Search:

The iterative deepening algorithm is a combination of DFS and BFS algorithms. This search
algorithm finds out the best depth limit and does it by gradually increasing the limit until a goal
is found.
This algorithm performs depth-first search up to a certain "depth limit", and it keeps increasing
the depth limit after each iteration until the goal node is found.
This Search algorithm combines the benefits of Breadth-first search's fast search and depth-first
search's memory efficiency.
The iterative search algorithm is useful uninformed search when search space is large, and depth
of goal node is unknown.
Advantages:
o Itcombines the benefits of BFS and DFS search algorithm in terms of fast search and
memory efficiency.

Disadvantages:
o The main drawback of IDDFS is that it repeats all the work of the previous phase.

Example:
Following tree structure is showing the iterative deepening depth-first search. IDDFS algorithm
performs various iterations until it does not find the goal node. The iteration performed by the
algorithm is given as:

KLNCE/BE/CSE/VI SEM/AI/2019-2020
1'st Iteration-----> A
2'nd Iteration----> A, B, C
3'rd Iteration------>A, B, D, E, C, F, G
4'th Iteration------>A, B, D, H, I, E, C, F, K, G
In the fourth iteration, the algorithm will find the goal node.
Completeness:
This algorithm is complete is ifthe branching factor is finite.
Time Complexity:
Let's suppose b is the branching factor and depth is d then the worst-case time complexity
is O(bd).
Space Complexity:
The space complexity of IDDFS will be O(bd).
Optimal:
IDDFS algorithm is optimal if path cost is a non- decreasing function of the depth of the node.

6. Bidirectional Search Algorithm:

KLNCE/BE/CSE/VI SEM/AI/2019-2020
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.
Bidirectional search replaces one single search graph with two small subgraphs in which one
starts the search from an initial vertex and other starts from goal vertex. The search stops when
these two graphs intersect each other.
Bidirectional search can use search techniques such as BFS, DFS, DLS, etc.
Advantages:
o Bidirectional search is fast.
o Bidirectional search requires less memory

Disadvantages:
o Implementation of the bidirectional search tree is difficult.
o In bidirectional search, one should know the goal state in advance.

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.
The algorithm terminates at node 9 where two searches meet.

Completeness: Bidirectional Search is complete if we use BFS in both searches.


Time Complexity: Time complexity of bidirectional search using BFS is O(bd).
Space Complexity: Space complexity of bidirectional search is O(bd).

KLNCE/BE/CSE/VI SEM/AI/2019-2020
Optimal: Bidirectional search is Optimal.
Informed Search Algorithm& Heuristics
Informed search algorithm contains an array of knowledge such as how far we are from the goal,
path cost, how to reach to goal node, etc. This knowledge help agents to explore less to the
search space and find more efficiently the goal node.
The informed search algorithm is more useful for large search space. Informed search algorithm
uses the idea of heuristic, so it is also called Heuristic search.
Heuristics function: Heuristic is a function which is used in Informed Search, and it finds the
most promising path. It takes the current state of the agent as its input and produces the
estimation of how close agent is from the goal. The heuristic method, however, might not always
give the best solution, but it guaranteed to find a good solution in reasonable time. Heuristic
function estimates how close a state is to the goal. It is represented by h(n), and it calculates the
cost of an optimal path between the pair of states. The value of the heuristic function is always
positive.
Admissibility of the heuristic function is given as:
1. h(n) <= h*(n)
Here h(n) is heuristic cost, and h*(n) is the estimated cost. Hence heuristic cost should be
less than or equal to the estimated cost.

Pure Heuristic Search:

Pure heuristic search is the simplest form of heuristic search algorithms. It expands nodes based
on their heuristic value h(n). It maintains two lists, OPEN and CLOSED list. In the CLOSED
list, it places those nodes which have already expanded and in the OPEN list, it places nodes
which have yet not been expanded.
On each iteration, each node n with the lowest heuristic value is expanded and generates all its
successors and n is placed to the closed list. The algorithm continues unit a goal state is found.
In the informed search we will discuss two main algorithms which are given below:
o Best First Search Algorithm(Greedy search)
o A* Search Algorithm

1.) Best-first Search Algorithm (Greedy Search):


Greedy best-first search algorithm always selects the path which appears best at that moment. It
is the combination of depth-first search and breadth-first search algorithms. It uses the heuristic
function and search. Best-first search allows us to take the advantages of both algorithms. With
the help of best-first search, at each step, we can choose the most promising node. In the best
first search algorithm, we expand the node which is closest to the goal node and the closest cost
is estimated by heuristic function, i.e.
1. f(n)= g(n).
KLNCE/BE/CSE/VI SEM/AI/2019-2020
Were, h(n)= estimated cost from node n to the goal.
The greedy best first algorithm is implemented by the priority queue.
Best first search algorithm:
o Step 1: Place the starting node into the OPEN list.
o Step 2: If the OPEN list is empty, Stop and return failure.
o Step 3: Remove the node n, from the OPEN list which has the lowest value of h(n), and
places it in the CLOSED list.
o Step 4: Expand the node n, and generate the successors of node n.
o Step 5: Check each successor of node n, and find whether any node is a goal node or not.
If any successor node is goal node, then return success and terminate the search, else
proceed to Step 6.
o Step 6: For each successor node, algorithm checks for evaluation function f(n), and then
check if the node has been in either OPEN or CLOSED list. If the node has not been in
both list, then add it to the OPEN list.
o Step 7: Return to Step 2.
Advantages:
o Best first search can switch between BFS and DFS by gaining the advantages of both the
algorithms.
o This algorithm is more efficient than BFS and DFS algorithms.
Disadvantages:
o It can behave as an unguided depth-first search in the worst case scenario.
o It can get stuck in a loop as DFS.
o This algorithm is not optimal.
Example:
Consider the below search problem, and we will traverse it using greedy best-first search. At
each iteration, each node is expanded using evaluation function f(n)=h(n) , which is given in the
below table.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
In this search example, we are using two lists which are OPEN and CLOSED Lists. Following
are the iteration for traversing the above example.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
KLNCE/BE/CSE/VI SEM/AI/2019-2020
Expand the nodes of S and put in the CLOSED list
Initialization: Open [A, B], Closed [S]
Iteration 1: Open [A], Closed [S, B]
Iteration 2: Open [E, F, A], Closed [S, B]
: Open [E, A], Closed [S, B, F]
Iteration 3: Open [I, G, E, A], Closed [S, B, F]
: Open [I, E, A], Closed [S, B, F, G]
Hence the final solution path will be: S----> B----->F----> G
Time Complexity: The worst case time complexity of Greedy best first search is O(bm).
Space Complexity: The worst case space complexity of Greedy best first search is O(bm).
Where, m is the maximum depth of the search space.
Complete: Greedy best-first search is also incomplete, even if the given state space is finite.
Optimal: Greedy best first search algorithm is not optimal.
2.) A* Search Algorithm:
A* search is the most commonly known form of best-first search. It uses heuristic function h(n),
and cost to reach the node n from the start state g(n). It has combined features of UCS and
greedy best-first search, by which it solve the problem efficiently. A* search algorithm finds the
shortest path through the search space using the heuristic function. This search algorithm
expands less search tree and provides optimal result faster. A* algorithm is similar to UCS
except that it uses g(n)+h(n) instead of g(n).
In A* search algorithm, we use search heuristic as well as the cost to reach the node. Hence we
can combine both costs as following, and this sum is called as a fitness number.

At each point in the search space, only those node is expanded which have the lowest value of
f(n), and the algorithm terminates when the goal node is found.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
Algorithm of A* search:
Step1: Place the starting node in the OPEN list.
Step 2: Check if the OPEN list is empty or not, if the list is empty then return failure and stops.
Step 3: Select the node from the OPEN list which has the smallest value of evaluation function
(g+h), if node n is goal node then return success and stop, otherwise
Step 4: Expand node n and generate all of its successors, and put n into the closed list. For each
successor n', check whether n' is already in the OPEN or CLOSED list, if not then compute
evaluation function for n' and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it should be attached to the back
pointer which reflects the lowest g(n') value.
Step 6: Return to Step 2.
Advantages:
o A* search algorithm is the best algorithm than other search algorithms.
o A* search algorithm is optimal and complete.
o This algorithm can solve very complex problems.
Disadvantages:
o It does not always produce the shortest path as it mostly based on heuristics and
approximation.
o A* search algorithm has some complexity issues.
o The main drawback of A* is memory requirement as it keeps all generated nodes in the
memory, so it is not practical for various large-scale problems.
Example:
In this example, we will traverse the given graph using the A* algorithm. The heuristic value of
all states is given in the below table so we will calculate the f(n) of each state using the formula
f(n)= g(n) + h(n), where g(n) is the cost to reach any node from start state.

Here we will use OPEN and CLOSED list.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
Solution:

Initialization: {(S, 5)}


Iteration1: {(S--> A, 4), (S-->G, 10)}
Iteration2: {(S--> A-->C, 4), (S--> A-->B, 7), (S-->G, 10)}
Iteration3: {(S--> A-->C--->G, 6), (S--> A-->C--->D, 11), (S--> A-->B, 7), (S-->G, 10)}
Iteration 4 will give the final result, as S--->A--->C--->G it provides the optimal path with cost
6.
Points to remember:
o A* algorithm returns the path which occurred first, and it does not search for all
remaining paths.
o The efficiency of A* algorithm depends on the quality of heuristic.
o A* algorithm expands all nodes which satisfy the condition f(n)<="" li="">

Complete: A* algorithm is complete as long as:


o Branching factor is finite.
o Cost at every action is fixed.

Optimal: A* search algorithm is optimal if it follows below two conditions:


KLNCE/BE/CSE/VI SEM/AI/2019-2020
o Admissible: the first condition requires for optimality is that h(n) should be an
admissible heuristic for A* tree search. An admissible heuristic is optimistic in nature.
o Consistency: Second required condition is consistency for only A* graph-search.

If the heuristic function is admissible, then A* tree search will always find the least cost path.
Time Complexity: The time complexity of A* search algorithm depends on heuristic function,
and the number of nodes expanded is exponential to the depth of solution d. So the time
complexity is O(b^d), where b is the branching factor.
Space Complexity: The space complexity of A* search algorithm is O(b^d).
Local Search Algorithms and Optimization Problems

They start from a prospective solution and then move to a neighboring solution. They can return
a valid solution even if it is interrupted at any time before they end.
Hill-Climbing Search
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
Disadvantage − This algorithm is neither complete, nor optimal.
Local Beam Search
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.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
function BeamSearch( problem, k), returns a solution state.
start with k randomly generated states
loop
generate all successors of all k states
if any of the states = solution, then return the state
else select the k best successors
end
Simulated Annealing
Annealing is the process of heating and cooling a metal to change its internal structure for
modifying its physical properties. When the metal cools, its new structure is seized, and the
metal retains its newly obtained properties. In simulated annealing process, the temperature is
kept variable.
We initially set the temperature high and then allow it to ‘cool' slowly as the algorithm
proceeds. When the temperature is high, the algorithm is allowed to accept worse solutions with
high frequency.
Start

● Initialize k = 0; L = integer number of variables;


● From i → j, search the performance difference Δ.
● If Δ <= 0 then accept else if exp(-Δ/T(k)) > random(0,1) then accept;
● Repeat steps 1 and 2 for L(k) steps.
● k = k + 1;
Repeat steps 1 through 4 till the criteria is met.End
Travelling Salesman Problem
In this algorithm, the objective is to find a low-cost tour that starts from a city, visits all cities
en-route exactly once and ends at the same starting city.
Start
Find out all (n -1)! Possible solutions, where n is the total number of cities.
Determine the minimum cost by finding out the cost of each of these (n -1)!
solutions.
Finally, keep the one with the minimum cost.
end

KLNCE/BE/CSE/VI SEM/AI/2019-2020
Searching with Partial Observations

Constraint Satisfaction Problems

Constraint satisfaction problems (CSPs) are mathematical questions defined as a set of objects
whose state must satisfy a number of constraints or limitations. CSPs represent the entities in a
problem as a homogeneous collection of finite constraints over variables, which is solved
by constraint satisfaction methods. CSPs are the subject of intense research in both artificial
intelligence and operations research, since the regularity in their formulation provides a common
basis to analyze and solve problems of many seemingly unrelated families. CSPs often exhibit
high complexity, requiring a combination of heuristics and combinatorial search methods to be
solved in a reasonable time. Constraint Programming (CP) is the field of research that
specifically focuses on tackling with this kind of problems [1][2]. Additionally, boolean
satisfiability problem (SAT), the satisfiability modulo theories (SMT), mixed integer
programming (MIP) and answer set programming (ASP) are all fields of research focusing on the
resolution of particular forms of the constraint satisfaction problem.
Examples of simple problems that can be modeled as a constraint satisfaction problem include:

● Eight queens puzzle


● Map coloring problem
● Sudoku, Crosswords, Futoshiki, Kakuro (Cross Sums), Numbrix, Hidato and many
other logic puzzles

Map coloring problem


Map coloring is a special case of graph labeling; it is an assignment of labels
traditionally called "colors" to elements of a graph subject to certain constraints. In its simplest
form, it is a way of coloring the vertices of a graph such that no two adjacent vertices are of the
same color; this is called a vertex coloring. Similarly, an edge coloring assigns a color to each
edge so that no two adjacent edges are of the same color, and a face coloring of a planar graph

KLNCE/BE/CSE/VI SEM/AI/2019-2020
assigns a color to each face or region so that no two faces that share
a boundary have the same color.
Vertex coloring is usually used to introduce graph coloring
problems since other coloring problems can be transformed into a
vertex coloring instance. For example, an edge coloring of a graph is
just a vertex coloring of its line graph, and a face coloring of a plane
graph is just a vertex coloring of its dual. However, non-vertex
coloring problems are often stated and studied as is. This is partly
pedagogical, and partly because some problems are best studied in
their non-vertex form, as in the case of edge coloring.
The convention of using colors originates from coloring the countries of a map, where each face
is literally colored. This was generalized to coloring the faces of a graph embedded in the plane.
By planar duality it became coloring the vertices, and in this form it generalizes to all graphs. In
mathematical and computer representations, it is typical to use the first few positive or non-
negative integers as the "colors". In general, one can use any finite set as the "color set". The
nature of the coloring problem depends on the number of colors but not on what they are.
Graph coloring enjoys many practical applications as well as theoretical challenges. Beside the
classical types of problems, different limitations can also be set on the graph, or on the way a
color is assigned, or even on the color itself. It has even reached popularity with the general
public in the form of the popular number puzzle Sudoku. Graph coloring is still a very active
field of research.

Constraint propagation: Inference in CSPs

A number of inference techniques use the constraints to infer which variable/value pairs are
consistent and which are not. These include node, arc, path, and k-consistent.
constraint propagation: Using the constraints to reduce the number of legal values for a
variable, which in turn can reduce the legal values for another variable, and so on.
local consistency: If we treat each variable as a node in a graph and each binary constraint as an
arc, then the process of enforcing local consistency in each part of the graph causes inconsistent
values to be eliminated throughout the graph.
There are different types of local consistency:
Node consistency
A single variable (a node in the CSP network) is node-consistent if all the values in the
variable’s domain satisfy the variable’s unary constraint.
We say that a network is node-consistent if every variable in the network is node-consistent.

Arc consistency
A variable in a CSP is arc-consistent if every value in its domain satisfies the variable’s binary
constraints.
Xi is arc-consistent with respect to another variable Xj if for every value in the current domain
Di there is some value in the domain Dj that satisfies the binary constraint on the arc (Xi, Xj).
A network is arc-consistent if every variable is arc-consistent with every other variable.
Arc consistency tightens down the domains (unary constraint) using the arcs (binary constraints).

KLNCE/BE/CSE/VI SEM/AI/2019-2020
AC-3 algorithm:

AC-3 maintains a queue of arcs which initially contains all the arcs in the CSP.
AC-3 then pops off an arbitrary arc (Xi, Xj) from the queue and makes Xi arc-consistent with
respect to Xj.
If this leaves Di unchanged, just moves on to the next arc;
But if this revises Di, then add to the queue all arcs (Xk, Xi) where Xk is a neighbor of Xi.
If Di is revised down to nothing, then the whole CSP has no consistent solution, return failure;
Otherwise, keep checking, trying to remove values from the domains of variables until no more
arcs are in the queue.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
The result is an arc-consistent CSP that have the same solutions as the original one but have
smaller domains.
The complexity of AC-3:
Assume a CSP with n variables, each with domain size at most d, and with c binary constraints
(arcs). Checking consistency of an arc can be done in O(d2) time, total worst-case time is O(cd3).

Path consistency
Path consistency: A two-variable set {Xi, Xj} is path-consistent with respect to a third variable
Xm if, for every assignment {Xi = a, Xj = b} consistent with the constraint on {X i, Xj}, there is an
assignment to Xm that satisfies the constraints on {Xi, Xm} and {Xm, Xj}.
Path consistency tightens the binary constraints by using implicit constraints that are inferred by
looking at triples of variables.

K-consistency
K-consistency: A CSP is k-consistent if, for any set of k-1 variables and for any consistent
assignment to those variables, a consistent value can always be assigned to any kth variable.
1-consistency = node consistency; 2-consisency = arc consistency; 3-consistensy = path
consistency.
A CSP is strongly k-consistent if it is k-consistent and is also (k - 1)-consistent, (k – 2)-
consistent, … all the way down to 1-consistent.
A CSP with n nodes and make it strongly n-consistent, we are guaranteed to find a solution in
time O(n2d). But algorithm for establishing n-consitentcy must take time exponential in n in the
worse case, also requires space that is exponential in n.

Global constraints
A global constraint is one involving an arbitrary number of variables (but not necessarily all
variables). Global constraints can be handled by special-purpose algorithms that are more
efficient than general-purpose methods.
1) inconsistency detection for Alldiff constraints
A simple algorithm: First remove any variable in the constraint that has a singleton domain, and
delete that variable’s value from the domains of the remaining variables. Repeat as long as there
are singleton variables. If at any point an empty domain is produced or there are more vairables
than domain values left, then an inconsistency has been detected.
A simple consistency procedure for a higher-order constraint is sometimes more effective than
applying arc consistency to an equivalent set of binary constrains.
2) inconsistency detection for resource constraint (the atmost constraint)
We can detect an inconsistency simply by checking the sum of the minimum of the current
domains;
e.g.
Atmost(10, P1, P2, P3, P4): no more than 10 personnel are assigned in total.
If each variable has the domain {3, 4, 5, 6}, the Atmost constraint cannot be satisfied.
We can enforce consistency by deleting the maximum value of any domain if it is not consistent
with the minimum values of the other domains.
e.g. If each variable in the example has the domain {2, 3, 4, 5, 6}, the values 5 and 6 can be
deleted from each domain.
3) inconsistency detection for bounds consistent

KLNCE/BE/CSE/VI SEM/AI/2019-2020
For large resource-limited problems with integer values, domains are represented by upper and
lower bounds and are managed by bounds propagation.
e.g.
suppose there are two flights F1 and F2 in an airline-scheduling problem, for which the planes
have capacities 165 and 385, respectively. The initial domains for the numbers of passengers on
each flight are
D1 = [0, 165] and D2 = [0, 385].
Now suppose we have the additional constraint that the two flight together must carry 420
people: F1 + F2 = 420. Propagating bounds constraints, we reduce the domains to
D1 = [35, 165] and D2 = [255, 385].
A CSP is bounds consistent if for every variable X, and for both the lower-bound and upper-
bound values of X, there exists some value of Y that satisfies the constraint between X and Y for
every variable Y.

Sudoku
A Sudoku puzzle can be considered a CSP with 81 variables, one for each square. We use the
variable names A1 through A9 for the top row (left to right), down to I1 through I9 for the
bottom row. The empty squares have the domain {1, 2, 3, 4, 5, 6, 7, 8, 9} and the pre-filled
squares have a domain consisting of a single value.
There are 27 different Alldiff constraints: one for each row, column, and box of 9 squares:
Alldiff(A1, A2, A3, A4, A5, A6, A7, A8, A9)
Alldiff(B1, B2, B3, B4, B5, B6, B7, B8, B9)

Alldiff(A1, B1, C1, D1, E1, F1, G1, H1, I1)
Alldiff(A2, B2, C2, D2, E2, F2, G2, H2, I2)

Alldiff(A1, A2, A3, B1, B2, B3, C1, C2, C3)
Alldiff(A4, A5, A6, B4, B5, B6, C4, C5, C6)

Backtracking search for CSPs

Backtracking search, a form of depth-first search, is commonly used for solving CSPs. Inference
can be interwoven with search.
Commutativity: CSPs are all commutative. A problem is commutative if the order of
application of any given set of actions has no effect on the outcome.
Backtracking search: A depth-first search that chooses values for one variable at a time and
backtracks when a variable has no legal values left to assign.
Backtracking algorithm repeatedly chooses an unassigned variable, and then tries all values in
the domain of that variable in turn, trying to find a solution. If an inconsistency is detected, then
BACKTRACK returns failure, causing the previous call to try another value.
There is no need to supply BACKTRACKING-SEARCH with a domain-specific initial state,
action function, transition model, or goal test.
BACKTRACKING-SARCH keeps only a single representation of a state and alters that
representation rather than creating a new ones.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
To solve CSPs efficiently without domain-specific knowledge, address following questions:
1)function SELECT-UNASSIGNED-VARIABLE: which variable should be assigned next?
function ORDER-DOMAIN-VALUES: in what order should its values be tried?
2)function INFERENCE: what inferences should be performed at each step in the search?
3)When the search arrives at an assignment that violates a constraint, can the search avoid
repeating this failure?

1. Variable and value ordering

KLNCE/BE/CSE/VI SEM/AI/2019-2020
SELECT-UNASSIGNED-VARIABLE
Variable selection—fail-first
Minimum-remaining-values (MRV) heuristic: The idea of choosing the variable with the
fewest “legal” value. A.k.a. “most constrained variable” or “fail-first” heuristic, it picks a
variable that is most likely to cause a failure soon thereby pruning the search tree. If some
variable X has no legal values left, the MRV heuristic will select X and failure will be detected
immediately—avoiding pointless searches through other variables.
E.g. After the assignment for WA=red and NT=green, there is only one possible value for SA, so
it makes sense to assign SA=blue next rather than assigning Q.
[Powerful guide]
Degree heuristic: The degree heuristic attempts to reduce the branching factor on future choices
by selecting the variable that is involved in the largest number of constraints on other unassigned
variables. [useful tie-breaker]
e.g. SA is the variable with highest degree 5; the other variables have degree 2 or 3; T has degree
0.

ORDER-DOMAIN-VALUES
Value selection—fail-last
If we are trying to find all the solution to a problem (not just the first one), then the ordering does
not matter.
Least-constraining-value heuristic: prefers the value that rules out the fewest choice for the
neighboring variables in the constraint graph. (Try to leave the maximum flexibility for
subsequent variable assignments.)
e.g. We have generated the partial assignment with WA=red and NT=green and that our next
choice is for Q. Blue would be a bad choice because it eliminates the last legal value left for Q’s
neighbor, SA, therefore prefers red to blue.

The minimum-remaining-values and degree heuristic are domain-independent methods for


deciding which variable to choose next in a backtracking search. The least-constraining-
value heuristic helps in deciding which value to try first for a given variable.
KLNCE/BE/CSE/VI SEM/AI/2019-2020
2. Interleaving search and inference
INFERENCE
forward checking: [One of the simplest forms of inference.] Whenever a variable X is assigned,
the forward-checking process establishes arc consistency for it: for each unassigned variable Y
that is connected to X by a constraint, delete from Y’s domain any value that is inconsistent with
the value chosen for X.
There is no reason to do forward checking if we have already done arc consistency as a
preprocessing step.

Advantage: For many problems the search will be more effective if we combine the MRV
heuristic with forward checking.
Disadvantage: Forward checking only makes the current variable arc-consistent, but doesn’t
look ahead and make all the other variables arc-consistent.
MAC (Maintaining Arc Consistency) algorithm: [More powerful than forward checking,
detect this inconsistency.] After a variable Xi is assigned a value, the INFERENCE procedure
calls AC-3, but instead of a queue of all arcs in the CSP, we start with only the arcs(X j, Xi) for all
Xj that are unassigned variables that are neighbors of X i. From there, AC-3 does constraint
propagation in the usual way, and if any variable has its domain reduced to the empty set, the call
to AC-3 fails and we know to backtrack immediately.

Game Playing
Game Playing is an important domain of artificial intelligence. Games don’t require much
knowledge; the only knowledge we need to provide is the rules, legal moves and the conditions
of winning or losing the game.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
Both players try to win the game. So, both of them try to make the best move possible at each
turn. Searching techniques like BFS(Breadth First Search) are not accurate for this as the
branching factor is very high, so searching will take a lot of time. So, we need another search
procedures that improve –
● Generate procedure so that only good moves are generated.
● Test procedure so that the best move can be explored first.
The most common search technique in game playing is Minimax search procedure.

Optimal Decisions in Games


Minimax search procedure is depth-first depth-

Minimax algorithm uses two functions –

MOVEGEN : It generates all the possible moves that can be generated from the current
position.
STATICEVALUATION : It returns a value depending upon the goodness from the viewpoint
otwo-player
This algorithm is a two player game, so we call the first player as PLAYER1 and second player
as PLAYER2. The value of each node is backed-up from its children. For PLAYER1 the backed-
up value is the maximum value of its children and for PLAYER2 the backed-up value is the
minimum value of its children. It provides most promising move to PLAYER1, assuming that
the PLAYER2 has make the best move. It is a recursive algorithm, as same procedure occurs at
each level. limited search procedure. It is used for games like chess and tic-tac-toe.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
Figure 1: Before backing-up of values

Figure 2: After backing-up of values


We assume that PLAYER1 will start the game. 4 levels are generated. The value to nodes H, I, J,
K, L, M, N, O is provided by STATICEVALUATION function. Level 3 is maximizing level, so
all nodes of level 3 will take maximum values of their children. Level 2 is minimizing level, so
all its nodes will take minimum values of their children. This process continues. The value of A
is 23. That means A should choose C move to win.
Alpha - Beta Pruning

Key points about alpha-beta pruning:


o The Max player will only Alpha-beta pruning is a modified version of the minimax
algorithm. It is an optimization technique for the minimax algorithm.
o As we have seen in the minimax search algorithm that the number of game states it has to
examine are exponential in depth of the tree. Since we cannot eliminate the exponent, but
we can cut it to half. Hence there is a technique by which without checking each node of
the game tree we can compute the correct minimax decision, and this technique is
called pruning. This involves two threshold parameter Alpha and beta for future
expansion, so it is called alpha-beta pruning. It is also called as Alpha-Beta Algorithm.
o Alpha-beta pruning can be applied at any depth of a tree, and sometimes it not only prune
the tree leaves but also entire sub-tree.
o The two-parameter can be defined as:
0. Alpha: The best (highest-value) choice we have found so far at any point along
the path of Maximizer. The initial value of alpha is -∞.
a. Beta: The best (lowest-value) choice we have found so far at any point along the
path of Minimizer. The initial value of beta is +∞.
The Alpha-beta pruning to a standard minimax algorithm returns the same move as the
standard algorithm does, but it removes all the nodes which are not really affecting the
final decision but making algorithm slow. Hence by pruning these nodes, it makes the
algorithm fast.
Condition for Alpha-beta pruning:
The main condition which required for alpha-beta pruning is:
1. α>=β
KLNCE/BE/CSE/VI SEM/AI/2019-2020
o update the value of alpha.
o The Min player will only update the value of beta.
o While backtracking the tree, the node values will be passed to upper nodes instead of
values of alpha and beta.
o We will only pass the alpha, beta values to the child nodes.
Pseudo-code for Alpha-beta Pruning:
function minimax(node, depth, alpha, beta, maximizingPlayer) is
if depth ==0 or node is a terminal node then
return static evaluation of node

if MaximizingPlayer then // for Maximizer Player


maxEva= -infinity
for each child of node do
eva= minimax(child, depth-1, alpha, beta, False)
maxEva= max(maxEva, eva)
alpha= max(alpha, maxEva)
if beta<=alpha
break
return maxEva

else // for Minimizer player


minEva= +infinity
for each child of node do
eva= minimax(child, depth-1, alpha, beta, true)
minEva= min(minEva, eva)
beta= min(beta, eva)
if beta<=alpha
break
return minEva
Working of Alpha-Beta Pruning:
Let's take an example of two-player search tree to understand the working of Alpha-beta pruning
Step 1: At the first step the, Max player will start first move from node A where α= -∞ and β=
+∞, these value of alpha and beta passed down to node B where again α= -∞ and β= +∞, and
Node B passes the same value to its child D.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
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

KLNCE/BE/CSE/VI SEM/AI/2019-2020
α>=β, 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.

KLNCE/BE/CSE/VI SEM/AI/2019-2020
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.

Move Ordering in Alpha-Beta pruning:


The effectiveness of alpha-beta pruning is highly dependent on the order in which each node is
examined. Move order is an important aspect of alpha-beta pruning.
It can be of two types:
o Worst ordering: In some cases, alpha-beta pruning algorithm does not prune any of the
leaves of the tree, and works exactly as minimax algorithm. In this case, it also consumes
more time because of alpha-beta factors, such a move of pruning is called worst ordering.
In this case, the best move occurs on the right side of the tree. The time complexity for
such an order is O(bm).

KLNCE/BE/CSE/VI SEM/AI/2019-2020
o Ideal ordering: The ideal ordering for alpha-beta pruning occurs when lots of pruning
happens in the tree, and best moves occur at the left side of the tree. We apply DFS hence
it first search left of the tree and go deep twice as minimax algorithm in the same amount
of time. Complexity in ideal ordering is O(bm/2).

Stochastic Games
The game is played in a sequence of stages. At the beginning of each stage the game is in
some state. The players select actions and each player receives a payoff that depends on the
current state and the chosen actions. The game then moves to a new random state whose
distribution depends on the previous state and the actions chosen by the players. The procedure is
repeated at the new state and play continues for a finite or infinite number of stages. The total
payoff to a player is often taken to be the discounted sum of the stage payoffs or the limit
inferior of the averages of the stage payoffs.
Stochastic games generalize both Markov decision processes and repeated games.

Two-player games
Stochastic two-player games on directed graphs are widely used for modeling and analysis of
discrete systems operating in an unknown (adversarial) environment. Possible configurations of a
system and its environment are represented as vertices, and the transitions correspond to actions
of the system, its environment, or "nature". A run of the system then corresponds to an infinite
path in the graph. Thus, a system and its environment can be seen as two players with
antagonistic objectives, where one player (the system) aims at maximizing the probability of
"good" runs, while the other player (the environment) aims at the opposite.
In many cases, there exists an equilibrium value of this probability, but optimal strategies for
both players may not exist.

KLNCE/BE/CSE/VI SEM/AI/2019-2020

You might also like