Week 8 - CLIPS

Assignment:

Review this document.

Read Chapters 5 (Inexact Reasoning) and 8 (Pattern Matching) in the text "Expert Systems: Principles and Programming".

Complete the following problems:

  1. A Herbicide Selection ES

    Problem Description

    Construct a CLIPS rule-based system that is capable of recommending a herbicide and the appropriate application rate for that herbicide for a given field situation. Turn in (email to engelb) a copy of the CLIPS knowledge base file(s) and two example runs of the knowledge base. The UNIX script command can be used to capture a session with your knowledge base. The user will be expected to supply the following information:

    1. Soil organic matter content possibilities will be < 2%, 2-4%, or > 4%
    2. Crop to be planted possibilities will be corn or soybeans
    3. Existing weed problem possibilities will be broadleaf or grass

    The problem of recommending a herbicide has been simplified greatly in order to keep it manageable as a classroom example. Information concerning the herbicides and their application guidelines are contained in the following table.

    The crops of concern are corn and soybeans and the weeds are grass and broadleaf. The following truth table should aid development of the necessary rules:

    Herbic.       Weed           Crop           Organic Matter  
                                          < 2%         2-4%        > 4%
    
    Sencor broadleaf C or S Do Not Use 3/4 pt/ac 3/4 pt/ac Lasso broadleaf or grass C or S 2 qt/ac 1 qt/ac 0.5 qt/ac Bicep broadleaf or grass C 1.5 qt/ac 2.5 qt/ac 3 qt/ac
    Note C is corn and S is soybeans in the above table.

    Use an editing tool (such as vi) to build the herbicide selection knowledge base. The knowledge base you create with an editor can then be saved in a file. The first rule from the table above might be:

    (defrule sencor-0
        (weed broadleaf)
        (crop corn | soybeans)
        (organic-matter  < 2%)
        =>
        (printout t crlf "Do not use Sencor!" crlf crlf))
    

    If you use templates, the same rule might be:

    (defrule sencor-0
        (field (weed broadleaf)
    	   (crop corn | soybeans)
    	   (organic-matter <2%))
        =>
          (printout t crlf "Do not use Sencor!" crlf crlf)
    )
    

    Note that if you use templates, the template you define might be something like:

    (deftemplate field
        (slot name)
        (slot weed)
        (slot crop)
        (slot organic-matter)
    )
    

    You will need to write approximately 9 rules.

    Once you have completed typing the rules, save them in a file with a name of your choice. Now follow the directions listed below:

    your/prompt% clips

    CLIPS > (watch all)      ; so you can follow what CLIPS is doing
    CLIPS > (load "file")    ; where file is your rule base
    

    If you have errors that CLIPS detects, exit CLIPS by typing (exit) and edit your file. If there are no errors, continue.

    CLIPS > (reset)
    CLIPS > (facts)
    CLIPS > (rules)
    CLIPS > (assert (crop corn))  ;this assumes you are NOT using the template approach
    CLIPS > (assert (weed broadleaf))
    CLIPS > (assert (organic-matter  < 2%))
    CLIPS > (run)
    
    Note that the facts that you assert may need to be modified slightly from those shown above, depending on the rules you create.

    If your program doesn't run, check your spelling in facts and in rules. Facts have to match rule conditions exactly!

    You may wish to try other runs with your knowledge base. CLIPS can be reset, additional facts asserted, and run. To do this, the following might be used:

    CLIPS > (reset)
    CLIPS > (assert (weed grass)(crop soybeans)(organic-matter  >4%))
    CLIPS > (run)
    

    To exit CLIPS type (exit) at the prompt.

    Facts can also be entered using a deffacts statement. Facts in a deffacts statement are placed in the fact list each time "(reset)" is typed.

    Using an editor, add the following deffacts statement to your herbicide selection knowledge base (note you may need to modify the form of the facts below depending on the form of your rules):

    (deffacts conditions
      (crop corn)
      (weed broadleaf)
      (organic-matter  _2-4%)
      )
    

    Save the knowledge base file, leave the editor and enter clips.

    CLIPS > (watch all)
    CLIPS > (load "kb.fil") ; where kb.fil is the name of your knowledge base file
    CLIPS > (facts)
    CLIPS > (rules)
    CLIPS > (reset)
    CLIPS > (facts)
    CLIPS > (run)
    CLIPS > (facts)
    CLIPS > (agenda)
    CLIPS > (reset)
    CLIPS > (facts)
    CLIPS > (agenda)
    CLIPS > (run)
    CLIPS > (exit)
    

    The previous knowledge base is not very useful since it can't interact with the user to acquire information. Add the following interface rule (or something comparable) to your knowledge base: The purpose of this rule is to ask 3 questions of the user, to allow him/her to enter the information, instead of having to assert facts at the CLIPS prompt.

    
    (defrule input
        (initial-fact)
        =>
        (printout t crlf "What is the crop? " crlf)
        (assert (crop =(read)))  ;this line reads what the user types
        (printout t crlf crlf "What is the weed problem?" crlf)
        (assert (weed =(read)))
        (printout t crlf crlf "What is the organic matter content?" crlf)
        (assert (organic-matter =(read))))
    

    After saving the knowledge base to a file, enter clips and type the following:

    
    CLIPS > (load "kb.fil")
    CLIPS > (reset)
    CLIPS > (facts)
    CLIPS > (run)
    

    Answer the questions. Remember that answers will have to match the format that was used in the rules exactly, i.e. soybeans can only be recognized by the rules as "soybeans", not beans, soybean, etc.

  2. Complete problem 7-9 in your book "Expert Systems: Principles and Programming". Do not worry about creating an interactive interface - asserting the facts is sufficient. Email the completed knowledge base for this problem to me (engelb).


Be sure to continue working on your term projects!