You are on page 1of 75

Save from: www.uotiq.

org/dep-cs

Artificial Intelligence language


2nd Class
‫ إﺳﺮاء ﻋﺒﺪ اﻷﻣﻴﺮ‬.‫ م‬.‫ م‬:‫أﺳﺘﺎذة اﻟﻤﺎدة‬
References: 
1‐ Elin Rich, “Artificial Intelligence”,1989. 
2‐ William A. Stubblefield & Luger E.George,”Artificial 
Intelligence  and  the  Design  of  Expert  Systems”, 
1998. 
 
Artificial Intelligence (A.I)

Artificial Intelligence (A.I) :- Is the branch of computer science that concerned with the
automation of intelligence behaviour.

A.I :- Is simply a way of making a computer think.

A.I:- Is the part of computer science concerned with designing intelligent computer system that
exhibit the character static associate with intelligent in human behavior. This require many
process:-
1- learning :- acquiring the knowledge and rules that used these knowledge.
2- Reasoning:- Used the previous rules to access to nearly reasoning or fixed reasoning.

A.I Principles:-
1- The data structures used in knowledge representation.
2- the algorithms needed to apply that knowledge.
3- the language and programming techniques used their implementation.

A.I Application:-
1- Game application.
2-Automated reasoning and theorm proving.
3-Perception.
4-Expert Systems
5- Natural language understanding and semantic modeling.
6- planning and robotics
machine learning
language & environment for A.I
Pattern recognition
A.I & Philosophy.

A.I Branches:-
1- Logical A.I
2-Search
3- Representation
4-Inference
5-knowledge & Reasoning
6- Planning
7- Epistemology.
8- Ontology.
9- Heuristics.
10-Genetig Programming.
Search Algorithms:
To successfully design and implement search algorithms, a programmer must be able to
analyze and predict their behavior.
Many questions needed to be answered by the algorithm these include:
- is the problem solver guranteed to find a solution?
- Will the problem solver always terminate , or can it become caught in an infinite loop?
- When a solution is found , is it guaranteed to be optimal?
- -What is the complexity of the search process in terms of time usage ? space search?
- How can the interpreter be designed to most effectively utilize a representation language?

-State Space Search


The theory of state space search is our primary tool for answering these questions , by
representing a problem as state space graph, we can use graph theory to analyze the structure
and complexity of both the problem and procedures used to solve it.

- Graph Theory:-
A graph consist of a set of a nodes and a set of arcs or links connecting pairs of nodes. The
domain of state space search , the nodes are interpreted to be stated in problem_solving
process, and the arcs are taken to be transitions between states.

Graph theory is our best tool for reasoning about the structure of objects and relations.

e
c

d
a
b

Nodes={a,b,c,d,e}
Arcs={(a,b), (a,d),(b,c),(c,b),(d,e),(e,c),(e,d)}
a

b d
c

e f g h i j

Nodes=={a,b,c,d,e,f,g,h,i}
Arcs={(a,b),(a,c),(a,d),(b,e),(b,f),(c,f),(c,g),(c,h),(c,i),(d,j)}

State Space Representation of Problems:-


A state space is represented by four_tuple [N,A,S,G,D], where:-
•N is a set of nodes or states of the graph. These correspond to the states in a problem –
solving process.
• A is the set of arcs between the nodes. These correspond to the steps in a problem –
solving process.
• S a nonempty subset of N ,contains the start state of the problem.
• GD a nonempty subset of N ,contains the goal state of the problem.
A solution path:- Is a path through this graph from a node S to a node in GD.

Example :- Traveling Saleman Problem


Starting at A , find the shortest path through all the cities , visiting each city exactly
once returning to A.
a
100
75
100

125 b
300
e 125
50

50
c
100
d

“An instance of traveling Saleman Problem”

The complexity of exhaustive search in the traveling Saleman is (N-1)!, where N is the No.
of cities in the graph. There are several technique that reduce the search complexity.

1- Branch and Bound Algorithm :-Generate one path at a time , keeping track of the best circute
so far. Use the best circuit so far as a bound of future branches of the search .
figure below illustrate branch and bound algoritm.
c

a
100 75
b
150 125 e
100
c

c d
250

d e c e
300

e d
e
375
a a a

a b c d e a=375 a b c e d a =425 a b d c e a=474 ………………

2- Nearest Nieghbor Heuristic: At each stage of the circuit , go to the nearest unvisited city. This
strategy reduces the complexity to N, so it is highly efficient , but it is not guranteed to find the
shortest path , as the following example:

a
100
75
100

125 b
300
e 125
50

50
c
100
d

Cost of Nearest neighbor path is a e d b c a=550


Is not the shortest path , the comparatively high cost of arc (C,A) defeated the heuristic.
State Space Searchs examples:-
1) Monkey and Banana Problem
There is a monkey at the door in to a room. In the middle of the room a banana is
hunging from the ceiling. The monkey is hungry and wants to get the banana , but he cannot
stretch high enough from the floor. At the window of the room there is a box the monkey
may use.
The monkey can perform the following actions:-

•walk on the floor


• Climb the box
• Push the box a round (if it is already at the box).
• Grasp the banana if standing on the box directly under the banana.
The question is (Can the monkey get the banana?), the initial state of the world is
setermind by:-
1- Monkey is at door.
2- Monkey is on floor.
3- Box is at Window.
4- Monkey does not have banana
Initial state :- State (at door, on floor, at window, has not).
At door horizontal position of monkey
On floor vertical position of monkey
At window Position of box
Has not monkey has not banana
Goal state:-State (_,_,_,has).
State1 state2
Move (state1, move, state2).
State1: is the state before the move.
Move: is the move executed.
State2:is the state after the move.
To answer the question :- Can the monkey in some initial state (state) get the banana?
This can be formulated as a predicate canget(state).The program canget can be based on
two observation:-
1) The program:- for any state in which the monkey already has the banana. The predicate
canget must certainly be true , no move is needed in this case:
Canget(state( state(_,_,_,has)).

2) In other cases one or more moves are necessary.


