Artificial Intelligence Adina Magda Florea 1 Lecture No

  • Slides: 25
Download presentation
Artificial Intelligence Adina Magda Florea 1

Artificial Intelligence Adina Magda Florea 1

Lecture No. 6 Prolog Language Rule-based Systems n n Knowledge representation using rules RBS

Lecture No. 6 Prolog Language Rule-based Systems n n Knowledge representation using rules RBS structure Inference cycle of a RBS Control strategy 2 2

1. Prolog R. Kowalski, A. Colmerauer - '70 Prolog control structure n A goal

1. Prolog R. Kowalski, A. Colmerauer - '70 Prolog control structure n A goal S is true in a Prolog program (can be satisfied or is a logical consequence of the program) iff: 1. There is a clause C in the program; 2. There is an instance I of the clause C such that: n 2. 1. the head of I is the asme with the head of S; 2. 2 all the goals in I’s body has true (can be satisfied) n 3 3

Prolog 4 4

Prolog 4 4

Prolog % parinte(Individ. X, Individ. Y) % stramos(Individ. X, Individ. Z) parinte(vali, gelu). parinte(ada,

Prolog % parinte(Individ. X, Individ. Y) % stramos(Individ. X, Individ. Z) parinte(vali, gelu). parinte(ada, mia). parinte(gelu, lina). parinte(gelu, misu). parinte(misu, roco). str 1(X, Z) : - parinte(X, Z). str 1(X, Z) : - parinte(X, Y), str 1(Y, Z). 5 5

Prolog % Change the order of the rules: str 2(X, Z) : - parinte(X,

Prolog % Change the order of the rules: str 2(X, Z) : - parinte(X, Y), str 2(Y, Z). str 2(X, Z) : - parinte(X, Z). % Change the order of goals: str 3(X, Z) : - parinte(X, Z). str 3(X, Z) : - str 3(X, Y), parinte(Y, Z). % Change both the order of rules and the order of goals: str 4(X, Z) : - str 4(X, Y), parinte(Y, Z). str 4(X, Z) : - parinte(X, Z). 6 6

Prolog ? - str 1(ada, misu). yes ? - str 2(ada, misu). yes ?

Prolog ? - str 1(ada, misu). yes ? - str 2(ada, misu). yes ? - str 3(ada, misu). yes 7 7

Prolog ? - str 4(ada, misu). ? - str 3(mia, roco). 8 8

Prolog ? - str 4(ada, misu). ? - str 3(mia, roco). 8 8

2. Knowledge representation using rules n Declarative representation / procedural representation R 1: if

2. Knowledge representation using rules n Declarative representation / procedural representation R 1: if the patient has fever and the type of the organism is gram-pozitiv and the patient has a stiff neck then the identity of the organism is streptococcus R 2: if the car does not start and the lights do not turn on the battery is faulty sau there is no contact of the battery R 3: if the temperature > 95 o C then open the protection valve 9 9

Reprezenthasa prin reguli - cont 10

Reprezenthasa prin reguli - cont 10

3. Structura SBR 11

3. Structura SBR 11

4. Inference cycle of an RBS n n n Match Selection Execution 12

4. Inference cycle of an RBS n n n Match Selection Execution 12

Inference cycle of an RBS - cont Algorithm: Functioning of an RBS 1. WM

Inference cycle of an RBS - cont Algorithm: Functioning of an RBS 1. WM Case data 2. repeat 2. 1. Identify WM and KB and create the conflict set (CS) 2. 2. Select a rule from the CS to apply 2. 3. Apply selected rule until there are no more applicable rules or WM satisfies goal or a predefined effort has been exhausted end. 13

5. Control strategy n n Selection criteria from the CS Direction of rule application

5. Control strategy n n Selection criteria from the CS Direction of rule application 14 14

5. 1 Selection criteria from the CS n n Select the first applicable rule

5. 1 Selection criteria from the CS n n Select the first applicable rule Select a rule from the CS n Preferences based on rule identity Specificity n n - the set of premises of R 1 includes the set of premises of R 2; - the set of conditions of R 1 are similar to that of R 2, but the premises of R 1 refer to constants while the premises of R 2 refer to variables. Time of last use n Preferences based on matched objects 15 15

Selection criteria from the CS - cont n Use of meta-rules if { then

Selection criteria from the CS - cont n Use of meta-rules if { then { n a rule has conditions A and B and the rule refers {does not refer} X not at all/ only in the LHS/ only in the RHS } the rule will be certainly useful probably useful/ probably useless/ certainly useless } Application of all rules in the CS 16 16

5. 2 Direction of rule application Forward chaining Backward chaining 17 17

5. 2 Direction of rule application Forward chaining Backward chaining 17 17

(a) Forward chaining 18 18

(a) Forward chaining 18 18

(b) Backward chaining 19 19

(b) Backward chaining 19 19

Algorithm: Find a value using forward chaining (a) Find. Value. D(A) 1. if A

Algorithm: Find a value using forward chaining (a) Find. Value. D(A) 1. if A has value then return SUCCESS 2. Build CS 3. return Find(A, CS) end. Find(A, CS) 1. if CS = {} then return FAIL 2. Choose a rule R CS according to a selection criterion (criteria) 3. for each premise Ij of R execute 3. 1. Find the truth of Ij 4. if all Ij , j=1, N are satisfied then 4. 1. if A has value then return SUCCESS 4. 2. Add the fact(s) refered in the RHS of R to WM 5. CS - R 6. return Find(A, CS) end. 20 20

Algoritm: Find a value using backward chaining (b) Detvalue(A) 1. Build the set if

Algoritm: Find a value using backward chaining (b) Detvalue(A) 1. Build the set if rules which refer A in RHS, CS 2. if CS = {} then 2. 1. Ask the user the value of A 2. 2. if the user answers A then stare this value in WM else return FAIL 3. else 3. 1. Choose a rule according to a selection criterion (criteria) 3. 2. for each premise Ij, j=1, N, in LHS of R execute 3. 2. 1. Be Aj refered by the premise Ij 3. 2. 2. if Aj has value then evaluate the truth of Ij 3. 2. 3. else i. if Detvalue(Aj) = SUCCESS then evaluate the truth of Ij ii. else consider Ij unsatisfied 21 21

4. 5. 3. 3. if all premises Ij, j=1, N are satisfied then store

4. 5. 3. 3. if all premises Ij, j=1, N are satisfied then store the value A obtained from the RHS of R in WM if A has value (in WM) then return SUCCESS else 5. 1 CS – R 5. 2 repeat from 2 end. 1'. if A is primary data then 1'. 1. Ask the user the value of A 1'. 2. if the user knows the value of A then - store this value in WM - return SUCCESS 1’. 3. else return FAIL 22 22

Example of RBS with forward chaining (a) R 11: if Etapa is Verifica-Decor and

Example of RBS with forward chaining (a) R 11: if Etapa is Verifica-Decor and exists o statuie and does not exist soclu then Add soclu la Obiecte-Neimpachetate R 17: if Etapa is Verifica-Decor then finish Etapa Verifica-Decor and start Etapa Obiecte-Mari …. . 23 23

Example of RBS with forward chaining - cont R 21: if Etapa is Obiecte-Mari

Example of RBS with forward chaining - cont R 21: if Etapa is Obiecte-Mari and exists un obiect mare de ambalat and exists un obiect greu de ambalat and exists o cutie cu mai putin de trei obiecte mari then pune obiectul greu in cutie R 22: if Etapa is Obiecte-Mari and exists un obiect mare de ambalat and exists o cutie cu mai putin de trei obiecte mari then pune obiectul mare in cutie R 28: if Etapa is Obiecte-Mari and exists un obiect mare de pus then foloseste o cutie noua R 29: if Etapa is Obiecte-Mari then finish Etapa Obiecte-Mari and start Etapa Obiecte-Medii …. . 24 24

Example of RBS with backward chaining (b) R 1: if X has par then

Example of RBS with backward chaining (b) R 1: if X has par then X is mamal R 2: if X feeds puii with milk then X is mamal R 3: if X is mamal and X has pointed teeth and X has falci then X is carnivor R 4: if X is carnivor and X is brown and X has pete then X is cheetah R 5: if X is carnivor and X is brown and X has strips then X is tiger What is X? 25 25