27
Problem Independent Forward Chaining Forward chaining is a reasoning process that begins with the known facts and tries to work forward to find a successful goal. Even though Prolog uses backward chaining as a control strategy, it is possible to write program in Prolog which uses forward chaining. In order to implement it, we require the use of dynamic database. In forward chaining, the facts from static and dynamic knowledge bases are taken and are used to test the rules through the process of unification. When a rule succeeds, then the rule is said to be fired and the conclusion

Problem Independent Forward Chaining Forward chaining is a reasoning process that begins with the known facts and tries to work forward to find a successful

Embed Size (px)

Citation preview

Problem Independent Forward Chaining• Forward chaining is a reasoning process that begins with the known facts and tries to work forward to find a successful goal. • Even though Prolog uses backward chaining as a control strategy, it is possible to write program in Prolog which uses forward chaining.• In order to implement it, we require the use of dynamic database. • In forward chaining, the facts from static and dynamic knowledge bases are taken and are used to test the rules through the process of unification. • When a rule succeeds, then the rule is said to be fired and the conclusion (head of the rule) is added to the dynamic knowledge.

• We code Prolog rules as facts with two arguments, first argument be left side of rule and second is the list of sub goals in the right side of the rule. • Let us name the predicate representing rule as a fact by rule_fact and simple facts by predicate name fact. • Consider the following rules and facts with their corresponding new fact representations.

a :- b. rule_fact(a, [b]).c :- b, e, f. rule_fact(c, [b, e, f]).b. fact(b).e. fact(e).f. fact(f).

Here a, b, c, e, f are atoms (predicates with arguments, if any).• Database file say, 'dfile' contains the following rules and facts coded in above mentioned format and is consulted at the time of executing forward chaining program given below.

rule_fact(a, [b]).rule_fact(c, [b, e, f]).fact(b). fact(e).fact(f).

• The program given below simulates forward chaining mechanism . forward :- finished, !. forward :- fact(F), doall(rule(F)), assertz(used_facts(F)),

retract(fact(F)), forward. rule(F) :- rule_fact(L, R), rule1(F, L, R). rule1(F, L, R) :- member(F, R), delete(F, R, NR), new_rule(L, NR). new_rule(L, [ ]):- not(fact(L)), asserta(fact(L)). new_rule(L, R):- not(R = []), asserta(rule_fact(L, R)). finished :- not(fact(X)). doall(P) :- not(alltried(P)). alltried(P) :- call(P), fail. Example: ?- consult (dfile).

?- forward. (Draw search tree)

Knowledge Representations• There are different ways of representing knowledge to be used in expert systems viz., predicate logic, semantic networks, frames etc. • In predicate logic, knowledge is represented in the form of rules and facts as is done in Prolog. • Rule based expert system incorporates knowledge in the form of rules and facts (discuss later). • Other representation schemes offer the ability to represent structured knowledge about physical or conceptual objects more easily than rules.

Semantic Network (Semantic Net): It is a formalism for representing information about objects, people, concepts and specific relationship between them. The syntax of semantic net is simple. It is a network of labeled nodes and links.

Objects are denoted by nodes (enclosed in oval shape) in the network and relations are denoted by links. Basically it is a directed graph with nodes corresponding to concepts, facts, objects etc. and arcs showing relation or association between two concepts. The commonly used links in semantic net are of the following types. Subc - subclass of entity (e.g., child hospital is subclass of hospital) Inst - particular instance of a class (e.g., India is an instance of

country) Prop - property link (e.g., property of dog is black colour)

Inheritance • It is a form of inferencing information associated with semantic nets. • Inheritance mechanism allows knowledge to be stored at the highest possible level of abstraction which reduces the size of knowledge base.

• It is a natural tool for representing taxonomically structured information and ensures that all the members and subclass of a class share common properties. • It also helps us to maintain the consistency of the knowledge base by adding new classes and members of existing classes. • Properties attached to a particular object (class) are to be inherited by all subclasses and members of that class. Example:Represent the following knowledge into its corresponding semantic net representation.