Canget (state):-move (state1,move,state2),canget (state2).

A program of monkey and banana problem:-


Move (state (at door , on floor , at window , has not), walk, state (at box , on floor , at
window , has not)).
Move (state (at box , on floor , at window , has not), push , state (middle, on floor, middle,
has not)).
Move (state (middle, on floor, middle, has not), climb, state (middle, on box, middle, has
not)).
Move (state (middle, on box, middle, has not),grasp, state (middle, on box, middle, has
not)).
Canget( state(_,_,_,has)).
Canget (State1):- move (state1,move,state2), canget (state2).

Goal= canget (state(at door, on floor, at window ,has not)).


• The monkey and banana problem can be represented by the following state space:-
state (at door , on floor , at
window , has not)

Walk(at door, at box)

state (at box , on floor , at


window , has not)
climb Push ( at box , middle)

State(at window, on box,


at window, has not).
, state (middle, on floor, middle,
has not)

No move possible state (middle, on box, middle,


has not)

state (middle, on box, middle,


has not)

2) The Farmer , Wolf, Goat and Cabbage Problem:-


A farmer wants to move himself , a wolf , a goat and some cabbage across a river.
Unfortunately his boat is soting , the farmer can take only one of his possession across any trip
worse yet, an attended wolf will eat a goat, and and attended gaot will eat cabbage , so the
farmer not leave the wolf alone with goat or the goat alone with the cabbage. What he is to do?
F
State(F,W,G,C) W Initial state
G
State(w, w ,w ,w) C

W F
C G
State(e, w , e ,w)

F
W G
State(w,w,e,w) C

W F C F
C State(e,w,e,e) State(e,e,e,e) G
G W

F
W C State(w,w,w,e) state(w,e,w,w) F
G C W
G
F
G W
State(e,e,w,e) C

F W
G C
state(w,e,w,e)

F
state(e,e,e,e) W
G Goal state
C
The following move rule operates only when the farmer and wolf are in the same location
and takes them to the opposite side of the river. Note that the goat and cabbage do not change their
present location:-

Move(state(X,X,G,C),state(Y,Y,G,C)):-opp(X,Y).
X opposite (opp) the value of Y

Opp (e , w).
Opp (w, e).
A predicate must be created to test whether each new state is safe , so that nothing is eaten
in the process of getting across the river. These unsafe situations may be represented with the
following rules:-

Unsafe (state(X,X,Y,Y)):-opp(X,Y).
Unsafe(state(X,W,Y,Y)):-opp(X,Y).

Now, a not unsafe test must be added to move rule:-


Move(state (X,X,G,C),state(Y,Y,G,C)):-opp(X,Y), not (unsafe (state (Y,Y,G,C))).

A program of FWGC problem:-


domains
s=symbol.
state=st(s,s,s,s).
L=state*.
H=symbol*.
predicates
move(state,state).
opp(s,s).
unsafe(state).
path(state,state,L).
member(state,L).
writelist(H).
rpt(L).
print(s).
clauses
move(st(X,X,G,C),st(Y,Y,G,C)):-opp(X,Y),not(unsafe(st(Y,Y,G,C))),writelist(["try farmer
takes wolf",Y,Y,G,C]).

move(st(X,W,X,C),st(Y,W,Y,C)):-opp(X,Y),not(unsafe(st(Y,W,Y,C))),writelist(["try farmer
takes goat",Y,W,Y,C]).

move(st(X,W,G,X),st(Y,W,G,Y)):-opp(X,Y),not(unsafe(st(Y,W,G,Y))),writelist(["try farmer
takes cabbage",Y,W,G,Y]).

move(st(X,W,G,C),st(Y,W,G,C)):-opp(X,Y),not(unsafe(st(Y,W,G,C))),writelist(["farmer
takes self",Y,W,G,C]).
move(st(F,W,G,C),st(f,w,g,c)):-writelist([" BACKTRACK from:",F,W,G,C]),FAIL.
path(Z,Z,L):-write("solution path is:"),rpt(L).
path(X,Y,L):-move(X,Z),not(member(Z,L)),path(Z,Y,[Z|L]).

member(X,[X|_]).
member(X,[_|T]):-member(X,T).
writelist([]):-nl.
writelist([H|T]):-print(H),writelist(T).
rpt([]).
rpt([H|T]):-rpt(T),write(H),nl.
opp(e,w).
opp(w,e).
unsafe(st(X,Y,Y,_)):-opp(X,Y).
unsafe(st(X,_,Y,Y)):-opp(X,Y).
print(A):-write(A).

goal= path(state(w,w,w,w),state(e,e,e,e),[state(w,w,w,w)]).

3) A water Jug Problem


You are give two jugs , a 4-gallon one and a 3-gallon one. Neither has any measuring
markers on it. There is a pump that can be used to fillthe jugs with water. How can you get
exactly 2 gallons of water in to the 4-gallon jug?
The state space for this problem can be described as the set of ordered pairs of integers (x,y) ,
such that x=0,1,2,3 or 4 and y=0,1,2, or 3; x represent the number of gallons of a water in the 4-
gallon jug , and y represents the quantity of water in 3-gallon jug. The start state is (0,0). The
goal state is (2,n) for any value of n ( since the problem does not specify how many gallons need
to be in the 3-gallon jug).

1) (X,Y: X<4) (4,Y) Fill the 4 – gallon jug


2) (X,Y: Y<3) (X,3) Fill the 3-gallon jug
3) (X,Y:X>0) (X-D,Y) Pour some water out of the 4- gallon jug
4) (X,Y:X>0) (X,Y-D) Pour some water out of the 3- gallon jug
5) (X,Y:X>0) (0,Y) Empty the 4-gallon jug on the ground
6) (X,Y: Y>0) (X,0) Empty the 3-gallon jug on the ground
7) (X,Y: X+Y>=4 ^ Y>0) (4,Y-(4-X)) pour water from the 3-gallon jug into the 4-gallon
jug until the 4-gallon jug is full.
8) (X,Y:X+Y<=4 ^ X>0) (X-(3-Y),3) pour water from the 4-gallon jug into the 3-
gallon jug until the 3-gallon jug is full.
9) (X,Y:X+Y<=4 ^ Y>0) (X+Y,0) pour all the water from 3-gallon jug into the 4-
gallon jug.
10) (X,Y: X+Y<=3 ^ X>0) (0,X+Y) pour all the water from 4-gallon jug into the 3-
gallon jug.

