Você está na página 1de 23

Artificial Intelligence is a branch of Science which deals with helping machines find solutions to complex problems in a more human-like

fashion. The study and design of intelligent agents where an intelligent agent is a system that perceives its environment and takes actions that maximize its chances of success.

The potential applications of Artificial Intelligence are abundant. Military for autonomous control and target identification. Entertainment industry for computer games. Hospitals, banks and insurance s, who can use AI to predict customer behavior and detect trends.

Expert systems Robotics Computer Vision and Image Processing Neural Networks

Rule-based expert systems (Newell and Simon, 1972) Expert System is an area of AI that explores how to computerize the expertise of a human expert. Five Major Players are:

The The The The The

project manager end-user domain expert knowledge engineer programmer

A rule-based expert system has five components:


Knowledge base Database Inference engine Explanation facilities User interface.

The main players of the expert system development team

Main players in expert system: The project manager is the leader, responsible for keeping the project on track

The end-user is a person who uses the expert system when it is developed. The domain expert is a knowledgeable and skilled person capable of solving problems in a specific area or domain. The knowledge engineer is someone who is capable of designing, building and testing an expert system. The programmer is the person responsible for the actual programming, describing the domain knowledge in terms that a computer can understand. The programmer also is involved in testing the expert system.

Structure of rule-based expert system

The knowledge base contains the domain knowledge useful for problem solving. In a rule-based expert system, the knowledge is represented as a set of rules. Each rule specifies a relation, recommendation, directive, strategy or heuristic and has the IF (condition) THEN (action) structure. When the condition part of a rule is satisfied, the rule is said to fire and the action part is executed.
The database includes a set of facts used to match against the IF (condition) parts of rules stored in the knowledge base. The inference engine carries out the reasoning whereby the expert system reaches a solution. It links the rules given in the knowledge base with the facts provided in the database. The explanation facilities enable the user to ask the expert system how a particular conclusion is reached and why a specific fact is needed. An expert system must be able to explain its reasoning and justify its advice, analysis or conclusion. The user interface is the means of communication between a user seeking a solution to the problem and an expert system. The communication should be as meaningful and friendly as possible.

An expert system shell can be considered as an expert system with the knowledge removed. Therefore, all the user has to do is to add the knowledge in the form of rules and provide relevant data to solve a problem.
Prolog is an AI shell.

Objects used in Prolog

Values

Variables(start with capitale letter)

simple (atomic)

structured (compound objects)

free --> has no value

bounded has value

anonymous has value but don't care for me

ex: integer, real, char 'A', strings and symbols

ex: lists, tree and graph

A fact is a predicate expression that makes a declarative statement about the problem domain.
/* John likes Suzie */ /* Everyone likes Suzie */ /* John likes everybody

likes(john, suzie). likes(X, suzie). likes(john, Y).

A rule is a predicate expression that uses logical implication (:-) to describe a relationship among facts. Thus a Prolog rule takes the form

left_hand_side :- right_hand_side .

Examples of valid rules: friends(X,Y) :- likes(X,Y),likes(Y,X). /* X and Y are friends if they like each other */

hates(X,Y) :- not(likes(X,Y)). /* X hates Y if X does not like Y. */ enemies(X,Y) :- not(likes(X,Y)),not(likes(Y,X)). /* X and Y are enemies if they don't like each other */ Examples of invalid rules: left_of(X,Y) :- right_of(Y,X)

/* Missing a period */
/* LHS is not a single literal */ /* LHS cannot be negated */

likes(X,Y),likes(Y,X) :- friends(X,Y). not(likes(X,Y)) :- hates(X,Y).

Any prolog program must contain three sections

Predicates definition of facts and rules


Must end with dot

Clauses implementation of facts and rules


Must be grouped by type Must be ended with dot Variable score in its rule only

Goal (query) a question that needs an answer


Must be ended with dot Yes/no question stops when find a match (get a solution) What question gets all possible solutions for the free variables Backtracking used by prolog to get all possible solutions for a variable to make the goal true.

