Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 16 - 17 Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,

Similar presentations


Presentation on theme: "1 Lecture 16 - 17 Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,"— Presentation transcript:

1 1 Lecture 16 - 17 Expert Systems &

2 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses, sometimes called directives, which act as operator definitions. An operator definition must appear in the program before any expression containing that operator. Ex: :- op(600, xfx, has) This tells Prolog that we want to use ‘has’ as an operator, whose precedence is 600 and its type is ‘xfx’, which is a kind of infix operator. The form of the specifier ‘xfx’ suggests that the operator, denoted by ‘f’, is between the two arguments denoted by ‘x’.

3 3 Notice that operator definitions do not specify any operation or action. In principle, no operation on data is associated with an operator (except in very special cases). Operators are normally used, as functors, only to combine objects into structures and not to invoke actions on data, although the word ‘operator’ appears to suggest an action.

4 4

5 5 Precedence of argument If an argument is enclosed in parentheses or it is an unstructured object then its precedence is 0. If an argument is a structure then its precedence is equal to the precedence of its principal functor.

6 6

7 7 - - - c a - prec. 0 prec. 0 A b b c Prec. 500 prec 500 interpretation as (a-b) - c interpretation as a-(b-c) Two interpretations of the expression a – b – c assuming that ‘ – ‘ has precedent 500. if ‘-’ is of type yfx, then interpretation 2 is invalid because the precedence of b-c is not less than the precedence of ‘-’

8 8 Several operators can be declared by one clause if they all have the same precedence and if they are all of the same type. In this case the operator’name are written as a list.

9 9 The following knowledge base is concerned with diagnosing the problem of water leaking in a fat. A problem can arise either in a bathroom or in the kitchen. In either case, the leakage also causes a problem (water on the floor) in the hall. Apart from its overall naivete, this knowledge base only assumes single faults; that is, the problem may be in the bathroom or the kitchen, but not in both of them at the same time.

10 10 Leak_in_bathroom :- hall_wet, kitchen_dry. problem_in_kitchen :- hall_wet, bathroom_dry. no_water_from_outside :- window_closed ; no_rain. leak_in_kitchen :- problem_in_kitchen, no_water_from_outside. The observed pieces of evidence can be started as Prolog facts: hall_wet. bathroom_dry. window_closed. The hypothesis can now be checked by: ?- leak_in_kitchen. yes

11 11 Disadvantages of using Prolog’s own syntax for rules: 1.This syntax may not be the most suitable for user unfamiliar with Prolog; for example, the domain expert should be able to read the rules, specify new rules and modify them. 2.The knowledge base is, therefore, not syntactically distinguishable from the rest of the program; a more explicit distinction between the knowledge base and the rest of the program may be desirable.

12 12 Knowledge base file if hall_wet and kitchen_dry then leak_in_bathroom. if hall_wet and bathroom_dry then problem_in_kitchen. if window_closed or no_rain then no_water_from_outside. if problem_in_kitchen and no_water_from_outside then leak_in_kitchen. The observed pieces of evidence can be started as prolog facts: fact(hall_wet). fact(bathroom_dry). fact(window_closed). Of course, we need a new interpreter for rules in the new syntax. Such an interpreter can be defined as the procedure Is_true(P)

13 13 % A backward chaining interpreter for if-then rules. % A simple backward chaining rule interpreter :- op( 800, fx, if). :- op( 700, xfx, then). :- op( 300, xfy, or). :- op( 200, xfy, and). is_true( P) :- fact( P). is_true( P) :- if Condition then P, % A relevant rule is_true( Condition). % whose condition is true is_true( P1 and P2) :- is_true( P1), is_true( P2). is_true( P1 or P2) :- is_true( P1) ; is_true( P2).

14 14 The interpreter can now be called by question: ?- is_true(leak_in_kitchen). yes A major practical disadvantage of the simple inference procedures in this section is that the user has to state all the relevant information as fact in advance, before the reasoning process is started. So the user may state too much or too little. Therefore, it would be better for the information to be provided by the user interactively in a dialogue when it is needed.

15 15 Data- Driven Reasoning (Forward Chaining) Data-Driven reasoning compare the contents of working memory with the conditions of each rule base using the ordering of the rule base. If the data in working memory supports a rule’s firing the result is placed in working memory and then control moves on the next rule. Once all rules have been considered search starts again at the beginning of the rule set. If a piece of information that makes up (part of) the premises of a rule is not the conclusion of some other rule then that fact will be deemed “askable” when control comes to the situation (rule) where that information is needed. Working memory initially has no information, and we examine premises of the four rules in order to see what information is askable. The premise, the engine is getting gas is not askable, so rule 1 fails and control moves to rule 2. The engine does not turn over is askable. Suppose the answer to this query is false, so the engine will turn over is placed in working memory. But rule 2 fails, since the first of two premises is false, and consideration moves to rule 3, where again, the first premise fails. At rule 4, both premises are askable. Suppose the answer to both questions is true. Then there is gas in the fuel tank and there is gas in the carburetor are placed in working memory, as is the conclusion of the rule, the engine is getting gas.

16 16 At this point all the rules have been considered so search now returns, with the new contents of working memory, to consider the rules in order a second time. When rule 1 is fired, its conclusion, the problem is spark plugs, is placed in working memory. In this example no more rules will match and fire and the problem-solving session is completed.

17 17 Fig 8.9The production system at the start of a consultation for data- driven reasoning.

