Artificial Intelligence Lecture No. 21 Dr. Asad Ali Safi Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology (CIIT) Islamabad, Pakistan.
Summary of Previous Lecture • • • •
Be Assertive variable more than once Retraction single-field wildcard
Today’s Lecture • Single-field wildcard • Multifield wildcard • single-field variable ? or a multifield variable ?$ • Anywhere • Single- and multifield wildcards
single-field wildcard • Instead of binding a field value to a variable, the presence of a nonempty field can be detected alone using a wildcard. • This type of situation, in which only part of the fact is specified, is very common and very important. To solve this problem, a wildcard can be used to fire the Richards. • The simplest form of wildcard is called a single-field wildcard and is shown by a question mark, "?". The "?" is also called a single-field constraint. • A single-field wildcard stands for exactly one field
(clear) (defrule meeting (meeting-with Khan ?) => (printout t " The meeting will be with Khan…" crlf))
(deffacts M1 (meeting-with Kamal) (meeting-with Khan ) (meeting-with Khan Ali ) (meeting-with Khan Ali Khan) (meeting-with Ali Khan)) (reset) (run)
Three fields • The pattern includes a wildcard to indicate that Khan's last name is not important. • So long as the first name is Khan, the rule will be satisfied and fire. • Now with pattern of three fields of which one is a single-field wildcard, only facts of exactly three fields can satisfy it. In other words, only Khan with exactly two names can satisfy this • Suppose you want to specify Khan with exactly three names?
• With first name (meeting-with Khan ? ?)
• or, if only persons with three names whose middle name (meeting-with ? Khan ?)
• or, if only the last name was Dopey, as in the following • (meeting-with ? ? Khan)
Multirules • Another interesting possibility occurs if Khan must be the first name, but only those Khan’s with two or three names are acceptable. One way of solving this problem is to write two rules. (defrule meeting (meeting-with Khan ?) => (printout t " The meeting will be with Khan " crlf)) (defrule meeting (meeting-with Khan ? ?) => (printout t " The meeting will be with Khan " crlf))
Bind • Of course, if you don't want anonymous meeting, you need to bind the Khan’s names with a variable and print them out. (meeting-with Khan ?a ?b)
Multifield wildcard • Rather than writing separate rules to handle each field, it's much easier to use the multifield wildcard. • This is a dollar sign followed by a question mark, "$?", and represents zero or more fields. • Notice how this contrasts with the single-field wildcard which must match exactly one field. • The two rules for dates can now be written in a single rule as follows.
(clear) (defrule meeting (meeting-with Khan $?) => (printout t " The meeting will be with Khan " crlf)) (deffacts M1 (meeting-with Kamal) (meeting-with Khan ) (meeting-with Khan Ali ) (meeting-with Khan Ali Khan) (meeting-with Ali Khan))
(reset) (run)
variable such as ?x, $?x • Wildcards have another important use because they can be attached to a symbolic • field to create a variable such as ?x, $?x, ?name, or $?name. • The variable can be a single-field variable or a multifield variable depending on whether a "?" or "$?" is used on the LHS. • Note that on the RHS only a ?x is used, where the "x" can be any variable name. • You can think of the "$" as a function whose argument is a single-field wildcard or a single-field variable and returns a multifield wildcard or a multifield variable, respectively.
(defrule meeting (meeting-with Khan $?name) => (printout t " The meeting will be with Khan " ?name crlf))
• As you can see, on the LHS, the multifield pattern is $?name but is ?name when used as a variable on the RHS. When you enter and run, you'll see the names of all eligible Khan. • The multifield wildcard takes care of any number of fields. Also, notice that multifield values are returned enclosed in parentheses.
Anywhere • Suppose you wanted a match of all candidates who had a Khan somewhere in their name, not necessarily as their first name. (defrule meeting (meeting-with $?First Khan $?Last) => (printout t " The meeting will be with” ?First ”Khan" ?Last crlf))
• The pattern matches any names that have a Khan anywhere in them.
Single- and multifield wildcards • Single- and multifield wildcards can be combined. For example, the pattern (meeting-with ? $? Khan ?)
• means that the first and last names can be anything and that the name just prior to the last must be Khan. • This pattern also requires that the matching fact will have at least four fields, since the "$?" matches zero or more fields and all the others must match exactly four.
Single- and multifield wildcards.. • Although multifield variables can be essential for pattern matching in many cases, their overuse can cause much inefficiency because of increased memory requirements and slower execution. – As a general rule of style, you should use $? only when you don't know the length of fields. Do not use $? simply as a typing convenience.
Summery of Today’s Lecture • Single-field wildcard • Multifield wildcard • single-field variable ? or a multifield variable ?$ • Anywhere • Single- and multifield wildcards