(0,0)

0,3 4,0

0,0 3,0 4,3 4,3 1,3 0,0

0,3 4,0
0,0 0,3
3,3 1,0

0,3 3,0 1,3 0,0


4,2 0,1

1,0
4,0 3,3 0,0 4,1
0,2

4,0 0,1
4,2 0,0 2,3
2,0
The goal
The goal

The solutions of water jug problem


Search Strategies

1) Data – Driven and Goal Driven Search (Reasoning Search):-


In data –driven search , sometimes called forward chaining (F.W) , the problem solver begins with
the give facts and a set of rules for changing the state. Search proceeds by apply rules to facts to
produce new facts . This process continues until it generates a path that satisfies the goal.
In Goal-Driven search , sometimes called backward chaining (B.W) , the problem solver
begins with the goal to be used to generate this goal and determine what conditions must be true to
use them. These conditions become the new goals, sub goals , for the search. This process
continues , working backward through successive sub goals, until a path is generated that lead
back to facts of the problem.

Example of Data Driven Search (F.W)


Using (F.W) to find if the goal w is true or false

a. b. c. d.
w:-r,z.
r:-a,b.
z:-c,d.

sol/
a. b. c. d. r. z. w. the goal is true
1) w:-r,z. 6
2) r:-a,b. 4
3) z:-c,d. 5
4)r:-b.
5) z:-d.
6)w:-z.
‫ﻣﻼﺣﻈﺎت ﻋﻦ ‪F.W‬‬

‫‪ (١‬ﻧﺒﺪأ ﺑﺎﻟﺤﻘﺎﺋﻖ )‪ (Facts‬ﺑﺎﻟﺒﺤﺚ ﻋﻨﻬﺎ ﻓﻲ اﻟﻘﻮاﻋﺪ)‪ (Rules‬ﻓﻲ ﺟﻬﺔ اﻟﻴﻤﻴﻦ ﻓﺄذا آﺎﻧﺖ ﻣﻮﺟﻮدة ﺗﺤﺬف ﻣﻦ اﻟﻄﺮف اﻟﻴﻤﻴﻦ‪-:‬‬
‫‪ (٢‬اذا أﺻﺒﺤﺖ ال‪ rule‬ﻓﻲ اﻟﻄﺮف اﻷﻳﻤﻦ ﻓﺎرﻏﺔ ﺗﺘﺤﻮل ال ‪ rule‬اﻟﻰ ‪ fact‬واﻻ ﻓﺘﻀﺎف ‪ rule‬ﺟﺪﻳﺪة ﺑﻌﺪ ﻋﻤﻠﻴﺔ اﻟﺤﺬف‪.‬‬
‫‪ (٣‬ﺑﻌﺪ ﺗﺄﺷﻴﺮ آﻞ ‪ facts‬إذا آﺎن اﻟﻬﺪف ﻣﻮﺟﻮد ﺿﻤﻦ ال‪ facts‬ﻓﻬﻮ ‪ true‬واﻻ ﻓﻬﻮ ‪.false‬‬

‫)‪Example of Goal Driven Search (B.W‬‬


‫‪Try the previous facts & rules to prove if (w) is true or false.‬‬
‫‪a.‬‬ ‫‪b.‬‬ ‫‪c.‬‬ ‫‪d.‬‬ ‫‪z.‬‬ ‫‪r.‬‬ ‫‪w. the goal is true‬‬
‫‪1) w:-r,z.‬‬ ‫‪6‬‬
‫‪2)z:-a,b.‬‬ ‫‪4‬‬
‫‪3)r:-c,d.‬‬ ‫‪5‬‬
‫‪4)r:-b.‬‬
‫‪5) r:-d.‬‬
‫‪6)w:-r.‬‬

‫ﻣﻼﺣﻈﺎت ﻋﻦ ‪F.W‬‬
‫‪ (١‬ﻧﺒﺪأ ﺑﺎﻟﻬﺪف وذﻟﻚ ﺑﺎﻟﺒﺤﺚ ﻋﻨﻪ ﻓﻲ ال ‪ facts‬إذا آﺎن ﻣﻮﺟﻮد ﻓﻬﻮ ‪ true‬وأﻻ ﻓﻬﻮ ‪.False‬‬
‫‪ (٢‬ﻧﺒﺤﺚ ﻋﻦ اﻟﻬﺪف ﻓﻲ ‪ Rules‬ﻓﻲ ﺟﻬﺔ اﻟﻴﺴﺎر ﻓﻲ ﺣﺎﻟﺔ وﺟﻮدﻩ ﻧﺤﺎول إﺛﺒﺎت اﻟﻄﺮف اﻷﻳﻤﻦ ‪.True‬‬

‫‪Example: Try the following facts & Rules with (F.W) & (B.W) Chaining.‬‬
‫‪a(1). B.‬‬ ‫‪c.‬‬ ‫‪d(1).‬‬
‫‪W:-r(x),z(x).‬‬
‫‪R(w):-a(w),b.‬‬
‫‪Z(v),c.‬‬
Sol/
a(1). B. c. d(1). r(1). Z(1). W. the goal is true
1) w:-r(x),z(x). 6

2) r (w):-a(w),b. 4

