Unit III Knowledge, Reasoning and Planning
Unit III Knowledge, Reasoning and Planning
• where each is a literal and i and m are complementary literals (i.e., one is the negation of
the other).
• Thus, the unit resolution rule takes a clause—a disjunction of literals—and a literal and
produces a new clause.
• Note that a single literal can be viewed as a disjunction of one literal, also known as a unit
clause.
Horn clauses and definite clauses
• The completeness of resolution makes it a very important inference
method.
• In many practical situations, however, the full power of resolution is not
needed.
• Some real-world knowledge bases satisfy certain restrictions on the form
of sentences they contain, which enables them to use a more restricted
and efficient inference algorithm.
• One such restricted form is the definite clause, which is a disjunction of
literals of which exactly one is positive.
• For example, the clause (¬L1,1 ∨ ¬Breeze ∨ B1,1) is a definite clause,
whereas (¬B1,1 ∨ P1,2 ∨ P2,1) is not.
Horn clauses and definite clauses
• Slightly more general is the Horn clause, which is a disjunction of
literals of which at most one is positive.
• So all definite clauses are Horn clauses, as are clauses with no
positive literals; these are called goal clauses.
• Horn clauses are closed under resolution: if you resolve two Horn
clauses, you get back a Horn clause
Horn clauses and definite clauses
Forward and backward chaining
• The forward-chaining algorithm PL-FC-ENTAILS?(KB, q) determines if a single
proposition symbol q—the query—is entailed by a knowledge base of definite clauses.
• It begins from known facts (positive literals) in the knowledge base.
• If all the premises of an implication are known, then its conclusion is added to the set
of known facts.
• For example, if L1,1 and Breeze are known and (L1,1 ∧ Breeze) ⇒ B1,1 is in the
knowledge base, then B1,1 can be added.
• This process continues until the query q is added or until no further inferences can be
made.
• The detailed algorithm is shown in Figure 7.15; the main point to remember is that it
runs in linear time.
Forward and backward chaining
• The best way to understand the algorithm is through an example and a
picture.
• Figure 7.16(a) shows a simple knowledge base of Horn clauses with A
and B as known facts.
• Figure 7.16(b) shows the same knowledge base drawn as an AND–
OR graph (see Chapter 4).
• In AND–OR graphs, multiple links joined by an arc indicate a
conjunction—every link must be proved—while multiple links without
an arc indicate a disjunction—any link can be proved.
• It is easy to see how forward chaining works in the graph.
Forward and backward chaining
FIRST-ORDER LOGIC-
The language of thought
• Natural languages (such as English or Spanish) are very expressive indeed.
• We managed to write almost this whole book in natural language, with only
occasional lapses into other languages (including logic, mathematics, and the
language of diagrams).
• There is a long tradition in linguistics and the philosophy of language that
views natural language as a declarative knowledge representation language.
• If we could uncover the rules for natural language, we could use it in
representation and reasoning systems and gain the benefit of the billions of
pages that have been written in natural language.
• The modern view of natural language is that it serves a as a medium for
communication rather than pure representation.
Combining the best of formal and natural
languages
• We can adopt the foundation of propositional logic—a declarative,
compositional semantics that is context-independent and unambiguous—and
build a more expressive logic on that foundation, borrowing representational
ideas from natural language while avoiding its drawbacks.
• When we look at the syntax of natural language, the most obvious elements
are nouns and noun phrases that refer to objects (squares, pits, wumpuses)
and verbs and verb phrases that refer to relations among objects (is breezy,
is adjacent to, shoots).
• Some of these relations are functions—relations in which there is only one
“value” for a given “input.”
• It is easy to start listing examples of objects, relations, and functions:
Combining the best of formal and natural
languages
• Objects: people, houses, numbers, theories, Ronald McDonald,
colors, baseball games, wars, centuries ...
• Relations: these can be unary relations or properties such as red,
round, bogus, prime, multistoried ..., or more general n-ary relations
such as brother of, bigger than, inside, part of, has color, occurred
after, owns, comes between, ...
• Functions: father of, best friend, third inning of, one more than,
beginning of ...
Syntax and Semantics for First-order Logic
Models for first-order logic
• The models of a logical language are the formal structures that constitute the
possible worlds under consideration.
• Each model links the vocabulary of the logical sentences to elements of the
possible world, so that the truth of any sentence can be determined.
• Thus, models for propositional logic link proposition symbols to predefined truth
values.
• Models for first-order logic are much more interesting.
• First, they have objects in them! The domain of a model is the set of objects
or domain elements it contains.
• The domain is required to be nonempty—every possible world must contain at
least one object
Models for first-order logic
• Richard the Lionheart, King of England from 1189 to 1199; his younger
brother, the evil King John, who ruled from 1199 to 1215; the left legs of
Richard and John; and a crown
• The objects in the model may be related in various ways.
• In the figure, Richard and John are brothers.
• Formally speaking, a relation is just the set of tuples of objects that are related.
• (A tuple is a collection of objects arranged in a fixed order and is written with
angle brackets surrounding the objects.)
• Thus, the brotherhood relation in this model is the set
• { Richard the Lionheart, King John, King John, Richard the Lionheart } .
Models for first-order logic
Symbols and interpretations
• The basic syntactic elements of first-order logic are the symbols that stand for
objects relations, and functions.
• The symbols, therefore, come in three kinds: constant symbols which stand for
objects; predicate symbols, which stand for relations; and function symbols, which
stand for functions.
• We adopt the convention that these symbols will begin with uppercase letters.
• For example, we might use the constant symbols Richard and John; the predicate
symbols Brother , OnHead, Person, King, and Crown; and the function symbol
LeftLeg.
• As with proposition symbols, the choice of names is entirely up to the user.
• Each predicate and function symbol comes with an arity that fixes the number of
arguments
Symbols and interpretations
• As in propositional logic, every model must provide the information required to
determine if any given sentence is true or false.
• Thus, in addition to its objects, relations, and functions, each model includes an
interpretation that specifies exactly which objects, relations and functions are
referred to by the constant, predicate, and function symbols.
• One possible interpretation for our example—which a logician would call the
intended interpretation—is as follows:
• Richard refers to Richard the Lionheart and John refers to the evil King John.
• Brother refers to the brotherhood relation, that is, the set of tuples of objects given in Equation
(8.1); OnHead refers to the “on head” relation that holds between the crown and King John;
Person, King, and Crown refer to the sets of objects that are persons, kings, and crowns.
• LeftLeg refers to the “left leg” function, that is, the mapping given in Equation (8.2)
Terms
• 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.
• This is what function symbols are for: instead of using a constant
symbol, we use LeftLeg(John).
• In the general case, a complex term is formed by a function symbol
followed by a parenthesized list of terms as arguments to the function
symbol.
Atomic sentences
• Now that we have both terms for referring to objects and predicate symbols for referring to
relations, we can put them together to make atomic sentences that state facts.
• An atomic sentence (or atom for short) is formed from a predicate symbol optionally followed by a
parenthesized list of terms, such as
• Brother (Richard, John).
• This states, under the intended interpretation given earlier, that Richard the Lionheart is the brother
of King John.6 Atomic sentences can have complex terms as arguments.
• Thus,
• Married(Father (Richard), Mother (John))
• states that Richard the Lionheart’s father is married to King John’s mother (again, under a suitable
interpretation).
• An atomic sentence is true in a given model if the relation referred to by the predicate symbol
holds among the objects referred to by the arguments.
Complex sentences
• We can use logical connectives to construct more complex sentences,
with the same syntax and semantics as in propositional calculus.
• Here are four sentences that are true in the model of Figure 8.2 under
our intended interpretation:
• ¬Brother (LeftLeg(Richard), John)
• Brother (Richard, John) ∧ Brother (John, Richard)
• King(Richard) ∨ King(John)
• ¬King(Richard) ⇒ King(John) .
Quantifiers
• Once we have a logic that allows objects, it is only natural to want to express properties of
entire collections of objects, instead of enumerating the objects by name.
• Quantifiers let us do this.
• First-order logic contains two standard quantifiers, called universal and existential.
• Universal quantification (∀)
• Rules such as “Squares neighboring the wumpus are smelly” and “All kings are persons” are
the bread and butter of first-order logic.
• We deal with the first of these in Section 8.3.
• The second rule, “All kings are persons,” is written in first-order logic as
• ∀ x King(x) ⇒ Person(x) .
• ∀ is usually pronounced “For all ...”.
Existential quantification (∃)
• Universal quantification makes statements about every object.
Similarly, we can make a statement about some object in the universe
without naming it, by using an existential quantifier.
• To say, for example, that King John has a crown on his head, we write
∃ x Crown(x) ∧ OnHead(x, John) .
• ∃x is pronounced “There exists an x such that ...” or “For some x...”
Nested quantifiers
• We will often want to express more complex sentences using multiple quantifiers.
• The simplest case is where the quantifiers are of the same type.
• For example, “Brothers are siblings” can be written as
• ∀ x ∀ y Brother (x, y) ⇒ Sibling(x, y)
• In other cases we will have mixtures.
• “Everybody loves somebody” means that for every person, there is someone that
person loves:
• ∀ x ∃ y Loves(x, y) .
• On the other hand, to say “There is someone who is loved by everyone,” we write
• ∃ y ∀ x Loves(x, y) .
Connections between ∀ and ∃
• The two quantifiers are actually intimately connected with each other,
through negation.
• Asserting that everyone dislikes parsnips is the same as asserting there
does not exist someone who likes them, and vice versa:
• ∀ x ¬Likes(x,Parsnips ) is equivalent to ¬∃ x Likes(x,Parsnips) .
Equality
• First-order logic includes one more way to make atomic sentences, other
than using a predicate and terms as described earlier.
• We can use the equality symbol 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.
• Because an interpretation fixes the referent of any term, determining the
truth of an equality sentence is simply a matter of seeing that the
referents of the two terms are the same object.
Assertions and queries in first-order logic
• Sentences are added to a knowledge base using TELL, exactly as in propositional logic.
• Such sentences are called assertions.
• For example, we can assert that John is a king, Richard is a person, and all kings are persons:
• TELL(KB, King(John)) .
• TELL(KB, Person(Richard)) .
• TELL(KB, ∀ x King(x) ⇒ Person(x)) .
• We can ask questions of the knowledge base using ASK.
• For example,
• ASK(KB, King(John))
• returns true.
• Questions asked with ASK are called queries or goals.
• Generally speaking, any query that is logically entailed by the knowledge base should be answered
affirmatively.
• For example, given the two preceding assertions, the query ASK(KB, Person(John)) should also return true.
• We can ask quantified queries, such as ASK(KB, ∃ x Person(x)) .
• The answer is true, but this is perhaps not as helpful as we would like. It is rather like answering “Can you
tell me the time?” with “Yes.”
The knowledge-engineering process
• Knowledge engineering projects vary widely in content, scope, and
difficulty, but all such projects include the following steps:
1. Identify the task
2. Assemble the relevant knowledge
3. Decide on a vocabulary of predicates, functions, and constants
4. Encode general knowledge about the domain
5. Encode a description of the specific problem instance
6. Pose queries to the inference procedure and get answers.
7. Debug the knowledge base
Identify the task
• The knowledge engineer must delineate the range of questions that the
knowledge base will support and the kinds of facts that will be available
for each specific problem instance.
• For example, does the wumpus knowledge base need to be able to
choose actions or is it required to answer questions only about the
contents of the environment?
• Will the sensor facts include the current location?
• The task will determine what knowledge must be represented in order to
connect problem instances to answers.
• This step is analogous to the PEAS process for designing agents
Assemble the relevant knowledge
• That is, 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.
• The ontology determines what kinds of things exist, but does not determine their
specific properties and interrelationships.
Encode general knowledge about the domain
• The knowledge engineer writes down the axioms for all the
vocabulary terms.
• This pins down (to the extent possible) 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.
Encode a description of the specific problem instance
• Alas, the answers to queries will seldom 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.
• Missing axioms or axioms that are too weak can be easily identified
by noticing places where the chain of reasoning stops unexpectedly.
CLASSICAL PLANNING
• The problem-solving agent of Chapter 3 can find sequences of actions that result in a
goal state.
• But it deals with atomic representations of states and thus needs good domain-
specific heuristics to perform well.
• For example, in the wumpus world, the simple action of moving a step forward
had to be repeated for all four agent orientations, T time steps, and n2 current
locations.
• In response to this, planning researchers have settled on a factored representation
— one in which a state of the world is represented by a collection of variables.
• We use a language called PDDL, the Planning Domain Definition Language, that
allows us to express all 4T n2 actions with one action schema.
CLASSICAL PLANNING
• We now show how PDDL describes the four things we need to define a search
problem:
• the initial state,
• the actions that are available in a state,
• the result of applying an action, and
• the goal test.
• Each state is represented as a conjunction of fluents that are ground, functionless atoms.
• For example, Poor ∧ Unknown might represent the state of a hapless agent, and a
state in a package delivery problem might be At(Truck 1, Melbourne) ∧ At(Truck 2,
Sydney).
• Actions are described by a set of action schemas that implicitly define the
ACTIONS(s) and RESULT(s, a) functions needed to do a problem-solving search.
CLASSICAL PLANNING
• A set of ground (variable-free) actions can be represented by a single
action schema.
• The schema is a lifted representation—it lifts the level of reasoning
from propositional logic to a restricted subset of first-order logic.
• For example, here is an action schema for flying a plane from one
location to another:
• The schema consists of the action name, a list of all the variables used
in the schema, a precondition and an effect.
CLASSICAL PLANNING
• We are free to choose whatever values we want to instantiate the
variables.
• For example, here is one ground action that results from substituting
values for all the variables:
CLASSICAL PLANNING
• The result of executing action a in state s is defined as a state s which
is represented by the set of fluents formed by starting with s, removing
the fluents that appear as negative literals in the action’s effects (what
we call the delete list or DEL(a)), and adding the fluents that are
positive literals in the action’s effects (what we call the add list or
ADD(a)):
• RESULT(s, a)=(s − DEL(a))∪ ADD(a) .
• For example, with the action Fly(P1, SFO, JFK), we would remove
At(P1, SFO) and add At(P1, JFK).
Example: Air cargo transport
Example: Air cargo transport
• Figure 10.1 shows an air cargo transport problem involving loading and
unloading cargo and flying it from place to place.
• The problem can be defined with three actions: Load, Unload, and Fly.
• The actions affect two predicates: In(c, p) means that cargo c is inside plane p,
and At(x, a) means that object x (either plane or cargo) is at airport a.
• Note that some care must be taken to make sure the At predicates are maintained
properly.
• When a plane flies from one airport to another, all the cargo inside the plane goes
with it.
• In first-order logic it would be easy to quantify over all objects that are inside the
plane
Example: Air cargo transport
• The approach we use is to say that a piece of cargo ceases to be At
anywhere when it is In a plane; the cargo only becomes At the new
airport when it is unloaded.
• So At really means “available for use at a given location.”
• The following plan is a solution to the problem:
Example: The spare tire problem
• Consider the problem of changing a flat tire (Figure 10.2).
• The goal is to have a good spare tire properly mounted onto the car’s axle, where the
initial state has a flat tire on the axle and a good spare tire in the trunk.
• To keep it simple, our version of the problem is an abstract one, with no sticky lug nuts or
other complications.
• There are just four actions: removing the spare from the trunk, removing the flat tire
from the axle, putting the spare on the axle, and leaving the car unattended
overnight.
• We assume that the car is parked in a particularly bad neighborhood, so that the effect of
leaving it overnight is that the tires disappear.
• A solution to the problem is
• [Remove(Flat, Axle), Remove(Spare, Trunk ),PutOn(Spare, Axle)].
Example: The spare tire problem
Example: The blocks world
• One of the most famous planning domains is known as the blocks world.
• This domain consists of a set of cube-shaped blocks sitting on a table.2
• The blocks can be stacked, but only one block can fit directly on top of another.
• A robot arm can pick up a block and move it to another position, either on
the table or on top of another block.
• The arm can pick up only one block at a time, so it cannot pick up a block that
has another one on it.
• The goal will always be to build one or more stacks of blocks, specified in
terms of what blocks are on top of what other blocks.
• For example, a goal might be to get block A on B and block B on C
Algorithms for Planning as State-Space
Search
• Forward (progression) state-space search
• From the earliest days of planning research (around 1961) until around 1998 it was
assumed that forward state-space search was too inefficient to be practical.
• It is not hard to come up with reasons why.
• First, forward search is prone to exploring irrelevant actions.
• Consider the noble task of buying a copy of AI: A Modern Approach from an
online bookseller.
• Suppose there is an action schema Buy(isbn) with effect Own(isbn). ISBNs are 10
digits, so this action schema represents 10 billion ground actions.
• An uninformed forward-search algorithm would have to start enumerating these 10
billion actions to find one that leads to the goal.
Forward (progression) state-space search
• Consider an air cargo problem with 10 airports, where each airport has 5 planes and 20 pieces
of cargo.
• The goal is to move all the cargo at airport A to airport B.
• There is a simple solution to the problem: load the 20 pieces of cargo into one of the planes at
A, fly the plane to B, and unload the cargo.
• Finding the solution can be difficult because the average branching factor is huge: each of the
50 planes can fly to 9 other airports, and each of the 200 packages can be either unloaded (if
it is loaded) or loaded into any plane at its airport (if it is unloaded).
• So in any state there is a minimum of 450 actions (when all the packages are at airports with
no planes) and a maximum of 10,450 (when all packages and planes are at the same airport).
• On average, let’s say there are about 2000 possible actions per state, so the search graph up to
the depth of the obvious solution has about 200041 nodes.
Backward (regression) relevant-states search
• In regression search we start at the goal and apply the actions
backward until we find a sequence of steps that reaches the initial
state.
• It is called relevant-states search because we only consider actions that
are relevant to the goal (or current state).
• As in belief-state search (Section 4.4), there is a set of relevant states
to consider at each step, not just a single state.
Heuristics for planning
• Neither forward nor backward search is efficient without a good
heuristic function.
• Recall from Chapter 3 that a heuristic function h(s) estimates the
distance from a state s to the goal and that if we can derive an
admissible heuristic for this distance—one that does not overestimate
—then we can use A∗ search to find optimal solutions. An admissible
heuristic can be derived by defining a relaxed problem that is easier to
solve.
• The exact cost of a solution to this easier problem then becomes the
heuristic for the original problem
PLANNING GRAPH
• A planning problem asks if we can reach a goal state from the initial state.
• Suppose we are given a tree of all possible actions from the initial state to
successor states, and their successors, and so on.
• If we indexed this tree appropriately, we could answer the planning question
“can we reach state G from state S0” immediately, just by looking it up.
• A planning graph is a directed graph organized into levels: first a level S0
for the initial state, consisting of nodes representing each fluent that holds in
S0; then a level A0 consisting of nodes for each ground action that might be
applicable in S0; then alternating levels Si followed by Ai; until we reach a
termination condition.
ONTOLOGICAL ENGINEERING
• In “toy” domains, the choice of representation is not that important; many choices
will work.
• Complex domains such as shopping on the Internet or driving a car in traffic
require more general and flexible representations.
• This chapter shows how to create these representations, concentrating on general
concepts—such as Events, Time, Physical Objects, and Beliefs— that occur in
many different domains.
• Representing these abstract concepts is sometimes called ontological engineering.
• The general framework of concepts is called an upper ontology because of the
convention of drawing graphs with the general concepts at the top and the more
specific concepts below them
CATEGORIES AND OBJECTS
CATEGORY
• The organization of objects into categories is a vital part of knowledge
representation.
• Although interaction with the world takes place at the level of
individual objects, much reasoning takes place at the level of
categories.
• For example, a shopper would normally have the goal of buying a
basketball, rather than a particular basketball such as BB9.
• Categories also serve to make predictions about objects once they are
classified.
Physical composition
• The idea that one object can be part of another is a familiar one.
• One’s nose is part of one’s head, Romania is part of Europe, and this chapter is part of
this book.
• We use the general PartOf relation to say that one thing is part of another.
• Objects can be grouped into PartOf hierarchies, reminiscent of the Subset hierarchy:
• PartOf (Bucharest, Romania)
• PartOf (Romania,EasternEurope)
• PartOf (EasternEurope,Europe)
• PartOf (Europe,Earth) .
• The PartOf relation is transitive and reflexive; that is,
• PartOf (x, y) ∧ PartOf (y, z) ⇒ PartOf (x, z) .
• PartOf (x, x) .
Measurements
• In both scientific and commonsense theories of the world, objects have
height, mass, cost, and so on.
• The values that we assign for these properties are called measures.
Ordinary quantitative measures are quite easy to represent.
• We imagine that the universe includes abstract “measure objects,”
such as the length that is the length of this line segment:
Objects: Things and stuff
• The real world can be seen as consisting of primitive objects (e.g., atomic particles) and
composite objects built from them.
• By reasoning at the level of large objects such as apples and cars, we can overcome the
complexity involved in dealing with vast numbers of primitive objects individually.
• There is, however, a significant portion of reality that seems to defy any obvious
individuation—division into distinct objects.
• We give this portion the generic name stuff.
• For example, suppose I have some butter and an aardvark in front of me.
• I can say there is one aardvark, but there is no obvious number of “butter-objects,”
because any part of a butter-object is also a butter-object, at least until we get to very small
parts indeed.
• This is the major distinction between stuff and things
EVENTs
• The events we have seen so far are what we call discrete events—they
have a definite structure.
• Shankar’s trip has a beginning, middle, and end.
• If interrupted halfway, the event would be something different—it
would not be a trip from San Francisco to Washington, but instead a
trip from San Francisco to somewhere over Kansas.