John is a man. Mary is a woman. Every man is a human.Every woman is a human. Every human is living things.Every animal is a living thing. Crow and ostrich are birds.All birds and fishes are animal. Shark and salmon are fishes.Every living thing breath. All fishes have fins, gills and can swim.All animals have skin, can move and eat.Ostrich has long legs and is tall.Shark is dangerous and can bite. Crow is black.

Following semantic net stores above knowledge. Properties are shown separately for the sake of convenience but should be part of this entire network.

Living_things

Subc Subc

Animal Human

Subc Subc Subc Subc

Bird Fish Man Woman

Inst Inst Inst Inst Inst Inst

Crow Ostrich Shark Salmon John Mary

• Above network can be easily simulated using Prolog facts as follows: Direction or arrow shows the positioning of arguments. All are binary predicates (i.e., having two arguments).

Following are the properties attached to each node in the network

has skin

Living_things breath Animal can move

eats

has wings has fins

Bird has feathers fish has gills

can fly can swim

crow black colour

dangerous

ostrich has long legs shark can bite

is tall has heavy weight

subc(animal, living_things). subc(human, living_things). subc(bird, animal )subc(fish, animal ). subc(man, human). subc(woman, human).

inst(crow, bird). inst(ostrich, bird). inst(shark, fish).inst(salmon, fish). inst(john, man). inst(mary, woman).

prop(living_things, breath). prop(animal, skin). prop(animal, move).prop(animal, eat). prop(bird, wings). prop(bird, feathers).prop(bird, fly). prop(fish, fins). prop(fish, gills).prop(fish, swim). prop(crow, black). prop(ostrich, tall).prop(ostrich, long_legs). prop(shark, dangerous). prop(shark, bite).prop(shark, heavy_wt).

A simple program for handling inheritance in semantic net is given below:

is_an_instance(X, Y) :- inst(X, Y).is_ an_instance (X, Y):- inst(X, Z), subclass(Z,Y).subclass(X, Y) :- subc(X, Y).subclass(X, Y) :- subc(X, Z), subclass(Z, Y) .has_a_property(X, Y) :- prop(X, Y).has_a_property(X, Y) :- is_an_instance(X, Z), has_a_property(Z, Y).has_a_property(X, Y) :- subclass(X, Z), has_a_property(Z, Y).

Queries:• Is john a living thing??- is_an_instance(john, living_things). - yes

• Is shark an animal??- is_an_instance (shark, animal). - yes

• Does mary breath??- has_a_property (mary, breath). - yes

•Does ostrich bite??- has_a_property(ostrich, bite). - no

• Is bird a subclass of living thing??- subclass(bird, living_things). - yes

• Does crow fly??- has_a_property(crow, fly). - yes

Knowledge Representation using Frames • Frames are the ways of organizing as well as packaging knowledge in more structured form and are used for representing objects, concepts etc. • They are organized into hierarchies or network of frames. • Lower level frames can inherit information from Frames in network which are connected using links viz.,

- ako / subc (links two class frames, one of which is subclass of other e.g., science_faculty class is ako of faculty class), - is_a / inst ( connects a particular instance of a class frame e.g., Renuka is_a science_faculty)

and - a_part_of (connects two class frames one of which is contained in other e.g., faculty class is_part_of department class).

• Property link of semantic net is replaced by SLOT field.

• A frame may have any number of slots as needed for describing object. e.g., faculty frame may have name, age, address, qualification etc as slot names. • Therefore, each frame includes two basic elements : slots and facets. • Each slot may contain one or more facets (called fillers) which may take many forms such as:

- value (value of the slot), - default (default value of the slot), - range (indicates the range of integer or enumerated

values, a slot can have), - demons (procedural attachments such as if_needed,

if_deleted, if_added etc.) and - other (may contain rules, other frames, semantic net or

any type of other information).

Frame network for university is shown given below.

university

a_part_of

department hostel

a_part_of is_a

faculty nilgiri hostel

ako

science_faculty

is_a

renuka

frame0

f_name: universityphone: (default: - 011686971)address : (default - IIT Delhi)

frame1 frame2

