Logical Agents
In AI, knowledge-based agents use a process of reasoning over an internal representation of knowledge to decide what actions to take.
Knowledge-Based Agents
The central component of a knowledge-based agent is its knowledge base, or KB. A knowledge base is a set of sentences. Each sentence is expressed in a language called a knowledge representation language and represents some assertion about the world. When the sentence is taken as being given without being derived from other sentences, we call it an axiom.
TELL and ASK are two operations that can add new sentences to the knowledge base. Both operations may involve inference —— that is, deriving new sentences from old.
// Knowledge-Based Agent |
A generic knowledge-based agent. Given a percept, the agent adds the percept to its knowledge base, asks the knowledge base for the best action, and tells the knowledge base that it has in fact taken that action. The agent may initially contain some background knowledge.
MAKE-PERCEPT-SENTENCE
constructs a sentence asserting that the agent perceived the given percept at the given time.MAKE-ACTION-QUERY
constructs a sentence that asks what action should be done at the current time.MAKE-ACTION-SENTENCE
constructs a sentence asserting that the chosen action was executed.- The details of the inference mechanisms are hidden inside
TELL
andASK
.
The Wumpus World
The wumpus world is a cave consisting of rooms connected by passageways.

A typical wumpus world. The agent is in the bottom left corner, facing east (rightward).
Performance measure: +1000 for climbing out of the cave with the gold, –1000 for falling into a pit or being eaten by the wumpus, –1 for each action taken, and –10 for using up the arrow. The game ends either when the agent dies or when the agent climbs out of the cave.
Environment: A 4×4 grid of rooms, with walls surrounding the grid. The agent always starts in the square labeled [1,1], facing to the east. The locations of the gold and the wumpus are chosen randomly, with a uniform distribution, from the squares other than the start square. In addition, each square other than the start can be a pit, with probability 0.2.
Actuators: The agent can move Forward, TurnLeft by 90◦, or TurnRight by 90◦. The agent dies a miserable death if it enters a square containing a pit or a live wumpus. (It is safe, albeit smelly, to enter a square with a dead wumpus.) If an agent tries to move forward and bumps into a wall, then the agent does not move. The action Grab can be used to pick up the gold if it is in the same square as the agent. The action Shoot can be used to fire an arrow in a straight line in the direction the agent is facing. The arrow continues until it either hits (and hence kills) the wumpus or hits a wall. The agent has only one arrow, so only the first Shoot action has any effect. Finally, the action Climb can be used to climb out of the cave, but only from square [1,1].
Sensors: The agent has five sensors, each of which gives a single bit of information:
- In the squares directly (not diagonally) adjacent to the wumpus, the agent will perceive a Stench.
- In the squares directly adjacent to a pit, the agent will perceive a Breeze.
- In the square where the gold is, the agent will perceive a Glitter.
- When an agent walks into a wall, it will perceive a Bump.
- When the wumpus is killed, it emits a woeful Scream that can be perceived anywhere in the cave.
The percepts will be given to the agent program in the form of a list of five symbols; for example, if there is a stench and a breeze, but no glitter, bump, or scream, the agent program will get [Stench, Breeze, None, None, None].
Two later stages in the progress of the agent. (a) After moving to [1,1] and then [1,2], and perceiving [Stench, None, None, None, None]. (b) After moving to [2,2] and then [2,3], and perceiving [Stench, Breeze, Glitter, None, None].
Logic
A logic must also define the semantics, or meaning, of sentences. The semantics defines the truth of each sentence with respect to each possible world. With more precision, the term model is used instead of “possible world”.
If a sentence
Logical entailment is defined as
which means
If an inference algorithm
which is pronounced “
An inference algorithm that derives only entailed sentences is call sound or truth preserving. An inference algorithm is complete if it can derive any sentence that is entailed.

