Knowledge based expert systems

There are knowledge based agents and expert systems that reason using rules of logic. These systems that do what an expert in a given field might do, tax consulting, medical diagnosis etc. They do well at the type of problem solving that people go to a university to learn. Usually predicate calculus is used to work through a given problem. This type of problem solving is known as ’system inference’. The program should be able to infer relationships, functions between sets, some type of grammar, and some basic logic skills. The system needs to have three major properties: soundness, confidence that a conclusion is true; completeness, the system has the knowledge to be able to reach a conclusion; and tractability, it is realistic that a conclusion can be reached.

Reasoning is commonly done with if-then rules in expert systems. Rules are easily manipulated, forward chaining can produce new facts and backward chaining can check statements accuracy. The newer expert systems are set up so that users, who are not programmers, can add rules and objects and alter existing rules and objects. This provides a system that can remain current and useful with out having to have a full time programmer working on it.

There are three main parts to the expert system: knowledge base, a set of if-then rules; working memory, a database of facts; inference engine, the reasoning logic to create rules and data.

The knowledge base is composed of sentences. Each sentence is a representation of a fact or facts about the world the agent exists in or facts the expert system will use to make determinations. The sentences are in a language known as the knowledge representation language.

Rule learning for knowledge based and expert systems is done with either inductive or deductive reasoning. Inductive learning creates new rules, that are not derivable from previous rules about a domain. Deductive learning creates new rules from existing rules and facts.

Rules are made of antecedent clauses (if), conjunctions (and, or) and consequent clauses (then). A rule in which all antecedent clauses are true is ready to fire or triggered. Rules are generally named for ease of use and usually have a confidence index. The confidence index (certainty factor) shows how true something is, i.e. 100\% a car has four wheels, 50\% a car has four doors. Sometimes sensors are also part of the system. They may monitor states in the computer or environment. The Rete algorithm is the most efficient of the forward chaining algorithms.

Reasoning can be done using ‘Horn Clauses’, these are first-order predicate calculus statements that have, at most, one true literal. Horn Clauses have linear order time algorithms and this allows for a faster method of reasoning through lots of information. This is usually done with PROLOG or lisp. Clauses are ordered as such: goal, facts, rules. Rules have one or more negative literals and one positive literal that can be strung together in conjunctions that imply a true literal. A fact is a rule that has no negative literals. A list of positive literals with out a consequent are a goal. The program loops checking the list in order, when a resolution is performed a new loop is begun with that resolution. If the program resolves its goal the proof can be given in tree form, ‘and/or tree’.

Nonmonotomic reasoning is used to fix problems created by a change in information over time. More information coming in negates a previous conclusion and a new one needs to be drawn.

A conflict resolution process must be put in place as well to deal with conflicting information. This can be done by: first come, first serve; most specific rule is kept; most recently changed data rule triggered; once rule is resolved take it out of the conflict resolution set.

Forward chaining takes the available facts and rules and deduces new facts which it then uses to deduce more new facts, or invoke actions. Forward chaining can also be done by simply the application of if-then statements: The RETE algorithm is the most efficient at doing forward chaining right now, it compiles the rules into a network that it traverses efficiently. This is similar to the blackboard systems.

Dynamic knowledge bases, known as truth maintenance systems, may be used. This uses a ’spreadline’ which is similar to a spread sheet that will calculate missing and updated values as other values entered.

General algorithm forward chaining
load rule base into memory
load facts into memory
load initial data into memory
match rules to data and collect triggered rules

LOOP

if conflict resolution done BREAK
use conflict resolution to resolve conflicts among rules
fire selected rules

END LOOP

Backward Chaining evaluates a goal and moves backward through the rules to see if true. An example is a medical diagnosis expert system, that takes in information from questions then returns a diagnoses. PROLOG systems are backward chaining.

General algorithm backward chaining
load rule base into memory
load facts into memory
load initial data
specify a goal
load rules specific to that goal onto a stack

LOOP
if stack is empty BREAK
pop stack

WHILE MORE ANTECEDENT CLAUSES