3) z (v):d(v),c. 5

4) r (1):-b.
5) z(v):-d(v).
6) w:-z(1).

Sol/ B.W Chaining


a(1). B. c. d(1). z (1). r(1). W. the goal is true
1) w:-z(x),r(x). 6

2) r(w):-a(w),b. 4

3) z(v):-d(v),c. 5

4) z(1):-c.
5) r(1):-b.
6) w:-r(1).

H.W/ Using B.W & F.W chaining to reasoning that the goal (Z) is true or not.
a(1). b(2). c(3). d(1). e.
r:-a(x),b(y).
z:-e, not (f),not(b(3)),w.
w:-c(z),d(l),not (a(3)),r.
2) Backtracking Search Algorithm
Backtracking search begins at the start state and pursues a path until it reaches a goal or "dead
end", if it reaches a goal , it returns the solution path and quits. If it reaches a dead end , it
backtracks to the most recent node in the path having unexamined siblings and continues down
on of those branches.
The backtrack algorithm uses three lists plus one variable:
SL, The state list, lists the states in the current path being tried , if a goal is found , SL contains
the ordered list of states on the solution path.

NSL, The new state list, contains nodes a waiting evaluation, nodes whose descendants have
not yet been generated and searched.

DE, dead ends, lists states whose descendants have failed to contain a goal end. If these states
are encountered again, they will be immediately eliminated from consideration.

CS, The current state.

The backtrack Algorithm


{
SL:=[start]; NSL:=[start]; DE:=[ ]; CS:=start;
While NSL!=[]
{
IF CS=goal (or meets goal description)
Return SL; //SUCCESS//
IF CS has no children ("except on DE, SL, NSL).
{
WHILE SL!= [] and CS=first element of SL
{
Add CS to DE; //Dead end//
Remove first element of SL; //Backtrack//
Remove first element of NSL;
CS:=first element of NSL;
}
Add CS to SL;
}
Place children of CS (except those on DE, SL,NSL) on NSL
CS:= first element of NSL;
Add CS to SL;
}
} [End algorithm]
Return fail; //failure//

Example: A trace of backtrack Algorithm

B C D

E G
F

H I J

Iteration CS SL NSL DE
0 A [A] [A] []
1 B [B A] [B C D A] []
2 E [E B A] [E F B C D A] []
3 H [H E B A] [H I E F B C D A] []
4 I [I E B A] [I E F B C D A] [H]
5 F [F B A] [F B C D A] [E I H]
6 J [J F B A] [J F B C D A] [E I H]
7 C [C A] [C D A] [B F J E I H]
8 G [G C A] [G C D A] [B F J E I H]
Blind Search
This type of search takes all nodes of tree in specific order until it reaches to goal. The order can
be in breath and the strategy will be called breadth – first – search, or in depth and the strategy will
be called depth first search.

Breadth – First – Search


In breadth –first search , when a state is examined , all of its siblings are examined before
any of its children. The space is searched level-by-level , proceeding all the way across one level
before doing down to the next level.

Breadth – first – search Algorithm


begin
open: = [start];
closed: = [ ];
while open ≠ [ ] do
begin
remove left most state from open, call it x;
if x is a goal the return (success)
else
begin
generate children of x;
put x on closed;
eliminate children of x on open or closed;
put remaining children on right end of open
end
end
return (failure)
end.
A

B C D

E F G H F J

K L M NO P Q R

Y
Fig (2 – 1): Breadth – first – search
1 – Open = [A]; closed = [ ].
2 – Open = [B, C, D]; closed = [A].
3 – Open = [C, D, E, F]; closed = [B, A].
4 – Open = [D, E, F, G, H]; closed = [C, B, A].
5 – Open = [E, F, G, H, I, J]; closed = [D, C, B, A].
6 – Open = [F, G, H, I, J, K, L]; closed = [E, D, C, B, A].
7 – Open = [G, H, I, J, K, L, M]; closed = [F, E, D, C, B, A].
8 – Open = [H, I, J, K, L, M, N,]; closed = [G, F, E, D, C, B, A].
9 – and so on until either U is found or open = [ ].

Depth – first – search


In depth – first – search, when a state is examined, all of its children and their descendants are
examined before any of its siblings.
Depth – first search goes deeper in to the search space when ever this is possible only when no
further descendants of a state cam found owe its
Depth – first – search Algorithm

Begin
Open: = [start];
Closed: = [ ];
While open ≠ [ ] do
Remove leftmost state from open, callitx;
Ifxis agoal then return (success)
Else begin

Geerate children of x;
Put x on closed;
Eliminate children of x on open or closed; put remaining children on left end of open end
End;
Return (failure)
End.

B C D

E F G H I J

K L M N O P Q R

S T U
Fig (2 – 2) Depth first search
1 – Open = [A]; closed = [ ].
2 – Open = [B, C, D]; closed = [A].
3 – Open = [E, F, C, D]; closed = [B, A].
4 – Open = [K, L, F, , D]; closed = [E, B, A].
5 – Open = [S, L, F, C, D]; closed = [K, E, B, A].
6 – Open = [L, F, C, D]; closed = [S, K, E, B, A].
7 – Open = [T, F, C, D]; closed = [L, S, K, E, B, A].
8 – Open = [F, C, D,]; closed = [T, L, S, K, E, B, A].
9 – Open = [M, C, D] as L is already on; closed = [F, T, L, S, K, E, B, A].
Heuristic Search
A heuristic is a method that might not always find the best solution but is guaranteed to find
a good solution in reasonable time. By sacrificing completeness it increase efficiency. Heuristic
search is useful in solving problems which:-

• Could not be solved any other way.


• Solution take an infinite time or very long time to compute.
• Heuristic search methods generate and test algorithm , from these methods are:-
1- Hill Climbing.
2- Best-First Search.
3- A and A* algorithm.

1) Hill Climbing
The idea here is that , you don’t keep the big list of states around you just keep track of the one state you
are considering , and the path that got you there from the initial state. At every state you choose the state
leads you closer to the goal (according to the heuristic estimate ), and continue from there.
The name “Hill Climbing” comes from the idea that you are trying to find the top of a hill , and you go in
the direction that is up from wherever you are. This technique often works, but since it only uses local
information.

