Predicate Logic
Predicate Logic
Predicate Logic
Predicate logic finds logic from a language, which suggests powerful way of deriving new knowledge from old i.e.
deriving new knowledge from old which is known.
Standard logic symbols: Material implication, Not, Or, And, for all, there exist
Real world facts can be represented in propositional logic written as wff (well formed formulas). Representing
simple facts
Instance & isa relationships: useful form of reasoning and property inheritance. The instance predicate has two
arguments first one is an object and second one is an class to which object belongs. Statement 3 says if a object is a
subclass of Pompeian, it will be also an instance of superclass Roman.
1) instance(Marcus, man)
2) instance(Marcus, Pompeian)
3) x: instance(x, Pompeian) instance(x, Roman)
4) instance(Caesar, ruler)
5) x: instance(x, Roman) loyalto(x, Caesar) hate(x, Caesar)
1
isa predicate used below to show the inheritance.
1) instance(Marcus, man)
2) instance(Marcus, Pompeian)
3) isa(Pompeian, Roman)
4) instance(Caesar, ruler)
5) x: instance(x, Roman) loyalto(x, Caesar) hate(x, Caesar)
Another Example:
1. Marcus was a man. man(Marcus)
2. Marcus was a Pompeian. Pompeian(Marcus)
3. Marcus was born in 40AD. born(Marcus, 40)
4. All men are mortal. x: man(x) mortal(x)
5. All Pompeians died in 79AD x: [Pompeian(x) died(x, 79)
6. Volcano erupted in 79AD erupted(volcano, 79)
7. No mortal lives longer than 150 years x: t1: t2: mortal (x) born(t1) gt(t2-t1, 150) dead(x, t2)
8. It is now 2012
9. Alive means not dead x: t: [alive(x, t) dead(x, t)] [ dead (x, t) alive(x, t)]
10. If someone dies then it is dead at all later time x: t1: t2: died(x, t1) gt(t2, t1) dead(x, t2)
Problem: All Romans who know Marcus either hate Caesar or think that anyone who hates anyone is crazy
wff: x: [Roman(x) know(x, Marcus)] hate(x, Caesar) (y: z: hate(y, z) thinkcrazy(x, y))]
Rule:3 Move all quantifier at the left side without changing their relative order (called prenex normal form)
x: y: z: [Roman(x) know(x, Marcus)] [hate(x, Caesar) (hate(y, z) thinkcrazy(x, y))]
Searching is efficient, much faster process and more accurate, though having several disadvantage
Takes lots of space to store the table that specifies the correct move
One has to do lot of work to enter the table
Very hard to enter all such manual entry w/o any error
To extend the game in 3D we need to start from scratch and to enter 327 entries.
Program-2
Same vector used as before. Value stored 2 indicating blank, 3 indicating X, indicating O.
Moves starts from 1 and end at 9.
Algorithm:
Make2: Returns 5 if centre sq. is blank (board[5] = 2), else returns any blank non-corner square.
Posswin(p): Return 0 if player p cannot win on the next move, else returns number of the sq. that crates the winning
move. Check each row, column and diagonal at a time. If product is 18 (3x3x2), then X can win, else if product is
50(5x5x2) then O can win.
Go(n): Move at n. If turn is odd (X) then board[n] = 3. If turn is even (O) then board[n] = 5. Increment turn by 1.
Comments: Program not efficient in terms of time, but really efficient in terms of space. Not pain to enter the table.
Program-3
Representation of the board 8 3 4 Create a magic square with addition is 15.
1 5 9
6 7 2
Algorithm:
Keep a list of each square a player holds.
If ( 15 sum of two squares <=0 or > 9, then two squares are not-collinear can be ignored
Else if the two squares represents a blank, that produce a win.
Since a player can hold max 4 squares, fewer squares to be examined rather than Program-2 in Posswin(p), so it
will be more faster than the previous.
3
Problems, Problem spaces and search
Describes kind of problem with which AI typically concerned. To build a solution we need 4 steps:
Define the problem. Detailed specification of each state.
Analyze the problem. To find out possible technique.
Represent task knowledge necessary to solve the problem efficiently
Choose best problem solving technique
Chess Board Problem: Chess Playing is not a problem statement only. We need to define initial board position,
all legal moves and board position for a win. Also not only playing legal move rather than play a move for a
possible win.
All possible move can be stored in a table, but requires huge space and not possible to enter all such alternatives.
Though processing is faster but not a real life solution. Better way to do this to write rules for several elements like
White Pawn at Square(row r, column c)
If (Square(r+1, c) = empty and Square(r+2, c) = empty)
Move White Pawn at Square(r+2, c)
This allows general rules defined for each element.
Allows formal definition of a problem to move from one state to another.
Water Jug Problem: Given 2 jugs one of 4lt and another 3lt, without any measuring marker in it. Water
supply is plenty. How we can get 2lt water in 4lt jug.
Problem defined as a pair (x,y), where x water in bigger jug, y water in smaller jug. Possible value
of x = 0, 1, 2, 3, 4 and y = 0, 1, 2, 3. Initial state (0, 0) and goal state is (2, n) for any value of n.
4lt Jug 3 lt jug
To provide formal description of the problem 0 0
Define state space 0 3
Specify one or more state as initial state from all available 3 0
Define one or more goal state 3 3
Specify set of rules 4 2
Production system: A set of rules consisting a pattern at the left side. And an 0 2
Operation to be performed in the rule at the right side. 2 0
One or more knowledge that contains information appropriate for a task
A control strategy that specifies order of the rules to compare
Control Strategy: To find out which rule to apply next
That causes motion towards goal state
To be systematic so that to eliminate arrival of a state several time unnecessarily
Construct a tree with initial state as root. Generate all offspring root. Now for each leaf node generate all
its successor by applying the rules that are appropriate. Use BFS or DFS to find out the tree.
(0,0)
(4,0) (0,3)
Heuristic Search
A search technique that constructs a control structure with the requirement of mobility and systematicity to solve a
hard problem. It no longer guaranteed to find best solution but always gives good solution. Improves efficiency of
search process by sacrificing claims for best solution.
Example is a TSP problem where search time reduced to N2 much less in comparison to n!. TSP can be solved with
nearest neighbor heuristic, where we find local superior each time. Possible example of TSP algorithm
Arbitrarily select a city Select next city by looking at all no visited city and the one closet to the previous city
Repeat until all cities are visited.
Heuristic function is a function that maps from problem state description to a number (weight). Based on the state,
heuristic function gives the weight to find any solution towards the solution. Purpose of heuristic function is to
guide the search process in most profitable direction. More accurate the function at any point, more directly towards
the solution.
Problem Characteristics
Is the problem decomposable into smaller sub problem?
An example to find integral calculus (x2 + x + sin2x)dx
At each steps is checks whether
it is solvable. If not it tries x2 dx x dx sin2x dx
to decompose in smaller
parts by recursive call to x3/3 x2/2 (1-cos2x)/2 dx
itself.
1/2 dx -1/2 cos2x dx
1. Generate a possible solution. For some problems, this means generating a particular point in the problem space.
For others it means generating a path from a start state
2. Test to see if this is actually a solution by comparing the chosen point or the endpoint of the chosen path to the
set of acceptable goal states.
3. If a solution has been found, quit, Otherwise return to step 1.
Hill climbing
Example: To reach to the downtown of a city. Proceed towards the tallest building.
Is a variant of generate-and test in which feedback from the test procedure is used to help the generator decide
which direction to move in search space.
The test function is augmented with a heuristic function that provides an estimate of how close a given state is
to the goal state.
Computation of heuristic function can be done with negligible amount of computation.
Hill climbing is often used when a good heuristic function is available for evaluating states but when no other
useful knowledge is available
7
Simulated Annealing
An alternative to a random-restart hill-climbing when stuck on a local maximum is to do a reverse walk to
escape the local maximum.
In the beginning of the process some downhill moves considered to explore whole space early, so that starting
state could be better.
Attempt to reach minimum rather than maximm. Actually it is a process of descending a valley.
The term simulated annealing derives from the roughly analogous physical process of heating and then slowly
cooling a substance to obtain a strong crystalline structure. Rate at which system cooled down is called
annealing schedule.
The simulated annealing process lowers the temperature by slow stages until the system ``freezes" and no
further changes occur.
Probability of transition to higher energy state is given by function:
P = e E/kt
Where E is the positive change in the energy level, T is the temperature, K is Boltzmann constant.
The algorithm for simulated annealing is slightly different from the simple-hill climbing procedure. The three
differences are:
The annealing schedule must be maintained
Moves to worse states may be accepted
It is good idea to maintain, in addition to the current state, the best state found so far.
1. Evaluate the initial state. If it is also a 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:
E = ( value of current ) ( value of new state)
If the new state is a goal state, then return it and quit.
If it is a goal state but is better than the current state, then make it the current state. Also set
BEST-SO-FAR to this new state.
If it is not better than the current state, then make it the current state with probability p as
defined above. This step is usually implemented by invoking a random number generator to
produce a number in the range [0, 1]. If the number is less than p, then the move is accepted.
Otherwise, do nothing.
c. Revise T as necessary according to the annealing schedule
5. Return BEST-SO-FAR as the answer
OR Graph
It is sometimes important to search graphs so that duplicate paths will not be pursued.
An algorithm to do this will operate by searching a directed graph in which each node represents a point in
problem space.
Each node will contain:
Description of problem state it represents
Indication of how promising it is
Parent link that points back to the best node from which it came
List of nodes that were generated from it
Parent link will make it possible to recover the path to the goal once the goal is found.
The list of successors will make it possible, if a better path is found to an already existing node, to
propagate the improvement down to its successors.
This is called OR-graph, since each of its branches represents an alternative problem solving path
two lists of nodes required
OPEN nodes that have been generated and have had the heuristic function applied to them but
which have not yet been examined. OPEN is actually a priority queue in which the elements with
the highest priority are those with the most promising value of the heuristic function.
CLOSED- nodes that have already been examined. We need to keep these nodes in memory if we
want to search a graph rather than a tree, since whenever a new node is generated, we need to
check whether it has been generated before.
Algorithm
1. Start with OPEN containing just the initial state
2. Until a goal is found or there are no nodes left on OPEN do:
a. Pick the best node on OPEN
b. Generate its successors
c. For each successor do:
i. If it has not been generated before, evaluate it, add it to OPEN, and record its parent.
ii. If it has been generated before, change the parent if this new path is better than the
previous one. In that case, update the cost of getting to this node and to any successors that
this node may already have.
Explanation
It proceeds in steps, expanding one node at each step, until it generates a node that corresponds to a goal
state.
At each step, it picks the most promising of the nodes that have so far been generated but not expanded.
It generates the successors of the chosen node, applies the heuristic function to them, and adds them to the
list of open nodes, after checking to see if any of them have been generated before.
By doing this check, we can guarantee that each node only appears once in the graph, although many nodes
may point to it as a successor.
9
Representing Knowledge using rule
Declarative Representation: Knowledge specified, but how to use the knowledge is not given. Must be augmented
as a program that specifies what is to be done and how. Program consists of several control structure
Procedural Representation: Control information embedded with the knowledge itself. An interpreter can interpret
this knowledge.
Logical negation cannot be presented, i.e. not possible to encode the following logical expression to program
x: dog(x) cat(x)
Instead negation represented implicitly by lack of assertion and returning the rule as failure. If the program asks
following query result will be false because there is no assertion on tom as cat. ?- cat(tom).
Advantage of logic programming: programmer needs to write only the rules and facts, since search engine is built
into the language. Disadvantage is that search control is fixed.
Forward vs Backward reasoning: Search procedure is to discover through problem space from initial state to goal
state. Procedure have two ways:
Forward search : initial to goal state. Initial state is the root of a tree. Generate next level with all rules whose left
side match the root and right side is the new state. Continue until goal state is reached.
Backward search : goal to initial state. Goal state is root of the tree. Generate next level with all rules whose right
side matches the root node. Left side of the rule generate the next level of the tree. Continue until initial state
reached.
The steps from an Integration to the result, it is easy to move in forward direction from start state, where as moving
from a unfamiliar point to home, backward direction is easier. Depending on the problem forward or backward
direction needs to be decided.
Bidirectional search: Start both from forward and backward direction, until two paths meet somewhere in between.
It also may be ineffective if both paths does not meet at all.
Prolog supports backward reasoning, where a query system searches from goal state.
10
Natural Language Processing
Language means communicating with the world in terms of speaking and writing. Processing written language is
easier than processing speech. To process spoken language, we need all facilities of written language with
additional knowledge to handle noise and ambiguities of audio signal.
Entire language processing task is divided in two parts:
Processing written language using lexical, syntactic, semantic knowledge of language
Processing spoken language using all information needed above along with phonology and information to
handle ambiguities.
Problem: English sentence are incomplete descriptions of the information that they are intend to convey
Some dogs are outside Some dogs are on the lawn. Three dogs are on the lawn.
Good side: Language allows speakers to be as vague or precise. Also allows speaker to leave out things they
believe the hearer already know.
Problem: No natural language program can be complete because new words can be generated.
I will google it.
Take an example of this sentence: I want to print Bills .init file.
Morphological Analysis: Individual words are analyzed into their components and non-word tokens such as
punctuation. Objective is to
Pull apart Bill into proper noun Bill and possessive suffix s
Recognize the sequence .init file as an extension that is functioning as an adjective.
Also assign syntactic categories to all words to interpret suffix and prefix. Like prints is the singular form of print.
Syntactic Analysis: Sentences transformed to a structure that shows how words S
relate to each other. This is called parsing and to convert a sentence to a structure.
Here syntax of the language grammar is verified but not the meaning of the NP VP
sentence. Like
Moon move round the earth or The earth moves round the moon. PN V NP
Both are syntactically correct but not semantically.
Based on the rule of grammar a parsed tree is generated. Here parse tree generated Bill printed the NP1
For Bill printed the file.
ADJ N
Top down parsing: Begin with start symbol, apply grammar rule until symbols at the terminal
Of the tree correspond to the components of the sentence
file
Bottom up parsing: Begin with sentence to be parsed, apply rule backward until a single tree
Appears with terminals are the word of the sentence nad top node is the start symbol.
Understanding a sentence having different meaning Have the students who missed the exam
Have used as main verb in an imperative sentence Have the students who missed the exam take it today
Have used as auxiliary verb in interrogative sentence Have the students who missed the exam taken it today?
Four ways to choose actual meaning from multiple interpretation
All Paths: Follow all possible paths to build all possible combination. Ultimately ignored some of the components
on lack of proper words. Like above sentence becomes interrogative when taken appears. But used as imperative
sentence if take appears. Not efficient because some spurious constituents are built and ignored later.
Best path with backtracking: Follow one path at a time and record each point which is required when that path
does not find the goal and needs to be backtracked from some earlier point. Disadvantage is that same constituents
may need to analyze several times. For wrong interpretation of have and backtracked then the students who
missed the exam needs to be analyzed again. Also needs time and space to maintain each state.
Best path and patchup: Follow one path at a time and for any error detected shuffle around the components
already detected. If have taken as auxiliary verb then the students who missed the exam is considered as subject.
If taken appears next path simply continued. If take appears next then have become main verb. Above
sentence becomes subject of the embedded sentence. Subject of the main sentence is filled with you.
Disadvantage is that interaction among the rules of the grammar required.
11
Planning
Components: Choose the best rule to apply, based on the best available heuristic information
Apply the chosen rule to compute new problem state
Detect when solution found
Detect dead end so that they can be abandoned and directed to fruitful direction
Detect when almost correct solution found, employ special technique to get totally correct state
Goal stack Planning
Problem solver uses a single stack containing both goals and operators that is proposed to attain the goal. Also
maintains database of current situation and provision to add, delete operators.
A flat surface with several blocks of same size. They can be stacked upon on another. A robot arm can manipulate
the block one at a time from the top. Few actions are
UNSTACK(A, B) Pickup block A from the top of B. Arm must be empty and no block on top of A
STACK(A, B) Place block A on top of B. Arm must be holding A and top of B is clear
PICKUP(A) Pickup a A from the table and hold it. Arm must be empty and nothing on top of A
PUTDOWN(A) Put A down on the table. The arm must be holding A.
x: HOLDING(x) ARMEMPTY
x: ONTABLE(x) y: ON(x, y)
x: y: ON(y, x) CLEAR(x)
B C B
A C D A D
Start state : ON(B,A) ONTABLE(A) Goal State : ON(C,A) ON(B,D)
ONTABLE(C) ONTABLE(D) ARMEMPTY ONTABLE(A) ONTABLE(D)
ONTABLE(A) and ONTABLE(D) are already true in initial state and in short mentioned as OTAD.
In backward reasoning process, goal stack contains ON(C,A) ON(B,D) OTAD
STACK(C, A) fig-1
In current state ON(C,A) is not true, we check for operator for which it is true. ON(B, D)
We need to stack C on A. So ON(C, A) replaced by STACK(C, A), that yields ON(C,A) ON(B,D) OTAD
CLEAR(A) fig-2
To satisfy STACK(C, A), previous condition needs to be satisfied, which is HOLDING(C)
CLEAR(A) HOLDING(C). So the goal stack is changed to fig 2 CLEAR(A) HOLDING(C)
STACK(C, A)
ON(B, D)
Next check whether CLEAR(A) is true and it is not. Only operator that makes ON(C,A) ON(B,D) OTAD
It true is UNSTACK(B,A), So the goal stack changed as shown fig 3. ON(B, A) fig-3
CLEAR(B)
Now ON(B,A), CLEAR(B) and ARMEMPTY all are satisfied, so they ARMEMPTY
poped from the stack (fig-4). Because of all precondition satisfied so the next ON(B,A) CLEAR(B) ARMEMPTY
element UNSTACK(B,A) is poped up. At this point the database contains UNSTACK(B,A)
HOLDING(C)
CLEAR(A) HOLDING(C)
ONTABLE(A) ONTABLE(C) ONTABLE(D) HOLDING(B) STACK(C, A)
CLEAR(A) ON(B, D)
And the goal stack contains as shown. How we need to satisfy HOLDING(C) ON(C,A) ON(B,D) OTAD
Which will be true when PICKUP(C) or UNSTACK(C,x) is true.fig(5a,5b) HOLDING(C) fig-4
CLEAR(A) HOLDING(C)
STACK(C, A)
ON(B, D)
ON(C,A) ON(B,D) OTAD 12
ONTABLE(C) fig-5a ON(C,x) fig-5b CLEAR(x) fig-6
CLEAR(C) CLEAR(C) HOLDING(C)
ARMEMPTY ARMEMPTY CLEAR(x) HOLDING(C)
ONTABLE(C) CLEAR(C) ARMEMPTY ON(C,x) CLEAR(C) ARMEMPTY STACK(C,x)
PICKUP(C) UNSTACK(C,x) CLEAR(C)
CLEAR(A) HOLDING(C) CLEAR(A) HOLDING(C) ARMEMPTY
STACK(C, A) STACK(C, A) ON(C,x) CLEAR(C) ARMEMPTY
ON(B, D) ON(B, D) UNSTACK(C,x)
ON(C,A) ON(B,D) OTAD ON(C,A) ON(B,D) OTAD CLEAR(A) HOLDING(C)
STACK(C, A)
ON(B, D)
If we consider the second condition is satisfied and to satisfy ON(C,x) we ON(C,A) ON(B,D) OTAD
need to use STACK to put C on x. fig-6.
CLEAR(D) fig-7
HOLDING(B)
If we consider first condition 5a is satisfied, then we get ONTABLE(C)
CLEAR(D) HOLDING(B)
And CLEAR(C) is true, but ARM is not empty because it is now holding STACK(B,D)
B. So we get fig 7. Here CLEAR(D) and HOLDING(B) are true, so ONTABLE(C) CLEAR(C) ARMEMPTY
STACK(B,D) moved B to D. Now the database is PICKUP(C)
CLEAR(A) HOLDING(C)
STACK(C, A)
ONTABLE(A) ONTABLE(C) ONTABLE(D) ON(B,D) ON(B, D)
ARMEMPTY ON(C,A) ON(B,D) OTAD
Now all precondition are satisfied to execute PICKUP(C) and then
STACK(C,A) is also executed and ON(B,D) is already true.
Game Playing
MINIMAX Search Technique
Depth first and depth limited search procedure. Idea is
to start at the current position
use a move generator to generate set of possible successor position
use static evaluation function to find the best one (static function returns large value for a possible win of us
and small value for a possible win of opponent. As an example if heuristic function return value from 10 to
-10, then 10 is a possible win for us and -10 for possible win for opponents.
A
Because of B has maximum value as the successor from A. Next position to move is B.
This is called one ply search where only one successor is considered, that may not give B(8) C(3) D(-2)
accurate solution. So more than one ply is used for several games like chess. Here we
look ahead to see what will happen, after opponents move. A two ply look ahead tree is shown here. Here
opponent move is also considered. It is clear opponent choose one of moves among E, F, G for a move of B.
Heuristic function chooses minimum value for opponents move, so for this case F (-6) will be chosen by the
opponent. Clearly B is not a good choice for my move in two ply search,
because it gives opponents move to -6. Whereas C gives A
Maximizing ply
opponents move -2, which greater than B. The two
ply system is shown here. So my choice will be C, which
gives least value of the opponent. B(-6) C(-2) D(-4)
Minimizing ply
Alpha-Beta cutoff E(9) F(-6) G(0) H(8) I(-2) J(-4) K(-3)
Use Branch and Bound technique to improve the solution, where if a
clearly worse solution can be found that is abandoned. It includes two bounds one for each player, called alpha-beta
pruning. It requires two threshold values, one representing lower value (alpha) and another upper value (beta).
A(>3)
After examining F, opponent is guaranteed to score -5 Maximizing ply
or less at C. But we also know A must be >3, which is
already achieved at B. So the score -5 is less than 3, that
B(3) C(<-5)
way when F is examined, then G can be abandoned.
Minimizing ply
D(3) E(5) F(-5) 13G
Use of two threshold alpha and beta. When sub tree of B
Is searched, it is clear that A can have at least 3. This is the A
Maximizing ply
Alpha value and it is passed while examining C sub tree.
After K is examined, we see I is guaranteed a maximum
Score of 0, which is less than alpha value (3). Also F can B C
have value 0, so other sub tree of I i.e. L need not to be Minimizing ply
examined. D(3) E(5) F G H(4)
Maximizing ply
Now consider how beta can be used. After cutting further
I J(5) M(7) N(8)
Examination of I, J is examined and yielding value 5. So Minimizing ply
F is assigned 5 (max of 0 and 5). Whereas C will get 5 or
less. Now if we expand G and gets value of M is 7, which K(0) L(7)
is greater than beta cutoff 5. Because of C wants minimum
value so G is not chosen, because an alternate move at F gives value 5. So any other branches of G needs not to be
examined.
So at maximizing level, we can rule out a move early if its value is less than cutoff threshold alpha. Also at
minimizing level, we can rule out a move early if its value is more than cutoff threshold beta.
14