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

Slides:



Advertisements
Similar presentations
3. Lists, Operators, Arithmetic. Contents Representation of lists Some operations on lists Operator notation Arithmetic.
Advertisements

EXPERT SYSTEMS AND KNOWLEDGE REPRESENTATION Ivan Bratko Faculty of Computer and Info. Sc. University of Ljubljana.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Logic.
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
PROLOG I/O I/O and the relational model. PROLOG I/O operators. –Not really predicates. I/O with characters. I/O with terms. I/O with lists. I/O.
FATIH UNIVERSITY Department of Computer Engineering Input and Output Notes for Ch.6 of Bratko For CENG 421 Fall03.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Knowledge Representation and Expert Systems by Sujan Pakala Stephen Brown.
Expert System Human expert level performance Limited application area Large component of task specific knowledge Knowledge based system Task specific knowledge.
Introduction to Structured Query Language (SQL)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
1 Chapter 9 Rules and Expert Systems. 2 Chapter 9 Contents (1) l Rules for Knowledge Representation l Rule Based Production Systems l Forward Chaining.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
EXPERT SYSTEMS Part I.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
JavaScript, Third Edition
Introduction to C Programming
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Knowledge based Humans use heuristics a great deal in their problem solving. Of course, if the heuristic does fail, it is necessary for the problem solver.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Introduction to Programming with RAPTOR
Declarative Programming Arithmetic in PROLOG Autumn 2014.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Week 3 Help desks – Mon 4-5 CO238 Tutor: Su – Wed 1-2 CO238 Tutor: Urvesh – Fri CO238 Tutor: Bing Lectures: Mon, Tue, 3-4, HU119 Tutorial: Fri, 3-4,
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Forward and Backward Chaining
Logic Programming Lecture 9: Constraint logic programming.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Artificial Intelligence Programming in Prolog
Bill Tucker Austin Community College COSC 1315
Expressions and Assignment
Introduction to Programming for Mechanical Engineers (ME 319)
Chapter 2 - Introduction to C Programming
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
The Selection Structure
Chapter 2 - Introduction to C Programming
Tests, Backtracking, and Recursion
Control Structures – Selection
Programming Funamental slides
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Chapter 2: Introduction to C++.
EXPERT SYSTEMS AND KNOWLEDGE REPRESENTATION Ivan Bratko Faculty of Computer and Info. Sc. University of Ljubljana.
Classes, Objects and Methods
OPERATORS in C Programming
Operator King Saud University
OPERATORS in C Programming
Presentation transcript:

1 Lecture Expert Systems &

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 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

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

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 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 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 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 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 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 % 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 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 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 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 Fig 8.9The production system at the start of a consultation for data- driven reasoning.

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 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 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 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 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 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 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 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 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 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

29

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

32

33

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 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 %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).