The smaller peak is an example of a “local maxima” in a domain (or “local minima”). Hill
climbing search works well (and is fast ,takes a little memory) if an accurate heuristic measure is
available in the domain , and there are now local maxima.
Hill Climbing Algorithm

Begin
Cs=start state;
Open=[start];
Stop=false;
Path=[start];
While (not stop) do
{
if (cs=goal) then
return (path);
generate all children of cs and put it into open
if (open=[]) then
stop=true
else
{
x:= cs;
for each state in open do
{
compute the heuristic value of y (h(y));
if y is better than x then
x=y
}
if x is better than cs then
cs=x
else
stop =true;
}
}
return failure;
}
A trace of Hill Climbing Algorithm

Searches for R4 (local maxima)

B2 C3 D1

E3 L4 G4 F2
Q5 H1 P7

N5 M4
T5 R4 U7 O6

R4 S4 R4

Open Close X
A _ A
C3 B2 D1 A C3
G4 F2 A C3 G4
N5 M4 A C3 N5
R4 S4 A C3 G4 N5 R4

The solution path is : A-C3-G4-N5


Hill climbing Problems:-
Hill climbing may fail due to one or more of the following reasons:-
1- a local maxima: Is a state that is better than all of its neighbors but is not better than some
other states.
A
‫إﺗﺒﺎع أﻋﻠﻰ‬
‫آﻠﻔﺔ ﺗﺆدي إﻟﻰ‬
E 25 ‫ وﻟﻴﺲ إﻟﻰ‬Z
B 10 C 20 D 15 ‫ وهﻮ اﻟﻬﺪف‬X
‫اﻟﻤﻄﻠﻮب‬
‫اﻟﻮﺻﻮل إﻟﻴﻪ‬

J 20 K 20
L 15
F 10 G 30 I 15

M 20 N 35
P 15 S 15
O 20 L 25

R 10 X 15 Y 25
Z 20
Goal state

2- A Plateau: Is a flat area of the search space in which a number of states have the same best
value , on plateau its not possible to determine the best direction in which to move.

A ‫اﻟﺴﻄﻮح‬
‫اﻟﻤﺴﺘﻮﻳﺔ‬

B 20 C 20 D 20 E 20

3- A ridge: Is an area of the search space that is higher than surrounding areas , but that can
not be traversed by a single move in any one direction.
A

E 25
B 10 C 20 D 15

J 20 H 15
F 10 G 20 H 25 I 10

‫ﺳﻠﺴﻠﺔ اﻻﻧﺨﻔﺎﺿﺎت و اﻻرﺗﻔﺎﻋﺎت‬

2) Best-First-Search
{
Open:=[start];
Closed:=[];
While open ≠ [] do
{
Remove the leftmost from open, call it x;
If x= goal then
Return the path from start to x
Else
{
Generate children of x;
For each child of x do
Do case
The child is not already on open or closed;
{ assign a heuristic value to the child state ;
Add the child state to open;
}
The child is already on open:
If the child was reached along a shorter path than the state currently on open then give the
state on open this shorter path value

The child is already on closed:


If the child was reached along a shoreter path than tha state currently on open then {
Give the state on closed this shorter path value
Move this state from closed to open
}
}
Put x on closed;
Re-order state on open according to heuristic (best value first)
}
Return (failure);
}
A

B4 C4 D6

E5 F5 G4 H3
I J

O2 R4
N
T5 R4 P3 Q

open closed
[A5] []
[B4 C4 D6] [A5]
[C4 E5 F5 D6] [B4 A5]
[H3 G4 E5 F5 D6] [C4 B4 A5]
[O2 P3 G4 E5 F5 D6] H3 C4 B4 A5]
P3 G4 E5 F5 D6] [O2 H3 C4 B4 A5]
The solution path is: A5 - B4 - C4 - H3 – O2- P3
8_PUZZLE AS APPLICATION OF (BFS)
‫‪Implementing Heuristic Evaluation Function:-‬‬