Predicates human(symbol). man(symbol). woman(symbol). parent(symbol, symbol). Clauses human(david). human(john). human(suzie). human(eliza). man(david). man(john). woman(suzie). woman(eliza). parent(david, john). parent(john, eliza). parent(suzie, eliza).

Goal human(john). Yes woman(john). No human(A). A=david;john;suzie;eliza; NO parent(Parent, Child). Parent = david Child = john ; Parent = john Child = eliza ; Parent = suzieChild = eliza ; No

Fail keyword enforces backtracking & ! keyword prevents backtracking Cut has several uses 1- To define a deterministic (functional) predicate. Example: a deterministic version of member, which is more efficient for doing member checking because it neednt give multiple solutions: membercheck(X, [X|_]) :- !. membercheck(X, [_|L]) :- membercheck(X,L). ?- membercheck(fred, [joe, john, fred, paul]).

yes.

?-membercheck(X, [a, b, c]).

X = a; no.
2- To specify exclusion of cases by committing to the current choice. Example: the goal max(X, Y, Z) instantiates Z to the greater of X and Y: max(X, Y, X) :- X >= Y. max(X, Y, Y) :- X < Y. Note that each clause is a logically correct statement about maxima. A version using cut can get rid of the second test, and might look like this: max(X, Y, X) :- X >= Y, !. max(X, Y, Y).

negation as failure Using cut together with the built-in predicate fail, we may define a kind of negation. Examples: Mary likes any animals except reptiles: likes(mary, X) :- reptile(X), !, fail. likes(mary, X) :- animal(X). A utility predicate meaning something like not equals: different(X, X) :- !, fail. different(_,_).
We can use the same idea of cut fail to define the predicate not, which takes a term as an argument. not will call the term, that is evaluate it as though it is a goal: not(G) fails if G succeeds not(G) succeeds if G does not succeed. In Prolog, not(G) :- call(G), !, fail. not(_).

Example on ! Predicates car(symbol,symbol,integer). colour(symbol,symbol). buy_car(symbol,symbol). Clauses car(mazda,green,25000). car(fiat,black,24000). car(dogan,red,24000). car(mercedes, red,27000). colour(red,nice). colour(black,wonderful). colour(, wonderful).

buy_car(M,C):car(M,C,P),!, P<25000,!, colour(C,nice),!. buy_car(M,C):car(M,C,_) ,colour(C,wonderful). Goal Buy_car(M,C).


1) M=dogan C=red M=mazda C=green M=fiat C=black 2) No solution 3) No solution 4) M=dogan C=red

Recursive Definition Factorial Iteration Definition

X!= 1 if X=1 X*(X-1)! else X!= 1*2*3*....*X OR X*(X1)*....*2*1

Non tail recursion

Tail recusrion

Factorial Example Non Tail Recursive factorial(0,1). factorial(N,F) :N>0, N1 is N-1, factorial(N1,F1), F is N * F1. ?- factorial(3,W). W=6

Tracing

Non-tail recursive method returning back from a recursive call, there is still one pending operation, multiplication. Takes more memory, but easy to read

Factorial Example (Tail Recursive) fact(N,F) :- fact_aux(N,F,1). fact_aux(0,F,F):-!. fact_aux(X,F,Acc):Nacc=Acc*X, Nx=X-1, fact_aux(Nx,F,Nacc). ?- fact(3,W). W=6

Tracing fact_aux(3,F,1)X=3,Acc=1,Nacc=3*1,Nx=3-1=2, fact_aux(2,F,3)X=2,Acc=3,Nacc=3*2,Nx=2-1=1, fact_aux(1,F,6)X=1,Acc=6,Nacc=6*1,Nx=1-1=0, fact_aux(0,F,6)fact_aux(0,F,6)

Tail recursive method has the recursive call as the last statement in the method. Advantage of Tail Recursive Method: Tail Recursive methods are easy to convert to iterative Smart compilers can detect tail recursion and convert it to iterative to optimize code Used to implement loops in languages that do not support loop structures explicitly (e.g. prolog)

In prolog , list represents a groups of item


Prolog also has a special facility to split the first part of the list (called the head) away from the rest of the list (known as the tail). [first,second,third] = [A|B] where A = first and B=[second,third] Examples: member , append , delete , insert , reverse