Różnice

Różnice między wybraną wersją a wersją aktualną.

Odnośnik do tego porównania

Nowa wersja
Poprzednia wersja
pl:dydaktyka:dss:rules:intro [2017/11/28 12:12]
msl utworzono
pl:dydaktyka:dss:rules:intro [2019/06/27 15:50] (aktualna)
Linia 1: Linia 1:
-Tłumacz +====== CLIPS Prolog ======
-Wyłącz szybkie tłumaczenie +
-5000/5000 +
-about the parent. How to extend the rules base of both tools with the help of the ** child ** concept so that they can tell who is the son and who is the daughter? * Define the appropriate rules. * Start the inference by checking if the defined rule is correct. * You can also view facts in CLIPS === 5.1.2. Marriage ​=== * The knowledge base can also be supplemented by a rule defining marriage **. With the help of existing facts and rules, you can define that two persons are married if: * they are different sexes * have a common child. * By defining the rule using the above conditions, the inference algorithm will only search for marriages with children. Marriages without children are impossible to find on the basis of available knowledge. Siblings === Knowing that the concepts ** brother ** and ** sister ** are defined as follows: * CLIPS: <​code>​ (defrule brother (male? X) => (assert ( (sister to x? y) => (assert (sister))) </ code> * Prolog: <​code>​ brother (X, Y): - parenting (X, Y), female (X, Y) Y '',​ having the following conditions: * '​X'​ '​and'​ '​Y'​ 'have a common parent. * ''​ X ''​ and ''​ Y ''​ are two different people (use the '​test'​ function) === 5.1.4. Grandparents === It's also possible to define ** grandparents ** and ** grandmothers **: * CLIPS: <​code>​ (defrule grandparents (male x?) => (Assert (grandfather?​ Prolog: grandfather (X, Y) - grandparents (X, Y), male (X) .babies (X, Y): - grandparents (X, Y), woman (X). * How to define concepts ** grandson **, ** granddaughter **, ** granddaughter **? 5.1.5. Culprit === The rule defining the terms' cousin (X, Y) ''​ has the following conditions: * '​X'​ and '​Y'​ have a common grandfather. * ''​ X ''​ and ''​ Y ''​ are two different people (use ** [[http://​clipsrules.sourceforge.net/​documentation/​v630/​bpg.htm#​_Toc60827727|test]] ** to define Run the inference and validate the results (in Prologue) with the following query: <​code>​ cousin (X, Y), parent (R, X), parent (R, Y). </ code> if the above query returns ** false ** the rule is well defined. **aunt**? How to use ** ** ** and ** parent ** rules to define the ** ** uncle? ** Propose appropriate rules and then validate the results. Facts also can be deduced, which children are half marriages ie they have one parent. Add the appropriate rules to the rule base. \\ For Prolog, define an alternative with '';​ ''​. ===== 5.1.8. *** Widow and widow === What conditions must be met for a person to be named widow? * How does the existing knowledge base relate to it? * Are there any facts that will allow people who are most likely to be widowers / widows? * We assume that: * No spouse means his death (not divorce, for example). * All appointed people are certainly widowers. * The person who is not designated may be a widower. The correct answer is '​you'​. * To find out who were nominated as widows / widowers, you can: * Prolog to ask: <​code>​ widow (X). </ Code> <​code>​ widower (X). </ Code> and display the results. * in CLIPS you can output the result of the rule: <​code>​ (defrule widow ... => (assert (widow x)) (printout t? x is a widow of "​crlf"​) </ code> * The correct solution is ''​ tobiasz ''​ - in a defined family there is no widow :-) * Did you get the correct result? ===== 6. Tracking the execution of the program ===== Because the results of the CLIPS program in the case of appointment ** widower * * are incorrect, you should follow the program execution method: ===== 6.1. Tracing Facts ===== Tracks Facts: Add, Remove, Change Values. - In the first step, we will start observing the ** widower rule ** to see what events trigger it: <​code>​ (watch rules widower) </ code> - We reset the fact base: <​code>​ (reset) </ code> - restart Inference: <​code>​ (run) </ code> - Now we see the triggers of the rules and the factual numbers from which they were run. - Now check which facts: <​code>​ (facts) </ code> ===== 6.2. Rule tracking ===== Enforces rules: firing, activation. - Because we did not know anything interesting,​ we will follow the rules ** ** widower ** and the rules it depends on: ** parent **, ** marriage **: <​code>​ (unwatch all) (watch rules married couple <​code>​ (reset) </ code> - Restart the inference: <​code>​ (run)</​code>​ - Jakie możemy poczynić obserwację,​ co jest powodem złego działania reguły **wdowiec**?​ - Jak możemy to naprawić?​====6.3. Śledzenie w Prologu ​=====Interpretery Prologu pozwalają na śledzenie pracy programu. W tym celu należy ustawić tzw. trace point.Trace points: * ''​trace/​0''​ - ustawia trace point na wszystkich predykatach,​ np.:<​code>?​- trace,​kobieta(K),​rodzic(K,​_).</​code>​ * **trace/1** - ustawia trace point na wskazanym predykacie, * **trace/2** - modyfikuje dla wskazanego predykatu śledzone zdarzenia (call, redo, exit, fail), np.: * Zdjęcie trace point ze wskazanego predykatu //​something/​1//<​code>​trace(something/​1,​-all)</​code>​ * Ustawienie trace point śledzącego jedynie wywołania na wskazane predykaty //​something//​ o dowolnej arności. <​code>​trace(something,​+call)</​code>​ * Predykat ''​debugging/​0''​ wypisuje ustawione trace points. * Predykat ''​debug/​0''​ wchodzi do trybu debug. * Predykat ''​nodebug/​0''​ wychodzi z trybu debug.Sprawdzić działanie:<​code>?​- trace(rodzic).[debug] ?- trace(mama).[debug] ?- trace(tata)[debug] ?- rodzic(dariusz,​ tomasz).[debug] ?- nodebug.</​code>​+
  
 +===== Download =====
 +Please download the following files containing simple knowledge bases about a family:
 +  * CLIPS file: {{ :​pl:​dydaktyka:​dss:​rules:​family_en.clp.zip |}}
 +  * Prolog file: {{ :​pl:​dydaktyka:​dss:​rules:​family_en.pl |}}
 +  * Manual for CLIPS: {{:​pl:​dydaktyka:​rules:​lab:​labs:​bpg.pdf|Basic Programming Guide}}, [[http://​clipsrules.sourceforge.net/​OnlineDocs.html|version online]]
  
 +===== Knowledge Bases =====
 +Both files have corresponding content:
 +  * The same facts.
 +  * The same relationships defined on the facts.
 +In addition, files contain a single rule defining the **child** relation.
 +
 +===== 1. Loading The Facts =====
 +After downloading the files, they have to be loaded into corresponding Knowledge Bases (KB):
 +  * CLIPS: <​code>​ (load family_en.clp) </​code>​ If everything was loaded correctly, you should a feedback similar to: <​code>​
 +Defining deffacts: initial-facts
 +Defining defrule: child +j
 +TRUE </​code>​ The ** TRUE ** is the most important part of this feedback. If ** FALSE ** appears, it means that loading failed.
 +  * Prolog: <​code>​ [family]. </​code>​ The succesful loading should display something similar to: <​code>​
 +% family compiled 0.01 sec, 9,232 bytes
 +true. </​code>​ Notice the ** true ** again.
 +
 +===== 2. Display the KB =====
 +To view the contents of the KB, enter:
 +  * CLIPS: <​code>​
 +(reset)
 +(facts) </​code>​
 +    * How many facts are in the KB?
 +  * Prolog: <​code>​listing. </​code>​
 +
 +===== 3. Inference =====
 +CLIPS uses the forward-chaining. The inference is implemented as a loop, that checks what rules can be executed based on the current state of the KB. Then the found rules are executed and the cycle repeats. The loop ends when it fails to find a rule to execute. The input of the forward-chaining algorithm is ** the set of facts ** in the knowledge base.
 +  * To check what rules are ready to be run, type <​code>​ (agenda) </​code>​
 +    * What rules are ready to run?
 +    * What facts trigger these rules?
 +    * How to explain the result?
 +  * To run the inference process, type <​code>​ (run) </​code>​
 +    * What is the output?
 +    * How many facts are in the database?
 +    * What facts were added?
 +    * Have you expected the result?
 +
 +In Prolog we use backward-chaining and the process is inverted. The input of this algorithm is **target** --- fact that we want to infer. We may say that this a goal-oriented approach. Let's ask:
 +  * What women are included in the knowledge base: <​code>​ woman(X). </​code>​ With the '';''​ key, we can continue to list all the possible answers of the inference. If you want to interrupt, press "​enter"​.
 +  * What pairs of type (mom, child) are included in the KB: <​code>​ mother(Mom, Child). </​code>​
 +  * What children does Dariusz have? <​code>​ father(dariusz,​ Child). </​code>​
 +  * What are the names of all the fathers: <​code>​ father(Father,​ _). </​code>​
 +  * What sons does Dariusz have: <​code>​ father(dariusz,​ Son), man(Son). </​code>​
 +
 +===== 4. How to use Rules =====
 +Both files contain a rule defines who/what is a ** child**.
 +  * How to interpret these rules?
 +  * Do they use the same notion of a **child**?
 +CLIPS failed to start this rule (previous point). In Prolog, we can query using this rule: <​code>​child(X,​ Y).</​code>​
 +  * Run the above query and ...
 +  * describe what happened?
 +  * Has the situation been analogous to that in CLIPS?
 +  * Does the rule in CLIPS failed because of the same reason?
 +
 +===== 5. Creating Rules =====
 +In order to run the **child** rule, we have to define:
 +  * In CLIPS: a rule that adds a parent to the knowledge base (changing the knowledge base will trigger the next inference cycle): <​code>​
 +(defrule parent
 + (or (mother ?x ?y) (father ?x ?y))
 +=>
 + ​(assert (parent ?x ?y))
 +)</​code>​
 +  * In Prolog: a rule defining a relationship (child, parent): <​code>​
 +parent(X, Y) :-
 +  father(X, Y).
 +parent(X, Y) :-
 +  mother(X, Y). </​code>​ It is worth noting that in the case of Prolog, we define two rules. One can assume that there is a conjuction (**OR**) between these two rules.
 +  * Repeat steps:
 +    * CLIPS: 1. and 3. (start with executing ''​(clear)''​ to clear the fact base)
 +    * Prolog: 1 and 4. 
 +  * How many facts are there in the CLIPS database?
 +  * What are the active rules in CLIPS?
 +  * Why is the **child** rule inactive?
 +
 +==== 5.1. Adding Knowledge to the KB ====
 +=== 5.1.1. Son and daughter ===
 +We can try now to supplement the current KB with new rules, that define other relationships:​
 +  * Both tools can answer who is the child of which parent. How to extend the rules base of both tools with the help of the **child** concept so that they can tell who is the son and who is the daughter?
 +    * Define the appropriate rules.
 +    * Start the inference to check if the defined rule is correct.
 +    * You can also try to simply display the facts in CLIPS.
 +
 +=== 5.1.2. Marriage ===
 +  * The knowledge base can also be supplemented by a rule defining **marriage**. With existing facts and rules, you can define that two people are married if:
 +    * are different sex,
 +    * have a common child.
 +  * This rule will cause the inference to omit the childless couples. In order to define the proper notion of **marriage**,​ KB would have to be supplemented with additional facts (what facts?)
 +
 +=== 5.1.3. Other ===
 +Knowing that the concepts **brother** is defined as follow:
 +  * CLIPS: <​code>​
 +(defrule brother
 + ​(siblings ?x ?y)
 + (man ?x)
 +=>
 + ​(assert (brother ?x ?y))
 +)</​code>​
 +  * Prolog: <​code>​
 +brother(X, Y) :-
 +  siblings(X, Y),
 +  man(X).</​code>​
 +Define the following rules:
 +  * siblings(X,​Y) --- two different people with a common parent; ​
 +    * tip: ** (test (neq ?x ?y)) ** checks inequality of variables.
 +    * question: why do you have to use ** test ** keyword?
 +  * sister(X,Y)
 +  * grandfather(X,​Y)
 +  * grandmother(X,​Y)
 +  * grandparent(X,​Y) --- tip: use ** OR **
 +  * cousins(X,​Y) --- two different people with a common grandparent. You can check this rule in Prolog with a query: <​code>​cousins(X,​Y),​ parent(R,​X),​ parent(R, Y)</​code>,​ it should return **false**
 +    * remember: siblings are not cousins! ​
 +  * aunt(X,Y) and uncle(X,Y) in terms of sister, brother and parent.
 +  * half-orphan(X) --- a person with only one parent. TIP: celina is the only half-orphan in the KB
 +  * widow(X) (and widower(X)) --- assume that all the single-parents are single because of the spouse'​s death. TIP: tobiasz is the only widower in the KB, there are no widows. In CLIPS you may check results of the following rule: <​code>​
 +(defrule widower
 + ...
 +=>
 + ​(assert (widower ?x))
 + ​(printout t ?x " is widower"​ crlf)
 +)</​code>​. Do you get the expected result?
 +
 +===== 6. Tracking the execution of the program =====
 +Since CLIPS doesn'​t correctly find the widowers, it is necessary to track the execution of the program:
 +
 +=== 6.1. Tracking the facts =====
 +Allows you to track operations on facts: add, delete, change of the value.
 +  - In the first step, we will start observing the **widower** rule to see what triggers it: <​code>​ (watch rules widower) </​code>​
 +  - We reset the fact base: <​code>​ (reset) </​code>​
 +  - Restart the inference: <​code>​ (run) </​code>​
 +  - Now we see the triggers and indexes of facts from which we started.
 +  - Now check which fact triggered the inference: <​code>​(facts)</​code>​
 +
 +===== 6.2. Rule tracking =====
 +
 +Allows tracking of rules: firing, activation.
 +  - Because we did learn nothing interesting tracing the fact, we will follow the **widow** rule and all the rules it depends on: **parent**, **marriage**:​ <​code>​
 +(unwatch all)
 +(watch rules marriage widow parent)
 +</​code>​
 +  - Now we reset the fact base: <​code>​ (reset) </​code>​
 +  - Restart the inference: <​code>​ (run) </​code>​
 +  - What is the reason behind the malfunction of the **widower** rule?
 +  - How can we fix it?
 +
 +=== 6.3. Tracking in Prolog =====
 +Prolog allows you to follow the program. To do so, you set the so-called. trace point.
 +
 +Trace points:
 +  * ''​trace/​0''​ - sets the trace point on all predicates, eg:
 +<​code>​
 +?- trace, woman(K), parent(K, _).
 +</​code>​
 +  * **trace/1** - sets the trace point at the indicated predicate,
 +  * **trace/2** - modifies trace point of the specific events (call, redo, exit, fail), eg:
 +    * remove the trace point from the specified predicate //​something/​1//​ <​code>​ trace(something/​1,​ -all) </​code>​
 +    * set a trace point tracking only calls to the specified predicates //​something//​ of arbitrary arity. <​code>​trace(something,​ call +)</​code>​
 +  * Predicate ''​debugging/​0''​ lists all the set trace points.
 +  * The ''​debug/​0''​ predicate activates debug mode.
 +  * Predicate ''​nodebug/​0''​ disactivates debug mode.
 +
 +Check following code:
 +<​code>​
 +?- trace(parent).
 +[debug] ?- trace(mother).
 +[debug] ?- trace(father)
 +[debug] ?- parent(dariusz,​ tomasz).
 +[debug] ?- nodebug.
 +</​code>​
pl/dydaktyka/dss/rules/intro.1511867579.txt.gz · ostatnio zmienione: 2019/06/27 15:57 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0