‫ﻣﻦ ﻣﺴﺎوئ ﺧﻮارزﻣﻴﺔ اﻟﺒﺤﺚ ﻋﻦ اﻻﻓﻀﻞ )‪ (Best – First – Search‬اﻧﻬﺎ ﺗﺮآﺰ ﻋﻤﻠﻴﺔ اﻟﺒﺤﺚ ﻋﻠﻰ ﻓﺮع ﻓﻲ ﻓﻀﺎء اﻟﺒﺤﺚ ﻋﻠﻰ‬
‫ﺣﺴﺎب ﺑﻘﻴﺔ اﻻﻓﺮع ‪ .‬وﻳﺤﺪث ذﻟﻚ اذا اﺳﺘﻤﺮ اﻻﻗﺘﺮان اﻻﺟﺘﻬﺎدي ) آﻠﻔﺔ ال ‪ (node‬ﻓﻲ اﻋﻄﺎء آﻠﻒ او ﻗﻴﻢ اﺟﺘﻬﺎدﻳﺔ ﺟﻴﺪة ﻟﻠﺤﺎﻻت‬
‫اﻟﻤﻮﺟﻮدة ﻋﻠﻰ ﻓﺮع ﻣﺎ‪ .‬وﻗﺪ ﻳﻜﻮن ذﻟﻚ ﺳﻴﺌﺎ ﻟﻌﺪم وﺟﻮد اﻟﺤﺎﻟﺔ اﻟﻬﺪﻓﻴﺔ ﻋﻠﻰ ذﻟﻚ اﻟﻔﺮع ﻣﻦ ﻓﻀﺎء اﻟﺒﺤﺚ‪ .‬او ﺑﺴﺒﺐ وﺟﻮد ﺣﺎﻟﺔ‬
‫هﺬﻓﻴﺔ اﻗﺮب ) ذات ﻣﺴﺎر اﻗﺼﺮ ( ﻋﻠﻰ ﻓﺮع اﺧﺮ‪.‬‬
‫وﻟﺘﺠﻨﺐ هﺬﻩ اﻟﻤﺸﻜﻠﺔ ﻳﻨﺼﺢ ﻋﺎدة ﺑﺎﺳﺘﺨﺪام اﻗﺘﺮان اﺟﺘﻬﺎدي )‪ (Heuristic function‬ﺗﺄﺧﺬ ﺑﻨﻈﺮ اﻻﻋﺘﺒﺎر ﻣﻘﺪار ﺑﻌﺪ اﻟﺤﺎﻟﺔ‬
‫اﻟﻤﺨﺘﺎرة ﻋﻦ ﺣﺎﻟﺔ اﻟﺒﺪاﻳﺔ ﺑﺤﻴﺚ ﺗﻔﻀﻞ اﻟﺤﺎﻟﺔ اﻷﻗﺮب‪ .‬وذﻟﻚ ﻟﺘﺠﻨﺐ اﻻﺳﺘﻤﺮار ﻓﻲ اﻟﺒﺤﺚ ﻓﻲ ﻓﺮع واﺣﺪ ﻋﻠﻰ ﺣﺴﺎب ﺑﻘﻴﺔ اﻻﻓﺮع ‪.‬‬
‫وﺳﺘﻜﻮن ‪ Heuristic Function‬ﻟﻬﺎ اﻟﺼﻴﻐﺔ اﻟﺘﺎﻟﻴﺔ‪-:‬‬
‫)‪Fn= g(n) + h(n‬‬
‫‪g(n):- Measures the actual length of path from any state (n) to the start state.‬‬
‫‪H(n):- is a heuristic estimate of the distance from state n to the goal.‬‬
‫• ﻋﻨﺪﻣﺎ ﺗﺴﺘﺨﺪم ﺧﻮارزﻣﻴﺔ ‪ Best-First‬ﻣﻊ ‪ Heuristic Function‬آﻤﺎ ﻓﻲ اﻟﺼﻴﻐﺔ )‪ (١‬ﻓﺎن اﻟﺨﻮارزﻣﻴﺔ اﻟﻨﺎﺗﺠﺔ ﺗﺴﻤﻰ‬
‫‪.A- Algorithm‬‬
‫‪A Algorithm‬‬
‫‪Trace of A- Algorithm (Search‬‬
‫)‪Connect(a,b,3‬‬
‫)‪Connect(a,c,4‬‬
‫)‪Connect(a,d,2‬‬
‫)‪Connect(b,e,7‬‬
‫)‪Connect(b,f,7‬‬
‫‪Connect(c,g,3).‬‬
‫‪Connect(c,h,2).‬‬
‫)‪Connect(d,I,4‬‬
‫‪Connect(d,j,3).‬‬
‫‪Connect(f,k,3).‬‬
‫‪Connect(h,k,1).‬‬
‫‪Connect(I,k,2).‬‬
A

B3 C4 D2

E7 F7 H2 G3
I4 J3

K3 K1 K2

The tree after evaluation function calculation is :


Connect(a,b,13)
Connect(a,c,7)
Connect(a,d,8)
Connect(b,e,23)
Connect(b,f,13)
Connect(c,g,13).
Connect(c,h,7).
Connect(d,I,8)
Connect(d,j,14).
Connect(f, k).
Connect(h, k).
Connect(I, k).
A

B 13 C7 D8

E 23 F13 H7 G 13
I8 J 14

K K K

Open Close
[A] []
[C7 D8 B13] [A]
[H7 D8 B13 G13] [C7 A]
[K D8 B13 G13] [H7 C7 A]
[K H7 C7 A]

The path is : A-C7- H7- K


A* Algorithm
Definition:-if A used with an evaluation function in which h(n) is less than or equal to the cost of
the minimal path from n to the goal , the resulting search algorithm is called A* Algorithm.
A* Algorithm Properties:-
1) Admissibility
A search algorithm is admissible if it is guaranteed to find a minimal cost to a solution
whenever such a path exists.
Admissibility Definition:
Consider the evaluation function f(n)=g(n)+h(n).
In determining the properties of admissible heuristics it is useful to define first an evaluation
function f*:
F*(n)=g*(n)+h*(n)
Where:
g*(n) is the cost of the shortest path from the start node n.

h*(n) returns the actual cost of the shortest path from n to the goal.

F*(n) is the actual cost of the optimal path from a start node to a goal node that passes through
node n.

If we employ best-first –search with the evaluation function F* ,the resulting search strategy is
admissible.
In A algorithm , g(n) , The cost of the current path to state n , is a reasonable estimate of g*(n),
but they may not be equal g(n)≥g*(n).These are equal only if the graph search has discovered the
optimal the optimal path to state n.
Similarly, We replace h*(n) with h(n) , a heuristic estimate of the minimal path to a goal state. If
A Algorithm uses an evaluation F in which h(n) ≤ h* (n) it is called A* Algorithm .So all A*
Algorithm are admissible.

2) Monotonicity
A heuristic function h is monotone if :-
a. For all state ni and nj , where nj is a descendant of ni
h(ni)-h(nj) ≤ cost(ni,nj).
Where cost (ni,nj) is the actual cost of going from state ni to nj.

