3ai

Upload: siddharth-kadian

Post on 07-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 3ai

    1/76

    UNIVERSITY OF TECHNOLOGY

    DEPARTMENT OF COMPUTER SCIENCE

    2010

    . :

    .

    10/6/2010

    Artificial Intelligence and its

    Programming Language (Prolog)

  • 8/4/2019 3ai

    2/76

    Artificial Intelligence and its Programming Language (Prolog)

    2

    1.1What is Prolog?Prolog (programming in logic) is one of the most widely used programming languages in

    artificial intelligence research.

    Programming languages are of two kinds:

    Procedural (BASIC, ForTran, C++, Pascal, Java);

    Declarative (LISP, Prolog, ML).

    In procedural programming, we tell the computer how to solve a problem, but in declarative

    programming, we tell the computer what problem we want solved. One makes statements about

    facts and relations among objects, and provides a set of rules (as predicate calculus implications)

    for making inference.

    1.2 What is Prolog used for?

    Good at

    Grammars and Language processing,

    Knowledge representation and reasoning,

    Unification,

    Pattern matching,

    Planning and Search.

    By following this course, you will learn how to use Prolog as a programming language to solve

    practical problems in computer science and artificial intelligence. You will also learn how the

    Prolog interpreter actually works. The latter will include an introduction to the logical

    foundations of the Prolog language.

    1.3 Prolog Terms

    The central data structure in Prolog is that of a term. There are terms of four kinds: atoms,

    numbers, variables, and compound terms. Atoms and numbers are sometimes grouped together

    and called atomic terms.

    Everything in a Prolog program both the program and the data it manipulates is build from

    Prolog terms.

  • 8/4/2019 3ai

    3/76

    Artificial Intelligence and its Programming Language (Prolog)

    3

    1.3.1 Constants

    Integers, real numbers, or an atom. Any name that starts with a lowercase letter (followed by

    zero or more additional letters, digits, or underscores) is an atom. Atoms look like variables of

    other languages, but are treated as constants in Prolog. Sequences of most non-alphanumeric

    characters (+, *, -, etc.) are also atoms.

    1.3.2 Variables

    A variable is any name beginning with an uppercase later or an underscore, followed by zero or

    more additional letters, digits, or underscores.

    For example:

    X, Y, Variable, _tag, X_526, and List, List24, _head, Tail, _inputand Outputare all Prolog

    variables.

    1.4 Basic Elements of Prolog

    There are only three basic constructs in Prolog: Facts, Rules, and Queries. A collection of facts

    and rules is called a knowledge base (or a database) and Prolog programming is all about writing

    knowledge bases. That is, Prolog programs simply areknowledge bases, collections of facts and

    rules which describe some collection of relationships that we find interesting.

    Some are always true (facts):

    father( john, jim).

    Some are dependent on others being true (rules):

    parent( Person1, Person2 ) :- father( Person1, Person2 ).

    To run a program, we ask questions about the database.

    1.4.1 Example of Facts Database:John is the father of Jim. father(john, jim).

    Jane is the mother of Jim. mother(jane, jim).

    Jack is the father of John. father(jack, john).

    1.4.2 Example of Rules Database: Person 1 is a parent of Person 2 ifPerson 1 is the father of Person 2 or Person 1 is the

    mother of Person 2.

  • 8/4/2019 3ai

    4/76

    Artificial Intelligence and its Programming Language (Prolog)

    4

    parent(Person1, Person2):- father(Person1, Person2).

    parent(Person1, Person2):- mother(Person1, Person2).

    Person 1 is a grandparent of Person 2 ifsome Person 3 is a parent of Person 2 and

    Person 1 is a parent of Person 3.

    grandparent( Person1, Person2 ):- parent( Person3, Person2 ), parent( Person1,

    Person3 ).

    1.4.3 Example questions:

    Who is Jim's father? ?- father( Who, jim ).

    Is Jane the mother of Fred? ?- mother( jane, fred ).

    Is Jane the mother of Jim? ?- mother( jane, jim ). Does Jack have a grandchild? ?- grandparent( jack, _ )

    1.5 Clauses

    Predicate definitions consist ofclauses. It is an individual definition (whether it be a fact or rule).

    mother(jane,alan). = Fact

    parent(P1,P2):- mother(P1,P2). = Rule

    A clause consists of a headand sometimes a body. Facts dont have a body because they are

    always true.

    A predicate head consists of apredicate name and sometimes some arguments contained within

    brackets and separated by commas.

    mother(jane, alan).

  • 8/4/2019 3ai

    5/76

    Artificial Intelligence and its Programming Language (Prolog)

    5

    1.6 Arithmetic Operators

    Operators for arithmetic and value comparisons are built-in to Prolog.

    Comparisons: , =, = (equals),< > (not equals)

    Mathematical precedence is preserved: /, *, +, - Can make compound sums using round

    brackets for example X = (5+4)*2 then X=18.

    1.7Tests within clausesThese operators can be used within the body of a clause to manipulate values:

    sum(X,Y,Sum):- Sum = X+Y.

    Goal: sum(3,5,S)

    Output: S=8

    sum(X,Y):-Sum=X+Y, write(Sum).

    Goal: sum(3,5)

    Output: 8

    We can write the rule without arguments for example:

    Sum:-readint(X),readint(Y),Sum=X+Y, write(Sum).

    Goal: sum

    Output: 8

    Also, these operators can be used to distinguish between clauses of a predicate definition:bigger(N,M):- N < M, write(The bigger number is ), write(M).

    bigger(N,M):- N > M, write(The bigger number is ), write(N).

    bigger(N,M):- N = M, write(Numbers are the same).

    Goal: bigger(6,7)

    Output: The bigger number is 7

    2.1 Recursion

    The recursion in any language is a function that can call itself until the goal has been succeed.

    In Prolog, recursion appears when a predicate contain a goal that refers to itself. There are two

    types of recursion.

  • 8/4/2019 3ai

    6/76

    Artificial Intelligence and its Programming Language (Prolog)

    6

    1. Tail Recursion: is recursion in which the recursive call is the last subgoal of the last

    clause. That is, in tail recursion the recursive call is always made just before the

    procedure exits: the last step. For example:

    tail_recursion(X):- b, c, tail_recursion(Y).

    2. Non Tail Recursion: when a recursive call is not last we don't have tail recursion. If you

    think about it, tail recursive calls can be implemented very efficiently because they can

    reuse the current stack frame or activation record rather than pushing a new one, filling it

    with data, doing some computation, and popping it. So tail recursion is often a fast

    option. For example:

    nontail_recursion(X):- b, nontail_recursion(X), c.

    2.2 Pattern Matching

    Prolog uses unification to match variables to values. An expression that contains variables like

    X+Y*Zdescribes a pattern where there are three blank spaces to fill in named X, Y, and Z. The

    expression 1+2*3 has the same structure (pattern) but no variables. If we input this query

    X+Y*Z=1+2*3.

    then Prolog will respond that X=1, Y=2, and Z=3! The pattern matching is very powerful

    because you can match variables to expressions like this

    X+Y=1+2*3.

    and get X=1 and Y=2*3! You can also match variable to variables:

    X+1+Y=Y+Z+2.

    This sets X=Y=2 and Z=1!

  • 8/4/2019 3ai

    7/76

    Artificial Intelligence and its Programming Language (Prolog)

    7

    Example 1:

    a(1).

    a(2).

    a(4).

    b(2).

    b(3).

    d(X,Y):-a(X),b(Y), X=4.

    Goal: d(X,Y)

    X=1, Y=2, 1=4 Fail

    X=1, Y=3, 1=4 Fail

    X=2, Y=2, 2=4 Fail

    X=2, Y=3, 2=4 Fail

    X=4, Y=2, 4=4 True

    X=4, Y=3, 4=4 True

    Output: 2 solutions X=4, Y=2 and X=4 Y=3.

    Goal: d(A,2)

    X=1, Y=2, 1=4 Fail

    X=2, Y=2, 2=4 Fail

    X=4, Y=2, 4=4 True

    Output: 1solution A=4.

    Goal: d(2,2)

    Output: no solution.

    2.3 Backtracking

    2.3.1 Cut

    Up to this point, the Prolog cutadds an 'else' to Prolog, we have worked with Prolog's

    backtracking. Sometimes it is desirable to selectively turn off backtracking. Prolog provides a

    predicate that performs this function. It is called the cut, represented by an exclamation point (!).

    The cut effectively tells Prolog to freeze all the decisions made so far in this predicate. That is, if

    required to backtrack, it will automatically fail without trying other alternatives.

  • 8/4/2019 3ai

    8/76

    Artificial Intelligence and its Programming Language (Prolog)

    8

    Example 1:

    a(1).

    a(2).

    a(4).

    b(2).

    b(3).

    d(X,Y):-a(X),b(Y), X=4,!.

    Goal: d(X,Y)

    X=1, Y=2, 1=4 Fail

    X=1, Y=3, 1=4 Fail

    X=2, Y=2, 2=4 Fail

    X=2, Y=3, 2=4 Fail

    X=4, Y=2, 4=4 True

    Output: 1 solutions X=4, Y=2.

    Example 2:

    a(1).

    a(2).

    a(4).

    b(2).

    b(3).

    d(X,Y):-a(X),!,b(Y), X=4.

    Goal: d(X,Y)

    X=1, Y=2, 1=4 Fail

    X=1, Y=3, 1=4 Fail

    Output: no solutions found.

    2.3.2 Fail

    The fail predicate is provided by Prolog. When it is called, it causes the failure of the rule. And

    this will be forever; nothing can change the statement of this predicate.

  • 8/4/2019 3ai

    9/76

    Artificial Intelligence and its Programming Language (Prolog)

    9

    Example of using fail in the program.

    clauses

    a(1).

    a(2).

    a(3).

    a(4).

    Begin:-a(X),write(X),fail.

    Goal: Begin

    Output: 1234 NO

    2.3.3 Passing Back Answers

    To report back answers we need to put an un instantiated variable in the query, and then,

    instantiate the answer to that variable when the query succeeds, Finally, pass the variable all the

    way back to the query.

    bigger(X,Y,Answer):- X>Y, Answer = X.

    bigger(X,Y,Answer):- X=

  • 8/4/2019 3ai

    10/76

    Artificial Intelligence and its Programming Language (Prolog)

    10

    random(X) Binds X to a random real; 0

  • 8/4/2019 3ai

    11/76

    Artificial Intelligence and its Programming Language (Prolog)

    11

    sum(X,Y,S):-Y1=Y+X,X1=X+1,sum(X1,Y1,S),!.

    Goal: sum(1,0,M)

    Output: ?

    /* summation of 10 given integer numbers*/

    sum_int(10,S,S):-!.

    sum_int(X,Y,S):-readint(Z),Z>0, X1=X+1,Y1=Y+Z,sum_int(X1,Y1,S),!.

    sum_int(X,Y,S):-X1=X+1,sum_int(X1,Y,S),!.

    Goal: sum_int(1,0,M)

    Output: ?

    /* factorial program*/

    fact(0,_,1):-!.

    fact(1,F,F):-!.

    fact(X,Y,F):-Y1=X*Y,X1=X-1,fact(X1,Y1,F),!.

    Goal: fact(3,1,F)

    Output: F=6

    /*power program*/

    power(_,0,P,P):-!.

    power(X,Y,Z,P):-Z1=Z*X,Y1=Y-1,power(X,Y1,Z1,P),!.

    Goal: power(5,2,1,P)

    Output: P=25

  • 8/4/2019 3ai

    12/76

    Artificial Intelligence and its Programming Language (Prolog)

    12

    4.1 Lists in Prolog

    Lists are ordered sequences of elements that can have any length. Lists can be represented as a

    special kind of tree. A list is either empty, or it is a structure that has two components: the head

    H and tail T. List notation consists of the elements of the list separated by commas, and the

    whole list is enclosed in square brackets.

    For example:

    [a] and [a,b,c], where a, b and c are symbols type.

    [1], [2,3,4] these are a lists of integer.

    [] is the atom representing the empty list.

    Lists can contain other lists. Split a list into its head and tail using the operation [X|Y].

    4.2 Examples about Lists

    1. p([1,2,3]).p([the,cat,sat,[on,the,hat]]).

    Goal: p([X|Y]).

    Output:

    X = 1 Y = [2,3] ;

    X = the Y = [cat,sat,[on,the,hat]].

    2. p([a]).Goal: p([H | T]).

    Output:

    H = a, T = [].

    3. p([a, b, c, d]).Goal: p([X, Y | T]).

    Output:

    X = a, Y = b, T = [c, d].

  • 8/4/2019 3ai

    13/76

    Artificial Intelligence and its Programming Language (Prolog)

    13

    4. P([[a, b, c], d, e]).Goal: p(H,T)

    Output:

    H = [a, b, c], T = [d, e].

    4.3List Membership Member is possibly the most used user-defined predicate (i.e. you have to define it every

    time you want to use it!).

    It checks to see if a term is an element of a list.

    it returns yes if it is.

    and fails if it isnt.

    member(X,[X|_]).

    member(X,[_|Y]) :- member(X,Y).

    It 1st checks if the Head of the list unifies with the first argument.

    If yes then succeed.

    If no then fail first clause.

    The 2nd clause ignores the head of the list (which we know doesnt match) and recourses

    on the Tail.

    Goal: member(a, [b, c, a]).

    Output: Yes

    Goal: member(a, [c, d]).

    Output: No.

    4.4 Print the contents of the list.print([]).

    print([i(X,Y)|T]):-write(X,Y),print(T).

    Goal: print([3,4,5])

    Output: 3 4 5

  • 8/4/2019 3ai

    14/76

    Artificial Intelligence and its Programming Language (Prolog)

    14

    4.5 Find the maximum value of the list

    list([H],H).

    list([H1,H2|T],H1):-H1>H2,list([H1|T],_).

    list([_,H2|T],H2):-list([H2|T],H2).

    Goal: list([3,9,4,5],M)

    Output: M=9

    4.6 Append two lists

    app([],L,L).

    app([X|L1],L2,[X|L3]) :-app(L1,L2,L3).

    Goal: app([3,4,5],[6,7,8],L)

    Output: L=[3,4,5,6,7,8]

    4.7 Write the contents of list inside at the given list

    list([[]]).

    list([[H|T]]):-write(H),list([T]).

    list([H|T]):-list([H]),list(T).

    Goal: list([[3,4,5],[6,7,8]])

    Output: 3 4 5 6 7 8

    4.8 Reveres the contents of the given list.

    app([],X,X).

    app([H|T1],X,[H|T]):-app(T1,X,T).

    rev([X],[X]).

    rev([H|T],L):-rev(T,L1),app(L1,[H],L).

    Goal: rev([a,b,c,d],R)

    Output: R=[d,c,b,a]

  • 8/4/2019 3ai

    15/76

    Artificial Intelligence and its Programming Language (Prolog)

    15

    5.1 Tail and non tail Recursive Programs.

    In general, tail-recursive programs are more efficient than non-tail-recursive programs.

    Non-tail Summation of 10 integer number.

    Sum-nontail(11,0).

    Sum-nontail(X,S):-X1=X+1, Sum-nontail(X1,S1),S=S1+X.

    Trace the above program with the goal ? Sum-nontail(4,S)

    Sum-nontail(1,S):-X1=2, Sum-nontail(2,S1),S=S1+1.

    Sum-nontail(2,S):-X1=3, Sum-nontail(3,S1),S=S1+2.

    Sum-nontail(3,S):-X1=4, Sum-nontail(4,S1),S=S1+3.

    Then ? Sum-nontail(4,S)

    S=6.

    Non-tail Factorial program.

    Fact(0,1).

    Fact(1,1).

    Fact(X,Y):-X1=X-1,fact(X1,Y1),Y=X*Y1.

    Non-tail Power program.

    Power(_,0,1).

    Power(X,Y,Z):-Y1=Y-1,power(X,Y1,Z1),Z=Z1+X.

  • 8/4/2019 3ai

    16/76

    Artificial Intelligence and its Programming Language (Prolog)

    16

    Standard String Predicates

    Prolog provides several standard predicates for powerful and efficient string manipulations. Inthis section, we summarize the standard predicates available for string manipulating and type

    conversion.

    1. str_len(String,Length) (string,integer) (i,o): Determines the length of String. Succeeds if the

    length could be matched with Len.

    str_len(prolog,X)

    X=6.

    2. str_char (String,Char) (string,char) (i,o) (o,i): Converts a string into a character or vice

    versa. If string is bound, it must have length 1 for the predicate to succeed.

    str_char(A,X)

    X=A.

    3. str_int(String,Int) (string,integer) (i,o) (o,i): Converts a string of one character to ASCII

    code or vice versa. If string is bound, it must contain a single number for the predicate to

    succeed.

    str_int(A,X)

    X=65 .

    4. char_int(Char,Int) (char,integer) (i,o) (o,i): Converts a character to ASCII code or viceversa.

    char_int(A,X)

    X=65.

    5. isname(String) (string) (i): Tests whether a string would match a prolog symbol.

    isname(s2) return YES.

    isname(4r) return NO.

    6. frontchar(String,FrontChar,RestString) (string,char,string) (i,x,x) (x,i,i): Extracts the first

    character from a string, the remainder is matched with RestString.

    frontchar(prolog,C,R)

    C=p, R=rolog.

    http://www.fraber.de/bap/bap209.html#lib_str_lenhttp://www.fraber.de/bap/bap209.html#lib_str_lenhttp://www.fraber.de/bap/bap207.html#lib_str_charhttp://www.fraber.de/bap/bap207.html#lib_str_charhttp://www.fraber.de/bap/bap208.html#lib_str_inthttp://www.fraber.de/bap/bap208.html#lib_str_inthttp://www.fraber.de/bap/bap99.html#lib_char_inthttp://www.fraber.de/bap/bap99.html#lib_char_inthttp://www.fraber.de/bap/bap157.html#lib_isnamehttp://www.fraber.de/bap/bap157.html#lib_isnamehttp://www.fraber.de/bap/bap143.html#lib_frontcharhttp://www.fraber.de/bap/bap143.html#lib_frontcharhttp://www.fraber.de/bap/bap143.html#lib_frontcharhttp://www.fraber.de/bap/bap143.html#lib_frontcharhttp://www.fraber.de/bap/bap143.html#lib_frontcharhttp://www.fraber.de/bap/bap143.html#lib_frontcharhttp://www.fraber.de/bap/bap157.html#lib_isnamehttp://www.fraber.de/bap/bap99.html#lib_char_inthttp://www.fraber.de/bap/bap208.html#lib_str_inthttp://www.fraber.de/bap/bap207.html#lib_str_charhttp://www.fraber.de/bap/bap209.html#lib_str_len
  • 8/4/2019 3ai

    17/76

    Artificial Intelligence and its Programming Language (Prolog)

    17

    7. fronttoken (String,Token,Rest) (string,string,string) (i,o,o) (i,i,o) (o,i,i): Skips all white

    space characters (blanks,tabs) and separates from the resulting string the first valid token.

    The remainder is matched with RestString. A valid token is either a variable or name

    'A'...'Z','a'...'z','','0'...'9', a number '0'...'9' or a single character. It fails if String was

    empty or contained only whitespace.

    fronttoken(complete prolog program,T,R)

    T=complete, R=prolog program.

    8. frontstr (StrLen,String,FrontStr,RestStr) (i,i,o,o): Extracts the first n characters from a

    string.This establishes a relation between String, Count, FronStr, and RestString, thus that String

    = FrontStr+RestString and str_len(FronStr,Count) is true. The String and Count arguments must

    be initalized.

    frontstr(3,cdab 2000,T,R)

    T=cda, R=b 2000.

    9. concat(Str1,Str2,ResStr) (string,string,string) (i,i,o): Merges to strings to one by appending

    the second argument to the first.

    concat(prolog,2011,R)R=prolog2011.

    Examples: Write prolog program to do the following:

    1. Convert the string of words into a list of words:split("",[]).

    split(S,[H|T]):-fronttoken(S,H,R),split(R,T).

    2. Count the number of words in the given string.

    counts("",0).

    counts(S,C):-fronttoken(S,_,R), counts(R,C1),C=C1+1.

    3.Count the number of characters in the given string.

    counts("",0).

    counts(S,C):-frontchar(S,_,R), counts(R,C1),C=C1+1.

    4. Reverse the given string.

    rev("","").

    rev(Str,R_Str):-frontstr(1,Str,C,RemStr),rev(RemStr,L1),concat(L1,C,R_Str).

    con([],"").

    http://www.fraber.de/bap/bap145.html#lib_fronttokenhttp://www.fraber.de/bap/bap145.html#lib_fronttokenhttp://www.fraber.de/bap/bap143.html#lib_frontcharhttp://www.fraber.de/bap/bap143.html#lib_frontcharhttp://www.fraber.de/bap/bap144.html#lib_frontstrhttp://www.fraber.de/bap/bap144.html#lib_frontstrhttp://www.fraber.de/bap/bap108.html#lib_concathttp://www.fraber.de/bap/bap108.html#lib_concathttp://www.fraber.de/bap/bap108.html#lib_concathttp://www.fraber.de/bap/bap108.html#lib_concathttp://www.fraber.de/bap/bap108.html#lib_concathttp://www.fraber.de/bap/bap108.html#lib_concathttp://www.fraber.de/bap/bap144.html#lib_frontstrhttp://www.fraber.de/bap/bap143.html#lib_frontcharhttp://www.fraber.de/bap/bap145.html#lib_fronttoken
  • 8/4/2019 3ai

    18/76

    Artificial Intelligence and its Programming Language (Prolog)

    18

    con([H|T],Str):-con(T,Str1),concat(H,Str1,Str).

    5. Count the number of words that contain tion in the given list.

    count(S):-frontstr(4,S,X,_),X="tion".

    count(S):-frontchar(S,_,R),count(R).

    set([],0).

    set([H|T],L):-count(H),set(T,L1),L=L1+1,!.

    set([_|T],L):-set(T,L).

    Prolog Database

    It uses to retrieve information from the database. The database predicate retrieves information

    from the database by matching its argument with any term stored in the database. Until the

    DATABASE keyword is implemented, we have to use this or clause to make a non-destructive

    database query. The database contains related predicates such

    as: asserta,assertz,retract,retractall, these predicates are standard prolog built-in as shown

    below:

    1. asserta() adds Term to the beginning of the database. Term must should bound to an

    atom or a struct, but may contain free variables. The database entry is not removedupon backtracking. Database entries can only be removed by the use of the.

    2. assertz() adds Term to the end of the database. Term must should bound to an atom

    or a struct, but may contain free variables. The database entry is not removed upon

    backtracking. Database entries can only be removed by the use of the retract()

    predicate.

    3. retract(): it retrieves information from the database by matching its argument with

    any term stored in the database. If it matches, the database entry is removed. Upon

    backtracking multiple solutions may occur. You may invoke indefinite loops by using

    retract(X),...,assertz(X),fail.construct.

    Different from standard prolog we may use this with an uninstantiated variable to

    remove all entries.

    http://www.fraber.de/bap/bap91.html#lib_assertahttp://www.fraber.de/bap/bap91.html#lib_assertahttp://www.fraber.de/bap/bap92.html#lib_assertzhttp://www.fraber.de/bap/bap92.html#lib_assertzhttp://www.fraber.de/bap/bap92.html#lib_assertzhttp://www.fraber.de/bap/bap194.html#lib_retracthttp://www.fraber.de/bap/bap194.html#lib_retracthttp://www.fraber.de/bap/bap195.html#lib_retractallhttp://www.fraber.de/bap/bap195.html#lib_retractallhttp://www.fraber.de/bap/bap194.html#lib_retracthttp://www.fraber.de/bap/bap194.html#lib_retracthttp://www.fraber.de/bap/bap195.html#lib_retractallhttp://www.fraber.de/bap/bap194.html#lib_retracthttp://www.fraber.de/bap/bap92.html#lib_assertzhttp://www.fraber.de/bap/bap91.html#lib_asserta
  • 8/4/2019 3ai

    19/76

    Artificial Intelligence and its Programming Language (Prolog)

    19

    4. retractall(): It succeeds once and remove all database entries that match Term on the

    way. Different from standard prolog we may use this with an uninstantiated variable

    to remove all entries.

    To load and save dos file, the predicates consult() and save() are used as shown below:

    1- consult (FileName): is used to read and parse the file given by FileName. The file may

    contain clauses and facts, separated by the dots. The current operator declarations are used. For

    example consult("data.dat"), where data.dat is dos file name.

    2- save(FileName): is used to save information the file given by FileName. For example

    save("data.dat"), where data.dat is dos file name.

    Examples: Write prolog program to perform the following:

    1- Read persons name S1, gender S2 and age I then save them into file p.dat.

    Solution:

    repeat.

    repeat:-repeat.

    a:-repeat,consult(a1.dat),readln(S1),readln(S2),readint(I),assertz(Per(S1,I,S2)),

    I

  • 8/4/2019 3ai

    20/76

    Artificial Intelligence and its Programming Language (Prolog)

    20

    4- Count how many cars with car name TOYOTA and owner name ALI?

    d:-consult("c.dat"),findall(X,car("toyota",X,_),L),count(L,C),write(C).

    count([],0).

    count([H|T],X):-H="ali",count(T,X1),X=X1+1.

    count([_|T],X):-count(T,X).

  • 8/4/2019 3ai

    21/76

    What areKnowledge Representation Schemes ?In Al, there are four basic categories of representational schemes: logical,

    procedural, network and structured representation schemes.

    1) Logical representation uses expressions in formal logic to represent its

    knowledge base. Predicate Calculus is the most widely used

    representation scheme.

    2) Procedural representation represents knowledge as a set ofinstructions for

    solving a problem. These are usually if-then rules we use in rule-based systems.

    3) Network representation captures knowledge as a graph in which the nodes

    represent objects or concepts in the problem domain and the arcs -represent relations or

    associations between them.

    4) Structured representation extends network representation schemesby allowing each

    node to have complex data structures named slots with attached values.

    We will focus on logic representation schemes in this chapter.

    2.1 The Propositional Calculus

    2.1.1 Symbols and Sentences

    The prepositional calculus and, in the next subsection, the predicate

    calculus are first of all languages. Using their words, phrases, and sentences, we

    can represent and reason about properties and relationships in the world. The

    first step in describing a language is to introduce the pieces that make it up: ils set

    of symbols.

    DEFINTION

    PROPOSITIONAL CALCULUS SYMBOLS

    The symbolsof prepositional Calculus are, the prepositional symbols:

    P. Q. R,S,T....

    truth symbols

  • 8/4/2019 3ai

    22/76

    true, false

    and connectives:

    ,,,,Prepositional symbols denote propositions ofstatements about the world that

    may be either true or raise, such as "the car is red" or "water is wet."

    Propositions are denoted by uppercase letters near the end of the English

    alphabet. Sentences in the propoEitional calculus are formed from these atomic

    symbols according to the following rules :

    DEFINITION

    PROPOSIONAL CALCULUS SENTENCES

    Every prepositional symbol and truth symbol is a sentence.

    For example: true, P, Q, and R are sentences.

    The negation of a sentence is a sentence

    forexample: P and false are sentences

    The conjunction or andtwo sentences : is a sentence

    For example: P P is a sentence.

    The disjunction orof two sentences is a sentence.

    For example P P. is a sentence,

    The implication of one sentence for another is a sentence.

    For example P Q is sentence.

    The equivalence of two sentences Is a sentence.

    For example: P v Q = R is a sentence.

  • 8/4/2019 3ai

    23/76

    Legal sentences are also called well-formed formulas 01 WFFs.

    IN expressions of the form P Q. P and Q are called the conjuncts In P Q and

    Q are referred to as disjuncts in an implication P Q, P is the premise or antecedent

    and Q, the conclusion or consequent .

    In propositional calculus sentences. the symbols( ) and [ ] are used to group

    symbols into subexpressions and so control their order of evaluation and meaning . for

    example (P Q )=R is quit different from P (Q=R), as can bedemonstrated using truth tables ( section 2.1.2).

    An expression is a sentence, or well-formed formula, of the prepositional calculus

    ifand only if it can be formed of legal symbols through some sequence of these rules . For

    example .

    P Q R = P v QvR

    is a well-formed sentence in the in the propositions calculus because:

    P, Q, and R are-proposition and .thus sentences.

    P Q, the conjunction of two sentences, is a sentence.

    (P Q) R, the implication of a sentence for another, is a sentence.

    P and Q, the negations of sentences, are sentences.

    P v Q, the disjunction of two sentences, is a sentence.

    P v Q v R, the disjunction of two sentences, is a sentence.

    ((P Q) R)= P v Q v R, the equivalence of two sentences, is a sentence.

    This is our original sentence, which has been Constructed through a series of applications of

    legal roles and is therefore well formed.

    DEFINITION

    PROPOSITIONAL CALCULUS SEMANTICS

    An interpretation of a set of propositional is the assignment of a truth

    value , either T or F , to each propositional symbol .

    The symbol true is always assigned T, and the symbol false is assigned F .

  • 8/4/2019 3ai

    24/76

    Th

    interpre

    The tr

    symb

    is F

    The tr

    have tr

    The tr

    truth va

    The tr

    symbol

    symbol

    The tr

    expres

    other

    Figure 2.

    ation or t

    th assign

    l , is F if t

    th assign

    th value T;

    th assign

    lue F; other

    th assign

    before the

    after the i

    th assign

    slone hav

    ise it is F

    Figu

    P Q

    T.

    T

    F

    F

    Truth ta

    uth value

    ment of n

    he assign

    ment of c

    otherwise i

    ment of d

    ise it is T.

    ment of i

    implication

    plication i

    ment of e

    e the sam

    .

    P

    e 2.1 trut

    P pv

    F

    F

    T

    T

    le demo

    for sente

    egation ,

    ent to P

    onjunctio

    t is F.

    isjunctio

    plicatio

    is T and t

    s F; otherw

    quivalenc

    assignm

    Q PQ

    h table fo

    PQ

    T T

    F F

    T T

    T T

    strating t(pvQ)

    ce is det

    P, wher

    is T , and

    , is T o

    , v is F onl

    , is F o

    e truth val

    ise it is T .

    e , = is T

    ent for all

    the oper

    (pvQ)=

    T

    T

    T

    T

    e equiva

    rmined b

    P is any

    if the as

    ly when b

    when both

    nly when t

    e of the co

    only whe

    possible

    ator

    ( PQ)

    ence of (

    y:

    propositi

    signment

    th conjunc

    disjuncts h

    e premise

    nsequent o

    both

    interpreta

    P

    Q) an

    nal

    to P

    s

    ave

    or

    tion

  • 8/4/2019 3ai

    25/76

    By demonstrating that they have identical truth tables, we can prove the following prepositional calculus

    equivalences. For prepositional expressions P, Q, and R;

    (P) = P

    (P v Q)=( P Q)

    The contra positive law : (PQ)=( QP)De morgan's law : (P v Q)=( PQ) and (PQ)=( P v Q)

    The commutative laws : (PQ)=( Q P) and ( P v Q ) = ( Q v P )Associative law : (( P Q) R) = ( P (Q R))

    Associative law : (( P v Q) v R) = ( P v (Q v R))

    Distributive law : P v (Q R) = ( P v Q) (P v R)

    Distributive law : P (Q v R) = ( P Q) v (P R)

    2.2 The Predicate Calculus

    In prepositional calculus, each atomic symbol (P, Q, etc.) denotes a proposition of some complexity.

    There isno way to access the components of an individual assertion. Predicate calculus provides this ability.

    For example, instead pfletting a single prepositional symbol, P, denote :1he entire sentence "it rained on

    Tuesday," we can create a predicate weather that describes a relationship between a date and the weather.

    weather (Tuesday, rain) through inference rules we can manipulate predicate calculus expression accessing

    their individual components and inferring new sentences.

    Predicate calculus also allows expressions^ contain variables. Variables let us create general

    assertions about classes of entities. For example, we could state that for:all values, of X, where X is a day of

    the week , the statement weather ( X, rain ) is true ; I,e., it rains it rains everyday. As with propositional

    calculus, we will first define the syntax of the language and then discuss its semantics.

  • 8/4/2019 3ai

    26/76

    Examples of English sentences represented in predicate calculus:

    1- If it doesn't rain tomorrow, Tom will go to the mountains. weather (rain, tomorrow) go(tom, mountains).

    2- Emma is a Doberman pinscher and a good dog.Good dog(emma) isa (emma, Doberman )

    3. All basketball players are tall.

    X (basketball _ player(X)tall (X))

    4- Some people like anchovies.

    X (person(X) likes(X, anchovies)),

    5- If wishes were horses, beggars would ride. equal(wishes,

    horses) ride(beggars).

    6- Nobody likes taxes

    X likes(X,taxes).

  • 8/4/2019 3ai

    27/76

    3-1What is Resolution?

    Resolution is a technique for theorem. proving in propositional and

    predicate calculus which attempts to show that the negation of thestatement produces a contradiction with the known statements.

    In the following expression, uppercase letters indicate variables

    (W,X,Y,andZ); lowercase letters in the middle of the alphabet

    indicate Constants or bound variables (I, m, and n); and early

    alphabetic lowercase letters indicate the predicate names (a, b, c, d,

    and e). To improve readability of the expressions, we use two types of

    brackets: ( ) and [ ]. Where possible in the derivation, we remove

    redundant brackets: The expression we will reduce to clause form is:

    , , ,

    1. First we eliminate the by using: a b a v b. This transformationreduces the expression in (i) above:

    , , , ,

    2. Next we reduce the scope of negation."This may be accomplished

    using a number of the transformations that includes:

    XbX X

    Using the second and fourth equivalences (ii) becomes:

    , , ,

  • 8/4/2019 3ai

    28/76

    3. Next we standardize by renaming all variables so that variables bound

    by different quantifiers have unique names. Because variable names

    are "dummies" or "place holders," the particularname chosen for a

    variable does not affect either the truth value or the generality of the

    clause. Transformations used at this step are of the form:

    ((X)a(X) v (X)b(X(X)a(X) v ()b()Because {iii)bas two instances of the variable X, we rename:

    , , ,,

    4. Move all quantifiers to the left without changing their order. This is

    possible. because step 3 has removed the possibility of any conflict

    between variable names. (iv) now becomes:

    , , ,

    After step 4 the clause is said to be in prenex normal form, because all

    the quantifiers are in front as a prefix and the expression or matrix

    follows after.

    5. At this point all existential quantifiers are eliminated by a process called

    skolemization. Expression (v) has an existential quantifier for Y.

    When an expression contains an existentially quantified variable, for

    example, ( Z)(foo(...Z,...)), it may be concluded that there is anassignment to Z under which foo is true. Skolemization identifies such a

    value. Skolemization does not necessarily show how to produce such avalue; It is only a method for giving a name to an assignment that must

    exist. If k represents that assignment, then we have foo(.K.).Thus:

    where the name fido is picked from the domain of definition of X to

  • 8/4/2019 3ai

    29/76

    represent that individual X. fido is called a skolem constant. If the

    prediCate has more than one argument and the existentially quantified

    variable is within the scope of universally quantified variables, the

    existential variable must be a function of those other variables. This is

    represented in the skolemizatlon process:

    ,This expression indicates that every person has' a mother. Every person is

    an X and the existing mother will be a function of the particular person X

    that is picked. Thus skolemization gives:

    ,

    which indicates that each X has a mother (the m of that X). In another

    example:

    ,,,Is skolemized to:

    ,,

    We note that the existentially quantified Zwas. Within the scope (to theright of) universally quantified X and Y Thus the skolemassignment is a

    function of X and Y but not ofW. With skolemization (v) becomes:

    bXcX,I c(f(X),Z)]d(X,f(X)))])e(W))

    where f is the skolem function of X that replaces the existential Y. Once

    the skolemization has occurred, step 6 can take place, which simply

    drops the prefix.

    6. Drop all universal quantification. By this point only universally

    quantified variables exist (step 5) with no variable conflicts (step 3).

    Thus all

  • 8/4/2019 3ai

    30/76

    ,,,

    7. Next we convert the expression to the conjunct of disjuncts form. This

    requires using the associative and distributive properties of A and v.

    Recall that

    which indicates that or may be grouped in any desired fashion. Thedistributive property is also used, when necessary. Because

    is already in clause form, A is not distributed However, must bedistributed across using:

    The final form of (vii) is:

    ,,,

    8. Now call each conjunct a separate clause. In the example (viii) above

    there are two clauses:

    , , ,

    9. The final step is to standardize the variables apart again. This requires

    giving. The variable in each clause generated by step 8 different names.This procedure arises from the following equivalence:

    Which follows from the nature of variable names as place holders. (ixa)

    and (ixb) now become, using new-variable names U and V:

    , , ,

    Ex (3): As a final example, suppose:

  • 8/4/2019 3ai

    31/76

    "All people who are not poor and are smart are hippy. Those people who'read are not stupid. John can read are is wealthy. Happy people haveexciting lives. Can anyone be found with an exciting life?"

    a) First change the sentences to predicate form:

    We assume X (smart (X) stupid (X) and Y (wealthy (Y) poor (Y)),and get:

    Poor X

    read (john) poor (john) (happy (Z) exciting (Z)The negation of the conclusion is:

    W (exciting (W))b) These predicate calculus expressions for the happy life problem are

    transformed into the following clauses:

    poor (X) smart (X) happy (X) read (Y) smart(Y)

    ......read (john)

    poor (john) happy (Z) exciting (Z) exciting (W)

    The resolution refutation for this example is found in Figure (3-4).

  • 8/4/2019 3ai

    32/76

    exciting(W) happy(Z) ) exciting (Z)

    (Z/W)

    happy (Z) poor (X) smart (X) happy (X)

    (X/Z)

    poor (X) smart (X) read (Y) smart(Y)

    (Y/X)

    poor(john) poor(Y) read (Y)

    (john/Y)

    read(john) read (john)

    ( )

    Figure (3-4):Resolution prove for the "exciting life" problem .

  • 8/4/2019 3ai

    33/76

    1 IntelligentSearchMethodsandStrategies

    Search is inherent to the problem and methods of artificial

    intelligence (AI). This is because AI problems are intrinsically complex.

    Efforts to solve problems with computers which human can routinely

    innate cognitive abilities, pattern recognition, perception and

    experience, invariablymust turn toconsiderationsofsearch.Allsearch

    methods essentially fall into one of two categories, exhaustive (blind)

    methodsandheuristicorinformedmethods.

    2 StateSpaceSearch

    The state space search is a collection of several states with

    appropriate connections (links) between them. Any problem can be

    representedas such space search tobe solvedbyapplying some rules

    withtechnicalstrategyaccordingtosuitableintelligentsearchalgorithm.

    What we havejust said, in order to provide a formal description of a

    problem,wemustdothefollowing:

    1 Defineastatespace thatcontainsall thepossibleconfigurationsoftherelevantobjects(andperhapssome impossibleones).Itis,

    of course, possible to define this space without explicitly

    enumeratingallofthestatesitcontains.

    2 Specify one or more states within that space that describepossible situations from which the problemsolving process may

    start.Thesestatesarecalledtheinitialstates.

    3 Specifyoneormorestatesthatwouldbeacceptableassolutionstotheproblem.Thesestatesarecalledgoalstates.

  • 8/4/2019 3ai

    34/76

    4 Specify a set of rules that describe the actions (operators)available. Doing this will require giving thought to the following

    issues:

    What unstated assumptions are present in the informal

    problemdescription?

    Howgeneralshouldtherulesbe?

    How much of the work required to solve the problem

    shouldbeprecomputedandrepresentedintherules?

    The problem can then be solved by using rules, in combination

    with an appropriate control strategy, to move through the problem

    spaceuntilapathfromaninitialstatetoagoalstateisfound.Thusthe

    process of search is fundamental to the problemsolving process. The

    fact that search provides the basis for the process of problem solving

    does not, however, mean that other, more direct approaches cannot

    alsobeexploited.Wheneverpossible, theycanbe includedassteps in

    the search by encoding them rules. Of course, for complex problems,

    more sophisticated computations will be needed. Search is a general

    mechanismthatcanbeusedwhennomoredirectmethodsisknown.At

    the same time, it provide the framework into which more direct

    methodsforsolvingsubpartsofaproblemcanbeembedded.

    Tosuccessfullydesignandimplementsearchalgorithms,aprogrammer

    mustbeabletoanalyzeandpredicttheirbehavior.Questionsthatneed

    tobeansweredinclude:

    Istheproblemsolverguaranteedtofindasolution?

    Willtheproblemsolveralwaysterminate,orcanitbecomecaught

    inaninfiniteloop?

  • 8/4/2019 3ai

    35/76

    Whenasolutionisfound,isitguaranteedtobeoptimal?

    What is the complexity of the search process in terms of time

    usage?Memoryusage?

    How can the interpreter most effectively reduce search

    complexity?

    How canan interpreterbedesigned tomosteffectivelyutilizea

    representationlanguage?

    Togetasuitableanswerforthesequestionssearchcanbestructured

    into threeparts.A firstpartpresentsasetofdefinitionsandconcepts

    thatlaythefoundationsforthesearchprocedureintowhichinductionis

    mapped.Thesecondpartpresentsanalternativeapproachesthathave

    beentakentoinductionasasearchprocedureandfinallythethirdpart

    present the version space as a general methodology to implement

    induction as a search procedure. If the search procedure contains the

    principles of the above three requirement parts, then the search

    algorithmcangiveaguaranteetogetanoptimalsolutionforthecurrent

    problem.

    3 GeneralProblemSolvingApproaches

    Thereexistquitealargenumberofproblemsolvingtechniquesin

    AI that rely on search. The simplest among them is the

    generateandtest method. The algorithm for the generateandtest

    methodcanbefom1allystatedinthefigure(1)follow.

    It is clear from the above algorithm that the algorithm continues the

    possibilityofexploringanewstate ineach iterationoftherepeatuntil

    loop and exits only when the current state is equal to the goal. Most

  • 8/4/2019 3ai

    36/76

    importantpartinthealgorithmistogenerateanewstate.Thisisnotan

    easytask.

    Procedure

    Generate

    &

    Test

    Begin

    Repeat

    Generateanewstateandcallitcurrentstate;

    Untilcurrentstate=Goal;

    End.

    Figure(1)GenerateandTestAlgorithm

    If generation of new states is not feasible, the algorithm should be

    terminated. Inoursimplealgorithm,we,however,didnot include this

    intentionallytokeepitsimplified.Buthowdoesonegeneratethestates

    of a problem? To formalize this, we define a four tuple, called state

    space,denotedby

    {nodes,arc,goal,current},

    where

    nodesrepresentthesetofexistingstatesinthesearchspace;

    an arc denotes an operator applied to an existing state to cause

    transition to another state; goal denotes the desired state to be

    identifiedinthenodes;andcurrentrepresentsthestate,nowgenerated

    for matching with the goal. The state space for most of the search

    problemstakestheformofatreeoragraph.Graphmaycontainmore

  • 8/4/2019 3ai

    37/76

    than one path between two distinct nodes, while for a tree it has

    maximumvalueofone.

    To

    build

    a

    system

    to

    solve

    a

    particular

    problem,

    we

    need

    to

    do

    four

    things:

    1. Definetheproblemprecisely.Thisdefinitionmustincludeprecisespecifications of what the initial situation(s) will be as well as

    what final situations constitute acceptable solutions to the

    problem.

    2. Analyzetheproblem.Afewveryimportantfeaturescanhaveanimmense impact on the appropriateness of various possible

    techniquesforsolvingtheproblem.

    3. Isolate and represent the task knowledge that is necessary tosolvetheproblem.

    4. Choosethebestproblemsolvingtechnique(s)andapplyit(them)totheparticularproblem.

    Measuring problemsolving performance is an essential matter in

    termofanyproblemsolvingapproach.Theoutputofaproblemsolving

    algorithmiseitherfailureorasolution.(Somealgorithmmightgetstuck

    in an infinite loop and never return an output.) We will evaluate an

    algorithm's

    performance

    in

    four

    ways:

    Completeness: Is the algorithm guaranteed to find asolutionwhenthereisone?

    Optimality:Doesthestrategyfindtheoptimalsolution? Timecomplexity:Howlongdoesittaketofindasoluon?1 Spacecomplexity:Howmuchmemoryisneededtoperform

    thesearch?

  • 8/4/2019 3ai

    38/76

    4 SearchTechnique

    Having formulatedsomeproblems,wenowneed tosolve them.

    Thisisdonebyasearchthroughthestatespace.Therootofthesearch

    treeisasearchnodecorrespondingtotheinitialstate.Thefirststepisto

    testwhetherthisisagoalstate.Becausethisisnotagoalstate,weneed

    to consider some other states. This is done by expanding the current

    state; that is, applying the successor function to the current state,

    therebygeneratinganewsetofstates.Nowwemustchoosewhichof

    thesepossibilitiestoconsiderfurther.Wecontinuechoosing,testingand

    expandingeitherasolution is foundor therearenomorestates tobe

    expanded. The choice of which state to expand is determined by the

    search strategy. It is important to distinguish between the state space

    and the search tree. For the route finding problem, there are only N

    states in the state space, one for each city. But there are an infinite

    numberofnodes.

    There are many ways to represent nodes, but we will assume that a node is

    a data structure with five components:

    STATE: the state in the state space to which the node corresponds

    PARENT-NODE: the node in the search tree that generated this

    node ACTION: the action that was applied to the parent to generate the

    node

    PATHCOST: the cost, traditionally denoted by g(n), of the path

    from the initial state to the node, as indicated by the parent

    pointers;and

    DEPTH:thenumberofstepsalongthepathfromtheinitialstate.

  • 8/4/2019 3ai

    39/76

    As usual, we differentiate between two main families of search

    strategies: systematic search and local search. Systematic search visitseachstatethatcouldbeasolution,orskipsonlystatesthatareshownto

    bedominatedbyothers,soitisalwaysabletofindanoptimalsolution.

    Localsearchdoesnotguaranteethisbehavior.Whenitterminates,afterhavingexhaustedresources(suchastimeavailableoralimitnumberof

    iterations), it reports the best solution found so far, but there is no

    guaranteethatitisanoptimalsolution.Toproveoptimality,systematic

    algorithmsarerequired,at theextracostof longerrunning timeswith

    respect to localsearch.Systematicsearchalgorithmsoftenscaleworse

    withproblemsizethanlocalsearchalgorithms.

    5 SearchTechniqueTypes

    Usuallytypesofintelligentsearchareclassifiedintothreeclasses;

    blind,heuristicand random search.Blind search isa technique to find

    thegoalwithoutanyadditional informationthathelpto inferthegoal,

    with this type there is no any consideration with process time or

    memory capacity. In theother side the heuristic search alwayshasan

    evaluating function calledheuristic functionwhichguidesand controls

    the behavior of the search algorithm to reach the goal with minimum

    cost,timeandmemoryspace.Whilerandomsearch isaspecialtypeof

    search in which it begins with the initial population that is generated

    randomly and the search algorithm will be the responsible for

    generatingthenewpopulationbasesonsomeoperationsaccordingtoa

    specialtypefunctioncalledfitnessfunction.Thefollowingsectionshave

    detailsofeachtypeofsearchwithsimpleexamples.

  • 8/4/2019 3ai

    40/76

    5.1BlindSearch

    There many search strategies that come under the heading of

    blind

    search

    (also

    called

    uniformed

    search).

    The

    term

    means

    that

    they

    havenoadditionalinformationaboutstatesbeyondthatprovidedinthe

    problem definition. All they can do is generate successors and

    distinguishagoalstatefromanongoalstate.

    Thus blind search strategies have not any previous information

    aboutthegoalnorthesimplepaths leadto it.Howeverblindsearch is

    not bad, since more problems or applications need it to be solved; in

    other words there are some problems give good solutions if they are

    solvedbyusingdepthorbreadthfirstsearch.

    Breadth first search is a simple strategy in which the root node is

    expanded first, then all the successors of the root nodeareexpanded

    next, then their successors, and so on. In general all the nodes are

    expandedatagivendepth in the search treebeforeanynodesat the

    next level are expanded. Breadth first search can be implemented by

    calling TREESEARCH with any empty fringe that is a firstinfirstout

    (FIFO) queue, assuring that the nodes that are visited first will be

    expanded first. In other words, calling TREESEARCH (problem,

    FIFOQUEUE) result in a breadth first search. The FIFO queue puts all

    newlygeneratedsuccessorsattheendofthequeue,whichmeansthat

    shallownodesareexpandedbeforedeepernodes.

    Depth first search always expands the deepest node in the current

    fringe of the search tree. The search proceeds immediately to the

    deepest

    level

    of

    the

    search

    tree,

    where

    the

    nodes

    have

    no

    successors.

    As

  • 8/4/2019 3ai

    41/76

    thosenodes areexpanded, theyaredropped from the fringe, so then

    the search backs up to the next shallowest node that still has

    unexplored successors. This strategy can be implemented by

    TREESEARCHwithalastinfirstout(LIFO)queue,alsoknownasastack.

    AsanalternativetotheTREESEARCH implementation, it iscommonto

    implementdepthfirstsearchwitharecursivefunctionthatcallsitselfon

    eachofitschildreninturn.

    Figure(2)BlindSearchTree

    Thepathsfromtheinitialstate(A)tothegoalstate(H)are:

    UsingBreadthfirstsearch ABCDEFGH

    UsingDepthfirstsearch ABDGEH

    A

    CB

    I

    D

    G H

    FE

    Initialstate

    Goalstate

  • 8/4/2019 3ai

    42/76

    5.2HeuriscSearch

    Classicallyheuristicsmeansruleofthumb.Inheuristicsearch,we

    generally

    use

    one

    or

    more

    heuristic

    functions

    to

    determine

    the

    better

    candidate states among a set of legal states that could be generated

    from a known state. The heuristic function, in other words, measures

    thefitnessofthecandidatestates.Thebettertheselectionofthestates,

    the fewer will be the number of intermediate states for reaching the

    goal.However;themostdifficulttaskinheuristicsearchproblemsisthe

    selectionoftheheuristicfunctions.Onehastoselectthemintuitively,so

    thatinmostcaseshopefullyitwouldbeabletoprunethesearchspace

    correctly.

    Thesearchprocesses thatwillbedescribed in thischapterbuildon

    the fact that thesearchdoesnotproceeduniformlyoutward from the

    start node: instead, it proceeds preferentially through nodes that

    heuristic, problemspecific information indicates might be on the best

    pathtoagoal.Wecallsuchprocessesbestfirstorheuristicsearch.Here

    isthebasicidea.

    1. We assume that we have a heuristic (evaluation) function,f. tohelp decide which node is the best one to expand next. This

    function isbasedon informationspecifictotheproblemdomain.

    Itisrealvaluedfunctionofstatedescriptions.

    2. Expand next that node, n, having the smallest value of f(n).Resolvetiesarbitrarily.

    3. Terminatewhenthenodetobeexpandednextisagoalnode.A function which applies such an algorithm to nodes and assigns a

    value to them accordingly is a heuristic function. Determining good

  • 8/4/2019 3ai

    43/76

    heuristics is very often the most difficult element involved in solving

    complexproblemsinthesymbolicartificialintelligencetradition.

    Therefore,

    heuristic

    can

    be

    defined

    as

    the

    study

    of

    the

    methods

    and

    rulesofdiscoveryandinvention.

    AIproblemssolversemployheuristicsintwobasicsituations:

    1. A problem may not have an exact solution because of inherentambiguities in the problem statement or available data. A given

    setofsymptomsmayhaveseveralpossiblecauses.

    2. Aproblemmayhaveanexactsolution,butthecomputationalcostof finding it may be prohibitive. In many problems state space

    growth iscombinatoriallyexplosive,with thenumberofpossible

    statesincreasingexponentiallyorfactoriallywiththedepthofthe

    search.

    Inordertosolvemanyhardproblemsefficiently,itisoftennecessary

    to compromise the requirements of mobility and systematicity and to

    construct a control structure that is no longer guaranteed to find the

    best answer but that will almost always find a very answer. Thus we

    introducetheideaofaheuristic.Aheuristicisatechniquethatimproves

    the efficiency of a search process, possibly by sacrificing claims of

    completeness. Heuristics are like tour guides. They are good to the

    extentthattheypointingenerallyinterestingdirections;theyarebadto

    theextentthattheymaymisspointsofinteresttoparticularindividuals.

    Some heuristics help to guide a search process without sacrificing any

    claims to completeness that the process might previously have had.

    Others (in fact, many of the best ones) may occasionally cause an

  • 8/4/2019 3ai

    44/76

    excellentpathtobeoverlooked.But,ontheaverage,they improvethe

    quality of the paths that are explored. Using good heuristics, we can

    hope to get good (though possibly nonoptimal) solutions to hard

    problems,suchasthetravelingsalesman,inlessthanexponentialtime.

    There are some good generalpurpose heuristics that are useful in a

    widevarietyofproblemdomains.Inaddition,it ispossibletoconstruct

    specialpurpose heuristics that exploit domainspecific knowledge to

    solveparticularproblems.

    Performance of the search can be drastically improved by using

    specificknowledgeaboutasearchproblemtoguidethedecisionsmade

    where to search. This knowledge can be presented in the form of

    heuristicsrulesthatguidethedecisionmakingduringthesearch.Thisgivesusanotherclassofheuristicsearchalgorithms. It isquiteobvious

    thatheuristicswilldependverymuchontheclassofsolvableproblems.

    The heuristic rules class is considered one of the more important and

    complex way to guide the search procedure in which to reach the

    solution (goalstate)suchclassusuallyused inembeddedsystemso to

    formwhattodayknownassearchingwithheuristicembeddedinrules.

    Thesearchforplans isguidedbyheuristicsthatprovideanestimate

    of

    the

    cost

    (heuristic

    value)

    that

    are

    extracted

    automatically

    from

    the

    problemencodingP. Inorder tosimplify thedefinitionof someof the

    heuristics, we introduce in some cases a new dummy End action withzerocost,whosepreconditionsG1,...,Gnarethegoalsoftheproblem,andwhoseeffect isadummyatomG.Insuchcases,wewillobtainthe

    heuristic estimate h(s) of the cost from state s to the goal, from theestimateh(G;s)ofachievingthedummyatomGfroms.Inthesection

  • 8/4/2019 3ai

    45/76

    3.7adetaildescriptionabouttheheuristicsearchalgorithmswithsimple

    examples.

    5.3

    Random

    Search

    Random Search is a method of searching with no planned

    structure or memory. This method has the same intent of the typical

    intuitive search, but the opposite strategy to achieve it. A random

    technique may be preferred if it takes more time to devise a better

    technique,ortheadditionalworkinvolvedisnegligible.

    RandomSearch (RS) isapopulationbasedsearchalgorithm. It is

    first proposed by Price and also called Price's algorithm. RS first

    randomlygeneratesapopulationofsamplesfromtheparameterspace,

    andthenusesdownhillsimplexmethodtomovethepopulationtowards

    theglobaloptima.Supposeandimensionalobjective function is tobe

    optimized,thebasicRSalgorithmcanbedescribedasfollows:

    1. Randomlygenerateapopulationofmpointsfromtheparameterspace.

    2. Randomly select n+1 points from the population and make adownhillsimplexmove.

    3. If the new sample is better than the worst member in thepopulation, then the worst member is replaced with this new

    sample.

    4. Repeat the above process until a certain stopping criterion issatisfied.

    InRS,randomsamplingisusedforexplorationanddownhillsimplexfor

    exploitation.

    Its

    balance

    strategy

    first

    executes

    exploration

    and

    then

  • 8/4/2019 3ai

    46/76

    switchescompletelytoexploitation.Sinceexplorationisonlyperformed

    inthebeginningofthesearch,theconvergencetotheglobaloptima is

    notguaranteed.Theproblemofgettingtrapped ina localextremecan

    bealleviatedbyusinga largepopulationor introducingnewmembers

    intothepopulationwithrandomsamplingduringthesearch.Despiteof

    thisdisadvantage,RShasproved tobeveryeffective inpracticeand is

    widelyused.Inaddition,sincethereisnolocalsearchmethodinvolved,

    RSismorerobusttonoiseintheobjectivefunction.Besidesitsfailingto

    provide the convergence guarantee, another disadvantage is its low

    efficiency. Especially in the beginning, the population is composed of

    randomsamples

    andRSessentiallyperformslikeapurerandomsampling.Therefore,for

    manysituationsinpracticewhereitisdesiredtoobtainagoodsolution

    quickly,RSmaynotbeabletofulfilltheobjective.

    TomakesurethatRSbeingwithsuitablesolutionsthealgorithm

    mustbeevolvedtodecidethefollowingfeatures:

    High efficiency is required for the desired search algorithm. More

    specifically,theemphasisofthesearchalgorithmshouldbeonfindinga

    betteroperatingpointwithinthe limitedtimeframe insteadofseeking

    thestrictlyglobaloptimum.

    Highdimension isanotherfeature insolveproblems.Highdimensional

    optimization problems are usually much more difficult to solve than

    lowdimensionalproblemsbecauseofcurseofdimensionality.

  • 8/4/2019 3ai

    47/76

    6 HeuristicSearchAlgorithms

    In this section, we can saw that many of the problems that fall

    withinthepurviewofartificialintelligencearetoocomplextobesolved

    by direct techniques; rather they must be attacked by appropriate

    searchmethodsarmedwithwhateverdirecttechniquesareavailableto

    guide the search. These methods are all varieties of heuristic search.

    They can be described independently any particular task or problem

    domain.ButwhenappliedtoParticularproblems,theirefficacyishighly

    dependentonthewaytheyexploitdomainspecificknowledgesince in

    and of themselves they are unable to overcome the combinatorial

    explosiontowhichsearchprocessesaresovulnerable.For thisreason,

    thesetechniquesareoftencalledweakmethods.Althougharealization

    of the limited effectiveness of these weak methods to solve hard

    problems by themselves has been an important result that emerged

    from the last decades of AI research, these techniques continue to

    provide the framework into which domainspecific knowledge can be

    placed,eitherbyhandorasaresultofautomaticlearning.

    Hill climbing is a variant of generateandtest in which feedback from

    thetestprocedureisusedtohelpthegeneratordecidewhichdirection

    tomoveinthesearchspace.Inapuregenerateandtestprocedure,the

    test functionrespondswithonlyayesorno.but if the test function is

    augmented with a heuristic function that provide an estimate of how

    closeagivenistoagoalstate.Thisisparticularlynicebecauseoftenthe

    computationoftheheuristicfunctioncanbedoneatalmostnocostat

    the same time that the test for a solution is being performed. Hill

    climbing is often used when a good heuristic function is available for

  • 8/4/2019 3ai

    48/76

    evaluatingstatesbutwhennootherusefulknowledge isavailable.For

    example,supposeyouare inanunfamiliarcitywithoutamapandyou

    want to get downtown. You simply aim for the tall buildings. The

    heuristic function isjustdistancebetweenthecurrent locationandthe

    locationofthetallbuildingsandthedesirablestatesarethoseinwhich

    thisdistanceisminimized.

    Nowletusdiscussanewheuristicmethodcalledbestfirstsearch,which

    is a way of combining the advantages of both depthfirst and

    breadthfirstsearchintoasinglemethod.

    Theactualoperationofthealgorithmisverysimple.Itproceedsinsteps,

    expanding one node at each step, until it generates a node that

    correspondstoagoalstate.Ateachstep,itpicksthemostpromisingof

    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

    checkingtoseeifanyofthemhavebeengeneratedbefore.Bydoingthis

    check,wecanguaranteethateachnodeonlyappearsonceinthegraph,

    althoughmanynodesmaypointtoitasasuccessors.Thenthenextstep

    begins.

    The figure (3) bellow illustrates the hill climbing steps algorithm as it

    describedintreedatastructure.

    Also the figure (4) bellow shows the steps of the best first search

    algorithmonagiventreeasanassumptionsearchspace.

  • 8/4/2019 3ai

    49/76

    Figure (3) Hill Climbing Search Tree

    Figure (3) Hill Climbing Search Tree

    A

    Step1

    A

    DCB

    3 5 1

    Step2

    A

    C DB

    E F

    3 5

    7 6

    Ste 3

    A

    C DB

    EF

    (3) (5)

    (7)

    Step4

    EF(2) (1)

  • 8/4/2019 3ai

    50/76

    Figure (4) Best First Search Tree

    Step1

    A

    DCB (3) (5) (1)

    Step2

    A

    C DB (3) (5)

    (4) (6)

    Step3

    C DB

    F

    (5)

    (4) (6)

    Step4

    G H(6) (5)

    Step5

    A

    C D

    E

    (5)

    (4)(6) (5)

    I J (1)(2)

  • 8/4/2019 3ai

    51/76

    The first advance approach to the best first search is known as

    Asearch algorithm.Aalgorithm is simply defineasa best first search

    plusspecificfunction.Thisspecificfunctionrepresenttheactualdistance

    (levels)betweentheinitialstateandthecurrentstateandisdenotedby

    g(n).Anoticewillbementionedherethatthesamestepsthatareused

    inthebestfirstsearchareusedinanAalgorithmbutinadditiontothe

    g(n)asfollow;

    F(n) h(n) + g(n) where h(n) is a heuristic function. The second

    advance approach to the best first search is known as A*search

    algorithm. A* algorithm is simply define as a best first search plus

    specific function. This specific function represent the actual distance

    (levels)betweenthecurrentstateandthegoalstateand isdenotedby

    h(n).Thisalgorithmwillbedescribedindetailinthenextsectionofthis

    chapter.

    But just as it was necessary to modify our search procedure

    slightly to handle both maximizing and minimizing players, it is also

    necessary to modify the branchandbound strategy to include two

    bounds, one for each of players. This modified strategy is called

    alphabetapruning. It requires themaintenanceof two threshold,one

    representing

    a

    lower

    bound

    on

    the

    value

    that

    a

    maximizing

    node

    may

    ultimatelybeassigned(wecallthisalpha)andanotherrepresentingan

    upperboundon thevalue thatminimizingnodemaybeassigned (this

    wecallbeta).

    Theeffectivenessofthealphabetaproceduredependsgreatlyon

    theorderinwhichpathsareexamined.Iftheworstpathsareexamined

    first,thennocutoffsatallwilloccur.But,ofcourse,ifthebestpathwere

  • 8/4/2019 3ai

    52/76

    known inadvanceso that itcouldbeguaranteed tobeexamined first,

    wewouldnotneed tobotherwiththesearchprocess. If,however,we

    knew how effective the pruning technique is in the perfect case, we

    wouldhaveanupperboundonitsperformanceinothersituations.Itis

    possible to prove that if the nodes are perfectly ordered, then the

    number of terminal nodes considered by a search to depth d using

    alphabeta pruning is approximately equal to twice the number of

    terminalnodesgeneratedbyasearchtodepthd/2withoutalphabeta.

    A doubling of the depth to which the search can be pursued is a

    significantgain.Eventhoughallofthisimprovementcannottypicallybe

    realized, the alphabeta technique is a significant improvement to the

    minimaxsearchprocedure.

    7 A*Search:minimizingthetotalestimatedsolutioncost

    The most widelyknown form of bestfirst search is called A*

    search (pronounced "Astar search"). It evaluates nodes by combining

    g(n),thecosttoreachthenode,andh(n),thecosttogetfromthenode

    tothegoal:

    f(n)=g(n)+h(n).

    Sinceg(n)givesthepathcostfromthestartnodetonoden,andh(n)is

    theestimatedcostofthecheapestpathfromntothegoal,wehave

    f(n)=estimatedcostofthecheapestsolutionthroughn .

    Thus,ifwearetryingtofindthecheapestsolution,areasonablethingto

    tryfirstisthenodewiththelowestvalueofg(n)+h(n).Itturnsoutthat

    this strategy is more thanjust reasonable: provided that the heuristic

    function h(n) satisfies certain conditions, A* search is both complete

  • 8/4/2019 3ai

    53/76

    and optimal. The optimality of A* is straightforward to analyze if it is

    used with TREESEARCH. In this case, A* is optimal if h(n) is an

    admissibleheuristicthat is,provided thath(n)neveroverestimatesthe

    cost to reach the goal. Admissible heuristics are by nature optimistic,

    becausetheythinkthecostofsolvingtheproblemislessthanitactually

    is.Sinceg(n)istheexactcosttoreachn,wehasimmediateconsequence

    thatf(n)neveroverestimatesthetruecostofasolutionthroughn.

    7.1A* SearchAlgorithm

    Aswementionedbefore,theA*algorithmtobediscussedshortly

    is a complete realization of the best first algorithm that takes into

    account these issues in detail. The following definitions, however, are

    requiredforrepresentingtheA*algorithm.Theseareinorder.

    Definion3.1:Anodeiscalledopenifthenodehasbeengeneratedand

    theh'(x)hasbeenappliedoveritbutithasnotbeenexpandedyet.

    Definion 3.2: A node is called closed if it has been expanded for

    generatingoffsprings.

    InordertomeasurethegoodnessofanodeinA*algorithm,werequire

    twocostfunctions:

    Heuristiccost.

    Generationcost.

    The heuristic cost measures the distance of the current node x

    withrespecttothegoalandisdenotedbyh(x).Thecostofgeneratinga

    node x, denoted by g(x), on the other hand measures the distance of

    node x with respect to the starting node in the graph. The total cost

  • 8/4/2019 3ai

    54/76

    function at node x, denoted by f(x), is the sum of g(x) plus h(x). The

    generation cost g(x) can be measured easily as we generate node x

    through a few slate transitions. For instance, if node x was generated

    fromthestartingnodethroughmstatetransitions,thecostg(x)willbe

    proportionaltom(orsimplym).Buthowdoesoneevaluatetheh(x)?It

    may be recollected thath(x) is thecostyet tobe spent to reach the

    goal from the current node x. Obviously, any costwe assign as h(x) is

    throughprediction.Thepredictedcost forh(x) isgenerallydenotedby

    h'(x).Consequently,thepredictedtotalcostisdenotedbyf(x),where:

    f'(x)=g(x)+h'(x).

    7.2A*Procedure

    Here are the basic steps that are considered to implement the A*

    proceduretosolveproblemsinanintelligentmanner:

    1. Operations on states generate children of the state currentlyunderexamination.

    2. Eachnewstate ischeckedtoseewhether ithasoccurredbeforetherebypreventingloops.

    3. Eachstatenisgivenanfvalueequaltothesumofitsdepthinthesearchspaceg(n)andaheuristicestimateofitsdistancetoagoal

    h(n).

    4. Statesonopenaresortedbytheirfexaminedoragoal.5. As an implementation point, the algorithms can be improved

    throughmaintenanceofperhapsasheapsorleftisttrees.

  • 8/4/2019 3ai

    55/76

    8 TheAlphaBetaSearchAlgorithm

    Theideaforalphabetasearchissimple:ratherthansearchingthe

    entire space to the ply depth, alphabeta search proceeds in a

    depthfirst fashion. Two values, called alpha and beta, are created

    during thesearch.ThealphavalueassociatedwithMAXnodes,can

    neverdecrease,and thebetavalueassociatedwithMINnodes,can

    neverincrease.Tworulesforterminatingsearch,basedonalphaand

    betavalues,are:

    1. SearchcanbestoppedbelowanyMINnodehavingabetavaluelessthanorequaltothealphavalueofanyofitsMAXancestors.

    2. Search can be stopped below any MAX node having an alphavaluegreater thanorequal tothebetavalueofanyof itsMIN

    nodeancestors.

    Alphabetapruningthusexpressesarelationbetweennodesatplyn

    andnodesatplyn+2underwhichenresubtreesrootedatleveln+

    1 can be eliminated from consideraon. Note that the resulng

    backedup value is identical to the minimax result and the search

    savingoverminimax isconsiderable.With fortuitousorderingstates

    in the search space, alphabeta caneffectivelydouble thedepthof

    the search considered with a fixed space/time computer

    commitment.Ifthereisaparticularunfortunateordering,alphabeta

    searchesnomoreof thespace thannormalminimax;however, the

    searchisdoneinonlyonepass.

  • 8/4/2019 3ai

    56/76

    8.1TheAlphaBetaCutoffProcedure(TreePruning)

    c(n)=M(n)O(n)

    WhereM(n)=numberofmypossiblewinninglines.

    Now, we will discuss a new type of algorithm, which does not

    require expansion of the entire space exhaustively. This algorithm is

    referred toasalphabetacutoffalgorithm. In thisalgorithm, twoextra

    ply of movements are considered to select the current move from

    alternatives.

    Alpha

    and

    beta

    denote

    two

    cutoff

    levels

    associated

    with

    MAXandMINnodes.AsitismentionedbeforethealphavalueofMAX

    nodecannotdecrease,whereasthebetavalueoftheMINnodescannot

    increase.Buthowcanwecomputethealphaandbetavalues?Theyare

    the backed up values up to the root like MINIMAX. There are a few

    interestingpointsthatmaybeexploredatthisstage.Priortotheprocess

    ofcomputingMAX /MINof thebackedupvaluesof the children, the

    alphabeta cutoffalgorithm estimates e(n)at' all fringe nodes n. Now,

    the values are estimated following the MINIMAX algorithm. Now, to

    prunetheunnecessarypathsbelowanode,checkwhether:

    The beta value of any MIN node below a MAX node is less than or

    equal to its alpha value. If yes. prune that path below the MIN node.

    The alpha value of any MAX node below a MIN node exceeds the

    beta value of the MIN node. if yes prune the nodes below the MAX

    node.

    Based on the above discussion, we now present the main steps in the -

    search algorithm.

  • 8/4/2019 3ai

    57/76

    1. Create a new node, if it is the beginning move, c1seexpand the

    existing tree by depth first manner. To make a decision about the

    selection of a move at depth d, the tree should be expanded at least

    up to a depth (d + 2).

    2. Compute e(n) for all leave (fringe) nodes n in the tree.

    3. Computermin (for max nodes) and max values (for min nodes) at the

    ancestors of the fringe nodes by the following guidelines. Estimate

    the minimum of the values (e or) possessed by the children of a

    MINIMIZER node N and assign it its max value. Similarly, estimate

    the maximum of the values (e or) possessed by the children of a

    MAXIMIZER node N and assign it its min value.

    4. If the MAXIMIZER nodes already possess min values, then their

    current min value = Max (min value, min,); on tile other hand, if the

    MANIMIZER nodes already possess max values, then their current

    max value = MIN (max value, max).

    5. If the estimated max value of a MINIMIZER node N is less than themin value of its parent MAXIMIZER node N' then there is no need

    to search below the node MINIMIZER node N. Similarly, if the min

    value of a MAXIMIZER node N is more than the max value of its

    parent node N then there is no need to search below node N.

  • 8/4/2019 3ai

    58/76

    1. Introduction to Expert Systems

    Expertsystemsarecomputerprogramsthatareconstructedtodo

    the kinds of activities that human experts can do such as design,

    compose, plan, diagnose, interpret, summarize,audit, give advice. The

    worksuchasystemisconcernedwithistypicallyataskfromtheworlds

    ofbusinessorengineering/scienceorgovernment.

    Expertsystemprogramsareusuallysetuptooperateinamanner

    that

    will

    be

    perceived

    as

    intelligent:

    that

    is,

    as

    if

    there

    were

    a

    human

    expertontheothersideofthevideoterminal.

    A characteristic body of programming techniques give these

    programs their power. Expert systems generally use automated

    reasoningandthesocalledweakmethods,suchassearchorheuristics,

    to do their work. These techniques are quite distinct from the well

    articulated algorithms and crisp mathematical procedures more

    traditionalprogramming.

    Figure(1)thevectorsofexpertsystemdevelopment

    Expert

    NewProgramming

    Methodfor

  • 8/4/2019 3ai

    59/76

    AsshowninFigure(1),thedevelopmentofexpertsystemsisbased

    ontwodistinct,yetcomplementary,vectors:

    a.

    New

    programming

    technologies

    that

    allow

    us

    to

    deal

    with

    knowledge

    andinferencewithease.

    b. New design and development methodologies that allow us to

    effectivelyusethesetechnologiestodealwithcomplexproblems.

    The successful development of expert systems relies on a well

    balancedapproachtothesetwovectors.

    2. Expert System Using

    Here is a short nonexhaustive list of some of the things expert

    systemshavebeenusedfor:

    To approve loan applications, evaluate insurance risks, and

    evaluateinvestmentopportunitiesforthefinancialcommunity.

    Tohelpchemistsfindthepropersequenceofreactionstocreate

    newmolecules.

    Toconfigure thehardwareandsoftware inacomputertomatch

    theuniquearrangementsspecifiedbyindividualcustomers.

    Todiagnoseand locate faults ina telephonenetwork from tests

    andtroublereports.

    Toidentifyandcorrectmalfunctionsinlocomotives.

    Tohelpgeologists interpretthedatafrom instrumentationatthe

    drilltipduringoilwelldrilling.

    Tohelpphysiciansdiagnoseandtreatrelatedgroupsofdiseases,

    suchasinfectionsofthebloodorthedifferentkindsofcancers.

  • 8/4/2019 3ai

    60/76

    To help navies interpret hydrophone data from arrays of

    microphonesontheoceanfloorthatareusedt\uthesurveillance

    ofshipsinthevicinity.

    To figure out a chemical compound's molecular structure from

    experiments with mass spectral data and nuclear magnetic

    resonance.

    Toexamineandsummarizevolumesofrapidlychangingdatathat

    aregeneratedtoolastforhumanscrutiny,suchastelemetrydata

    fromlandsatsatellites.

    Mostoftheseapplicationscouldhavebeendoneinmoretraditional

    waysaswellas throughanexpert system,but inall these cases there

    wereadvantagestocastingthemintheexpertsystemmold.

    In some cases, this strategy made the program more human

    oriented.Inothers,itallowedtheprogramtomakebetterjudgments.

    In others, using an expert system made the program easier to

    maintainandupgrade.

    3. Expert Systems are Kind of AI Programs

    Expert systems occupy a narrow but very important corner of the

    entireprogrammingestablishment.Aspartofsayingwhattheyare,we

    need to describe their place within the surrounding framework of

    establishedprogrammingsystems.

    Figure(2) shows the three programming styles that will most

    concern us. Expert systems are part of a larger unit we might call AI

    (artificial intelligence)programming. Proceduralprogramming iswhat

    everyone learns when they first begin to use BASIC or PASCAL or

  • 8/4/2019 3ai

    61/76

    FORTRAN. Procedural programming and A.I programming are quite

    differentinwhattheytrytodoandhowtheytrytodoit.

    Figure(2)threekindsofprogrammingIn traditional programming (procedural programming), the

    computerhastobetoldingreatdetailexactlywhattodoandhowtodo

    it.Thisstylehasbeenverysuccessfulforproblemsthatarewelldefined.

    Theyusuallyarefound indataprocessingor inengineeringorscientific

    work.

    AI programming sometimes seems to have been defined by

    default,asanything thatgoesbeyondwhat iseasy todo in traditional

    procedural programs, but there are common elements in most AI

    programs. Whatcharacterizesthesekindsofprogramsisthattheydeal

    with complex problems that are often poorly understood, for which

    there is no crisp algorithmic solution, and that can benefit from some

    sortofsymbolicreasoning.

    Procedural

    Expertsystems

    programming

  • 8/4/2019 3ai

    62/76

    There are substantial differences in the internal mechanisms of

    the computer languages used for these two sorts of problems.

    Procedural programming focuses on the use of the assignment

    statement("="or":")formovingdatatoandfromfixed,prearranged,

    named locations in memory. These named locations are the program

    variables.Italsodependsonacharacteristicgroupofcontrolconstructs

    thattellthecomputerwhattodo.Controlgetsdonebyusing

    ifthenelse goto

    dowhile procedurecalls

    repeatuntil sequentialexecution(asdefault)

    AIprogramsareusuallywritten in languages likeLispandProlog.

    Program variables in these languages have an ephemeral existence on

    the stack of the underlying computer rather than in fixed memory

    locations.Datamanipulation isdone throughpatternmatchingand list

    building.Thelisttechniquesaredeceptivelysimple,butalmostanydata

    structure can be built upon this foundation. Many examples of list

    buildingwillbeseenlaterwhenwebegintouseProlog.AIprogramsalso

    useadifferentsetofcontrolconstructs.Theyare:

    procedurecalls

    sequentialexecution

    recursion

  • 8/4/2019 3ai

    63/76

    4. Expert System, Development Cycle

    The explanation mechanism allows the program to explain its

    reasoning to the user, these explanations includejustification for the

    system'sconclusions,explanationof whythesystemneedsaparticular

    pieceofdata.WhyquesonsandHowquesons.Figure(3)belowshows

    theexploratorycycleforrulebasedexpertsystem.

    Figure(3)Theexploratorycycleforexpertsystem

    failedYesNo

    No

    Begin

    Defineproblemsandgoals

    Designandconstructprototype

    Test/usesystem

    Analyzeandcorrectshortcoming

    Final

    evaluation

    Readyfor

    final

    evaluation

    Aredesign

    assumptions

  • 8/4/2019 3ai

    64/76

    5. Expert System Architecture and Components

    Thearchitectureoftheexpertsystemconsistsofseveralcomponentsas

    showninfigure(4)below:

    Figure(4)Expertsystemarchitecture

    5.1. User Interface

    Theuserinteractswiththeexpertsystemthroughauserinterface

    thatmakeaccessmorecomfortable for thehumanandhidesmuchof

    the system complexity. The interface styles includes questions and

    answers,menudriver,naturallanguages,orgraphicsinterfaces.

    Explanation

    processor

    Inference

    engine

    User

    Interface

    Knowledge

    base

    Working

    memory

  • 8/4/2019 3ai

    65/76

    5.2. Explanation Processor

    Theexplanationpartallows theprogram toexplain its reasoning

    to the user. These explanations includejustifications for the system's

    conclusion (HOW queries), explanation of why the system needs a

    particularpieceofdata(WHYqueries).

    5.3. Knowledge Base

    The heart of the expert system contains the problem solving

    knowledge (which defined as an original collection of processed

    information) of the particular applications, this knowledge is

    representedinseveralwayssuchasifthenrulesform.

    5..4 Inference Engine

    The inference engine applies the knowledge to the solution of

    actual problems. It s the interpreter for the knowledge base. The

    inferenceengineperformstherecognizeactcontrolcycle.

    Theinferenceengineconsistsofthefollowingcomponents:

    1. Ruleinterpreter.

    2. Scheduler

    3. HOWprocess

    4. WHYprocess

    5. knowledgebaseinterface.

  • 8/4/2019 3ai

    66/76

    5.5. Working Memory

    It is a part of memory used for matching rules and calculation.

    Whentheworkisfinishedthismemorywillberaised.

    6. Systems that Explain their Actions

    An interfacesystem thatcanexplain itsbehaviorondemandwillseem

    muchmorebelievableand intelligent to itsusers. Ingeneral, thereare

    twothingsausermightwanttoknowaboutwhatthesystem isdoing.

    When thesystemasks forapieceofevidence, theusermightwant to

    ask,

    "Whydoyouwantit?"

    When the system statesa conclusion, theuserwill frequentlywant to

    ask,

    "Howdidyouarriveatthatconclusion?"

    Thissectionexploressimplemechanismsthataccommodateboth

    kindsofquestioning.HOWandWHYquestionsaredifferent in several

    rather obvious ways that affect how they can be handled in an

    automatic reasoning program. There are certain natural places where

    these questions are asked, and they are at opposite ends of the

    inferencetree.ItisappropriatetolettheuseraskaWHYquestionwhen

    thesystem isworkingwith implicationsatthebottomofthetree;that

    is:whenitwillbenecessary toasktheusertosupplydata.

  • 8/4/2019 3ai

    67/76

    Thesystem neverneedstoaskforadditionalinformationwhenit

    is working in the upper parts of the tree. These nodes represent

    conclusionsthatthesystemhasfiguredout.ratherthanaskedfor.soa

    WHYquestionisnotpertinent.

    To be able to make the conclusions at the top of the tree,

    however,isthepurposeforwhichallthereasoningisbeingdone. The

    system is trying todeduce informationabout theseconclusions. It is

    appropriatetoaskaHOWquestionwhenthesystemreportstheresults

    ofitsreasoningaboutsuchnodes.

    There is also a difference in timing of the questions. WHY

    questions will be asked early on and then at unpredictable points all

    throughout the reasoning. The system asks for information when it

    discovers that it needs it. The. time for the HOW questions usually

    comesattheendwhenallthereasoning iscompleteandthesystem is

    reportingitsresults.

  • 8/4/2019 3ai

    68/76

    1- Breadth First Search Algorithm

    Function Breadth First Search

  • 8/4/2019 3ai

    69/76

    Example with Breadth First Search:

    A trace of breadth_first_search on the graph of Figure above follows.

    Each successive number, 2,3,4, ... , represents an iteration of the "while"

    loop. U is the goal state.

  • 8/4/2019 3ai

    70/76

    2- Depth First Search Algorithm

    Function Depth First Search

    Example with Depth First Search:

    A trace of depth_first_search on the graph of Figure above appears

    below. Each successive iteration of the "while" loop is indicated by a single

    line (2, 3, 4, ...). The initial states of open and closed are given on line 1.

    Assume U is the goal state.

  • 8/4/2019 3ai

    71/76

  • 8/4/2019 3ai

    72/76

    3- Beast First Search Algorithm

    Function Best First Search

  • 8/4/2019 3ai

    73/76

    Example with Best First Search

    A trace of the execution of best first search on this graph appears above.

    Suppose P is the goal state in the graph. Became P is the goal, states along

    the path to P and to have low heuristic values. The heuristic is fallible: the

    state O has a lower value than the goal itself and is examined first. Unlike

    hill climbing, which does not maintain apriority queue for the selection of

    "next" states, the algorithm recovers from this error and errors the correct

    goal.

  • 8/4/2019 3ai

    74/76

    4- Implementing Heuristic Evaluation Functions

    We now evaluate the performance of several different heuristics for solving

    the 8-puzzle. Figure below shows a start and goal state for the 8-puzzle,

    along with the first three states generated in the search.

  • 8/4/2019 3ai

    75/76

    The simplest heuristic counts the tiles out of place in each state when it is

    compared with the goal.

    To summarize:

    1. Operations on states generate children of the state currently under

    examination.

    2. Each new state is checked to see whether it has occurred before (is on

    either open or closed), thereby preventing loops.

    3. Each state n is given an I value equal to the sum of its depth in the search

    space g(n) and a heuristic estimate of its distance to a goal h(n). The h value

    guides search toward heuristically promising states while the g value

    prevents search from persisting indefinitely on a fruitless path.

  • 8/4/2019 3ai

    76/76

    4. States on open are sorted by their f values. By keeping all states on open

    until they are examined or a goal is found, the algorithm recovers from dead

    ends.

    5. As an implementation point, the algorithm's efficiency can be improvedthrough maintenance of the open and closed lists, perhaps as heaps or leftist

    trees.