Artificial-Intelligence NOTES
Artificial-Intelligence NOTES
IT
SEMESTER - V (CBCS)
ARTIFICIAL INTELLIGENCE
Published by
Director
Institute of Distance and Open learning ,
University of Mumbai,Vidyanagari, Mumbai - 400 098.
UNIT I
1. Introduction 1
2. Intelligent Agents 13
UNIT II
3. Solving Problems By Searching 29
4. Beyond Classical Search 53
UNIT III
5. Adversarial Search 70
6. Logical Agent 89
UNIT IV
7. First-Order Logic 109
8. Inference In First-Order Logic 133
UNIT V
9. Planning 162
10. Knowledge Representation 174
*****
Syllabus
B. Sc. (Information Technology) Semester – V
Course Name: Artificial Intelligence Course Code: 53704
(Elective I)
Periods per week (1 Period is 50 minutes) 5
Credits 2
Hours Marks
Evaluation System Theory
TheoryExamination
Examination Internal
2½ 75
Internal - 25
*****
UNIT I
1
INTRODUCTION
Unit Structure
1.1 Objectives
1.2 Introduction
1.2.1 Introduction to AI
1.2.2 Objectives of AI
1.3 What is Artificial Intelligence?
1.3.1 What is AI.
1.3.2 Definitions of AI
1.4 Foundations of AI
1.4.1 Introduction
1.4.2 Turing Test
1.4.3 Weak AI versus Strong AI
1.4.4 Philosophy
1.5 History
1.6 The state of art AI today.
1.6.1 Current AI Innovations
1.6.2 Practical Applications of AI
1.7 Summary
1.8 Unit End Questions
1.9 References
1.1 OBJECTIVES
After going through this unit, you will be able to:
Define Artificial Intelligence
Understand foundations of AI
Explain how the AI was evolved
Explain history of AI.
Describe the Today’s AI, and Where and What research/ work is
going in AI field.
1.2 INTRODUCTION
1.2.1 Introduction to Ai:
Artificial Intelligence is to make computers intelligent so that they can act
intelligently as humans. It is the study of making computers does things
which at the moment people are better at.
1
AI has made great effort in building intelligent systems and understanding Introduction
them. Another reason to understand AI is that these systems are interesting
and useful in their own right.
Many tools and techniques are used to construct AI systems. The AI
encompasses a huge variety of fields like computer science, mathematics,
logical reasoning, linguistics, neuro-science and psychology to perform
specific tasks.
AI can be used in many areas like playing chess, proving mathematical
theorems, writing poetry, diagnosing diseases, creating expert systems,
speech recognition etc.
2
Logical Agent
Artificial Intelligence 1.3.2 Definitions of AI:
Artificial intelligence is “The science and engineering of making
intelligent machines, especially intelligent computer programs”.
Artificial Intelligence is making a computer, a computer-controlled robot,
or a software think intelligently, as the intelligent humans think. AI is
accomplished by studying how the human brain thinks and how humans
learn, decide, and work while trying to solve a problem, and then using the
outcomes of this study as a basis of developing intelligent software and
systems.
3
Category 4) Systems that act rationally: Introduction
1.4 FOUNDATIONS OF AI
1.4.1 Introduction:
AI is a field which has inherited many ideas, viewpoints, and techniques
from other disciplines like mathematics, formal theories of logic,
probability, decision making, and computation.
From over 2000 years of tradition in philosophy, theories of reasoning and
learning have emerged, along with the viewpoint that the mind is
constituted by the operation of a physical system. From over 400 years of
mathematics, we have formal theories of logic, probability, decision
making, and computation. From psychology, we have the tools with which
to investigate the human mind, and a scientific language within which to
express the resulting theories. From linguistics, we have theories of the
structure and meaning of language. Finally, from computer science, we
have the tools with which to make AI a reality.
1.4.2 Turing Test:
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
4
Logical Agent
Artificial Intelligence ● Robotics to manipulate objects and move about.
Problem: Turing test is not reproducible, constructive, or amenable to
mathematical analysis
1.4.3 Weak Ai Versus Strong Ai:
The definition of AI is along two dimensions, human vs. ideal and thought
vs. action. But there are other dimensions that are worth considering. One
dimension is whether we are interested in theoretical results or in practical
applications. Another is whether we intend our intelligent computers to be
conscious or not.. The claim that machines can be conscious is called the
strong AI claim; the WEAK AI position makes no such claim. Artificial
intelligence is …
a. "a collection of algorithms that are computationally tractable,
adequate approximations of intractably specified problems"
(Partridge, 1991)
b. "the enterprise of constructing a physical symbol system that can
reliably pass the Turing Test" (Ginsberg, 1993)
c. "the field of computer science that studies how machines can be made
to act intelligently" (Jackson, 1986)
d. "a field of study that encompasses computational techniques for
performing tasks that apparently require intelligence when performed
by humans" (Tanimoto, 1990)
e. "a very general investigation of the nature of intelligence and the
principles and mechanisms required for understanding or replicating
it" (Sharpies et ai, 1989)
f. "the getting of computers to do things that seems to be intelligent"
(Rowe, 1988)
Mathematics:
Philosophers staked out most of the important ideas of AI, but to make the
leap to a formal science required a level of mathematical formalization in
three main areas: computation, logic,
5
Algorithm and Probability: Introduction
Linguistics (1957-Present):
Linguists showed that language use fits into this model. Modern
linguistics and AI, then, were "born" at about the same time, and grew up
together, intersecting in a hybrid field called computational linguistics or
natural language processing.
6
Logical Agent
Artificial Intelligence In 1957. B. F. Skinner published Verbal Behavior. It is related to
behaviorist approach to language learning. But curiously, a review of the
book became as well-known as the book itself, and served to almost kill
off interest in behaviorism. The author of the review was Noam Chomsky,
who had just published a book on his own theory. Syntactic Structures.
Chomsky showed how the behaviorist theory did not address the notion of
creativity in language—it did not explain how a child could understand
and make up sentences that he or she had never heard before. Chomsky's
theory—based on syntactic models going back to the Indian linguist
Panini (c. 350 B.C.)—could explain this, and unlike previous theories, it
was formal enough that it could in principle be programmed.
1.5 HISTORY OF AI
The Gestation of Artificial Intelligence (1943-1955):
The first work that is now generally recognized as AI was done by Warren
McCulloch and Walter Pitts (1943). They drew on three sources:
knowledge of the basic physiology and function of neurons in the brain;
the formal analysis of propositional logic due to Russell and Whitehead;
and Turing's theory of computation.
McCarthy convinced Minsky, Claude Shannon, and Nathaniel Rochester
to help him bring together U.S. researchers interested in automata theory,
neural nets, and the study of intelligence. They organized a two-month
workshop at Dartmouth in the summer of 1956. Perhaps the longest-
lasting thing to come out of the workshop was an agreement to adopt
McCarthy's new name for the field: artificial intelligence.
7
A Dose of Reality (1966-1973): Introduction
Recent Events:
In recent years, approaches based on hidden Markov models (HMMs)
have come to dominate the area. Speech technology and the related field
of handwritten character recognition are already making the transition to
widespread industrial and consumer applications.
The Bayesian network formalism was invented to allow efficient
representation of, and rigorous reasoning with, uncertain knowledge.
8
Logical Agent
Artificial Intelligence such as Apple's Siri, Amazon's Alexa, Google's Assistant, and Microsoft's
Cortana. Some of them are listed below in tabular format.
Game Playing:
IBM's Deep Blue became the first computer program to defeat the world
champion in a chess match when it bested Garry Kasparov by a score of
3.5 to 2.5 in an exhibition match (Goodman and Keene, 1997).
Autonomous Control:
The ALVINN computer vision system was trained to steer a car to keep it
following a lane. It was placed in CMU's NAVLAB computer-controlled
minivan and used to navigate across the United States-for 2850 miles it
was in control of steering the vehicle 98% of the time.
Diagnosis:
Medical diagnosis programs based on probabilistic analysis have been
able to perform at the level of an expert physician in several areas of
medicine.
Logistics Planning:
During the Persian Gulf crisis of 1991, U.S. forces deployed a Dynamic
Analysis and Replanning Tool, DART (Cross and Walker, 1994), to do
automated logistics planning and scheduling for transportation. This
involved up to 50,000 vehicles, cargo, and people at a time, and had to
9
account for starting points, destinations, routes, and conflict resolution Introduction
among all parameters. The AI planning techniques allowed a plan to be
generated in hours that would have taken weeks with older methods. The
Defense Advanced Research Project Agency (DARPA) stated that this
single application more than paid back DARPA's 30-year investment in
AI.
Robotics:
Many surgeons now use robot assistants in microsurgery. HipNav
(DiGioia et al., 1996) is a system that uses computer vision techniques to
create a three-dimensional model of a patient's internal anatomy and then
uses robotic control to guide the insertion of a hip replacement prosthesis.
1.7 SUMMARY
Artificial intelligence is “The science and engineering of making
intelligent machines, especially intelligent computer programs”.
Turing test: To pass the complete Turing test, the computer will need
Computer vision to perceive the objects, and Robotics to manipulate
objects and move about.
Artificial intelligence is to make computers intelligent so that they can act
intelligently as humans.
Categorical definitions of AI are: Systems that think like humans, Systems
that think rationally, Systems that act like humans, Systems that act
rationally.
AI comprises of Philosophy, Mathematics, Algorithm and Probability,
Psychology, Computer Engineering, Linguistics etc.
Work on Machine Intelligence started early. But the period which is
considered as History of AI started from The Gestation of Artificial
Intelligence (1943-1955), till The Return of Neural Networks (1986-
Present) and Recent Events.
There are various current AI innovations such as Autonomous cars
(Driverless car), Speech Recognition, Chatbot, Web search engines,
Translator, Natural Language Processing, Medical Diagnosis, Imaging
Pattern Detection etc.
Various practical applications of AI such as Autonomous Planning And
Scheduling, Game Playing, Autonomous Control, Diagnosis, Logistics
10
Logical Agent
Artificial Intelligence Planning, Robotics, Language Understanding And Problem Solving etc.
11
that AI's traditional focus on higher-level cognitive abilities is Introduction
misplaced?
1.7 "Surely computers cannot be intelligent—they can only do what their
programmers tell them." Is the latter statement true, and does it imply
the former?
1.8 "Surely animals cannot be intelligent—they can only do what their
genes tell them." Is the latter statement true, and does it imply the
former?
1.9 REFERENCES
Artificial Intelligence: A Modern Approach, 4th US ed.by Stuart Russell
and Peter Norvig.
Deepak Khemani, “A first course in Artificial Intelligence”, McGraw Hill
edition, 2013.
Patrick Henry Winston , “Artificial Intelligence”, Addison-Wesley, Third
Edition
*****
12
2
INTELLIGENT AGENTS
Unit Structure
2.1 Objectives
2.2 Introduction
2.3 Agents and environment
2.4 Good Behavior
2.4.1 Rational Agent
2.4.2 Mapping from percept sequences to actions
2.4.3 Performance Measure
2.4.4 PEAS
2.5 Nature of environment
2.6 The structure of agents
2.6.1 Structure
2.6.2 Softbots
2.6.3 PAGE
2.6.4 Types of Agent
2.7 Summary
2.8 Unit End Questions
2.9 References
2.1 OBJECTIVES
After going through this unit, you will be able to:
Define AI Agent,
Understand the Agent and its Environment
Understand the Rationality of agent, Performance measure
Identify the PEAS, and PAGE
Understand the nature of Environment
Explain the Structure of Agents
2.2 INTRODUCTION
An agent is anything that can be viewed as capturing its environment
through cameras, sensors or some other input devices and acting upon that
environment through effectors. A human agent has eyes, ears, and other
organs for sensors, and hands, legs, mouth, and other body parts for
effectors. A robotic agent substitutes cameras and infrared range finders
for the sensors and various motors for the effectors. A software agent has
13 encoded bit strings as its percepts and actions.
The focus is on design agents that do a good job of acting on their Intelligent Agents
environment,
What we mean by a good job, rationality, performance measure, then
different designs for successful agents, which things should be known to
the agent and several kinds of environments.
14
Artificial Intelligence Agent perceives the signals of other robotic agent actions, temperature,
humidity, pressure, air type, atmospheric conditions, climate, other
surrounding conditions and many more. In addition to this, in taxi driving
example – road conditions, traffic conditions, weather conditions, rain or
smoke fog, wind, driving rules etc.
Let’s Consider the Vacuum Cleaner robotic agent. It cleans the room. For
simplicity two tiles are considered. The Vacuum cleaner agent is present
on Tile A and observing the environment i.e. Tile A and Tile B both. Both
the tiles have some dirt. This agent has to clean the dirt by sucking it.
The following table shows the percept sequence and its associated action.
16
Artificial Intelligence Autonomous Agent:
An agent is autonomous to the extent that its action choices depend on its
own experience, rather than on knowledge of the environment that has
been built-in by the designer.
There are various autonomous agent are in development stage. Driverless
car- Waymo, Google’s Alexa, etc.
Examples:
1) Automated Car Driving agent/Driverless Car:
Performance measures: Safety, Optimum speed, Comfortable journey,
Maximize profits.
Environment: Roads, Traffic conditions, Clients.
Actuators: Steering wheel, Accelerator, Gear, Brake, Light signal, Horn.
Sensors: Cameras, Sonar system, Speedometer, GPS, Engine sensors, etc.
2) Medical Diagnosis system:
Performance measures: Healthy patient (Sterilized instruments), minimize
costs.
17
Environment: Patients, Doctors, Hospital environment. Intelligent Agents
Example:
Accessible environment- An empty closed room whose state can be
defined by its temperature, air pressure, humidity.
Inaccessible environment- Information about an event occurs in the
atmosphere of the earth.
18
Artificial Intelligence previous episodes. Episodic environments are much simpler because the
agent does not need to think ahead. However, in a non-episodic
environment, an agent requires memory of past actions to determine the
next best actions.
19
The relationship among agents, architectures, and programs can be Intelligent Agents
summed up as follows:
agent = architecture + program
20
Artificial Intelligence pictures, gestures performance
instructor, note- measure:
classmates taking (?) grade
)
sound
(language)
4. Satellite Pixels of Print a Correct Images
image varying categorizat categorizatio from
analysis intensity, ion of n orbiting
system color scene satellite
5. Part- Pixels of Pick up Place parts in Conveyor
picking varying parts and correct bins belt
robot intensity sort into with parts
bins
6. Refinery Temperatu Open, Maximize Refinery
controlle re, close purity,
r pressure valves; yield, safety
readings adjust
temperatur
e
7. Interacti Typed Print Maximize Set of
ve words exercises, student's students
English suggestion score on
tutor s, test
correction
s
22
Artificial Intelligence 2) Type 2: Agents that keep track of the world:
It is a reflex agent with internal state.
Internal State is a representation of unobserved aspects of current state
depending on percept history.
Consider an example of a driverless car. If the car in front is a recent
model, and has the centrally mounted brake light, then it will be possible
to tell if it is braking from a single image. Unfortunately, older models
have different configurations of tail lights, brake lights, and turn-signal
lights, and it is not always possible to tell if the car is braking.
Thus, even for the simple braking rule, our driver will have to maintain
some sort of internal state in order to choose an action. Here, the internal
state is not too extensive—it just needs the previous frame from the
camera to detect when two red lights at the edge of the vehicle go on or off
simultaneously.
Consider one case in car driving: from time to time, the driver looks in the
rear-view mirror to check on the locations of nearby vehicles. When the
driver is not looking in the mirror, the vehicles in the next lane are
invisible (i.e., the states in which they are present and absent are
indistinguishable); but in order to decide on a lane-change maneuver, the
driver needs to know whether or not they are there.
It happens because the sensors do not provide access to the complete state
of the world. In such cases, the agent may need to maintain some internal
state information in order to distinguish between world states that generate
the same perceptual input but nonetheless are significantly different. Here,
"significantly different" means that different actions are appropriate in the
two states.
As time passes internal state information has to be updated. For this two
kinds of knowledge have to be encoded in the agent program. First, we
need some information about how the world evolves independently of the
agent—for example, that an overtaking car generally will be closer behind
than it was a moment ago. Second, we need some information about how
the agent's own actions affect the world—for example, that when the agent
changes lanes to the right, there is a gap (at least temporarily) in the lane it
was in before.
Following Figure shows how the current percept is combined with the old
internal state to generate the updated description of the current state.
23
Intelligent Agents
Diagram: Schematic Diagram for Agent that keep track of the world/
Reflex Agent with Internal State
Agent Program:
The interesting part is the function Update-State, which is responsible for
creating the new internal state description. As well as interpreting the new
percept in the light of existing knowledge about the state, it uses
information about how the world evolves to keep track of the unseen parts
of the world, and also must know about what the agent's actions do to the
state of the world.
Following is the reflex agent with internal state. It is an agent that keeps
track of the world. It works by finding a rule whose condition matches the
current situation (as defined by the percept and the stored internal state)
and then doing the action associated with that rule.
function Reflex-Agent-With-State(percept) returns action
static: state, a description of the current world state
rules, a set of condition-action rules
state Update-State (state, percept)
rule Rule-Match (state, rules)
action Rule-Action [rule]
state Update-State (state, action)
return action.
3) Type 3: Goal-based agents:
Goal-based agents act so that they will achieve their goal
If just the current state of the environment is known then it is not always
enough to decide what to do.
24
Artificial Intelligence The agent tries to reach a desirable state, the goal may be provided from
the outside (user, designer, environment), or inherent to the agent itself
The agent performs the action by considering the Goal. To achieve the
goal, agent has to consider long sequences of actions. Here, Search &
Planning is required. Decision making is required. It involves
consideration of the future—both "What will happen if I do such-and-
such?" and "Will that make me happy?
Goal based agents can only differentiate between goal states and non goal
states. Hence, their performance can be 100% or zero.
Goal-based agent is more flexible than reflex agent since the knowledge
supporting a decision is explicitly modeled, thereby allowing for
modifications.
Limitation: Once the goal is fixed, all the actions are taken to fulfill it.
And the agent loses flexibility to change its actions according to the
current situation.
Example: Vacuum cleaning Robot agent whose goal is to keep the house
clean all the time. This agent will keep searching for dirt in the house and
will keep the house clean all the time.
25
reliable, or cheaper than others. Goals just provide a crude distinction Intelligent Agents
between "happy" and "unhappy" states, whereas a more general
performance measure should allow a comparison of different world states
(or sequences of states) according to exactly how happy they would make
the agent if they could be achieved. Because "happy" does not sound very
scientific, the customary terminology is to say that if one world state is
preferred to another, then it has higher utility for the agent.
Utility is therefore a function that maps a state onto a real number, which
describes the associated degree of happiness. A complete specification of
the utility function allows rational decisions in two kinds of cases where
goals have trouble. First, when there are conflicting goals, only some of
which can be achieved (for example, speed and safety), the utility function
specifies the appropriate trade-off. Second, when there are several goals
that the agent can aim for, none of which can be achieved with certainty,
utility provides a way in which the likelihood of success can be weighed
up against the importance of the goals
Utility function is used to map a state to a measure of utility of that state.
We can define a measure for determining how advantageous a particular
state is for an agent. To obtain this measure a utility function can be used.
The term utility is used to depict how ―HAPPY‖ the agent is to find out a
generalization.
Examples: 1) Google Maps – A route which requires Least possible time.
1) Automatic Car: Should reach to the target location within least possible
time safely and without consuming much fuel.
2.7 SUMMARY
An agent is anything that perceives its environment through sensors and
acting upon that environment through effectors.
A rational agent is the one that does ―right‖ things and acts rationally so as
to achieve the best outcome even when there is uncertainty in knowledge.
26
Artificial Intelligence PEAS- Performance, Environment, Actuators, Sensors determines the
behavior of an agent.
There are various nature of environment or properties of environment,
they are as Accessible vs. inaccessible, Deterministic vs. non
deterministic, Episodic vs. non episodic, Static vs. dynamic, Discrete vs.
continuous, etc.
Structure of Intelligent Agent: agent = architecture + program.
PAGE is used for high-level characterization of agents.
Different types of Agent program are as Simple reflex agents, Agents that
keep track of the world, Goal-based agents, Utility-based agents.
27
2.9 REFERENCES Intelligent Agents
*****
28
UNIT II
3
SOLVING PROBLEMS BY SEARCHING
Unit Structure
3.1 Objective
3.2 Introduction
3.3 Problem Solving Agents
3.4 Examples problems
3.5 Searching for solutions
3.6 Uninformed search
3.7 Informed search strategies
3.8 Heuristic functions
3.9 Summary
3.10 Exercises
3.11 Bibliography
3.1 OBJECTIVES
After this chapter, you should be able to understand the following
concepts:
1. Understand how to formulate a problem description.
2. Understand and solve some problems of Artificial Intelligence.
3. Know about uninformed search and based algorithms.
4. Understand informed search and based algorithms.
5. Know about heuristic functions and strategies.
3.2 INTRODUCTION
A problem-solving agent firstly formulates a goal and a problem to solve.
Then the agent calls a search procedure to solve it. It then uses the solution
to guide its actions. It will do whatever the solution recommends as the
next thing to do and then remove that step from the sequence. Once the
solution has been executed, the agent will formulate a new goal. Searching
techniques like uninformed and informed or heuristic use in many areas
like theorem proving, game playing, expert systems, natural language
processing etc. In search technique, firstly select one option and leave
other option if this option is our final goal, else we continue selecting,
testing and expanding until either solution is found or there are no more
states to be expanded.
29
Solving Problems by
3.3 PROBLEM SOLVING AGENTS Searching
30
Artificial Intelligence Sometimes the goal is specified by an abstract property rather than an
explicitly enumerated set of states. For 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) A path cost function that assigns a numeric cost to each path. The
problem-solving agent chooses a cost function that reflects its own
performance measure. For the agent trying to get to Mumbai, time is of
the essence, so the cost of a path might be its length in kilometres. The
step cost of taking action a in state x to reach state y is denoted by c (x,
a, y).
A solution to a problem is an action sequence that leads from the initial
state to a goal state. Solution quality is measured by the path cost function,
and an optimal solution has the lowest path cost among all solutions.
31
1) States: Solving Problems by
Searching
The state is determined by both the agent location and the dirt locations.
The agent is in one of two locations, each of which might or might not
contain dirt.
2) Initial state:
Our vacuum can be in any state of the 8 states shown in the figure 3.2.
3) Actions:
Each state has just three actions: Left, Right, and Suck
4) Transition Model:
The actions have their expected effects, except that moving Left in the
leftmost square, moving Right in the rightmost square, and Sucking in a
clean square have no effect.
5) Goal test:
No dirt at all locations.
6) Path cost:
Each cost step is 1.
Fig. 3.2 The state space for the vacuum world. Links denote actions: L
= Left, R= Right, S= Suck
The second example we examine is 8 puzzle problem.
The 8-puzzle consists of eight numbered, movable tiles set in a 3x3 frame.
Each tile has a number on it. A tile that is adjacent to the blank space can
slide into the space. A game consists of a starting position and a specified
goal position. The goal is to transform the starting position into the goal
position by sliding the tiles around.
32
Artificial Intelligence This can be formulated as problem as follows:
1) States:
Location of eight tiles and blank.
2) Initial state:
Any state can be designed as the initial state.
3) Actions:
Move blank left, right, up or down.
4) Goal test:
This checks whether the states match the goal configuration.
5) Path cost:
Each step cost is 1.
1) States:
Any arrangement of 0 to 8 queens on the board is a state
2) Initial state:
No queens on board.
3) Actions:
Add a queen to any empty square.
33
4) Transition Model: Solving Problems by
Searching
Returns the board with a queen added to the specified square.
5) Goal Test:
8 queens are on the board, none attacked.
Rules:
1. Fill the 4-Gallon Jug (x,y) (4,y)
2. Fill the 3-Gallon Jug. (x,y) (x,3)
3. Pour some water out of 4 Gallon jug (x,y)(x-d,y)
4. Pour some water out of 3 Gallon jug (x,y)(x,y-d)
5. Empty 4 Gallon jug on the ground. (x,y)(0,y)
6. Empty 3 Gallon jug on the ground (x,y)(x,0)
34
Artificial Intelligence 7. Pour some water from 3 Gallon jug into the 4 Gallon jug until the 4
gallon jug is full. (x,y)(4, y - (4 - x))
8. Pour some water from 4 Gallon jug into the 3 Gallon jug until the 3
gallon jug is full. (x,y)(x - (3-y), 3)
9. Pour all water from 3 to 4 gallon. (x,y)(x+y, 0)
10. Pour all water from 4 to 3 Gallon jug. (x,y)(0, x+y)
11. Pour 2 gallon from 3 G to 4 G. (0,2)(2,0) if x<y
12. Empty the 2 G in 4 G on the ground. (2,y)(0,y) if x<y
Rules:
1. (0, M): One missionary sailing the boat from bank-1 to bank-2.
2. (M,0): One missionary sailing boat from bank-2 to bank-1.
3. (M, M): Two missionaries sailing the boat from bank 1 to bank 2.
4. (M, M): Two missionaries sailing the boat from bank 2 to bank 1.
5. (M, C): One missionary & one cannibal sailing the boat from bank 1
to bank-2.
6. (C, M): One missionary & one cannibal sailing the boat from bank 2
to bank-1.
7. (C, C): Two cannibals sailing the boat from bank-1 to bank-2.
8. (C, C): Two cannibals sailing the boat from bank-2 to bank-1.
9. (0, C): One cannibal sailing the boat from bank-1 to bank-2.
35
10. (C,0): One cannibal sailing the boat from bank-2 to bank-1. Solving Problems by
Searching
SSS
1) States:
Each state obviously includes a location (e.g., an airport) and the current
time.
2) Initial state:
This is specified by the user‘s query.
3) Actions:
Take any flight from the current location, in any seat class, leaving after
the current time, leaving enough time for within-airport transfer if needed.
36
Artificial Intelligence 4) Transition Model:
The state resulting from taking a flight will have the flight‘s destination as
the current location and the flight‘s arrival time as the current time
5) Goal test:
Are we at the final destination specified by the user?
6) Path cost:
This depends on monetary cost, waiting time, flight time, customs and
immigration procedures, seat quality, time of day, type of airplane,
frequent-flyer mileage awards, and so on.
Touring Problem:
This is closely related to route-finding problems, but with an important
difference. Consider, for example, the problem visits every city in Fig. 3.1
at least once. Starting and ending at Solapur. The actions correspond to
trips between adjacent cities. Each state must include not just the current
location but also the set of cities the agent has visited. So, the initial state
would be In (Solapur), visited (Solapur). and the goal test would check
whether the agent is in Bucharest and all 27 cities have been visited.
1) States:
Cities or locations
Each city must be visited exactly once. Visited cities must be kept as state
information.
2) Initial state:
Starting point, no cities visited.
3) Successor function:
Move from one city to another city.
4) Goal test:
All cities visited, Agent at the initial location.
5) Path cost:
Distance between cities.
37
Example 3: VLSI Layout Problem: Solving Problems by
Searching
problem requires positioning millions of components and connections on a
chip to minimize area, minimize circuit delays, minimize stray
capacitances, and maximize manufacturing yield.
1) States:
Position of components, wires on a chip.
2) Initial State:
Incremental: no components placed
Complete-state: All components place (randomly, manually)
3) Successor Function:
Incremental: placed components, route wire
Complete-state: move components, move wire
4) Goal test:
All components placed; components connected as specified
5) Path cost:
Complex.
1) States:
Locations, Position of actuators.
2) Initial state:
Start position
3) Successor Function:
Movement, action of actuators
4) Goal Test:
Task-dependent
38
Artificial Intelligence 5) Path cost:
complex
1) States:
Location of components
2) Initial state:
No components assembled
3) Successor function:
Place component
4) Goal test:
5) Path cost:
Number of moves
39
Solving Problems by
3.6 UNINFORMED SEARCH Searching
This topic covers several search strategies that come under the heading of
uninformed search or blind search. This search algorithm uses only initial
state, search operators and a test for a solution. It proceeds systematically
by exploring nodes either randomly or in some predetermined order.
40
Artificial Intelligence The BFS algorithm, presented above, explore the space in a level-by-level
fashion. It maintains two lists namely open and closed. Open list contains
states which have been generated but whose children have not been
expanded and closed list records states that have already been examined.
Open is maintained as a queue First in first out (FIFO).
Time complexity: Equivalent to the number of nodes traversed in BFS
until the shallowest solution.
Space complexity: Equivalent to how large can the fringe get.
Completeness: BFS is complete, meaning for a given search tree, BFS
will come up with a solution if it exists.
Optimality: BFS is optimal as long as the costs of all edges are equal.
3.6.2 Depth First Search (DFS):
The algorithm starts at the root node and explores as far as possible along
each branch before backtracking.
DFS traverses the tree deepest node first. It would always pick the deeper
branch until it reaches the solution. The strategy can be implemented by
tree search with LIFO (Last in First Out) queue, most popularly known as
a stack.
Function Breadth First Search (start)
Begins
Open start
Closed [ ];
While (open # [ ]) do
Begin
X remove first [open]
If x is goal then return success
Else begin
Left end (open) generated child(x)
Closed x;
End
End
Return fail
End
DFS always expands the deepest nodes in the current fringe of the search
tree. The search proceeds immediately to the deepest level of the search
tree.
41
Solving Problems by
Searching
42
Artificial Intelligence Uniform cost search is equivalent to BFS algorithm if the path cost of all
edges is the same.
43
This Search algorithm combines the benefits of Breadth-first search's fast Solving Problems by
Searching
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.
Function iterative deepening search (problem): returns solution or
failure
Begin
Inputs, problem, depth
For depth 0 to ∞ do
Result <- depth – limited – search (problem, depth)
If result # cut-off then return result.
End.
Completeness:
This algorithm is complete is if the 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).
44
Artificial Intelligence Optimal:
IDDFS algorithm is optimal if path cost is a non- decreasing function of
the depth of the node.
45
Solving Problems by
3.7 INFORMED SEARCH STRATEGIES Searching
In this topic we will see more efficient search strategy, the informed
search strategy or Heuristic Search. It is a search strategy that uses
problem-specific knowledge beyond the definition of the problem itself.
The term Heuristic is used for algorithms which find solutions among all
possible ones. Heuristic is a rule of thumb or judgement technique that
leads to a solution but provides no guarantee of success.
Advantages:
Best first search can switch between BFS and DFS by gaining the
advantages of both the algorithms.
This algorithm is more efficient than BFS and DFS algorithms.
Disadvantages:
It can behave as an unguided depth-first search in the worst-case scenario.
It can get stuck in a loop as DFS.
This algorithm is not optimal.
State H(n)
S 13
A 12
B 4
C 7
D 3
E 8
46
Artificial Intelligence F 2
H 4
I 9
G 0
3.7.2 A* search:
A* combines the value of the heuristic function h(n) and the cost to reach
the node n, g(n).
47
F(n)= g(n)+h(n) Solving Problems by
Searching
Since g(n) gives the path cost from the start node to node n, and h(n) is the
estimated cost of the cheapest path from n to the goal, we have f(n) =
estimated cost of the cheapest solution through n.
It has combined features of UCS and greedy best-first search, by which it
solves 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).
Algorithm of A* Algorithm:
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: Goto to Step 2.
Advantages:
A* search algorithm is the best algorithm than other search algorithms.
A* search algorithm is optimal and complete.
This algorithm can solve very complex problems.
Disadvantages:
It does not always produce the shortest path as it mostly based on
heuristics and approximation.
A* search algorithm has some complexity issues.
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.
48
Artificial Intelligence Example:
State H(n)
S 5
A 3
B 4
C 2
D 6
G 0
49
Optimal: A* search algorithm is optimal. Solving Problems by
Searching
1 2 3
8 4
7 6 5
1 2 3
8 6
7 5 4
50
Artificial Intelligence
3.9 SUMMARY
To solve Artificial Intelligence problem, we need to follow certain steps.
Firstly, define the problem precisely it means specify the problem space,
the operators for moving within the space and the starting and goal state.
Secondly, analyse the problem and finally, select one or more techniques
for representing knowledge and for problem solving and apply it to the
problem.
A problem consists of five parts: the initial state, a set of actions, a
transition model describing the results of those actions, a goal test
function, and a path cost function. The environment of the problem is
represented by a state space. A path through the state space from the initial
state to a goal state is a solution
Uninformed search methods have access only to the problem definition.
The basic algorithms are as follows:
Breadth-first search expands the shallowest nodes first; it is complete,
optimal for unit step costs, but has exponential space complexity.
Uniform-cost search expands the node with lowest path cost, g(n), and
is optimal for general step costs.
Depth-first search expands the deepest unexpanded node first. It is
neither complete nor optimal, but has linear space complexity. Depth-
limited search adds a depth bound.
Iterative deepening search calls depth-first search with increasing
depth limits until a goal is found. It is complete, optimal for unit step
costs, has time complexity comparable to breadth-first search, and has
linear space complexity.
Bidirectional search can enormously reduce time complexity, but it is
not always applicable and may require too much space.
Informed search methods may have access to a heuristic function h(n)
that estimates the cost of a solution from n.
The generic best-first search algorithm selects a node for expansion
according to an evaluation function.
Greedy best-first search expands nodes with minimal h(n). It is not
optimal but is often efficient.
3.10 EXERCISES
1. Compare uniformed search with informed search.
2. What are the problems with Hill climbing and how can they be solved
51
3. State the best first search algorithm. Solving Problems by
Searching
4. Explain advantages and disadvantages of DFS and BFS.
5. You have two jugs, measuring 8 gallons, 5 gallons and a water faucet.
You can fill the jugs up or empty them out from one to another or
onto the ground. You need to measure out exactly three gallons.
6. Three towers labelled A, B and C are given in which tower A has
finite number of disks (n) with decreasing size. The task of the game
is to more the disks from tower A to tower C using tower B as an
auxiliary. The rules of the game are as follows:
1. Only one disk can be moved among the towers at any given time.
2. Only the ―top‖ disk can be removed.
3. No large disk can site over a small disk.
Solve the problem and describe the following terms:
a) State b) Initial state c) Goal state d) Operators e) Solution
7 Write down the heuristic function for 8-puzzle problem and solve the
problem.
8 What is the use of online search agent in unknown environment?
9 Define the following terms in reference to Hill Climbing
a) Local Maximum
b) Plateau
c) Ridge
10 Using suitable example, illustrate steps of A * search. Why is A*
search better than best first search?
11 Describe A* search algorithm. Prove that A* is complete and optimal
3.11 BIBLIOGRAPHY
Deva Rahul, Artificial Intelligence a Rational Approach, Shroff, 2014.
Khemani Deepak, A First course in Artificial Intelligence, McGraw
Hill, 2013.
Chopra Rajiv, Artificial Intelligence a Practical Approach, S. Chand,
2012.
Russell Stuart, and Peter Norvig Artificial Intelligence a Modern
Approach, Pearson, 2010.
Rich Elaine, et al. Artificial intelligence, Tata McGraw Hill, 2009.
*****
52
4
BEYOND CLASSICAL SEARCH
Unit Structure
4.1 Objective
4.2 Introduction
4.3 Local search algorithms
4.4 Searching with non-deterministic action
4.5 Searching with partial observations
4.6 Online search agents and unknown environments
4.7 Summary
4.8 Unit End Questions
4.9 Bibliography
4.1 OBJECTIVES
After this chapter, you should be able to:
Understand the local search algorithms and optimization problems.
Understand searching techniques of non-deterministic action.
Familiar with partial observation.
Understand online search agents and unknown environments.
4.2 INTRODUCTION
This chapter covers algorithms that perform purely local search in the state
space, evaluating and modifying one or more current states rather than
systematically exploring paths from an initial state. These algorithms are
suitable for problems in which all that matters is the solution state, not the
path cost to reach it. The family of local search algorithms includes
methods inspired by statistical physics (simulated annealing) and
evolutionary biology (genetic algorithms).
The search algorithms we have seen so far, more often concentrate on path
through which the goal is reached. But the problem does not demand the
path of the solution and it expects only the final configuration of the
solution then we have different types of problem to solve.
Local search algorithm operates using single path instead of multiple
paths. They generally move to neighbours of the current state. Local
algorithms use very little and constant amount of memory. Such kind of
algorithms have ability to figure out reasonable solution for infinite state
spaces.
They are useful for solving pure optimization problems. For search where
path does not matter, Local search algorithms can be used, which operates
53
by using a single current state and generally move only to neighbours of Beyond Classical Search
the state.
Algorithm:
1) Evaluate the initial state. If it is goal state then return and quit.
2) Loop until a solution is found or there are no new operators left.
3) Select & apply new operator.
4) Evaluate new state
If it is goal state, then quit.
54
Artificial Intelligence If it is better than current state then makes it new current state.
If it is not better than current then go to step 2.
Local maxima:
Local maximum is a state which is better than its neighbour states, but
there is also another state which is higher than it. They are also called as
foot-hills. It is shown in Fig. 4.2.2
Local Maximum
55
Solution to this problem: Beyond Classical Search
Plateau:
It is a flat area of the search space in which a whole set of neighbouring
states (nodes) have the same heuristic value. A plateau is shown in Fig.
4.3.
Ridge:
It’s an area which is higher than surrounding states but it cannot be
reached in a single move. It is an area of the search space which is higher
than the surrounding areas and that itself has a slope. Ridge is shown in
Fig. 4.4.
Ridge
56
Artificial Intelligence Solution to this problem:
Trying different paths at the same time is a solution. With the use of
bidirectional search, or by moving in different directions, we can improve
this problem.
Algorithm:
1. Evaluate the initial state. If it is also goal state, then return it and quit.
Otherwise, continue with the initial state as the current state.
2. Initialize BEST-SO-FAR to the current state.
3. Initialize T according to the annealing schedule.
4. Loop until a solution is found or until there are no new operators left
to be applied in the current state.
a) Select an operator that has not yet been applied to the current
state and apply it to produce a new state.
b) Evaluate the new state. Compute
57
E = (value of current)- (value of new state) Beyond Classical Search
58
Artificial Intelligence intelligent exploitation of random search provided with historical data to
direct the search into the region of better performance in solution space.
They are commonly used to generate high-quality solutions for
optimization problems and search problems.
Genetic algorithms simulate the process of natural selection which means
those species who can adapt to changes in their environment are able to
survive and reproduce and go to next generation. In simple words, they
simulate ―survival of the fittest‖ among individual of consecutive
generation for solving a problem. Each generation consist of a population
of individuals and each individual represents a point in search space and
possible solution. Each individual is represented as a string of
character/integer/float/bits. This string is analogous to the Chromosome.
Fitness Function:
The fitness function determines how fit an individual is (the ability of an
individual to compete with other individuals). It gives a fitness score to
each individual. The probability that an individual will be selected for
reproduction is based on its fitness score.
Selection:
The idea of selection phase is to select the fittest individuals and let them
pass their genes to the next generation. Two pairs of individuals (parents)
are selected based on their fitness scores. Individuals with high fitness
have more chance to be selected for reproduction.
Crossover:
It is the most significant phase in a genetic algorithm. For each pair of
parents to be mated, a crossover point is chosen at random from within the
genes. Offspring are created by exchanging the genes of parents among
themselves until the crossover point is reached.
Mutation:
In certain new offspring formed, some of their genes can be subjected to a
mutation with a low random probability. This implies that some of the bits
in the bit string can be flipped.
59
Beyond Classical Search
Fig. 4.5 The genetic algorithm, illustrated for digit strings representing 8-
queens states. The initial population in (a) is ranked by the fitness function
in (b), resulting in pairs for mating in (c). They produce offspring in (d),
which are subject to mutation in (e)
Fig. 4.6 The eight possible states of the vacuum world; states 7 and 8
are goal states.
60
Artificial Intelligence In the erratic vacuum world, the Suck action works as follows:
• When applied to a dirty square the action cleans the square and
sometimes cleans up dirt in an adjacent square, too.
• When applied to a clean square the action sometimes deposits dirt on
the carpet.
• a contingency plan such as the following:
[Suck, if State = 5 then [Right, Suck] else [ ]]
If the environment is observable, deterministic, and completely known,
then the problem is trivially solvable by any of the algorithms.
For example, if the initial state is 1, then the action sequence [Suck, Right,
Suck] will reach a goal state, 8. Thus, solutions for nondeterministic
problems can contain nested if–then–else statements;
61
incarnation can be discarded. With this check, we ensure that the Beyond Classical Search
algorithm terminates in every finite state space, because every path must
reach a goal, a dead end, or a repeated state.
Example.
62
Artificial Intelligence
Transition model: The agent doesn’t know which state in the belief
state is the right one; so as far as it knows, it might get to any of the
states resulting from applying the action to one of the physical states in
the belief state.
Goal test: The agent wants a plan that is sure to work, which means
that a belief state satisfies the goal only if all the physical states in it
satisfy GOAL-TEST P. The agent may accidentally achieve the goal
earlier, but it won’t know that it has done so.
Path cost: This is also tricky. If the same action can have different
costs in different states, then the cost of taking an action in a given
belief state could be one of several values.
63
Beyond Classical Search
Fig. 4.10 (a) Predicting the next belief state for the sensorless vacuum
world with a deterministic action, right. (b) Prediction for the same
belief state and action in the slippery version of the sensorless vacuum
world.
4.5.2 Searching with observations:
For a general partially observable problem, we have to specify how the
environment generates percepts for the agent. For example, we might
define the local-sensing vacuum world to be one in which the agent has a
position sensor and a local dirt sensor but has no sensor capable of
detecting dirt in other squares. The formal problem specification includes
a PERCEPT(s) function that returns the percept received in a given state.
(If sensing is nondeterministic, then we use a PERCEPTS function that
returns a set of possible percepts.) For example, in the local-sensing
vacuum world, the PERCEPT in state 1 is [A, Dirty]. Fully observable
problems are a special case in which PERCEPT(s) = s for every state s,
while sensorless problems are a special case in which PERCEPT(s) = null.
• The ACTIONS, STEP-COST, and GOAL-TEST are constructed from
the underlying physical problem just as for sensorless problems, but
the transition model is a bit more complicated.
• The prediction stage is the same as for sensorless problems: given the
action a in belief state b, the predicted belief state is ˆb = PREDICT
(b, a).
• The observation prediction stage determines the set of percepts o that
could be observed in the predicted belief state: POSSIBLE-
PERCEPTS (ˆb) = {o : o = PERCEPT(s) and s ∈ ˆb} .
• The update stage determines, for each possible percept, the belief state
that would result from the percept. The new belief state bo is just the
set of states in ˆb that could have produced the percept: bo =
UPDATE (ˆb, o) = {s : o = PERCEPT(s) and s ∈ ˆb}.
• Putting these three stages together, we obtain the possible belief states
resulting from a given action and the subsequent possible percepts:
RESULTS (b, a) = {bo : bo = UPDATE(PREDICT(b, a), o) and o ∈
POSSIBLE-PERCEPTS(PREDICT(b, a))} .
64
Artificial Intelligence Fig.4.10 Two example of transitions in local-sensing vacuum worlds. (a)
In the deterministic world, Right is applied in the initial belief state,
resulting in a new belief state with two possible physical states; for those
states, the possible percepts are [B, Dirty] and [B, Clean], leading to two
belief states, each of which is a singleton. (b) In the slippery world, Right
is applied in the initial belief state, giving a new belief state with four
physical states; for those states, the possible percepts are [A, Dirty], [B,
Dirty], and [B, Clean], leading to three belief states as shown.
66
Artificial Intelligence 1. ACTIONS(s)
2. The step-cost function
3. GOAL-TEST(s).
For example, in the maze problem shown in Figure, the agent does not
know that going Up from (1,1) leads to (1,2); nor, having done that, does it
know that going Down will take it back to (1,1).
Necessary in unknown environments
Robot localization in an unknown environment (no map)
Does not know about obstacles, where the goal is, that UP from (1,1)
goes to (1, 2)
Once in (1, 2) does not know that down will go to (1, 1)
Some knowledge might be available
If location of goal is known, might use Manhattan distance heuristic
Competitive Ratio = Cost of shortest path without exploration/Cost of
actual agent path
Irreversible actions can lead to dead ends and CR can become infinite.
Hill- Climbing is already an online search algorithm but stops at local
optimal.
67
Beyond Classical Search
Fig. 4.14 An online search agent that uses depth-first exploration. The
agent is applicable only in state spaces in which every action can be
―undone‖ by some other action
It is fairly easy to see that the agent will, in the worst case, end up
traversing every link in the state space exactly twice. For exploration, this
is optimal; for finding a goal, on the other hand, the agent’s competitive
ratio could be arbitrarily bad if it goes off on a long excursion when there
is a goal right next to the initial state.
Because of its method of backtracking, ONLINE-DFS-AGENT works
only in state spaces where the actions are reversible. There are slightly
more complex algorithms that work in general state spaces, but no such
algorithm has a bounded competitive ratio.
68
Artificial Intelligence 4.7 SUMMARY
This chapter has examined search algorithms for problems beyond the
―classical‖ case of finding the shortest path to a goal in an observable,
deterministic, discrete environment. Local search methods such as hill
climbing operate on complete-state formulations, keeping only a small
number of nodes in memory. Several stochastic algorithms have been
developed, including simulated annealing, which returns optimal solutions
when given an appropriate cooling schedule.
A genetic algorithm is a stochastic hill-climbing search in which a large
population of states is maintained. New states are generated by mutation
and by crossover, which combines pairs of states from the population.
In nondeterministic environments, agents can apply AND–OR search to
generate contingent plans that reach the goal regardless of which outcomes
occur during execution.
When the environment is partially observable, the belief state represents
the set of possible states that the agent might be in. Standard search
algorithms can be applied directly to belief-state space to solve sensor less
problems, and belief-state AND–OR search can solve general partially
observable problems.
4.8 UNIT END QUESTIONS
1 What do you mean by local maxima with respect to search technique?
2 Explain Hill Climbing Algorithm.
3 Explain AO* Algorithm.
4 Explain a searching with nondeterministic action.
5 Explain searching with partial observations.
6 Write a note on simulated annealing.
7 Explain a local search algorithm.
8 Write a note on online search agents.
9 Write a note on unknown environments.
10 Consider the sensorless version of the erratic vacuum world. Draw the
belief-state space reachable from the initial belief state {1, 2, 3, 4, 5,
6, 7, 8}, and explain why the problem is unsolvable.
4.9 BIBLIOGRAPHY
Deva Rahul, Artificial Intelligence a Rational Approach, Shroff, 2014.
Khemani Deepak, A First course in Artificial Intelligence, McGraw
Hill, 2013.
Chopra Rajiv, Artificial Intelligence a Practical Approach, S. Chand,
2012.
Russell Stuart, and Peter Norvig Artificial Intelligence a Modern
Approach, Pearson, 2010.
Rich Elaine, et al. Artificial intelligence, Tata McGraw Hill, 2009.
*****
69
UNIT III
5
ADVERSARIAL SEARCH
Unit Structure
5.0 Objective
5.1 Introduction
5.2 Games
5.3 Optimal Decision in Games
5.3.1 Minimax Algorithm
5.3.2 Optimal Decision in Multilayer Games
5.4 Alpha-Beta Pruning
5.4.1 Move Ordering
5.5 Imperfect Real-Time Decision
5.5.1 Evaluation Function
5.5.2 Cutting off Search
5.5.3 Forward Pruning
5.5.4 Search versus Lookup
5.6 Stochastic Games
5.6.1 Evaluation functions for games of chance
5.7 Partially Observable Games
5.7.1 Kriegspiel: Partially observable Chess
5.7.2 Card Games
5.8 State-of-Art Game Programming
5.9 Summary
5.10 Exercise
5.11 Bibliography
5.0 OBJECTIVE
The objective of this chapter is to study the different strategies used by
players to compete each other and try to win the game.
5.1 INTRODUCTION
In this chapter we will see Adversarial search method in which we
examine the situation where agent compete with one another and try to
defeat one another in order to win the game. Adversarial search is a game
playing technique in which two or more players with conflicting goals are
trying to explore the same search space for the solution.
70
Artificial Intelligence
5.2 GAMES
In single agent environment the solution is expressed in the form of
sequence of actions. But there might be a situation occur in a game
playing where more than one agent searching for a solution in the same
search space. The environment with more than one agent is called as
multi-agent environment, in which each agent has to consider the action of
other agent and how they affect its own welfare. Such conflicting goal
give rise to the adversarial search. So, searches in which two or more
players with conflicting goals are trying to explore the same search space
for the solution are known as Games.
72
Artificial Intelligence game, then the minimax value of a node is the utility of being in the
corresponding state. The minimax value of a terminal state is just its
utility. MAX prefers to move to a state of maximum value and MIN
prefers a state of minimum value then
73
Example: Adversarial Search
1 The algorithm generates the entire game tree and apply the utility
function to get the utility values for the terminal state. Now consider
in the following diagram A is the initial state.
1) Now, first find the utilities value for MAX, so we will compare
each value in terminal state with initial value for MAX and
determine the higher node values.
So, for node D max (-1, 8) => 8
for node E max (-3, -1) => -1
for node F max (2, 1) => 2
for node G max (-3, 4) => 4
3) Now its turn for MAX to choose the maximum of all node values
and find the maximum value for the root node
for node A max ( -1, 2) => 2
75
Now, for nonterminal states, consider the node X in the game tree as Adversarial Search
shown in below figure. In that state, player C decides what to do. The two
choices lead to terminal states with utility vectors (VA =1, VB =2, VC =6)
and (VA =4, VB =2, VC =3). C choose the first move because 6 is bigger
than 3, means if state X is reached, subsequently play will lead to a
terminal state with utilities (VA =1, VB =2, VC =6). So, in this vector it
backed-up the values of X.
Fig. The first three piles of game tree with three players (A, B, C).
Example:
1) In the first step MAX player start with first move from node A
where α= -∞ and β= +∞. Now these values of α and β are passed to
node B and then node D.
77
Adversarial Search
2) At node D the MAX player will play and calculate the α value. The
value of α compare with 2 and 3. So max (2, 3) =3. So, 3 will be the
value of α at node D.
3) Now backtracking is performed at node B. The value of β will
change as MIN player will play. Now β= +∞, will compare with the
available subsequent nodes value. So, min (∞, 3) = 3, hence at node
B now α= -∞, and β= 3.
78
Artificial Intelligence
79
Adversarial Search
80
Artificial Intelligence maxaЄAction(s)H-MINIMAX(RESULT(s,a),d+1 if
Player(s)=MAX minaЄ Action(s)
H-MINIMAX (RESULT(s,a),d+1 if
Player(s)=MIN
81
alpha beta search algorithm and replace terminal state with cutoff-test and Adversarial Search
utility is replace by EVAL.
if Cutoff-Test(state, depth) then return Eval(state)
by setting a fixed depth limit is the easiest way to control the amount of
searching so that cutoff return true for all depth greater than fixed depth d.
In order to select a move within the allocated time, the depth d is chosen
for it. Iterative deepening search can be applied and also helps with move
ordering. The program returns the move selected by the deepest completed
search as time runs out.
82
Artificial Intelligence Backgammon game combine luck and skill of players. Dice are rolled at
the starting of player’s turn to find the legal moves. Each player moves the
pieces according to the dice are thrown. For example, if a player who
plays with black piece roll the dice and dice shown 6-5 and has four
possible moves.
The goal of the game is to move all one’s pieces off the board. White
moves clockwise and black move anticlockwise. A piece can move to any
position until multiple opponents are pieces are there. White knows his or
her own legal moves but don’t know about the black legal moves. So
white cannot build a standard game tree. A game tee in a backgammon
must include chance node in addition with MIN and MAX node. The
possible dice node denoted by the branches leading from each chance nod.
Brach is labeled with the roll and its probability. There are 36 ways to
thrown the dice, but because a 6-5 is the same as 5-6, there are only 21
distinct rolls.
84
Artificial Intelligence function need not return actual expected values as long as the ordering of
the states is the same.
This kind of analysis required too many categories and hence too much
experience to estimate all the probabilities of winning. Instead, most
evaluation functions compute separate numerical contributions for each
feature and then combine them to calculate the total value. For example,
introductory chess books give an approximate material value for each
piece: each pawn is worth 1, a knight or bishop is worth 3, a rook 5, and
the queen 9. Other features such as “good pawn structure” and “king
safety” might be worth half a pawn, say. These feature values are then
simply added up to obtain the evaluation of the position.
A secure advantage equivalent to a pawn gives a substantial likelihood of
winning, and a secure advantage equivalent to three pawns should give
almost certain victory Mathematically, this kind of evaluation function is
called a weighted linear function and denoted as
85
Kriegspiel state estimation directly map onto the partially observable, non- Adversarial Search
deterministic framework. For partially observable game the notion of
strategy is altered. a move is making for every possible percept sequence
that might receive. For Kriegspiel a winning strategy is one that, for each
possible percept sequence leads to an actual checkmate foe every possible
board state in the current belief state, regardless of how opponent moves.
Chess:
it is well known for defeating world champion Garry Kasparov. Deep Blue
examined 200 million positions per second, used very sophisticated
evaluation and undisclosed methods for extending some lines of search up
to 40 ply. The key to its success seems to have been its ability to generate
singular extensions beyond the depth limit for sufficiently interesting lines
of forcing/forced moves.
Checkers:
Jonathan Schaeffer and colleagues developed CHINOOK which runs on
regular computers and it uses alpha beta search. It is used an endgame
database defining perfect play for all positions.
86
Artificial Intelligence Othello:
it is also called as Reversi. It is more popular as a computer game than a
board game. It has a smaller search space than chess and 5 to 15 legal
moves.
Backgammon:
The most of the work has gone into improving evaluation function. Gerry
Tesauro combined reinforcement learning with neural network to develop
accurate evaluator.
Go:
It is the most popular board game in Asia. It is 19 by 19 board game in
which moves are allowed into every empty square. The branching factor
starts at 361 which is too daunting for alpha-beta search method. The
evaluation function for this game is difficult to write because control
territory is often very unpredictable until the endgame.
Bridge:
It is multiplayer game in which cards are hidden from the other player.
Players are paired into two teams for four player games.
5.9 SUMMARY
A game has five components such as initial state, legal actions, result
of action, terminal test and utility function.
Instead of considering whole game tree, cut the search off at some
point and apply evaluation function to calculate the utility of state.
5.10 EXERCISE
87
5) Explain alpha-beta pruning Adversarial Search
5.11 BIBLIOGRAPHY
*****
88
UNIT III
6
LOGICAL AGENT
Unit Structure
6.0 Objective
6.1 Introduction
6.2 Knowledge-Based Agent
6.3 The Wumps World
6.4 Logic
6.5 Propositional Logic: A Very Simple Logic
6.5.1 Syntax
6.5.2 Semantics
6.5.3 A Simple Knowledge Base
6.5.4 A Simple Inference Procedure
6.6 Propositional Theorem Proving
6.6.1 Inference and proofs
6.6.2 Proof by Resolution
6.6.3 Horn Clauses and Definite Clauses
6.6.4 Forward and Backward Chaining
6.7 Effective Propositional Model Checking
6.7.1 A Complete Backtracking Algorithm
6.7.2 A Local Search Algorithm
6.7.3 The Landscape of random SAT Problems
6.8 Agent Based on Propositional Logic
6.9 Summary
6.10 Bibliography
6.11 Unit End Exercise
6.0 OBJECTIVE
In this chapter we design agent-based system that can represent a complex
world. The new representation about the world can be derived by using
process of inference and these new representations to deduce what to do.
Also develop a logic as a general class of representation to support
knowledge-based agent.
6.1 INTRODUCTION
Knowledge can be a particular path towards the solution. The knowledge
89 must be represented in a particular format before using it. In 8 puzzle
.
transition model, the knowledge of what the actions do is hidden inside the Logical Agent
domain specific code of the RESULT function. I observable environment
as agent can represent what it knows about the current state is to list all
possible concrete states. In this chapter logic is develop as a general class
of representations to support knowledge-based agent. The knowledge-
based agent can accept new tasks in the form of explicitly described gals.
The agent can achieve ability quickly by learning new knowledge about
the environment and also update the relevant knowledge so they can adapt
to changes in the environment.
90
Artificial Intelligence
91
There are some points which helps the agent to navigate through the cave Logical Agent
safely such as the rooms adjacent to the Wumpus are smelly, so it would
have some stench. The room adjacent to pits has a breeze, so when agent
reach near pits, he feels the breeze. If the room is glitter, then it Contin
gold. The Wumpus can be killed by the agent if is facing it.
Performance measure- +1000 if the agent come out from the cave
with gold. -1000 if agent falls into the pits or eaten by the Wumpus. -1
for each action taken and -10 for using an arrow.
Environment:
4 X 4 grid of rooms connected to each other.
The agent always starts from the position [1,1] facing to right.
The location of gold and the Wumpus are selected randomly from the
squares.
Each square of the cave other than the start can be pit with probability
0.2.
Actuators:
the agent can move forward
turn left and turn right by 900.
The grab action can be used to pick up the gold.
The action shoot can be used to fire an arrow.
The arrow continues until it either hits the Wumpus or wall. The agent
has only one arrow.
The action climb can be used to climb out of the cave.
92
Artificial Intelligence Sensors: The agent has five sensors as follows.
The agent will perceive the stench if he is in the room which is adjacent
to Wumpus.
It perceives breeze if the room directly adjacent to pit.
The glitter is perceived by agent when room contains gold.
It perceives bump if agent walk into the wall.
When the Wumpus is shot, it releases a horrible scream which can be
perceived anywhere in the cave.
The percept will be given to agent program in the form of five symbols if
there is a stench and breeze but no glitter, bump or scream the agent
program will get [Stench, Breeze, None, None, None]
Consider the following example, the agent’s initially starts from the
location [1,1] and [1,1] is a safe square. Suppose it can be denoted by “A”
or “OK” respectively in square [1,1]. So, the first percept is [None, None,
None, None, None] from which agent can conclude about neighboring
squares such as [1,2] and [2,1] are out of dangers. They are OK.
93
Logical Agent
6.4 LOGIC
The proof or validation of a reason can be defined as Logic. Knowledge
base consist of sentence and these sentences are expressed according to
the syntax. Syntax is a formal description of the structure of program in a
given language. The notion of syntax must be in well forms for example
“x+y=10” is a well-formed but “xyz+=” is not in well formed.
A logic can also define the semantic or meaning of sentence. Semantic is a
formal description of the programs in a given language. The truth of each
sentence with respect to each possible world defined by semantics. For
example, consider the sentence “x+y=10” is true in a world where possible
value of x is 5 and y is 5, but false where x is 1 and y is1. In standard logic
each sentence must be either true or false only. The term Model is used
instead of possible world when need to be more precise.
If a sentence α is true in a model then m is a model of α and M(α) notation
is used to mean the set of all models of α. Logical reasoning is the process
through which any assertion is verified. It is relation of logical entailment
between sentences. It can be denoted by α |=β and mean that the sentence
α entails the sentence β. The entailment is defined as α |=β if and only if,
in every model in which α is true then β is also true and can be written as
94
Artificial Intelligence Propositional logic consists of object, relation or functions and logical
connectivity. In tautology propositional formula is always true whereas in
contradiction propositional formula is always false.
6.5.1 Syntax:
Syntax of propositional logic defines allowable sentences in the model.
The atomic sentences can be made up of single propositional symbol.
Each symbol stands for proposition that can be either true or false. The
uppercase symbols are used for example, A,P,R etc. Complex sentences
are constructed from simple sentences by using parentheses and logical
connectives. There are five connectives are as follows
1) Negation or not- A sentence such as ¬ P is called as negation of P. A
literal is either a positive or negative literal.
For example, Sonu did not read the Novels can be represented as ¬READ
(Sonu, Novels)
2) and – it is called as conjunction used to represent compound statement.
Denoted by ∧.
For example, Sonu is intelligent and hardworking can be represented as
follows
Suppose P- Sou is intelligent
95
Logical Agent
6.5.2 Semantics:
The semantic defines the rules for determining the truth of a sentence with
respect to particular model. In a propositional logic, model fixes the truth
value as true or false for every proposition symbol. Suppose if the
sentence in the knowledge base makes use of the proposition symbols
such as P1,2, P2,2 and P3,1 then model is as follows
M1 = {P1,2=false, P2,2=false, P3,1=false}
With these three symbols there are 23=8 possible models. The semantic
must specify how to compute the truth value of any sentence and it done
recursively. By using atomic sentences and five connectives all sentences
are constructed. So, it’s necessary to specify how to compute the truth of
atomic sentence and sentence which are formed with each of the five
connectives. The atomic sentences are easy
True is true and False is false in every model.
The truth value of every other proposition symbol must be specified
directly in the model.
There are five rules for complex sentence, that hold any substances P and
Q in any model m:
¬ P is true iff P is false in m.
P∧ Q is true iff both P and Q are true in m.
P ∨ Q is true iff either P or Q is true in m.
P=>Q is true unless P is true and Q is false in m.
P ⇔ Q is true iff P and Q are both true or both false in m.
96
Artificial Intelligence 6.5.3 A Simple Knowledge Base:
Now consider immutable aspects of the Wumpus world with the following
symbol for each location [x,y] such as
Px,y is true if there is a pit in [x,y]
Wx,y is true if there is Wumpus in [x,y] dead or alive
Bx,y is true if the agent perceives a breeze in [x,y]
Sx,y is true if the agent perceive a stench in {x,y\
Now consider the following situations and label each sentence Ri so it can
be referred further.
1) there is no pit in [1,1]
R1: ¬ P1,1
2) A square is breeze if and only if there is pit in a neighboring square
R2: B1,1 ⇔ (P1,2 V P2,1)
R3: B2,1 ⇔ (P1,1 V P2,2 V P3,1)
3) Now the breeze percept has been included for the first two squares
R4: ¬ B1,1
R5: ¬ B2,1
97
Logical Agent
It indicates that any sentence of the form α ⇒ β and α are given, then the
sentence β can be inferred
98
Artificial Intelligence 2) And-elimination: It says that from conjunction any conjuncts can be
inferred
for example, from the sentence (Wumpus Ahead ∧ Wumpus Alive) the
inference, “Wumpus Alive” can be drawn.
3) Monotonicity: it says that the set of entailed sentences can only
increases as information is added to the knowledge base. For any
sentence α and β
99
3) CNF requires ¬ to appears only in literals, so it is necessary to move ¬ Logical Agent
inwards by repeated application of the following equivalence.
¬(¬α) ≡ α (double-negation elimination)
A resolution Algorithm:
It takes the input as a knowledge base and α and its output is either true or
false. First (KB ∧ ¬ α) is converted into CNF. Then the resolution rule is
applied to the resulting clauses. Each pair in which complementary literals
are there is resolved to produce anew clause and it is added to the new set
if it is not there. The procedure continues until one of the two things
happens first, there are no new clauses that can be added in which KB
does not entails α or second two clauses resolve to yield the empty clause
in which case KB entails α. Th resolution algorithm is as shown belove.
100
Artificial Intelligence For example, (¬L1,1 ∨ ¬Breeze ∨ B1,1) is a definite clause whereas (¬B1,1 ∨
P1,2 ∨ P2,1) is not definite clause.
Horn clause is a disjunction of literals with at most one positive literal. So,
all definite clauses are horn clauses. Goal cluses are those in which there is
no positive literals.
101
Rule 2 and 3 are already added. Logical Agent
102
Artificial Intelligence 5) Enemy of country B is hostile. Enemy (p, B)-> Hostile( p)
6) Country A is an enemy of Country B. Enemy (A , B)
7) John is from country B. Country B(John)
Steps- in backward chaining start with goal state which is Criminal ( Jhon)
1) We will take the goal fact and from this infer other facts. At the end
prove that those facts true. So, now goal fact is Jhon is Criminal as
shown below
2) We will infer other facts from all fact which satisfies the rules. In rule 1
goal predicate is present with substitution {Jhon/p}. So, add all the
conjunctive facts below the first level and replace p with Jhon.
4) now infer facts Missile(t1) and Owns (A, T1) from Sells (Jhon, T1, r)
which satisfies the rule 4 with substitute of A in place of r. so two
statements are proved here.
103
Logical Agent
5) Infer the fact Enemy (A, B) from Hostile( A) which satisfies rule 6 an
hence all the statements are proved true using backward chaining.
104
Artificial Intelligence
105
6.7.3 The Landscape of random SAT Problems Logical Agent
There are some SAT problems that are harder than others. Simple
problems can be solved with any old algorithm. Because SAT is NP-
complete, at least some problem instances will require exponential running
time. The n-queen problem quite tricky for backtracking search algorithm
but trivially easy for local search methods such as min conflict. This is
because solutions are very densely distributed in the space of assignments,
and any initial assignment is guaranteed to have a nearby solution.
Therefore, n-queens is easy because it is underconstrained.
In the case of satisfiability problems in conjunctive normal form, an
underconstrained problem is one with relatively few clauses constraining
the variables. Consider the following example with a randomly generated
3-CNF sentence with five symbols and five clauses
(¬D ∨ ¬B ∨ C) ∧ (B ∨ ¬A ∨ ¬C) ∧ (¬C ∨ ¬B ∨ E) ∧ (E ∨ ¬D ∨ B) ∧ (B ∨
E ∨ ¬C) .
For this sentence sixteen of the 32 possible assignments are model, so, it
would take just two random guesses to find a model. In general,
underconstrained problems like this are easily satisfiable. In contrast, an
overconstrained problem will have many clauses in relation to the number
of variables and is likely to have no solution.
Beyond these basic intuitions, we have to define how random sentences
are generated. The notation CNFk(m,n) denotes a k-CNF sentence with m
cluses and n symbols, where the clauses are chosen uniformly,
independently and without replacement from all clauses with k different
literals, which are positive or negative at random.
The probability of satisfiability can be measured by examining the sources
of random sentences. As shown in following figure (a), plots the
probability for CNF3(m,50), that is, sentences with 50 variables and 3
literals per clause, as a function of clause/ symbol ratio, m/n. For small
m/n the probability of satisfiability is close to 1, whereas for large m/n, the
probability is close to 0. The probability drops fairly sharply around
m/n=4.3. so, as n increases “cliff” gets sharper and sharper and stay in
roughly the same place for k=3. Theoretically, the satisfiability threshold
conjecture says that for every k ≥ 3, there is a threshold ratio rk such that,
as n goes to infinity, the probability that CNFk(n, rn) is satisfiable becomes
1 for all values of r below the threshold, and 0 for all values above the
threshold. The conjecture remains unproven.
106
Artificial Intelligence
(a) Graph showing the probability that a random 3-CNF sentence with n = 50
symbols is satisfiable, as a function of the clause/symbol ratio m/n. (b) Graph of
the median run time on random 3-CNF sentences
6.9 SUMMARY
Intelligent agent require knowledge about the world in order to take
good decision. Knowledge is contained in agent in the form of
sentences.
Sentences in a knowledge representation language are stored in a
knowledge base.
Basic concept of logic is represented by syntax, semantics, entailment,
inference, soundness and completeness.
Syntax is a formal structure of sentences. Semantics is truth of
sentences based on models.
Entailments is necessary truth of one sentence given another.
Inference is deriving sentences from other sentences
In sound inference algorithm, derivations produce only entailed
sentences. In complete algorithm, derivations can produce all entailed
sentences.
Propositional logic isa simple language consisting of proposition
symbols and logical connectives.
Resolution is complete for propositional logic, forward, backward
chaining are linear time, complete for Horn clauses.
107
6.11 UNIT END QUESTIONS Logical Agent
6.10 BIBLIOGRAPHY
1) Artificial Intelligence: A Modern Approach by Stuart Russell and Peter
Norvig
2) Artificial Intelligence: javapoint.com
*****
108
UNIT IV
7
FIRST-ORDER LOGIC
Unit Structure
7.0 Objectives
7.1 Introduction
7.2 First-Order Logic
7.3 Syntax and semantics of First-Order Logic
7.3.1 Models for First-Order Logic
7.3.2 Symbols and Interpretations
7.3.3 Terms
7.3.4 Atomic Sentences
7.3.5 Complex Sentences
7.3.6 Quantifiers
7.3.7 Equality
7.3.8 An alternative semantics
7.4 Using First-Order Logic
7.4.1 Assertions and Queries in First-Order Logic
7.4.2 The kinship domain
7.4.3 Numbers, Sets
7.4.4 The Wumpus World
7.5 Knowledge Engineering in First-Order Logic
7.5.1 Knowledge Engineering process
7.5.2 Electronic Circuit Domain
7.6 Summary
7.7 Unit End Exercises
7.8 Bibliography
7.9 List of References
7.0 OBJECTIVES
After going through this chapter, you will be able to:
Represent knowledge in knowledge base using First-Order logic.
Understand syntax and semantics of First-Order logic.
Make use of Quantifiers for representation of knowledge.
Learn use of First-Order logic in kinship domain, number and sets,
and in Wumpus world.
Understand Knowledge-Engineering process.
Apply First-Order logic in electronic circuit domain.
109
7.1 INTRODUCTION First-Order Logic
110
Artificial Intelligence o Function: Father of, best friend, third inning of, end of, etc.
As a natural language, first-order logic also has two main parts:
o Syntax
o Semantics
1. Constant symbols:
Constants symbols refer to objects in a universe of discourse. Objects can
be anything like integers, people, real numbers and geometric shapes etc.
Example: Richard, John
2. Predicate symbols:
A predicate symbol represents a property of or relation between terms that
can be true or false. Each predicate symbol has an associated arity
indicating its number of arguments. Brother, OnHead, Person, King, and
Crown, etc. are predicate symbols.
Example:
King(John) is a unary predicate which is having one arguments.
Brother(John, Richard) is a Binary predicate which is having two
arguments.
3. Function symbols:
Function constants refer to functions like mother, age, LeftLeg, plus and
times. Each function symbol has an associated arity indicating its number
of arguments. Example:
mother(Jane) - here, mother has arity one (unary) while in times(3,2) -
times has arity two(binary).
| Sentence ∧ Sentence
| Sentence ∨ Sentence
| Sentence ⇒ Sentence
| Sentence ⇔ Sentence
| Quantifier Variable, . . . Sentence
Term → Function(Term, . . .)
| Constant
| Variable
Quantifier → ∀| ∃
Constant → A | X1 | John | · · ·
Variable → a | x | s | ···
Predicate → True | False | After | Loves | Raining | ···
Function → Mother | LeftLeg | · · ·
113
LeftLeg refers to the “left leg” function. First-Order Logic
7.3.3 Terms:
Objects are represented by terms. Terms are simply names for objects. A
term is a logical expression that refers to an object. Constant symbols are
therefore terms, but it is not always convenient to have a distinct symbol
to name every object.
For example, in English we might use the expression “King John’s left
leg” rather than giving a name to his leg. At that time instead of using a
constant symbol, we use LeftLeg(John). A complex term is formed by a
function symbol followed by a parenthesized list of terms as arguments to
the function symbol.
Complex term is not a “subroutine call” that “returns a value.” There is no
LeftLeg subroutine that takes a person as input and returns a leg. We can
reason about left legs (e.g., stating the general rule that everyone has one
and then deducing that John must have one) without ever providing a
definition of LeftLeg.
For example, suppose the LeftLeg function symbol refers to the function
and John refers to King John, then LeftLeg(John) refers to King John’s
left leg.
Example:
1. Ravi and Ajay are brothers: Brothers(Ravi, Ajay).
2. Chinky is a cat: cat (Chinky).
3. John owns a car: Owns(John, Car)
Atomic sentences can also have complex terms as arguments. Such as,
Married(Father (Ravi),Mother (Ajay))
states that Ravi’s father is married to Ajay’s mother.
( Sentence ) | [ Sentence ]
114
Artificial Intelligence ¬Sentence
Sentence ∧ Sentence
Sentence ∨ Sentence
Sentence ⇒ Sentence
Sentence ⇔ Sentence
Quantifier Variable, . . . Sentence
Here we have four sentences that are true in the model that we have
considered previously.
¬Brother (LeftLeg(Richard), John)
¬King(Richard) ⇒ King(John)
7.3.6 QUANTIFIERS
A quantifier is a language element which generates quantification, and
quantification specifies the quantity of specimen in the universe of
discourse. These are the symbols that permit to determine or identify the
range and scope of the variable in the logical expression. There are two
types of Quantifiers.
1. Universal Quantifier, (for all, everyone, everything)
2. Existential quantifier, (for some, at least one).
Example 1:
The rule, “All kings are persons,” is written in first-order logic as
115
∀ x King(x) ⇒ Person(x) First-Order Logic
Thus, the sentence will be read as, “For all x, if x is a king, then x is a
person.” The symbol x is called variable. By convention, variables are
lowercase letters.
Variable:
A variable is a term by itself.
It can also serve as the argument of a function for example, LeftLeg(x).
A term with no variables is called a ground term.
Consider the model that we have used previously and the intended
interpretation that goes with it. We can extend the interpretation in five
ways by asserting all possible values of x:
x → Richard the Lionheart,
x → King John,
x → Richard’s left leg,
x → John’s left leg,
x → the crown.
Example 2:
All man drink coffee.
Let a variable x which refers to a man so all x can be represented as
below:
x1 drinks coffee.
x2 drinks coffee.
x3 drinks coffee.
.
116
Artificial Intelligence .xn drinks coffee.
We can write it as:
∀x man(x) → drink (x, coffee).
It will be read as: For all x, if x is a man, then x drinks coffee.
2. Existential Quantification:
Existential quantifiers are the type of quantifiers, which express that the
statement within its scope is true for at least one instance of something. It
is denoted by the logical operator ∃, which resembles as inverted E. When
it is used with a predicate variable then it is called as an existential
quantifier. The sentence ∃x P says that P is true for at least one object x.
Note: In Existential quantifier we always use AND or Conjunction symbol
(∧).
Example 1:
King John has a crown on his head, we write
Example 2:
Some boys are intelligent.
Properties of Quantifiers:
In universal quantifier, ∀x∀y is similar to ∀y∀x.
In Existential quantifier, ∃x∃y is similar to ∃y∃x.
∃x∀y is not similar to ∀y∃x.
117
Nested quantifiers: First-Order Logic
∀ x, y Sibling(x, y) ⇔ Sibling(y, x)
In other cases, we will have mixtures. “Everybody read few books” means
that for every person, there is some book that person read:
∀ x ∃ y Read(x, y)
On the other hand, to say “There is someone who is liked by everyone,”
we write
∃ y ∀ x Likes(x, y)
The order of quantification is therefore very important. It becomes clearer
if we insert parentheses.
∀ x Likes(x, IceCream)
is equivalent to
¬∃ x ¬Likes(x, IceCream)
118
Artificial Intelligence Examples of First-Order Logic:
Some Examples of First-Order Logic using quantifiers:
1. All birds fly.
In this statement the predicate is fly(bird).
And since there are all birds who fly so it will be represented as follows.
∀x bird(x) →fly(x)
2. Every man respects his parent.
In this statement, the predicate is respect(x, y), where x=man, and y=
parent.
Since there are not all students, so we will use ∀ with negation, so
following representation for this:
7.3.7 Equality:
Equality symbol can be used to signify that two terms refer to the same
object. For example, Father (John)=Henry says that the object referred to
by Father (John) and the object referred to by Henry are the same.
119
The equality symbol can be used to state facts about a given function, as First-Order Logic
we just did for the Father symbol. It can also be used with negation to
insist that two terms are not the same object. To say that Richard has at
least two brothers, we would write
120
Artificial Intelligence TELL(KB, King(John)) .
TELL(KB, Person(Richard)) .
2. Queries:
We can ask questions from the knowledge base using ASK. For example,
ASK(KB, King(John))
Above query returns true. Questions asked with ASK are called queries or
goals.
ASK(KB, Person(John))
This query also returns true using above assertions. We can ask quantified
queries, such as
ASK(KB, ∃ x Person(x)) .
The answer is true, but we do not know the value of x makes the sentence
true. If we want to know what value of x makes the sentence true, we will
use a different function which is called ASKVARS.
ASKVARS(KB, Person(x))
Above function yields a stream of answers. In this case there will be two
answers: {x/John} and {x/Richard}. Such an answer is called a
substitution or binding list. Which means in above query x can be
substituted with John or Richard.
121
Example: First-Order Logic
∀ x Male(x) ⇔ ¬Female(x)
4. Parent and child are inverse relations:
∀ x, y Sibling(x, y) ⇔ Sibling(y, x)
∀ n NatNum(n) ⇒ NatNum(S(n))
122
Artificial Intelligence Means for every object n, if n is a natural number, then S(n) is a natural
number. So the natural numbers are 0, S(0), S(S(0)), and so on.
We also need axioms to constrain the successor function:
∀ n 0 ≠ S(n)
∀ m, n m ≠ n ⇒ S(m) ≠ S(n)
Now we can define addition in terms of the successor function:
∀m NatNum(m) ⇒ + (0,m) = m
Above axiom says that adding 0 to any natural number m gives m itself.
∀ m, n NatNum(m) ∧ NatNum(n) ⇒ (m + 1) + n = (m + n) + 1
This axiom reduces addition to repeated application of the successor
function.
Once we have addition, it is straightforward to define multiplication as
repeated addition, exponentiation as repeated multiplication, integer
division and remainders, prime numbers, and so on. Thus, the whole of
number theory (including cryptography) can be built up from one
constant, one function, one predicate and four axioms.
ii. Set:
The domain of sets is also fundamental to mathematics as well as to
commonsense reasoning. We want to be able to represent individual sets,
including the empty set. We need a way to build up sets by adding an
element to a set or taking the union or intersection of two sets. We will
want to know whether an element is a member of a set and we will want to
distinguish sets from objects that are not sets.
1. The empty set is a constant written as {}.
2. There is one unary predicate, Set, which is true of sets.
3. The binary predicates are x∈ s (x is a member of set s) and s1 ⊆ s2 (set
s1 is a subset of set s2).
4. The binary functions are s1 ∩ s2 (the intersection of two sets), s1 ∪ s2
(the union of two sets), and {x|s} (the set resulting from adjoining
element x to set s).
One possible set of axioms is as follows:
1. The only sets are the empty set and those made by adjoining something
to a set:
123
2. The empty set has no elements adjoined into it. In other words, there is First-Order Logic
no way to decompose {} into a smaller set and an element:
¬∃ x, s {x|s}={}
3. Adjoining an element already in the set has no effect:
∀ x, s x∈ s ⇔ s={x|s}
4. The only members of a set are the elements that were adjoined into it.
We express this recursively, saying that x is a member of s if and only if s
is equal to some set s2 adjoined with some element y, where either y is the
same as x or x is a member of s2:
∀t At(Wumpus, [2,2], t)
We can then say that objects can only be at one location at a time:
∀ x, s1, s2, t At(x, s1, t) ∧ At(x, s2, t) ⇒ s1 = s2
Given its current location, the agent can infer properties of the square from
properties of its current percept. For example, if the agent is at a square
125
and perceives a breeze, then that square is breezy: First-Order Logic
126
Artificial Intelligence 3. Decide on a vocabulary of predicates, functions, and constants:
Translate the important domain-level concepts into logic-level names.
This involves many questions of knowledge-engineering style. Like
programming style, this can have a significant impact on the eventual
success of the project. For example, should pits be represented by objects
or by a unary predicate on squares? Should the agent’s orientation be a
function or a predicate? Should the wumpus’s location depend on time?
Once the choices have been made, the result is a vocabulary that is known
as the ontology of the domain. The word ontology means a particular
theory of the nature of being or existence.
4. Encode general knowledge about the domain:
The knowledge engineer writes down the axioms for all the vocabulary
terms. This pins down the meaning of the terms, enabling the expert to
check the content. Often, this step reveals misconceptions or gaps in the
vocabulary that must be fixed by returning to step 3 and iterating through
the process.
5. Encode a description of the specific problem instance:
If the ontology is well thought out, this step will be easy. It will involve
writing simple atomic sentences about instances of concepts that are
already part of the ontology. For a logical agent, problem instances are
supplied by the sensors, whereas a “disembodied” knowledge base is
supplied with additional sentences in the same way that traditional
programs are supplied with input data.
6. Pose queries to the inference procedure and get answers:
We can let the inference procedure operate on the axioms and problem-
specific facts to derive the facts we are interested in knowing. Thus, we
avoid the need for writing an application-specific solution algorithm.
7. Debug the knowledge base:
The answers to queries will be correct on the first try. More precisely, the
answers will be correct for the knowledge base as written, assuming that
the inference procedure is sound, but they will not be the ones that the user
is expecting. For example, if an axiom is missing, some queries will not be
answerable from the knowledge base. A considerable debugging process
could ensure that no axioms are missing.
7.5.2 The Electronic Circuit Domain:
In this topic, we will understand the Knowledge engineering process in an
electronic circuit domain. This approach is mainly suitable for creating
special-purpose knowledge base. Following are some main steps of the
knowledge-engineering process. Using these steps, we will develop a
knowledge base which will allow us to reason about digital circuit (One-
bit full adder) which is given below.
127
First-Order Logic
3. Decide on vocabulary:
The next step of the process is to select functions, predicate, and constants
to represent the circuits, terminals, signals, and gates. Firstly, we will
distinguish the gates from each other and from other objects. Each gate is
represented as an object which is named by a constant, such as, Gate(X1).
The functionality of each gate is determined by its type, which is taken as
constants such as AND, OR, XOR, or NOT.
128
Artificial Intelligence i. Circuits will be identified by a predicate: Circuit (C1).
ii. For the terminal, we will use predicate: Terminal(x).
iii. For gate input, we will use the function In(1, X1) for denoting the first
input terminal of the gate, and for output terminal we will use Out (1,
X1). The function Arity(c, i, j) is used to denote that circuit c has i
input, j output.
iv. The connectivity between gates can be represented by predicate
Connect(Out(1, X1), In(1, X1)).
v. We use a unary predicate On(t), which is true if the signal at a
terminal is on.
129
∀ g Gate(g) ∧ Type(g) = NOT → Signal (In(1, g)) ≠ Signal (Out(1, First-Order Logic
g))
ix. All the gates in the above circuit have two inputs and one output
(except NOT gate).
∃ i1, i2, i3 Signal (In(1, C1))=i1 ∧ Signal (In(2, C1))=i2 ∧ Signal (In(3,
C1))= i3 ∧ Signal (Out(1, C1)) =0 ∧ Signal (Out(2, C1))=1
130
Artificial Intelligence
7.6 SUMMARY
First-order logic a representation language that is far more powerful
than propositional logic.
First-order logic makes use of objects and relations and thereby it gains
more expressive power.
7.9 BIBLIOGRAPHY
1. Ayorinde, I. and Akinkunmi, B. (2013). Application of First-Order
Logic in Knowledge Based Systems. African Journal of Computing &
ICT
2. https://www.javatpoint.com/ai-knowledge-engineering-in-first-order-
logic
*****
132
8
INFERENCE IN FIRST-ORDER LOGIC
Unit Structure
8.0 Objectives
8.1 Introduction
8.2 Propositional vs. First-order inference
8.2.1 Inference rules for quantifiers
8.2.1.1 Universal Instantiation
8.2.1.2 Existential Instantiation
8.2.1.3 Universal Generalization
8.2.1.4 Existential Generalization
8.2.2 Reduction to propositional inference
8.3 Unification and lifting
8.3.1 A first-order inference rule
8.3.2 Unification
8.3.3 Storage and retrieval
8.4 Forward and Backward chaining
8.4.1 Forward chaining
8.4.2 Backward chaining
8.4.2.1 Backward chaining Example
8.4.2.2 Logic Programming
8.4.3 Forward chaining vs. Backward chaining
8.5 Resolution
8.5.1 Conjunctive Normal Form
8.5.2 The resolution inference rule
8.5.3 Example proof
8.5.4 Equality
8.5.5 Resolution strategies
8.5.6 Practical uses of resolution theorem provers
8.6 Summary
8.7 Unit End Exercises
8.8 List of References
8.9 Bibliography
8.0 OBJECTIVES
After going through this chapter, you will learn:
Inference rule for quantifiers.
8.1 INTRODUCTION
Inference in First-Order Logic is used to derive new facts or sentences
from existing sentences. In inference we define effective procedures for
answering questions posed in first- order logic. In propositional logic we
studied how inference can be achieved for propositional logic. In this
chapter, we extend those results to obtain algorithms that can answer any
answerable question stated in first-order logic. We will introduce inference
rules for quantifiers and show how to reduce first-order inference to
propositional inference. Then we introduce the idea of unification,
showing how unification can be used to construct inference rules that
work with first-order sentences. Forward chaining, backward chaining and
logic programming systems are also covered in this chapter. Forward and
backward chaining can be very efficient, but those are applicable only to
knowledge bases that can be expressed as sets of Horn clauses. General
first-order sentences require resolution-based theorem proving, which is
also discussed in this chapter.
It can be represented as
Example:1
IF "Every person like ice-cream"=> ∀x P(x). So we can infer that
"John likes ice-cream" => P(c)
Example: 2
Let's take a famous example,
"All kings who are greedy are Evil." So let our knowledge base contain
this detail as in the form of First-Order Logic:
135
Example: Inference in First-Order
Logic
From the given sentence:
This rule can be used if we want to show that every element has a
similar property.
Example:
Let's represent, P(c): "A byte contains 8 bits", so for ∀ x P(x) "All bytes
contain 8 bits.", it will also be true.
Example:
Let's say that,
"Priyanka got good marks in English."
"Therefore, someone got good marks in English."
136
Artificial Intelligence 8.2.2 Reduction to Propositional Inference:
Once we have defined rules for inferring non-quantified sentences from
quantified sentences, it is possible to reduce first-order inference to
propositional inference.
The first idea is that, just as an existentially quantified sentence can be
replaced by one instantiation, a universally quantified sentence can be
replaced by the set of all possible instantiations. For example, suppose our
knowledge base contains just the sentences
Example:
We will use this rule for Kings are evil, so we will find some x such that x
is king, and x is greedy so we can infer that x is evil.
Here let say,
p1' is king(John) p1 is king(x)
p2' is Greedy(y) p2 is Greedy(x)
θ is {x/John, y/John} q is Evil(x)
SUBST(θ, q) is Evil(John) .
8.3.2 Unification:
Unification is a process of making two different logical atomic
expressions identical by finding a substitution. Unification depends on the
substitution process. It takes two literals as input and makes them identical
using substitution.
Lifted inference rules require finding substitutions that make different
logical expressions look identical. This process is called unification and is
138
Artificial Intelligence a key component of all first-order inference algorithms. The algorithm
takes two sentences and returns a unifier for them if one exists:
UNIFY(p, q)= θ where SUBST(θ, p)= SUBST(θ, q) .
Let us look at some examples of how UNIFY should behave. Suppose
we have a query
AskVars (Knows (John , x)): whom does John know?
Answers to this query can be found by finding all sentences in the
knowledge base that unify with Knows(John , x). Here are the results of
unification with four different sentences that might be in the knowledge
base:
UNIFY(Knows (John, x), Knows (John, Jane)) = {x/Jane }
UNIFY(Knows (John, x), Knows (y, Bill )) = {x/Bill, y/John }
UNIFY(Knows (John, x), Knows (y, Mother (y))) = {y/John , x/Mother
(John )}
UNIFY(Knows (John, x), Knows (x, Elizabeth )) = fail .
The last unification fails because x cannot take on the values John and
Elizabeth at the same time. Now, remember that Knows(x, Elizabeth)
means “Everyone knows Elizabeth,” so we should be able to infer that
John knows Elizabeth. The problem arises only because the two sentences
happen to use the same variable name, x. The problem can be avoided by
standardizing apart one of the two sentences being unified, which means
renaming its variables to avoid name clashes. For example, we can rename
x in Knows(x, Elizabeth) to x17 (a new variable name) without changing
its meaning. Now the unification will work:
UNIFY(Knows(John, x), Knows(x17, Elizabeth)) = {x/Elizabeth,
x17/John} .
There is one more complication: we said that UNIFY should return a
substitution that makes the two arguments look the same. But there could
be more than one such unifier.
For example, UNIFY(Knows(John, x), Knows(y, z)) could return {y/John,
x/z} or {y/John, x/John, z/John}. The first unifier gives Knows(John, z) as
the result of unification, whereas the second gives Knows(John, John).
The second result could be obtained from the first by an additional
substitution {z/John}; we say that the first unifier is more general than the
second, because it places fewer restrictions on the values of the variables.
It turns out that, for every unifiable pair of expressions, there is a single
most general unifier (or MGU) that is unique up to renaming and
substitution of variables. (For example, {x/John} and {y/John} are
considered equivalent, as are {x/John, y/John} and {x/John, y/x}.) In this
case it is {y/John, x/z}.
139
8.3.3 Storage and Retrieval: Inference in First-Order
Logic
The TELL and ASK functions used to inform and interrogate a knowledge
base are the more primitive STORE and FETCH functions. STORE(s)
stores a sentence s into the knowledge base and FETCH(q) returns all
unifiers such that the query q unifies with some sentence in the knowledge
base. The problem we used to illustrate unification for finding all facts that
unify with Knows(John, x) is an example of FETCH.
The simplest way to implement STORE and FETCH is to keep all the
facts in one long list and unify each query against every element of the
list. Such a process is inefficient, but it works. We can make FETCH more
efficient by ensuring that unifications are attempted only with sentences
that have some chance of unifying. For example, there is no point in trying
to unify Knows(John, x) with Brother (Richard, John). We can avoid such
unifications by indexing the facts in the knowledge base. A simple scheme
called predicate indexing puts all the Knows facts in one bucket and all the
Brother facts in another. The buckets can be stored in a hash table for
efficient access. Predicate indexing is useful when there are many
predicate symbols but only a few clauses for each symbol.
Sometimes, however, a predicate has many clauses. For example, suppose
that the tax authorities want to keep track of who employs and we use a
predicate Employs(x, y). This would be a very large bucket with millions
of employers and tens of millions of employees. Answering a query such
as Employs(x,Richard ) with predicate indexing would require scanning
the entire bucket. For this particular query, it would help if facts were
indexed both by predicate and by second argument, perhaps using a
combined hash table key. Then we could simply construct the key from
the query and retrieve exactly those facts that unify with the query.
For other queries, such as Employs(IBM , y), we would need to have
indexed the facts by combining the predicate with the first argument.
Therefore, facts can be stored under multiple index keys, rendering them
instantly accessible to various queries that they might unify with.
Given a sentence to be stored, it is possible to construct indices for all
possible queries that unify with it. For the fact Employs(IBM ,Richard),
the queries are
Employs(IBM ,Richard) Does IBM employ Richard?
Employs(x,Richard ) Who employs Richard?
Employs(IBM , y) Whom does IBM employ?
Employs(x, y) Who employs whom?
140
Artificial Intelligence
Inference engine:
The inference engine is the component of the intelligent system in
artificial intelligence, which applies logical rules to the knowledge base to
infer new information from known facts. The first inference engine was
part of the expert system. Inference engine commonly proceeds in two
modes, which are:
1. Forward chaining
2. Backward chaining
141
knowledge base to use a more restricted and efficient inference algorithm. Inference in First-Order
Logical inference algorithms use forward and backward chaining Logic
approaches, which require knowledge base in the form of the first-order
definite clause.
It is equivalent to p ∧ q → k.
Example:
"As per the law, it is a crime for an American to sell weapons to hostile
142
Artificial Intelligence nations. Country A, an enemy of America, has some missiles, and all the
missiles were sold to it by Robert, who is an American citizen."
Prove that "Robert is criminal."
To solve the above problem, first, we will convert all the above facts into
first-order definite clauses, and then we will use a forward-chaining
algorithm to reach the goal.
∃p Owns(A, p) ∧ Missile(p)
It can be written in two definite clauses by using Existential
Instantiation, introducing new Constant T1.
Owns(A, T1) ......(2)
Missile(T1) .......(3)
3. All of the missiles were sold to country A by Robert.
143
Forward chaining proof: Inference in First-Order
Logic
Step-1:
In the first step we will start with the known facts and will choose the
sentences which do not have implications, such as: American(Robert),
Enemy(A, America), Owns(A, T1), and Missile(T1). All these facts will
be represented as below.
Step-2:
At the second step, we will see those facts which infer from available facts
and with satisfied premises.
Rule-(1) does not satisfy premises, so it will not be added in the first
iteration.
Rule-(2) and (3) are already added.
Rule-(4) satisfy with the substitution {p/T1}, so Sells (Robert, T1, A) is
added, which infers from the conjunction of Rule (2) and (3).
Rule-(6) is satisfied with the substitution(p/A), so Hostile(A) is added and
which infers from Rule-(7).
Step-3:
At step-3, as we can check Rule-(1) is satisfied with the substitution
{p/Robert, q/T1, r/A}, so we can add Criminal(Robert) which infers all the
available facts. And hence we reached our goal statement.
144
Artificial Intelligence Hence it is proved that Robert is Criminal using forward chaining
approach.
Backward-Chaining proof:
In Backward chaining, we will start with our goal predicate, which is
Criminal(Robert), and then infer further rules.
Step-1:
At the first step, we will take the goal fact. And from the goal fact, we will
infer other facts, and at last, we will prove those facts true. So, our goal
fact is "Robert is Criminal," so following is the predicate of it.
Step-2:
At the second step, we will infer other facts form goal fact which satisfies
the rules. As we can see in Rule-1, the goal predicate Criminal(Robert) is
present with substitution {Robert/P}. So, we will add all the conjunctive
facts below the first level and will replace p with Robert.
Here we can see American (Robert) is a fact, so it is proved here.
146
Artificial Intelligence Step-3:
At step-3, we will extract further fact Missile(q) which infer from
Weapon(q), as it satisfies Rule-(5). Weapon (q) is also true with the
substitution of a constant T1 at q.
Step-4:
At step-4, we can infer facts Missile(T1) and Owns(A, T1) form
Sells(Robert, T1, r) which satisfies the Rule- 4, with the substitution of A
in place of r. So these two statements are proved here.
Step-5:
At step-5, we can infer the fact Enemy(A, America) from Hostile(A)
which satisfies Rule- 6. And hence all the statements are proved true using
backward chaining.
147
Inference in First-Order
Logic
A∧B⇒C
In Prolog we have
C :- A, B.
Here is a typical example:
criminal(X) :- american(X), weapon(Y), sells(X,Y,Z), hostile(Z).
The notation [E|L] denotes a list whose first element is E and whose rest is
L. Here is a Prolog program for append(X,Y,Z), which succeeds if list Z is
the result of appending lists X and Y:
append([],Y,Y).
append([A|X],Y,[A|Z]) :- append(X,Y,Z).
In English, we can read these clauses as
148
Artificial Intelligence 1. appending an empty list with a list Y produces the same list Y and
2. [A|Z] is the result of appending [A|X] onto Y, provided that Z is the
result of appending X onto Y.
We can ask the query
append(X,Y,[1,2]): what two lists can be appended to give [1,2]?
We get the solutions
X=[] Y=[1,2];
X=[1] Y=[2];
X=[1,2] Y=[]
The execution of Prolog programs is done through depth-first backward
chaining. Some aspects of Prolog fall outside standard logical inference:
There are built-in predicates that have side effects when executed.
These include input-output predicates and the assert/retract predicates
for modifying the knowledge base. Such predicates have no
counterpart in logic and can produce confusing results.
Sr.
Forward Chaining Backward Chaining
No
Forward chaining starts from Backward chaining starts from
known facts and applies the goal and works backward
1 inference rule to extract more through inference rules to find
data unit it reaches to the goal. the required facts that support
the goal.
2 It is a bottom-up approach It is a top-down approach
Forward chaining is known as Backward chaining is known as
data-driven inferencegoal-driven technique as we
3 technique as we reach to the start from the goal and divide
goal using the available data. into sub-goal to extract the
facts.
Forward chaining reasoning Backward chaining reasoning
4 applies a breadth-first search applies a depth-first search
strategy. strategy.
Forward chaining tests for all Backward chaining only tests
5 the available rules for few required rules.
Forward chaining is suitable Backward chaining is suitable
for the planning, monitoring, for diagnostic, prescription, and
6
control, and interpretation debugging application.
application.
Forward chaining can generate Backward chaining generates a
7 an infinite number of possible finite number of possible
conclusions. conclusions.
8.5 RESOLUTION
Resolution is a valid inference rule producing a new clause implied by two
clauses containing complementary literals. A literal is an atomic symbol or
its negation, i.e., P, ~P. Resolution is a theorem proving technique that
proceeds by building refutation proofs, i.e., proofs by contradictions. It
was invented by a Mathematician John Alan Robinson in the year 1965.
A Knowledge Base is actually a set of sentences all of which are true, i.e.,
150
Artificial Intelligence a conjunction of sentences. To use resolution, translate knowledge base
into conjunctive normal form (CNF), where each sentence is written as a
disjunction of (one or more) literals.
Resolution is used, if there are various statements are given, and we need
to prove a conclusion of those statements. Unification is a key concept in
proofs by resolutions. Propositional resolution using refutation is a
complete inference procedure for propositional logic. Here, we describe
how to extend resolution to first-order logic. Resolution is a single
inference rule which can efficiently operate on the conjunctive normal
form or clausal form.
1. Eliminate implications:
∀ x [¬∀ y ¬Animal(y) ∨ Loves(x, y)] ∨ [∃ y Loves(y, x)]
2. Move ¬ inwards:
In addition to the usual rules for negated connectives, we need rules for
negated quantifiers. Thus, we have
¬∀x p becomes ∃ x ¬p
¬∃x p becomes ∀ x ¬p
151
Our sentence will go through the following transformations: Inference in First-Order
Logic
∀ x [∃ y ¬(¬Animal(y) ∨ Loves(x, y))] ∨ [∃ y Loves(y, x)]
3. Standardize variables:
For sentences like (∃xP(x))∨(∃xQ(x)) which use the same variable name
twice, change the name of one of the variables. This avoids confusion later
when we drop the quantifiers. Thus, we have
4. Skolemize:
Skolemization is the process of removing existential quantifiers by
elimination. In the simple case, it is just like the Existential Instantiation
rule: translate ∃x P(x) into P(A), where A is a new constant. However, we
can’t apply Existential Instantiation to our sentence above because it
doesn’t match the pattern ∃x P(x); only parts of the sentence match the
pattern.
Thus, we want the Skolem entities to depend on x and z:
6. Distribute ∨ over ∧:
[Animal (F(x)) ∨ Loves(G(z), x)] ∧ [¬Loves(x, F(x)) ∨ Loves(G(z), x)]
This step may require flattening out nested conjunctions and disjunctions.
The sentence is now in CNF and consists of two clauses.
152
Artificial Intelligence 8.5.2 The Resolution Inference Rule:
The resolution rule for first-order clauses is simply a lifted version of the
propositional resolution rule. Resolution can resolve two clauses if they
contain complementary literals, which are assumed to be standardized
apart so that they share no variables.
Propositional literals are complementary if one is the negation of the other.
First-order literals are complementary if one unifies with the negation of
the other. Thus, we have
153
logic. Inference in First-Order
Logic
e. ∀x ¬ eats(Anil, x) V eats(Harry, x)
g. ∀x ¬ alive(x) V ¬ killed(x)
h. likes(John, Peanuts)
e. ∀x ¬ eats(Anil, x) V eats(Harry, x)
f. ∀x ¬killed(x) ] V alive(x)
g. ∀x ¬ alive(x) V ¬ killed(x)
h. likes(John, Peanuts)
154
Artificial Intelligence iii. Rename variables or standardize variables
a. ∀x ¬ food(x) V likes(John, x)
b. food(Apple) Λ food(vegetables)
f. ∀g ¬killed(g) ] V alive(g)
g. ∀k ¬ alive(k) V ¬ killed(k)
h. likes(John, Peanuts)
155
Step-4: Draw Resolution graph: Inference in First-Order
Logic
Now in this step, we will solve the problem by resolution tree using
substitution. For the above problem, it will be given as follows:
156
Artificial Intelligence
8.5.4 Equality:
None of the inference methods described so far handle an assertion of the
form x = y. Three distinct approaches can be taken. The first approach is
to write down sentences about the equality relation in the knowledge base.
Equality is reflexive, symmetric, and transitive and we can substitute
equals for equals in any predicate or function. So, we need three basic
axioms, and then one for each predicate and function:
∀x x=x
∀ x, y x=y ⇒ y =x
∀ x, y, z x=y ∧ y =z ⇒ x=z
Example:
Given,
Father (Father (x)) = PaternalGrandfather (x)
Birthdate(Father (Father (Bella)), 1926)
we can conclude by demodulation
Birthdate(PaternalGrandfather (Bella), 1926) .
157
1. Demodulation: For any terms x, y, and z, w here z appears Inference in First-Order
Logic
somewhere in literal and where UNIFY(x, z) = θ,
Where, SUBST is the usual substitution of a binding list, and SUB(x, y,m)
means to replace x with y everywhere that x occurs within m.
The rule can also be extended to handle non-unit clauses in which an
equality literal appears.
3. Paramodulation: For any terms x, y, and z, where z appears
somewhere in literal ,
and where UNIFY(x, z) = θ,
3. Equational unification:
A third approach handles equality reasoning entirely within an extended
unification algorithm. That is, terms are unifiable if they are provably
equal under some substitution, where “provably” allows for equality
reasoning. For example, the terms 1 + 2 and 2 + 1 normally are not
unifiable, but a unification algorithm that knows that x + y=y + x could
unify them with the empty substitution. Equational unification of this kind
can be done with efficient algorithms designed for the particular axioms
using commutativity, associativity, and so on rather than through explicit
inference with those axioms.
1. Unit preference:
This strategy prefers to do resolutions where one of the sentences is a
158
Artificial Intelligence single literal (also known as a unit clause). The idea behind the strategy is
that we are trying to produce an empty clause, so it might be a good idea
to prefer inferences that produce shorter clauses. Resolving a unit sentence
(such as P) with any other sentence (such as ¬P ∨¬Q∨R) always yields a
clause (in this case, ¬Q ∨ R) that is shorter than the other clause. This
strategy leads to a dramatic speedup, making it feasible to prove theorems
that could not be handled without the preference. Unit resolution is a
restricted form of resolution in which every resolution step must involve a
unit clause. Unit resolution is incomplete in general, but complete for
Horn clauses.
2. Set of support:
Preferences that try certain resolutions first are helpful, but in general it is
more effective to try to eliminate some potential resolutions altogether.
For example, we can insist that every resolution step involve at least one
element of a special set of clauses called the set of support. The resolvent
is then added into the set of support. If the set of support is small relative
to the whole knowledge base, the search space will be reduced. The set-of-
support strategy has the additional advantage of generating goal-directed
proof trees that are often easy for humans to understand.
3. Input resolution:
In this strategy, every resolution combines one of the input sentences
(from the KB or the query) with some other sentence. The space of proof
trees of this shape is smaller than the space of all proof graphs. Linear
resolution is complete.
4. Subsumption:
The subsumption method eliminates all sentences that are subsumed by
(that is, more specific than) an existing sentence in the KB. For example,
if P(x) is in the KB, then there is no sense in adding P(A) and even less
sense in adding P(A) ∨ Q(B). Subsumption helps keep the KB small and
thus helps keep the search space small.
159
4. In the case of software, reasoning about programs is quite similar to Inference in First-Order
reasoning about actions, axioms describe the preconditions and effects Logic
of each statement.
5. Similar techniques are now being applied to software verification by
systems such as the SPIN model checker. For example, the Remote
Agent spacecraft control program was verified before and after flight.
6. The RSA public key encryption algorithm and the Boyer–Moore
string-matching algorithm have been verified this way.
8.6 SUMMARY
In this chapter we analyzed logical inference in first-order logic.
8.9 BIBLIOGRAPHY
Ayorinde, I. and Akinkunmi, B. (2013). Application of First-Order
Logic in Knowledge Based Systems. African Journal of Computing &
ICT
https://www.javatpoint.com/ai-resolution-in-first-order-logic
https://www.javatpoint.com/forward-chaining-and-backward-chaining-
in-ai
*****
161
UNIT V
9
PLANNING
Unit Structure
9.0 Definition of Classical Planning
9.0.1 Planning assumptions
9.1 Algorithms for planning as State Space Search
9.1.1 Forward State Space
9.1.2 Backward State Space
9.2 Planning Graphs
9.2.1 Algorithm to build a plan graph
9.3 Other Classical Planning Approaches
9.4 Analysis of planning approaches
9.5 Time, schedules and resources
9.5.1 Representing Temporal and resource constraints
9.5.2 Aggregation
9.5.3 Solving Scheduling Problems
9.6 Hierarchical Planning
9.6.1 Hierarchical Task Network (HTN)
9.6.2 Partial Order Planning (POP)
9.6.3 POP one level planner
9.7 Planning and acting in Nondeterministic Domains
9.7.1 Some strategies are listed below
9.8 Multiagent planning
9.9 Summary
9.10 Unit End Questions
9.11 References
163
2. In this state space planning the state will start from the goal state the it Planning
will go towards the initial state.
3. In the below example if the target is Delhi, then the first state from
which it will start is Train is at Delhi then it will move towards the
initial state i.e., Train is at Mumbai.
165
It searches through space not through state of the plan. Planning
166
Artificial Intelligence When it comes to Job Shop Scheduling machine can do only one
work at a time.
It follows non-pre-emptive approach i.e., if the job starts with one task
then it has to complete fully, it can’t stop in between.
In the below example J1, J2 and J3 represents job and at the left end
Machines are present with a number line starting at zero and going till
7.
So, from the diagrammatic representation we can say that it takes
around 7 for all the jobs to finish.
It should be noted that the values by this is not always optimal in
nature it may vary on the basis of machine, resource, kind of job etc.
9.5.2 Aggregation:
Aggregation is one in which the variable represented in the following
manner like Man(3) instead of Man(l1), Man(l2) and Man(l3).
The main aim of aggregation is nothing but groupism i.e., it may
happen that there is a need of whole object to be used together in such
scenarios we may use aggregation instead of calling each object
individually.
167
9.6 HIERARCHICAL PLANNING Planning
168
Artificial Intelligence 9.6.2 Partial Order Planning (POP):
In this the planning and action is done at the partial level i.e.; we will
go little deeper in our scenario and try to achieve the goal.
POP doesn’t follows the action sequences, if you have 2 action then
any of the one will be chosen and executed the main aim is to reach
the goal.
To understand POP let’s take an example of colouring a flower which
has a stem and petals
So, imagine we coloured the petal first, then we coloured the stem.
At the end the goal is achieved called as colouring.
169
In this plan has to be done with very less or no details Planning
Contingent Planning:
It is also called as conditional planning
On the basis of condition, the agent makes the plan, applies the action
and executes it to reach the goal.
The plan made on the basis of environment which can be partially
observable or fully observable is appropriate.
The variable which is used in contingent should be existential
quantifier in nature.
In this it can also use belief state if needed but this belief state must
satisfy the condition, it may use formula for the same.
Online replanning:
In this particular technique a continuous checking is done which is
nothing but the monitoring.
Replanning is done by execution of it and a new plan is created.
It is not always necessary that the whole plan has to be replanned,
sometimes replanning may be done in particular part as well
according to the need.
170
Artificial Intelligence In the below figure P is nothing but the plan that has been applied
using this plan the agent will move ahead and side by side it will also
observe the whole execution and once it gets to know that in the
particular state say E the execution or the action that has been applied
is not according to the need to reach the goal it will change the plan
which is nothing but replanning or we can also call it as repairing.
Now after replanning again the new actions are applied and executed
to reach the goal in a very efficient manner.
3 monitoring are supported
1. Action monitoring: here the agent will check whether the actions are
according to the precondition.
2. Plan monitoring: In this before running the action the agent will verify
if it is according to the plan and whether it will give success.
3. Goal monitoring: Betterment of the goal is checked here by checking
it.
Categories:
Multieffector planning: An agent having multiple effector associated
with it is called as multieffector planning agent. To understand this,
consider the following example: A person can sing and dance at the
same time
If these effectors are divided then we will get multibody.
171
Planning with multiple simultaneous actions: Planning
9.9 SUMMARY
In this chapter how are what kind of planning has to be implemented
to achieve goal is getting explained in a very brief manner.
In forward chaining a initial phase is considered and it will move
further till the final state whereas in backward chaining vice a versa is
applied.
Graphical representation of plan is done using GRAPHPLAN
Decomposition of plan is done to get the other solutions that is
possible in the graph such a way of finding the solution is called as
hierarchical plan approach.
Planning in nondeterministic approach is the one in which the details
like environment or data are not known prior.
Online planning is the one in which continuous monitoring is
emphasised and replanning is made again to fine the most apt
solution.
172
Artificial Intelligence 9.10 UNIT END QUESTIONS
Explain replanning in detail.
What do you mean by subgoals?
What are nondeterministic domains? Explain planning adaption for
non-deterministic domains
Explain general characteristics of uncertain environments.
Write a short note on conditional planning or contingency planning
Explain online replanning in detail.
Write a short note on how planning is done with Multiple
simultaneous actions.
Write a short note one sensorless planning or conformant planning
9.11 REFERENCES
Artificial Intelligence: A modern approach by Stuart Russel and peter
Norvig
Publisher: Pearson 3rd edition year is 2015
*****
173
10
KNOWLEDGE REPRESENTATION
Unit Structure
10.0 Categories and Objects
10.1 Events
10.2 Mental events and mental objects
10.3 Reasoning systems for categories
10.3.1 Semantic networks
10.3.2.1.1 Description logics
10.4 Reasoning with default information
10.4.1 Circumscription and default logic
10.5 Internet shopping world
10.5.1 Following links
10.6 Summary
10.7 Unit End Questions
10.8 References
10.1 EVENTS
In the above topic we saw that the things were working on the basis of
situation, but in this topic the event is done on the basis of time.
175
10.2 MENTAL EVENTS AND MENTAL OBJECTS Knowledge Representation
In our life also many times it happens that we want to know about
something. We want to gain knowledge of something new. This new
knowledge about something can be gained by asking a query from
someone.
176
Artificial Intelligence
Based on the above rule we can say that the Dog(i) if true if it barks(i) .
177
If any probability interferes in the rule then it may be replaced or Knowledge Representation
substituted.
Using AI we can even judge which shopping website has the maximum
visitors and accordingly ranking can be done.
On all the modules of the website the links are added between the
modules.
This module will be able to interact and make it work with each other
easily
10.6 SUMMARY
In this chapter we saw that how the we categories the data.
There are subcategories that can be applied. It uses fluent and objects.
Knowledge gaining from someone else mind is explained in which
mental object comes into picture.
Reasoning system of categories is of two types one is semantic
network and second one is efficient algorithms both helps us to
represent the facts and knowledges through graphs and algorithms.
178
Artificial Intelligence Artificial Intelligence plays one of the important roles in internet
shopping world as almost everything from creating the account till the
product/service delivery requires intelligence.
10.8 REFERENCES
Artificial Intelligence: A modern approach by Stuart Russel and peter
Norvig Publisher: Pearson 3rd edition year is 2015
*****
179