b. The heuristic evaluation of the goal state is zero , or h(goal )=0.

3) Informedness
For two A* heuristics h1 and h2 , if h1(n) ≤ h2(n), for all states n in the search space , heuristics h2
is said to be more informed than h1.
Knowledge Representation
There are many methods can be used for knowledge representation and they can be
described as follows:-
1- Semantic net.
2- Conceptual graph.
3- Frames
4- Predicates logic.
5- Clause forms

1) Semantic Net
It is consist of a set of nodes and arcs , each node is represented as a rectangle to describe
the objects, the concepts and the events. The arcs are used to connect the nodes and they
divided to three parts:-

Is a: for objects & types


Is To define the object or describe it
Has a

can

‫ﻟﺘﻤﺜﻴﻞ اﻷﻓﻌﺎل واﻷﺣﺪاث واﻟﻜﺎﺋﻨﺎت‬

‫ﻟﺘﻤﺜﻴﻞ اﻟﻌﻼﻗﺔ ﺑﻴﻦ اﻟﻜﺎﺋﻨﺎت‬

(Reciever) ‫( واﻟﻤﺴﺘﻘﺒﻞ‬agent) ‫ ﺗﺨﺮج ﻣﻦ اﻟﻔﻌﻞ ﻟﺘﻮﺿﺢ او ﻟﺘﺸﻴﺮ اﻟﻰ اﻟﻔﺎﻋﻞ‬arcs ‫ﻓﻲ وﺻﻒ اﻟﻠﻐﺎت اﻟﻄﺒﻴﻌﻴﺔ ﻓﺎن‬
.‫ اﻟﺤﺎﺿﺮ او اﻟﻤﺴﺘﻘﺒﻞ‬، ‫( آﻤﺎ ﺗﺸﻴﺮ اﻟﻰ وﻗﺖ ﺣﺪوث اﻟﻔﻌﻞ أي ﻓﻲ اﻟﻤﺎﺿﻲ‬object) ‫واﻟﻜﺎﺋﻦ‬
Example1: Computer has many part like a CPU and the computer divided into two type, the first
one is the mainframe and the second is the personal computer ,Mainframe has line printer with
large sheet but the personal computer has laser printer , IBM as example to the mainframe and PIII
and PIV as example to the personal computer.

Has a
Computer CPU

Is a Is a
Has a Has a
Laser PC Mainframe Line
printer printer

Is a Is a Is a

PIV PII IBM

Example 2: Layla gave Selma a book

agent receiver
Layla gave Selma

time
object

Past book

Example 3: Layla told Suha that she gave Selma a book


agent receiver
Layla told Suha

time
time

past
proposition

receiver
Gave Selma

object

past a book
Example 4: Ali gave Ban a disk which is Zaki bought

agent receiver
Ali gave Ban

time
object

Past disk

object
agent
Zaki bought

time

Past

2) The Conceptual Graph

:‫ وﺗﺘﻜﻮن ﻣﻦ ﺟﺰﺋﻴﻴﻦ‬Semantic Net ‫وهﻲ ﻃﺮﻳﻘﺔ ﻟﺘﻤﺜﻴﻞ اﻟﻤﻌﺮﻓﺔ ﻣﺸﺎﺑﻬﺔ ﻟﻄﺮﻳﻘﺔ‬

‫ﻳﺴﺘﺨﺪم ﻟﺘﻤﺜﻴﻞ اﻷﺳﻤﺎء واﻟﺼﻔﺎت واﻷﻓﻌﺎل واﻟﺜﻮاﺑﺖ‬

‫ﻳﺴﺘﺨﺪم ﻟﺘﻤﺜﻴﻞ أدوات اﻟﺘﻌﺮﻳﻒ واﻟﻌﻼﻗﺎت‬


Example 1: Ahmed read a letter Yesterday

Ahmed agent read object letter

time

present

Example 2:- The dog Scratch it ear with is paw

The dog agent scratch object ear

instrument time

Part of paw present


Example 3: Ahmed tell Saad that he saw Suha

Ahmed agent tell receiver Saad

time

present

proposition saw receiver Suha

time

past
3) Frame:

Frame-list

Slot-list

Frame-list( node-name, parent, [child]).


Slot-list(node-name, parent).

Example:

Internal Disk controller


Structure

monitor
computer Mother board

keyboard
No. of function key

peripherals ports
printer

speed

plotters mouse

Frame –list( computer,_ ,[Internal structure, monitor, keyboard , plotters]).


Frame-list(Internal structure, computer, [disk controller, mother board]).
Frame- list(printer, peripheral, [speed, ports]).
Slot-list(motherboard, Internal structure).
Slot-list(mouse, peripheral).
Homework 1: solve with Semantic net
Ships are divided in two types , the first is “ Ocean lines” and the second is “Oil tank” ,
the ships has an engine , the oil tank are specified to transfer oil therefore it has “ fire tools” , the
ocean lines are specified to transfer the traveler therefore it has “ swimming poot” , Ibnkaldon as
an example to oil tank and ship b and ship n as an example to ocean line.
Homework 2: Using Semantic Net and Conceptual graph to solve the following
statement:
1) Suha send a book to Tom.
2) Tom believe that Mustafa like cheese.
3) Monkey ema grasp the banana with hand.
4) Predicate Logic
The predicates is the logical description that is essentially building on Boolean algebra that
includes:
1) Quantifiers
2) Logical Λ,¬,→,V,
Means all , anything, any one
Means a , a person
V or
Λ and
¬ not
→ for, reasoning
-:‫ﻓﻲ اﻟﺠﻤﻞ ﻧﺴﺘﺪل ﻋﻠﻰ اﻻﺳﺘﻨﺘﺎج ﻣﻦ اﻟﻜﻠﻤﺎت اﻻﺗﻴﺔ‬
Was is
Were are
Then
That
To that
Example:- John likes food
Likes(John , Food).