Sentences are physical configurations of the agent, and reasoning is a process of constructing new physical configurations from old ones. Logical reasoning should ensure that the new configurations represent aspects of the world that actually follow from the aspects that the old configurations represent.
The final issue to consider is grounding —— the connection between logical reasoning processes and the real environment in which the agent exists. In particular, how do we know that KB is true in the real world? A simple answer is that the agent’s sensors create the connection.
Propositional Logic: A Very Simple Logic
Syntax
The syntax of propositional logic defines the
allowable sentences. The atomic sentences consist of a
single proposition symbol. Each symbol stands for a
proposition that can be true or false. There are two proposition symbols
with fixed meanings: True
is the always-true proposition
and False
is the always-false proposition.
Complex sentences are constructed from simpler sentences, using parentheses and operators called logical connectives.
(not): A literal is either an atomic sentence (a positive literal) or a negated atomic sentence (a negative literal). (and): A sentence whose main connective is is called a conjunction; its parts are the conjuncts. (or): A sentence whose main connective is is a disjunction; its parts are disjuncts. (implies): Implications are also known as rules or if-then statements. (if and only if): The sentence is biconditional.
Below gives the grammar of propositional logic.
A BNF (Backus–Naur Form) grammar of sentences in propositional logic, along with operator precedences, from highest to lowest.
Semantics
In propositional logic, a model simply set the truth value —— true or false —— for every proposition symbol. The semantics for propositional logic must specify how to compute the truth value of any sentence, given a model.
Rules for evaluating atomic sentences.
True
is true in every model andFalse
is false in every model.- The truth value of every other proposition symbol must be specified directly in the model.
For complex sentences, the following rules hold for subsentences
is true iff is false in . is true iff both and are true in . is true iff either or are true in . is true unless is true and is false in . is true iff and are both true or both false in .
false | false | true | false | false | true | true |
false | true | true | false | true | true | false |
true | false | false | false | true | false | false |
true | true | false | true | true | true | true |
Truth tables for the five logical connectives. To use the table to compute, forexample, the value of P ∨ Q when P is true and Q is false, first look on the left for the row where P is true and Q is false (the third row). Then look in that row under the P ∨ Q column to see the result: true.
A Simple Knowledge Base
For the wumpus world, define the following symbols for each
is true if there is a pit in . is true if there is a wumpus in , dead or alive. is true if there is a breeze in . is true if there is a stench in . is true if the agent is in location .
Sentences in the knowledge base of the wumpus world are something like
There is no pit in
:A square is breezy if and only if there is a pit in a neighboring square.
The breeze percepts for the first two squares visisted in the specific world the agent is in, leading up to the situation in the above figure (b).
A Simple Inference Procedure
The goal now is to decide whether
KB | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
false | false | false | false | false | false | false | true | true | true | true | false | false |
false | false | false | false | false | false | true | true | true | false | true | false | false |
false | true | false | false | false | false | true | true | true | false | true | true | false |
false | true | false | false | false | false | true | true | true | true | true | true | true |
false | true | false | false | false | true | false | true | true | true | true | true | true |
false | true | false | false | false | true | true | true | true | true | true | true | true |
false | true | false | false | true | false | false | true | false | false | true | true | false |
true | true | true | true | true | true | true | false | true | true | false | true | false |
A truth table constructed for the knowledge base given in the text. KB is true if R1 through R5 are true, which occurs in just 3 of the 128 rows (the ones underlined in the right-hand column). In all 3 rows, P1,2 is false, so there is no pit in [1,2]. On the other hand, there might (or might not) be a pit in [2,2].
function TT_Entails( |
A truth-table enumeration algorithm for deciding
propositional entailment. (TT stands for truth table.)
PL-TRUE?
returns true
if a sentence
holds within a model. The variable model represents a partial
model —— an assignment to some of the symbols. The keyword
and here is an infix function symbol in the pseudocode
programming language, not an operator in propositional logic; it takes
two arguments and returns true
or
false
.
Propositional Theorem Proving
Entailment can be done by theorem proving —— applying rules of inference directly to the sentences in our knowledge base to construct a proof of the desired sentence without consulting models. If the number of models is large but the length of the proof is short, then theorem proving can be more efficient than model checking.
Two sentences
A sentence is valid if it is true in all models. For
example, the sentence
Deduction theorem:
Hence, we can decide if
A sentence is satisfiable if it is true in, or satisfied by, some model. Satisfiability can be checked by enumerating the possible models until one is found that satisfies the sentence. The problem of determining the satisfiability of sentences in propositional logic —— the SAT problem —— was the first problem proved to be NP-complete.
Validity and satisfiability are connected:
Proving
Inference and Proofs
Inference rules can be applied to derive a proof —— a chain of conclusions that leads to the desired goal. The best-known rule is called Modus Ponens and is written
which means that, whenever any sentences of the form
Another useful inference rule is And-Elimination, which says that, from a conjunction, any of the conjuncts can be inferred.
Standard logical equivalences are shown as below
The symbols
Any of the search algorithms can be used to find a sequence of steps that constitutes the proof. The proof problem can be defined as:
- Initial State: the initial knowledge base.
- Actions: the set of actions consists of all the inference rules applied to all the sentences that match the top half of the inference rule.
- Result: the result of an action is to add the sentence in the bottom half of the inference rule.
- Goal: the goal is a state that contains the sentence we are trying to prove.
One final property of logical systems is monotonicity, which says
that the set of entailed sentences can only increase as information is
added to the knowledge base. For any sentences
Monotonicity means that inference rules can be applied whenever suitable premises are found in the knowledge base —— the conclusion of the rule must folow regardless of what else is in the knowledge base.
Proof by Resolution
A inference rule, resolution, that can yield a complete inference algorithm when coupled with any complete search algorithm.
The unit resolution inference rule is defined as below:
where each
where
Conjunctive Normal Form
Every sentence of propositional logic (that is, disjunctions of literals) is logically equivalent to a conjunction of clauses. A sentence expressed as a conjunction of clauses is said to be in conjunctive normal form or CNF.
A grammar for conjunctive normal form, Horn clauses, and definite
clauses. A CNF clause such as
Example: converting the sentence
into CNF.
Eliminate
, replacing with :Eliminate
, replacing with :CNF requires
to appear only in literals, so repeatedly using double-negation elimination and De Morgan to move inwards.Then apply the distributivity law to further reduce nested
and . >
A Resolution Algorithm
Inference procedures based on resolution work by using the principle
of proof by contradiction. That is, to show that
First,
- there are no new clauses that can be added, in which case
does not entail . - two clauses resolve to yield the empty clause, in which case
entails .
function PL_Resolution(KB: KnowledgeBase, a: Query): boolean { |
PL-RESOLVE
returns the set of all possible clauses
obtained by resolving its two inputs.

Completeness of Resolution
The algorithm PL-RESOLUTION
is complete. Define
resolution closure PL-RESOLUTION
computes as the final value of the variable
clauses. Thanks to the factoring step, there are only finitely many
distinct clauses that can be constructed out of the symbols PL-RESOLUTION
always terminates.
The completeness theorem for resolution in propositional logic is called the ground resolution theorem:
Horn Clauses and Definite Clauses
Definite clause is a disjunction of literals of
which exactly one is positive. 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. Goal
clauses are those with no positive literals.
Knowledge bases containing only definite clauses are interesting for three reasons:
- Every definite clause can be written as an implication whose premise
is a conjunction of positive literals and whose conclusion is a single
positive literal. For example, the definite clause
can be written as the implication . In Horn form, the premise is called the body and the conclusion is called the head. A sentence consisting of a single positive literal is called a fact. - Inference with Horn clauses can be done throught the forward-chaining and backward-chaining.
- Deciding entailment with Horn clauses can be done in time that is linear in the size of the knowledge base.
Forward and Backward Chaining
The forward-chaining algorithm PL-FC-ENTAILS?(KB, q)
determines if a single proposition symbol
// Forward-chaining Algorithm |
The forward-chaining algorithm for propositional logic. The
queue
keeps track of symbols known to be true but
not yet “processed.” The count
table keeps track
of how many premises of each implication are not yet proven. Whenever a
new symbol p
from the agenda is processed, the
count is reduced by one for each implication in whose premise
p
appears (easily identified in constant time with
appropriate indexing.) If a count reaches zero, all the premises of the
implication are known, so its conclusion can be added to the agenda.
Finally, we need to keep track of which symbols have been processed; a
symbol that is already in the set of inferred symbols need not be added
to the agenda again. This avoids redundant work and prevents loops
caused by implications such as

- A set of Horn clauses. (b) The corresponding AND–OR graph.
Forward chaining is an example of the general concept of data-driven reasoning —— that is, reasoning in which the focus of attention starts with the known data. It can be used within an agent to derive conclusions from incoming percepts, often without a specific query in mind.
Effective Propositional Model Checking
A Complete Backtracking Algorithm
DPLL takes an input a sentence in conjunctive normal form —— a set of
clauses. It embodies three improvements over the simple scheme of
TT-ENTAILS?
.
- Early termination: The algorithm detects whether the sentence must be true or false, even with a partially completed model.
- Pure symbol heuristic: A pure symbol is a symbol that always appears with the same “sign” in all clauses.
- Unit clause heuristic: A unit clause was defined earlier as a clause with just one literal. In the context of DPLL, it also means clauses in which all literals but one are already assigned false by the model.
// Forward-chaining Algorithm |
Agents based on Propositional Logic
The Current State of the World
The knowledge base is composed of axioms —— general knowledge about how the world works —— and percept sentences obtained from the agent’s experience in a particular world.
For each square, it knows that the square is breezy if and only if a neighboring square has a pit; and a square is smelly if and only if a neighboring square has a wumpus. In such a way, a large collection of axioms can be included.
The agent also knows that there is exactly one wumpus. First, we have to say that there is at least one wumpus.
Then we have to say that there is at most one wumpus.
The idea of associating propositions with time steps extends to any
aspect of the world that changes over time. For example, the initial
knowledge base includes
We can connect stench and breeze percepts directly to the properties
of the squares where they are experienced as follows. For any time step
Then we need axioms that allow the agent to keep track of fluents
such as
We would need one such sentence for each possible time step, for each
of the 16 squares, and each of the four orientations. We would also need
similar sentences for the other actions:
There is some information that remains unchanged as the result of an
action. The need to do this gives rise to the frame
problem. One possible solution to the frame problem would be to
add frame axioms explicitly asserting all propositions that remain the
same. For example, for each time
In a world with
For each fluent
The successor-state axiom for
For the agent’s location, the successor-state axioms are more elaborate.
The most important question for the agent is whether a square is OK to move into —— that is, whether the square is free of a pit or live wumpus. It’s coinvenient to add axioms for this
So finally, the agent can move into any square where
A hybrid agent
The agent program maintains and updates a knowledge base as well as a
current plan. The initial knowledge base contains atemporal axioms ——
those that don’t depend on

The main body of the agent program constructs a plan based on a decreasing priority of goals. First, if there is a glitter, the program constructs a plan to grab the gold, follow a route back to the initial location, and climb out of the cave. Otherwise, if there is no current plan, the program plans a route to the closest safe square that it has not visited yet, making sure the route goes through only safe squares.