18 18 Fig 8.10The production system after evaluating the first premise of Rule 2, which then fails. Fig 8.11The data-driven production system after considering Rule 4, beginning its second pass through the rules.

19 19 Fig 8.10The production system after evaluating the first premise of Rule 2, which then fails. Fig 8.11The data-driven production system after considering Rule 4, beginning its second pass through the rules.

20 20 At any time during the execution of a Prolog program, only two files are ‘active’: one for input and one for output. These two files are called the current input stream and the current output stream respectively. At the beginning of execution these two streams correspond to the user’s terminal. The current input stream can be changed to another file, Filename, by the goal: See(Filename) Communication with Files

21 21 Ex: reads something from file1 and then switches back to terminal: …… see(file1), read_from_file(Information), see(user), …….. The current output stream can be changed by a goal of the form: tell(Filename) A sequence of goals to output some information to file3, and then redirect succeeding output back to the terminal is: …. tell(file3), write_on_file(Iformation), tell(user), …….

22 22 The goal Seen Closes the current input file. The goal told we will assume here that files can only be processed sequentially. There are two main ways in which files can be viewed in Prolog: 1.One way is to consider the character as basic element of the file. One input or output request will cause a single character to be read or written get, get0 and put. 2.The other way of viewing a file is to consider bigger units of information as basic building blocks of the file. Such a natural bidder unit is the prolog term. So each input/output request of this type would transfer a whole term from the current input stream or to the current output stream respectively. Read and write.

23 23 Prolog has various input and output predicates. –write/1 - writes out a prolog term. –put/1 - writes one character with the given ASCCII code. –read/2 - reads in a prolog term. –get0/1 - reads next character. –get/1 – input next ‘printable’ character. –nl - causes the start of a new line at output. –tab(N) – output N blanks. Example: –sayhello :- write(‘Hello’), nl.

24 24 read(X) Will cause the next term, T, to be read, and this term will be matched with X. if X is a variable then, as a result, X become instantiated to T. If matching does not succeed then the goal read(X) fails. The predicate read is deterministic, so in the case of failure there will be no backtracking to input another term. Each term in the input file must be followed by a full stop and a space or carriage-return. If read(X) is executed when the end of the current input file has been reached then X will become instantiated to the atom end_of_file.

25 25 Update Program Some built-in predicates make it possible to update the facts and rules during the execution of the program. This is done by adding (during execution) new clauses to the program or by deleting existing clauses. Predicates that serve these purposes are: Assert ( C): causes a clause C to be asserted – that is, added to the database. Asserta( C) : adds C at the beginning of the database. Assertz( C) : adds C at the end of the database. Retract( C): it deletes a clause that matches C.

26 26 Clauses of any form can be asserted or retracted. Predicate type: 1.Static predicate 2.Dynamic predicate Only dynamic predicates can be manipulated by assert(a/z) and retract. A predicate is assumed static, unless announced in the program by declaration of the form: :- dynamic PredicateName(PredicateArity). Ex: :- dynamic member(2).

27 27 Arithmetic and Comparative Operators Position. –infix operators: +, -, *, /. –prefix operator: -. –postfix operator: !. Precedence. –Each operator has a precedence value associated with it. –Precedence values are used to decide which operator is carried out first. –In Prolog, multiplication and division have higher precedence values than addition and subtraction. Associativity. –An operator is either left associative or right associative. –In Prolog, arithmetic operations are left associative. –Round brackets can be used to enforce precedence and associativity.

28 28

29 29

30 30 Arithmetic and Comparative Operators Arithmetic expression: involving arithmetic operators A built-in operator (predicate) is can be used in an evaluation goal for evaluating an arithmetic expression (i.e. carrying out an arithmetic calculation). –The left hand side is usually a variable. –The right hand side is an arithmetical expression. Built-in predicates for making comparison between two arithmetic expressions in a goal. X =:= Ythe values of X and Y are equal. X =\= Y the values of X and Y are not equal. X < YX is less than Y. X > Y X is greater than Y. X =< Y X is less than or equal to Y. X >= Y X is greater than or equal to Y.

31 31

32 32

33 33

34 34 Clauses of any form can be asserted or retracted. Predicate type: 1.Static predicate 2.Dynamic predicate Only dynamic predicates can be manipulated by assert(a/z) and retract. A predicate is assumed static, unless announced in the program by declaration of the form: :- dynamic PredicateName(PredicateArity). Ex: :- dynamic member(2).

35 35 Forward Chaining Interpreter The following interpreter starts with what is already known (started in the fact relation), derives all conclusions that follow from this and adds (using assert) the conclusions to the fact relation. Our example knowledge base is run by this interpreter thus: ?- forward. Derived: problem_in_kitchen Derived: no_water_from_outside Derived: leak_in_kitchen No more facts

36 36 %A forward chaining rule interpreter. % Simple forward chaining in Prolog forward :- new_derived_fact( P), % A new fact !, write( 'Derived: '), write( P), nl, assert( fact( P)), forward % Continue ; write( 'No more facts'). % All facts derived new_derived_fact( Concl) :- if Cond then Concl, % A rule not fact( Concl), % Rule's conclusion not yet a fact composed_fact( Cond). % Condition true? composed_fact( Cond) :- fact( Cond). % Simple fact composed_fact( Cond1 and Cond2) :- composed_fact( Cond1), composed_fact( Cond2). % Both conjuncts true composed_fact( Cond1 or Cond2) :- composed_fact( Cond1) ; composed_fact( Cond2).


Download ppt "1 Lecture 16 - 17 Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,"

Similar presentations


Ads by Google