if antecedent is false pop stack and NEXT WHILE
if antecedent true fire rule and NEXT WHILE
if antecedent unknown PUSH onto stack (we may later ask user for more information about this antecedent.

END LOOP

The Rete Algorithm is considered to be the best algorithm for forward chaining expert systems. It is the fastest but also requires much memory. It uses temporal redundancy, rules only alter a few facts at a time, and structural similarity in the left hand side of rules to do so.

The Rete is a an acyclic graph that has a root node. The nodes are patterns and the paths are the left hand sides of the rules. The root node has one kind node attached to it for each kind of fact. Each kind node has one alpha node attached to it for each rule and pattern. Then the alpha nodes have associated memories which describe relationships. Each rule has two beta nodes. The left part is from alpha(i) and the right from alpha(i+1). Each beta node stores the JOIN relationships. Changes to rules are entered at the root and propagated through the graph.

Knowledge based agents loop through two main functions. One is to sense the world and TELL the knowledge base what it senses, two is to ASK what it should do about what it senses, which it then does. An agent can be constructed by giving it all the sentences it will need to perform its functions. An agent can also be constructed by a learning mechanism that takes perceptions about the environment and turns them into sentences that it adds to the knowledge base.

The Plant Doctor (perl)
Plant doctor in action

24 Comments

  • mngmuia

    So which algorithm is used to develop the plant doctor

  • ljmacphee

    Forward chaining.

    The information was collected from expert sources.
    Then I put it into a graph form.
    From graph form I put it into the final statistical format that is used in the program.

  • mngmuia

    I have developed something similar but it is proving not to be intelligence.For instance when I input all the symptoms for a particular disease it gives many diseases probabilities.

    How did you actually implement the weighting?

  • ljmacphee

    I used a 2 dimensional graph - plant diseases across the top and plant symptoms down the side.

    Diseases that only had one symptom - that symptom gets 100% the rest of the percents were evenly divided % for each disease - 3 symptoms meant 33% each.

    So when the program runs the user checks off all the symptoms. Then the percents skew towards the most likely plant problem.

  • mngmuia

    I wish you could send me your documentation.My email is mngmuia@yahoo.com.

    I still do not understand how learning takes place coz I want users to be able to add diseases without going to the code.

    Please assist

  • ljmacphee

    I am sorry but everything I know about ai so far is here on the website. There is no other documentation to send you.

    If I understand you correctly you want users to add diseases and symptoms, then another user could just add symptoms and a list of likely diseases would list?

    First you will have to build the database of symptoms and diseases. Otherwise it won’t be adaptable.

    Then you will need an interface so the users can enter the data into the database with out needing to access the code.

    Then after you have the data, you will have to decide which ai type is the best for giving you the correct answers and you will have to experiment some I expect to get it to work correctly.

    I’d try an expert system like the ones described above first. If that didn’t work I’d look into bayesian systems and finally a neural network.

    You are going to have to experiment with your data to see what works best.

    Expert systems get there name because they are built using the knowledge of experts in a given field. Have you spoken to any doctors about how they diagnose patients? I used experts in plants to help determine what symptoms tied into what plant problems.

  • mngmuia

    What I meant is how you mapped the diseases and symptoms on the graph.If you could sent me that document not the code so I can look at it and really understand

    By the way can I conclude that you used rete algorithm to implement the forward chaining?

    I did see a doctor and I have all the onfo I need and I want to use your approach.However I want some learning

  • kendihilla

    Your expert system works perfectly.
    I am doing a similar system but for dairy cattle disease diagnosis.
    I’ve looked and used your code and dont quite seem to understand how it works.
    could you explain to me what happens when diseases share symptoms;what do you do with the weights and how do you produce the confidence factors.
    thanks

  • ljmacphee

    What I did is what I explained ( this code is almost 10 years old there are no notes hanging around ).

    You draw a grid. List symptoms on one axis and diseases on the other axis.

    Then weight the symptoms so that they only trigger the correct disease response.

    There is nothing complicated about what I did. You are looking for something more complex but it is really very simple. I did it with a pencil and paper in a few hours.

    The user then checks off symptoms in the form.

    The program then returns the probable causes and returns the most likely or only one if that is the case. ( It often is )

    There are no fancy calculations here it is a very simple problem. A more complex problem would require more complex calculations.

    Some groups of symptoms produce only one possible cause, other groups of symptoms may have multiple causes and these are weighted as to what causes are most likely.

    The code is the end result- the weighting etc was all done on the graph with a pencil.

  • ljmacphee

    For example:

    Symptoms:
    red dots
    red dots w/ white tips
    fever

    Disease:
    Chicken pox ( red dots/red dots w/ white tips/fever )
    Measles ( red dots/fever )
    Mosquito bites ( red dots )
    Fire ant bites ( red dots/ red dots w/ white spots )

    Now I can tell by which symptoms are checked exactly which disease has occurred.

    If they don’t map exactly I can estimate the likely ones.

    No magic here just simple logic and a grid of symptoms and diseases with dots in places they match.

  • kendihilla

    am a student just learning.I have conceptualized this in a graph,perfect.The problem is how to weight the symptoms.

    For example:
    amongst other symptoms which are shared by other diseases,there is one that is distinct to the disease.How do you ensure that once that symptom is encountered,that disease is output.

    secondly,which part of your code illustrates the weighting.Sorry am not a very good coder;hoping to be.
    My code seems to give me the likely diseases more than one actual diseases. so i suspect i need to learn a trick so that this part
    if ($tooMuchWater > .07){ $tmw = 1; }
    if ($tooLittleWater > .07 ){ $tlw = 1; }
    if ($tooMuchSun > .1 ) { $tms = 1; }
    if ($tooLittleSun > .07 ) { $tls = 1;}
    if ($tooMuchHumidity > .1) { $tmh = 1;}

    how to convert my graph to this.
    thanks

  • ljmacphee

    That’s the end result.

    First draw a grid. On the x-axis list all the diseases
    On the y-axis list all the symptoms.

    Put a dot in each square where the symptom is part of the disease.

    When you are done you will have some sets of symptoms that match only one disease ( like the white-dot, red-bump, temperature matches only chicken pox. ) So if you get that combination of symptoms you know it must be chicken pox.

    So match all the specific sets to specific diseases and pull those out ( if x1, x2, x3 then disease = y1 )

    Now you have to decide how to weight the partial ones. Like red bumps can be any of the diseases so give chicken pox .34 since there are 3 possible symptoms, give measles .5 since it has 2 possible symptoms.

    When every thing is tallied up print out the top 3 most likely diseases.

    Work it through on paper with just a dozen or some diseases and get a feel for it before you try coding.

    You may decide you need to weight your symptoms a bit different and that is where playing with it and getting a feel for the specific problem comes in.

    You want to hard code as much of you program as possible to make it faster, or to let it do the calculating if you are more interested in flexibility rather than speed. Since this code is running online I wanted it to be fast for people.

  • kendihilla

    This was perfect,i get it.
    being a Kenyan, i understand farming elements quite well.so i will use your example.
    In your code you seem to increment once and other times by 2 or 3,how is this related to the weights you manually assigned?

    Secondly,how does it relate to the end result
    e.g if ($tooMuchWater > .07){ $tmw = 1; } or there are actually pieces of code that are missing from what you have published?

    I intend to have mine run online so speed is an issue.

  • ljmacphee

    The code is all there.

    Yes, I walked through symptoms on paper and adjusted to be sure answer is correct for all possibilities. That is how weights were adjusted.

    That’s just saying if the $tooMuchWater tally is greater than .07 that’s surely part of the problem so flag tmw ( too much water ) as true.

  • kendihilla

    In which part of your code have you matched all the specific sets of symptoms to specific diseases ?
    i only see you weight the partial ones.

    Then, where are your percentages coming from in the case where you are not sure of the answer? i dont see it reflected in the code.
    Thanks

  • ljmacphee

    There weren’t any for this example.

    And, again, it was done with pencil and paper, the code is the final result.

    The code does not calculate any percents. It was done this way so as to make the code faster.

    Please re-read previous replies to this same question.

  • kendihilla

    Am having problem with the percentages that appear when you run the application,yet i dont see how they are reflected in the code.I mean,no print statement for percentages.

    please send me the current code for what is running in the internet.
    Thanks

  • ljmacphee

    You have the current code.

    But if you are truly interested in learning how to develop AI expert systems, perhaps instead of trying to clone my system you should work on developing your own method for solving this problem.

    Expert systems are in part created from different algorithms and in part from an art form of putting experts skills into a working computer program.

    I suggest you spend more time talking to your experts and get a better feel for how they come to their answers.

    You mentioned you are a student, perhaps you can get one of your professors to sit down with you and better explain how expert systems are developed?

  • kendihilla

    can i do this with php

  • ljmacphee

    Totally I’ve re-written that program in PHP and it only took an hour to switch.

    Converting old Perl scripts to PHP

  • kendihilla

    i successfully managed to develop something(in perl) but i am now interested in integrating some case based reasoning. is it possible to do some without redoing the code?
    secondly,can you state authoratitivelythat the plant doctor is a rule based expert system?
    thanks

  • ljmacphee

    You will have to redo the code, case based reasoning is a different algorithm.

    I can not say anything about your code, I have not seen it.

  • kendihilla

    i dint mean you comment on my code but yours

  • ljmacphee

    But you have my code and I’ve told you how I developed it already.

You must be logged in to post a comment.