CS141: An Expert System for Medical Diagnosis


In this exercise, we will implement a medical diagnosis system.

Rules for Diagnosing a Patient

The first step in solving any problem is to make sure you fully understand the problem. Don't worry about how you are going to implement the solution, instead, just make sure you understand what your code must do.

To understand the medical diagnosis system, look through the pseudocode for the full rules:

if the patient's body temperature is 95 degrees or less

ask if the patient has a pulse

if so,

tell the doctor to run a hot bath

else

tell the doctor to call the morgue

else if the patient has a normal temperature

ask if the patient's head itches and has white egglike dots or crawling things in his hair

if so

tell the doctor to soak the patient's head in insecticide

else

ask if the patient has been losing extremities, such as fingers, toes, or nose, for unexplained reasons

if so,

caution the doctor not to give direct mouth-to-mouth resuscitation

else // the patient has a temperature of 101 or greater

ask if the patient has a bad cough

if so,

ask if he/she has blue fingernails

if so

tell the doctor to give antibiotics

else

tell the doctor to give Tylenol

else

find out if the patient is foaming at the mouth

if so

find out if the patient was recently bitten by a dog, cat, or bat

if so,

tell the doctor to notify the next of kin that the patient might not get better

The Decision Tree

Based on the above rules, draw a decision tree so that you can see the full structure. You can check your tree with the one given here (however, try to construct the tree yourself before looking!) You will need this tree diagram to test your program.

From the tree, it is easy to see that some of the sequences of answers lead to no diagnosis (these are the missing leaves). Rather than have no diagnosis, you will need to add additional code so that the program tells the patient that the doctor doesn't know what's wrong. No matter what, your program should give some answer even if it is to say it doesn't know the answer.

Note, you should not have written any code yet!

Stepwise Refinement

Stepwise refinement is a problem solving technique where a problem is broken down into a sequence of easy to solve subtasks. By sequentially solving (and testing) each of the subtasks, you easily work your way towards the final full solution.

For example, in this problem, you don't want to implement all of the rules at once. If you do, you will most likely end up with many errors that are hard to track down. If you instead implement a little at a time, you will have less trouble pinpointing any problems.

Subtask 1: Ok, we are now ready to begin coding. The first subtask will be to implement the outermost if-else statements. That is, write a program that prompts the user for their name (string) and temperature (integer) and then prints out to the screen whether the temperature is 1) 95 degress or less, 2) is within normal range, or 3) 101 or greater:

if the patient's body temperature is 95 degrees or less

print "The temperature is 95 degrees or less"

else if the temperature is normal

print "The temperature is normal"

else // the patient has a temperature of 101 or greater

print "The temperature is 101 or greater"

Be very careful with your indentation and with where you place brackets. The indentation helps you see where logical errors might occur. If the brackets are not correct, the program may run but not be correct (run-time error).

Before continuing to the next subtask, it is important that you thoroughly test the above code for both compile and run-time errors.. Assuming that the code compiles, run the code multiple times so that it prints out each of the three statements. Check that the boundary conditions are coded correctly, e.g. does the program output the correct answer when the temperature entered is exactly 95 or 101 degrees?

Subtask 2: The next subtask might be to replace the first print statement ("The temperature is 95 degrees or less"") with the appropriate if-else statement from the full set of rules (hot bath or morgue). The added if-else statement should be nested inside the body of the first outermost if statement.

Test your code for subtask 2. Each time you implement a little more, use the decision tree to help direct the testing of your program.

Testing the Entire Code: When you think you are completely done, test each leaf of the tree. That is, for each leaf, enter the sequence of answers that should lead to that leaf. Does it give the right answer?