f_name : department f_name : hostela_part_of : frame0 a_part_of : frame0programme : [Btech, Mtech, Ph.D] room : (default - 100)

frame11 frame21

f_name: faculty f_name : nilgiria_part_of : frame1 is_a : frame2age : range (25 - 60) phone : 0116862345nationality: (default - Indian)qual: (default - Post graduate)

frame12 frame13

f_name : science faculty f_name : renukaako : frame11 is_a : frame12qual : (default - M.Sc) qual : Ph.D

age: 45adrress: Janak Puri

• Each frame represents either a class or an instance. • Class frame represents a general concept whereas instance frame represents a specific occurrence of the class instance. • Class frame generally have default values which can be redefined at lower levels otherwise if class frame has actual value facet then decedent frames can not modify that value. • Value remains unchanged for subclasses and instances. Inheritance in Frames: • Suppose we want to know nationality or phone of an instance frame frame13 of renuka which is not given in this frame. • Search will start from frame13 in upward direction till we get our answer or have reached root frame. • The frames can be easily represented in prolog by choosing predicate name as frame with two arguments. • First argument is the name of the frame and second argument is a list of slot - facet pair.

• Prolog facts representing above network is as follows:

frame(university, [phone (default, 011686971), address (default, IIT Delhi)]).

frame(deaprtment, [a_part_of (university), [programme ([Btech, Mtech, Ph.d]))]]).

frame(hostel, [a_part_of (university), [room(default, 100)]]).

frame(faculty, [a_part_of (department), age(range,25,60), nationality(default, indian), qual(default, postgraduate)]]).

frame(nilgiri, [is_a (hostel), [phone(011686234)]]).frame(science_faculty, [ako (faculty), [qual(default,

M.Sc.)]]).frame(renuka, [is_a (science_faculty), [qual(Ph.D.),

age(45), address(janakpuri)]]).

• Inheritance program for Frames:

find(X, Y) :- frame(X, Z), search(Z, Y), !.find(X, Y) :- frame(X, [is_a(Z),_]), find(Z, Y), !.find(X, Y) :- frame(X, [ako(Z), _]), find(Z, Y), !.find(X, Y) :- frame(X, [a_part_of(Z), _]),

find(Z, Y).

• Predicate search will basically retrieve the list of slots-facet pair and will try to match Y for slot. • If match is found then its facet value is retrieved otherwise process is continued till we reach to root frame.

Expert Systems (ES)

• Expert systems are knowledge based programs which provide expert quality solutions to the problems in specific domain of applications. • Generally its knowledge is extracted from human domain experts. • The core components of expert system are knowledge base and navigational capability (inference engine) which makes ES different from any other application programs. Knowledge base - It consists of knowledge about problem domain in the form of static and dynamic databases. - Static knowledge consists of rules and facts which is complied as a part of the system and does not change during execution of the system.

- Dynamic knowledge consists of facts related to a particular consultation of the system. - At the beginning of the consultation, the dynamic knowledge base often called working memory is empty. - As a consultation progresses, dynamic knowledge base grows and is used along with static knowledge in decision making. - Working memory is deleted at the end of consultation of the system. Inference Engine - It consists of inference mechanism and control strategy. - Inference means search through knowledge base and derive new knowledge. - It involves formal reasoning involving matching and unification similar to the one performed by human expert to solve problems in a specific area of knowledge.

- Inference operates by using modus ponen rule. Control strategy determines the order in which rules are applied. There are mainly two types of control mechanism viz., forward chaining and backward chaining. Knowledge acquisition - It allows system to acquire more knowledge about the problem domain from experts and may have learning module attached to it employing machine learning techniques. Case history - The file is created by inference engine using the dynamic database created at the time of different consultation of the system and is used by learning module to enrich is knowledge base.

Explanation module - It explains user about the reasoning behind any particular problem solution. - It consists of 'How' and 'Why' modules. The submodule 'How' tells user about the process through which system has reached to a particular solution whereas "Why' submodule tells that why is that particular solution. User interfaces - It allows user to communicate with system in interactive mode and helps system to create working knowledge for the problem to be solved. - Knowledge may be entered using some editor. Special interfaces - It may be used for specialized activities such as handling uncertainty fuzziness in knowledge etc.

