build up your own expert system using prolog 04, 2016 · 7/11/2015 budditha hettige...

Post on 27-Mar-2018

217 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS 6033

Logic Programming

Budditha HettigeDepartment of Computer Science

Faculty of ComputingGeneral Sir John Kotelawala Defence University

SOURCE : Building Expert Systems in Prolog, Dennis MerrittURL: http://www.inf.fu-berlin.de/lehre/SS09/KI/folien/merritt.pdf

Build up your own Expert System using PROLOG

Overview

• What is an Expert System?

• What are the Expert system features?

• Steps to Expert System development

– Knowledgebase

– User Interface

– Shell

• Prolog Java Interface (JPL)

• Prolog Web Interface

• Examples

27/11/2015 Budditha Hettige (budditha@yahoo.com)

Expert System

• Computer applications which embody some non-

algorithmic expertise for solving certain types of

problems

7/11/2015 Budditha Hettige (budditha@yahoo.com) 3

{Dennis Merritt}

Expert System Features

• Goal driven reasoning or backward chaining

• Coping with uncertainty

• Data driven reasoning or forward chaining

• Data representation

• User interface

• Explanations

7/11/2015 Budditha Hettige (budditha@yahoo.com) 4

Steps to

Expert System Development

• Identify the problem

• Build up Knowledge base

– Fats

– Rules

– Expirations

• Build up Inference Engine

– Way to sole problem

• Design a user Interface

7/11/2015 Budditha Hettige (budditha@yahoo.com) 5

Identify the problem

• Input

– What are the Questions?

– What is the order of the Questions?

• Output

– What is the final result(s)?

• Facts

– What are the fats requires to collect the answers?

• Explanations

– In case, what are the explanations?

7/11/2015 Budditha Hettige (budditha@yahoo.com) 6

Build up Knowledgebase

• What are the facts?

• What are the rules?

• How they represents?

• Example

family(duck):-

order(waterfowl), feed(on_water_surface), flight(agile).

bird(mallard):- family(duck), voice(quack), head(green).

bird(mallard):- family(duck), voice(quack), color(mottled_brown).

bird(pintail):- family(duck), voice(short_whistle).

7/11/2015 Budditha Hettige (budditha@yahoo.com) 7

Build up Inference Engine

• Prolog has a built-in backward chaining inference engine that can be used to partially implement some expert systems

• The rules for expert systems are usually written in the form:

– bird(pintail):- family(duck), voice(short_whistle).

7/11/2015 Budditha Hettige (budditha@yahoo.com) 8

User Interface

• The system can be dramatically improved by

providing a user interface

• Identify Attribute and values for the each case

7/11/2015 Budditha Hettige (budditha@yahoo.com) 9

Ask Questions

7/11/2015 Budditha Hettige (budditha@yahoo.com) 10

Menu for a user

• Example:

7/11/2015 Budditha Hettige (budditha@yahoo.com) 11

Remembering the answer

• Example:

7/11/2015 Budditha Hettige (budditha@yahoo.com) 12

Expert System Shell

• Architecture of the Native shell.

7/11/2015 Budditha Hettige (budditha@yahoo.com) 13

The Shell

• The shell will seek to solve a generic predicate called

top_goal.

top_goal(X) :- bird(X).

• The shell has a predicate called solve, which does some

housekeeping and then solves for the top_goal.

• Example

7/11/2015 Budditha Hettige (budditha@yahoo.com) 14

Explanation

• Expert systems have ability to explain theirbehavior.

• This means the user can ask the system for justification of conclusions or questions at any point in a consultation with an expert system

• Types of Explanation

– A rule trace, which reports on the progress of a consultation;

– Explanation of how the system reached a given conclusion;

– Explanation of why the system is asking a question;

– Explanation of why not a given conclusion.

7/11/2015 Budditha Hettige (budditha@yahoo.com) 15

Explanation (Questions)

The why questions occur at the bottom of an inference

chain, and the how questions occur at the top

7/11/2015 Budditha Hettige (budditha@yahoo.com) 16

Java + Prolog Interface

7/11/2015 Budditha Hettige (budditha@yahoo.com) 17

Java Prolog Interface

• SET PATH for the swipl.exe

– C:\Program Files (x86)\swipl\bin

• Add jpl.jar to your project

• Create suitable prolog source file

• Add prolog source file into your java project folder

• Import jpl and complete your development

7/11/2015 Budditha Hettige (budditha@yahoo.com) 18

Java Codes for Prolog connection

• Import JPL libraryimport org.jpl7.JPL;

import org.jpl7.Query;

import org.jpl7.Term;

• Collect JPL information

7/11/2015 Budditha Hettige (budditha@yahoo.com) 19

JPL Code

• Consult prolog sourceString t1 = "consult('family.pl')";System.out.println(t1 + " " + (Query.hasSolution(t1) ? "succ" : "failed"));

• Run Prolog predicate String t2 = "child_of(joe, ralf)";

System.out.println(t2 + " is " + (Query.hasSolution(t2) ? "pro" : "not"));

• Get one solutionString t4 = "descendent_of(X, ralf)";

System.out.println("first solution of " + t4 + ": X = " +

Query.oneSolution(t4).get("X"));

7/11/2015 Budditha Hettige (budditha@yahoo.com) 20

JPL Code (2)

String t4 = "descendent_of(X, ralf)";

Map<String,Term>[] ss4 =

Query.allSolutions(t4);

System.out.println("all solutions of " + t4);

for (int i = 0; i < ss4.length; i++)

{

System.out.println("X = " + ss4[i].get("X"));

}

7/11/2015 Budditha Hettige (budditha@yahoo.com) 21

JPL Code (3)

String t5 = "descendent_of(X,Y)";

Query q5 = new Query(t5);

System.out.println("each solution of " + t5);

while (q5.hasMoreSolutions())

{

Map<String, Term> s5 = q5.nextSolution();

System.out.println("X = " + s5.get("X") + ",

Y = " + s5.get("Y"));

}

7/11/2015 Budditha Hettige (budditha@yahoo.com) 22

Example (relation.pl)

male(somapala).

male(gunapala).

male(rathnapala).

female(seela).

female(susila).

female(neetha).

parent(seela,kamal).

parent(seela,nayana).

parent(seela,saman).

parent(somapala,kamal).

parent(somapala,nayana).

parent(somapala,saman).

parent(kamal,sanjaya).

parent(ruvani,sanjaya).

parent(saman,susi).

parent(saman,yasith).

parent(saman,oshan).

parent(kumari,susi).

parent(kumari,yasith).

parent(kumari,oshan).

print:-write('how are you').

print(X):- write('Anwer is '),write(X),nl.

print(X,Y):- write('Anwer is '),write(X), write(Y),nl.

mother(X,Y):-female(X),parent(X,Y).

father(X,Y):-male(X),parent(X,Y).

son(X,Y):- male(X),parent(Y,X).

whois(X, Y) :- mother(X,Y), write(X), write(' is mother of'), write(Y).

whois(X, Y) :- father(X,Y), write(X), write(' is father of'), write(Y).

whois(X, Y) :-

son(X,Y), write(X), write(' is son of'), write(Y).

7/11/2015 Budditha Hettige (budditha@yahoo.com) 23

Example:

7/11/2015 Budditha Hettige (budditha@yahoo.com) 24

top related