Example1:
1) Fido is a dog
Dog (Fido).
2)All dogs are animals.

(dog(x)→ animal(x))
3) All animals will die.

(animal(y)→ die(y)).
4) Prove that ,Fido will die
¬ die (Fido)

Example 2:-
1) Any one passing their history exam and winning the lottery is happy.

X (pass(x, history exam) Λ win (x, the lottery)→happy(x))


2) Any one who studies or is lucky can pass all the exam

X y (study(x) V lucky(x) → pass(x, y)).


3) John did not study but he is lucky
¬ study (John) Λ lucky(John).
4) Any one is lucky wins the lottery

X (lucky(x) → win(x, the lottery)).


5) Prove that John is happy.
¬ happy(John).
Example 3:- All people that are not poor and smart are happy. Those people that read are not
stupid. John can read and wealthy. Happy people have exciting lives. Can any one be found with
an exciting life?
1) All people that are not poor and smart are happy.

X ( ¬ poor(x) Λ smart(x)→ happy(x).


2) Those people that read are not stupid.

X (read (x) → ¬ stupid(x)).


3) John can read and wealthy.

Read (John) → wealthy (John).


4) Happy people have exciting lives.

W (happy (w) → exciting (w)).


5) The negation of the conclusion is:
6) W (¬ exciting (w)).

Homework:- Convert the following statements to predicate logic?


Marcus was a man. Marcus was a Pompeian. All Pompeian were Romans. Caeser was
ruler . All Romans were either loyal to Caeser or hated him. Everyone is loyal to someone but not
assassinate to someone. Prove that, People not try to assassinate Caeser.

5) Clause Forms:-
The statements that produced from predicate logic method are nested and very complex
to understand, so this will lead to more complexity in resolution stage , therefore the following
algorithm is used to convert the predicate logic to clause forms:-
Resolution Theorem Proving:
Resolution is a technique for proving theorems in the predicate calculus that has been a
part of A.I –Problem solving research using Resolution by refutation.
The resolution refutation proof procedure answers a query or deduces a new result by
reducing the set of clause to a contradiction , represented by the full clause ( ).

Resolution by Refutation Algorithm:-


1- Convert the statement to predicate logic.
2- Convert the statement from predicate logic to clause form.
3- Add the negation of what is to be proved to the clause forms.
4- Resolve these clauses together , producing new clauses.
5- Produce a contradication by generating the empty clause
Example: Consider now an example from the propositional calculus , where we want to
prove (a) from the following axioms:-
Natural Language
One of the most A.I Application is the Natural Language Processing (NLP), This
application treat with “How make the computer understand the natural language “ this mean how
can the computer speak or make discussion with the user using natural language.
For example “ How the computer can understand the following statement”:

Ali eat the apple


To answer this question , the statement should be analyzed ,this process called Syntax Analysis
which include represent the parser tree of the statement using Context Free Grammar (CFG).

Ali eat the apple

Noun verb del noun

np

np vp

S→ NP VP
NP→ noun | del noun
VP → verb NP
noun→ Ali| verb NP
verb → eat
del → the
The sentences in natural language include:
Nouns , verbs , adjectives , adverbs & prepositions.

The nouns like: Ahmed, Ali ,the man , the girl, apple, animals, ………. etc.

The verbs like: eat, push , walk, ……etc .

The adjectives like : the tall , big , the shorter , the shortest, thin ,…… ,etc.

The adverbs like : essentially, exactly , happily, absolutely,…. etc.

The prepositions like : in , on, at , about, over, under, through, during.

The determiner like: a , an , & the .

The Noun Phrase (NP) can include only noun such as (Ahmed) or determiner noun such as
( The man) or determiner adjective noun (the tall man ) or determiner adjective adverb noun
( the last month exactly).

The Verb Phrase (VP) can include only verb like ( eat) or verb followed by Noun Phrase(
NP) such as (eat apple) or ( threw the ball).
The preposition Phrase (PP) can include only the preposition with noun ( at Sunday) or
preposition with determiner noun such as (in the school).
Examples:
1- animals
noun → animals

2- large cities
NP → adj noun

3- the broker
np → del noun

4- the tallest smart man


np → del adj adj noun

5- In the bank
PP → preposition np

6- thinks
vp → verb

7- schemed obsessively
vp→ verb adverb.
Example : “The boy throw the ball through the window” phrase the sentence above ,
then write a complete prolog program for the syntax rules.

The boy throw the ball through the window

del noun verb del noun pre del noun

NP NP

NP VP PP

S
S → NP VP PP
NP→ del noun
VP → verb NP
PP → pre NP
noun→ boy| ball | window
verb → throw
del → the
pre → through

domains
s=string
a=s*
predicates
sen (a)
np(a).
vp(a).
pp(a).
noun(s).
del(s).
verb(s).
pre(s).
append(a,a,a).
clauses
sen(S):-np(S1),write(S1),vp(S2),write(S2),append(S1,S2,S3),write(S3),pp(S4),write(S4),!,
append(S3,S4,S),write("the statement is True").
np(X):- del(X1),noun(X2),append([X1],[X2],X).
vp(X):-verb(X1),np(X2),append([X1],X2,X).
pp(X):-pre(X1),np(X2),append([X1],X2,X).
del("the").
noun("boy").
noun("ball").
noun("window").
verb("throw").
pre("through").

append([],L,L).
append([H|T],L,[H|T1]):-append(T,L,T1).

You might also like