Rule Based Expert Systems • The most popular ways of storing knowledge in expert systems is in the form of rules and facts of a particular domain.• A rule based expert system is one in which knowledge base is in the form of rules and facts. It is also called production system. • It contains knowledge of the domain in the form of rules and facts used to make decision. Suppose doctor gives a rule for measles as follows:

"If symptoms are fever, cough, running_nose, rash and conjunctivitis then patient probably has

measles".• Prolog is more suitable for implementing such systems.

• Rules (called production rules) and facts of production system can be easily expressed in Prolog as follows:

hypothesis(measles) :-symptom(fever), symptom(cough), symptom(running_nose), symptom(conjunctivitis), symptom(rash).

• Prolog has its own inference engine. It performs unification, determines the order in which the rules are scanned and performs conflict resolution. • These features of Prolog makes designing of production system relatively simpler. Prolog uses backward chaining i.e., it starts with the goal and works backward. • It is also limited to depth first search in which all the rules relative to a particular goal are scanned as deeply as possible for a solution before Prolog backtracks and tries an alternative goal. • However user has less control but meta level facilities help user to implement systems using different search strategies.

Rule Based Expert System

consultation:- init_database, writeln(‘Welcome to Medical Consultation System’), writeln(‘Input your name), readln(Name), hypothesis(Disease), !, writeln(Name, ‘probably has’, Disease), clear_consult_facts.

consultation:- writeln(‘Sorry, not able to diagnose’),clear_consult_facts.init_database:- assert(positive(x)), assert(negative(y)).

/*******************/hypothesis(flu):- symptom(fever), symptom(headache), symptom(body_ache), symptom(sore_throat), symptom(cough), symptom(chills),

symptom(running_nose), symptom(conjunctivitis).hypothesis(cold):- symptom(headache), symptom(sneezing), symptom(chills),

symptom(sore_throat), symptom(running_nose).hypothesis(measles):- symptom(fever), symptom(cough),

symptom(running_nose), symptom(conjunctivitis),

symptom(rash).hypothesis(mumps):- symptom(fever), symptom(swollen_glands).

hypothesis(cough) :- symptom(cough), symptom(sneezing), symptom(running_nose).

hypothesis(chicken_pox):- symptom(fever), symptom(chills),symptom(body_ache), symptom(rash).

/*******************/symptom(fever) :- positive_ symp(‘Do you have fever(y/n) ?’,

fever).symptom(rash) :- positive_ symp(‘Do you have rash(y/n) ?’, rash).symptom(body_ache):- positive_ symp(‘Do you have body_ache (y/n) ?’,

body_ache).symptom(cough) :- positive_ symp(‘Do you have cough (y/n) ?’,

cough).symptom(chills) :- positive_ symp(‘Do you have chills (y/n) ?’,

chills).symptom(conjunctivitis):- positive_ symp(‘Do you have conjunctivitis

(y/n)?', conjunctivitis).symptom(headache):- positive_ symp(‘Do you have headache (y/n) ?’,

headache).

symptom(sore_throat):- positive_ symp(‘Do you have sore_throat (y/n) ?’, sore_throat).

symptom(running_nose):- positive_ symp(‘Do you have running_nose (y/n)?’, running_nose).

symptom(sneezing):- positive_ symp(‘Do you have sneezing (y/n) ?’, sneezing).

symptom(swollen_glands):- positive_symp(‘Do you have swollen_glands(y/n) ?’, swollen_glands).

/*********************/positive_ symp(_, X) :- positive(X), !.positive_ symp(Q, X):- not(negative(X)), ask_query(Q, X, R), R = ‘y’.ask_query(Q, X, R) :- writeln(Q), readln(R), store(X, R).store(X, ‘y’) :- asserta(positive(X)).store(X, ‘n’) :- asserta(negative(X)).clear_consult_facts :- retractall(positive(_)).clear_consult_facts :- retractall(negative(_)).