CMPU 102 - Assignment 8 Due: by 11:59 PM on Thursday, April 27th

$Revision: 1.1 $

Writing an Expert System

In this assignment you will use a binary tree to implement an expert system.  An expert system represents knowledge about some problem domain using a decision tree.  Based on the decision tree, the expert system can ask a series of yes or no questions to solve a classification problem.  If the expert system fails (reaches a point where the questions it asks do not correctly classify the input that the user is describing), it prompts the user for the correct answer, a new question that will be able to correctly classify the input in the future.  In this assignment, the classification problem is "What kind of animal are you?".

Here is an example of the user interacting with the expert system (user input shown in bold):

Welcome to the Animal Guessing Game!  I will
ask you a sequence of questions with yes or no
answers and then I will make a guess.  Ready?  
Here goes!!

Are you a mammal? [y or n]: y

Are you terrestrial? [y or n]: y

My guess is kangaroo.  Am I right? [y or n]: n

I give up.  What are you? horse

Please type a question that will distinguish a horse from
a kangaroo:  Are you a marsupial?

Are you a marsupial, horse? [y or n]: n

Shall we play again? [y or n]: y

Are you a mammal? [y or n]: y

Are you terrestrial? [y or n]: y

Are you a marsupial? [y or n]: n

My guess is horse.  Am I right? [y or n]: y

Shall we play again? [y or n]: n

Thank you for teaching me a thing or two.

In this example, if the initial tree looks like this:

           Are you a mammal?
                /\
               y  n
              /    \
     Are you        shark
   terrestrial?      
       /\             
      y  n            
     /    \          
kangaroo  whale   

The final tree looks like this:

                  Are you a mammal?
                       /\
                      y  n
                     /    \
            Are you        shark
          terrestrial?      
              /\             
             y  n            
            /    \          
     Are you a    whale
     marsupial?  
       /\
      y  n
     /    \
kangaroo   horse

Tasks

First, implement the expert system so that it starts out with the following initial decision tree:

  Are you a mammal?
        /\
       y  n
      /    \
kangaroo   shark

By playing the guessing game with the user, the expert system should add additional nodes in order to be able to classify additional animals.

The next task, once you have your expert system working correctly, is that when the user chooses not to play again, the current decision tree should be saved in a file.  When the program is restarted, the program should try to re-load the decision tree from that file (falling back to the original fixed initial decision tree if the file cannot be found).

Hints

Your decision tree will be a binary tree.  The leaf nodes are answers, and the interior nodes are questions.

You can use a recursive method to save the decision tree to a file.  Suggestion: store each node of the decision tree as a single line of text.  The format might look something like this:

Question:Are you a mammal?
Answer:kangaroo
Answer:shark

Questions are followed by the "yes" subtree and then the "no" subtree.

Once you can successfully save a decision tree to a file, you should be able to read it back using a very similar method.  You can use BufferedReader to read each line of input, and then determine whether each line starts with "Question:" or "Answer:" in order to determine what kind of node should be created.  The rest of the line is the text of either a question or an answer.

The file that you use to load and save the decision tree should be called "decisiontree.dat".

Getting Started

Get started by importing assign8.zip into your Eclipse workspace.  The class containing the main method for the expert system program is ExpertSystem.  A skeletal structure is provided; you will need to fill in the details.  Note that you will need to add at least one additional class: a class to represent the nodes in your decision tree.  To execute the expert system, right click on "ExpertSystem.java" and choose "Run As->Java Application".  You should be able to interact with the program through the Console window.

When You Are Done

Make sure your expert system can learn new classifications based on user input, and that you can save and load the decision tree to and from the file called "decisiontree.dat".

To submit: from a terminal window:

cd
cd eclipse-workspace
submit102 assign8