C Inference (Part 1 of 4)

TOOLS tools at raybed2.UUCP
Tue Jan 28 03:09:53 AEST 1986


As requested by the author we are reposting the source and documentation of
a C inference engine. Address all questions to George Hageman at:
UUCP: {asgb!benish}!hageman
MAIL:	George W. Hageman
	P.O. Box 11234
	Boulder, Colorado  80301
NOTE: RAYTHEON Inc. is not reponsible for the contents and/or consequences
of use of this software. This software is totally the work of George Hageman
and is being reposted as per his request (see following message). Address all
questions, comments, etc. to him.
=========================================================================
>From linus!decvax!seismo!hao!asgb!benish!hageman Fri Jan 24 07:31:04 1986
>Subject: Re:  C inference engine
>
>	... it seems that
>	the probability of getting somthing out to net.and is
>	inversely proportional to the number of hops it has to go.
>
>	I'll send you all of the shars (inference rulecompiler and 
>	the storm expert).  If you could make sure that they are
>	available at your site either by reposting them from your
>	end or by some other means it would be appreciated.
>
>	Thanks,
>
>George [Hageman]
=========================================================================
-------------------------CUT HERE----------------------------------------
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	README
#	inference.doc
sed 's/^X//' << 'SHAR_EOF' > README
XFrom linus!decvax!decwrl!pyramid!ut-sally!seismo!hao!asgb!hageman Tue Dec 31 15:31:49 1985
XRelay-Version: version B 2.10 5/3/83; site raybed2.UUCP
XPosting-Version: version B 2.10.2 9/18/84; site asgb.UUCP
XPath: raybed2!linus!decvax!decwrl!pyramid!ut-sally!seismo!hao!asgb!hageman
XFrom: hageman at asgb.UUCP (George W. Hageman)
XNewsgroups: net.sources
XSubject: Inference engine documentation
XMessage-ID: <831 at asgb.UUCP>
XDate: Tue, 31-Dec-85 15:31:49 EST
XArticle-I.D.: asgb.831
XPosted: Tue Dec 31 15:31:49 1985
XDate-Received: Wed, 1-Jan-86 20:25:24 EST
XDistribution: na
XOrganization: Burroughs Corp. ASG, Boulder Colo.
XLines: 1201
X
XThe following is the documentation file for an inference engine
Xwritten in C.   There will be  three other submittals to net.souces
Xconsisting of the source for a rule compiler, inference engine, and,
Xa weather prediction expert rule base including an animals expert.
X
XHave fun....
X
X-----------------------  Cut Here -----------------------------
SHAR_EOF
sed 's/^X//' << 'SHAR_EOF' > inference.doc
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        INTRODUCTION: 
X
X        The  software contained in this distribution is copyright (C)  by 
X        George   Hageman   1985   and  is  released   into   the   public               
X        domain with the following restrictions:                         
X
X             (1)   This  software  is intended  for  non-commertial usage.                                              
X             (2)    I   am   held  save  from  damages   resulting   from               
X             its use, and
X             (3)   The following concepts and legal jargon are agreed  to 
X             by the user of this software.
X
X             User-supported software concept:
X
X                  IF          you find use for this software
X                  ANDIF       it saves you some development time
X                  THEN            send me $10.00
X                  ANDTHENHYP      you will feel good!
X
X        This source code is provided on an "as is" basis without warranty 
X        of any kind,  expressed or implied,  including but not limited to 
X        the  implied  warranties  of merchantability and  fitness  for  a 
X        particular   purpose.    The  entire  risk  as  to  quality   and 
X        performance  of this software is with you.   Should the  software 
X        prove  defective,  you  assume the entire cost of  all  necessary 
X        repair, servicing, or correction.  In no event will the author be 
X        liable to you for any damages,  including any lost profits,  lost 
X        savings, or other incidental or consequential damages arising out 
X        of  the   use  of inability to use this software.   In  short  my 
X        friends,  I  have done  a reasonable amount of work in  debugging 
X        this  software and I think it is pretty good but,  as  you  know, 
X        there  is always some chance that a bug is still lurking  around. 
X        If you should happen to be lucky enough to  find one,  please let 
X        me know so I    can make an attempt to fix it.          
X
X             The  following  is  a short description of how  to  use  the 
X        inference  engine  and rule-compiler contained in  this  software 
X        release.    The source and object files for the rule compiler and 
X        the inference engine are contained in the rcomp.lbr and infer.lbr 
X        respectively.   There are common files contained in each library.  
X        These  common  files  are header files which are used  to  define 
X        common terms between the different sources.    The most important 
X        header file is the file named "expert.h" which not only  contains 
X        common  definitions  used  between  the  rule  compiler  and  the 
X        inference  engine,  but has a short description of their usage as 
X        well.    This inference engine, and its associated rule compiler, 
X        represents  a  significant  time investment for  me,  so  if  you 
X        believe in the  shareware concept please remember my address.
X
X                            George W. Hageman
X                            P.O. Box 11234
X                            Boulder, Colorado  80301
X
X
X
X
X
X        George W. Hageman            --1--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X             This  software compiles using the Microsoft C  Compiler  Rev 
X        3.0  using the make function which comes with the Microsoft Macro 
X        Assembler Rev.  4.0.  I have nothing but good things to say about 
X        these  two products and suggest that you consider their  purchase 
X        if  you are into serious software development for the  PC.   This 
X        software  also compiles and runs under UNIX system  V.   Use  the 
X        UNIXSV  flag  in the makefile or use a -DUNIXSV when you  compile 
X        it.
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X        George W. Hageman            --2--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        INFERENCE ENGINES:
X
X             An  inference engine is merely a program which  attempts  to 
X        prove consequents given a certain set of antecedents and a set of 
X        rules  which define the TRUTH or FALSEness of each consequent  in 
X        terms  of the antecedents.     The consequents,  antecedents  and 
X        rules  for  this  inference engine are contained in a  text  file 
X        which is compiled by the rule-compiler into a form the  inference 
X        engine  can understand.   Often these two functions are contained 
X        in the same executeable,  but I have decided to split them up  to 
X        make the inference engine as small as possible.
X
X             Rules  are collections of ANTECEDENTS and CONSEQUENTS formed 
X        into  TRUTH statements.   Each rule is an attempt state that  "If 
X        all of the antecedents for this particular RULE are  TRUE,   then 
X        all  of the consequents connected to this rule are TRUE.   If one 
X        or  more  of the antecedents for a RULE are  FALSE,  then  it  is 
X        assumed that this rule cannot prove the TRUTH of the consequents, 
X        but  this does not necessarily prove the consequents FALSE  since 
X        some  other rule may prove them TRUE."  Rules must have at  least 
X        one  ANTECEDENT  and  at least one CONSEQUENT  to  be  considered 
X        valid. 
X
X             Each   ANTECEDENT  and  CONSEQUENT  is  a  simple  statement 
X        consisting  of  a  leading  KEYWORD,   and  a  FOLLOWING  STRING.  
X        KEYWORDS tell the inference engine what the FOLLOWING STRING will 
X        mean or what is to be done with it.  The FOLLOWING STRINGs may be 
X        either  in upper or lower case and are either statements such  as 
X        "THE  ANIMAL IS A BAT",  or,  a pathname to an executeable  which 
X        will  be loaded and executed depending on what is defined by  the 
X        KEYWORD. Strings, except for the number of leading blanks, can be 
X        considered equal only if they are identical.  The reason for this 
X        rule  will  become  apparent later.   An example  of  a  pathname 
X        FOLLOWING STRING is "/b1/hageman/expert/storm/gt_3200  data.fil".  
X        Note  that  the strings denoting pathnames should be  exactly  as 
X        they  would  be if the pathname were to be given at  a  terminal, 
X        also,  you  may  include  parameters with  any  pathname.   These 
X        parameters  are no different than the parameters that  you  would 
X        use  if  you  were initiating the executeable from  the  terminal 
X        rather than via the inference engine.    Under MS_DOS these  path 
X        names  can  either  be  upper or lower case,  and  for  the  UNIX 
X        operating system, they must correspond to the exact path name.
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X        George W. Hageman            --3--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        QUICK AND DIRTY:
X
X             Impatient?   Well  here  are some quick ways to get  started 
X        with the inference engine,  leave all that reading till later!
X
X        ONE:
X
X             Use  your  text  editor (WS in the  Non-document  mode  only 
X        Please) to create a quick rule file,  or use the animals  example 
X        contained in this release.   Skip to the next page if you want to 
X        find out what the KEYWORDS are and how to use them.
X
X        TWO:
X
X             Compile the rules with the rule-compiler by typing..
X
X             rulecomp inputfile outputfile 
X
X             where  the inputfile is the filename of the file  containing 
X        your  rules,  and the outputfile is the file inwhich you want the 
X        compiled rules to be written to.  Caution, the rule compiler does 
X        not  check  for the equivalence of the inputfile  and  outputfile 
X        filenames,   if they are identical you will probably have to type 
X        in your rules again.
X
X        THREE:
X
X             Run the inference engine by typing ..
X
X             inference outputfile
X
X             answer the questions and go back to step ONE if you found an 
X        error with your rules or you want to expand them.
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X        George W. Hageman            --4--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        KEYWORDS:
X
X             The  following are the KEYWORDS which the rule compiler  can 
X        recognize,  and  a short description of what each means  and  how 
X        each  would be used.    Note that the members of each group  have 
X        identical meaning and can therefore be substituted for each other 
X        without  effecting the sense of the rule.   Latter examples  will 
X        attempt to demonstrate this fact.
X
X
X
X        IF, AND, ANDIF:
X
X             These KEYWORDS define the FOLLOWING STRING as an ANTECEDENT, 
X             and  the TRUTH sense of the string is TRUE if the string  is 
X             TRUE.    These  KEYWORDS can be used interchangeable without 
X             effecting the sense of the rule being expressed.
X
X                  Example:
X
X                       IF the animal is a mammal
X                       ANDIF the animal has hooves
X                       AND the animal has horns         
X
X                  This is equivalent to:
X
X                       AND the animal is a mammal
X                       IF the animal has hooves
X                       ANDIF the animal has horns
X
X             Note that since the antecedent part of a rule is essentially 
X             a  large AND statement,  the order in which  the  individual 
X             statements  are  arranged  is a matter  of  esthetics  only.  
X             However,  it may be more readable to the human, if a certain 
X             order is maintained.
X
X        IFNOT, ANDNOT
X
X             These  KEYWORDS  are  essentially  identical  to  the  above 
X             KEYWORDS except that the sense of the statement is reversed.   
X             That is to say that if the following string is TRUE then the 
X             truth   value   of   the  statement  is  FALSE.              
X
X                  Example:
X
X                       IFNOT the animal is a mammal
X                       ANDNOT the animal has smooth skin
X                       ANDNOT the animal breaths air
X                       THEN animal is a fish 
X
X
X
X
X
X
X
X
X        George W. Hageman            --5--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        IFRUN, ANDRUN, ANDIFRUN
X
X             These KEYWORDS tell the inference engine that the  FOLLOWING 
X             STRING is to be used as a pathname to an executeable.   This 
X             executeable  is to be loaded and run and the resultant TRUTH 
X             value returned when the routine exits is the TRUTH value  of 
X             the ANTECEDENT statement.   Like the AND, IF, ANDIF KEYWORDS 
X             above,  each  of  these  may be substituted for any  of  the 
X             others  without effecting the sense of the  rule  statement.   
X             Note  that  the  full path name of the  executeable  is  not 
X             needed  if  the  executeable  file resides  in  the  working 
X             directory from which the inference engine was initiated.
X
X                  Example:
X
X                       IFRUN /b1/hageman/expert/gt3000
X                       ANDRUN /b1/hageman/expert/sedir direction.dat
X                       ANDIFRUN falling barompress
X                       THEN sorry about the picnic
X               
X        IFNOTRUN, ANDNOTRUN     
X
X             These are used as the KEYWORDS above are used -- to initiate 
X             the execution of an ANTECEDENT executeable file, except that 
X             the truth value of the result is reversed as with the IFNOT, 
X             ANDNOT and the ANDIFNOT KEYWORDS.
X
X                  Example:
X                       
X                       IFNOTRUN gt3000
X                       ANDNOTRUN sedir direction.dat
X                       ANDNOTRUN falling barompress
X                       THEN how about a picnic?
X         
X        THEN, ANDTHEN, THENHYP, ANDTHENHYP    
X
X             These KEYWORDS tell the inference engine that the  FOLLOWING 
X             STRING  is  a  CONSEQUENT.     If  all  of  the  immediately 
X             proceeding ANTECEDENTS have a truth value of TRUE,  then the 
X             inference  engine INFERS that the CONSEQUENT is  TRUE.   The 
X             THEN KEYWORDS ending in "HYP" tell the inference engine that 
X             the  FOLLOWING STRING is an ending CONCLUSION and no further 
X             processing  or inferencing is required.   The  routine  will 
X             exit  when one of the THENHYP or ANDTHENHYP CONSEQUENTS  are 
X             proven   TRUE.     Therefore,   one  should  use  the  "HYP" 
X             CONSEQUENT KEYWORDS with care. 
X
X                  Examples:
X
X                       IF you have an aunt
X                       ANDIF your aunt has a child
X                       THEN you have a cousin
X
X                       IF you have a cousin
X                       THENHYP you have at least two relatives
X
X
X        George W. Hageman            --6--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        THENRUN, ANDTHENRUN, THENRUNHYP, ANDTHENRUNHYP 
X
X             These are similar to the THEN,  ANDTHEN etc. KEYWORDS except 
X             they  perform  the  loading and execution of  the  FOLLOWING 
X             STRING  path  name  if they  are  proven  TRUE.   The  value 
X             returned  by  the executeable file loaded becomes the  value 
X             remembered  for  the  CONSEQUENT.   The truth  value  for  a 
X             CONSEQUENT proceeded with the KEYWORDS of THEN or ANDTHEN is 
X             only remembered if it is proven TRUE.  However, with the RUN 
X             type of CONSEQUENT since it is initiated only when found  to 
X             be proven, the predicate value it returns is remembered even 
X             if  it is FALSE.  This prevents the rerunning of  CONSEQUENT 
X             routines.  
X
X             In this manner one can use rules to determine weather or not 
X             a particular routine should be executed,  then use the truth 
X             value  returned  by the routine upon its exit in other  rule 
X             statements.   An obvious use for such a function would be in 
X             the field of diagnostics.   Through a set of rules it  could 
X             be  determined  that a particular diagnostic should be  run,  
X             once  the diagnostic has been run,  further rules could  use 
X             the  fact  of whether the diagnostic test passed  (TRUE)  or 
X             failed (FALSE) to make decisions about the further isolation 
X             of the hardware failure.
X
X                  Example:
X                       
X                       !
X                       IFNOTRUN isdev1
X                       IFNOTRUN isdev2
X                       IFNOTRUN isdev3
X                       THENHYP there are no devices to run diagnostics on
X                       !     
X                       ! see note below for the following rule
X                       !
X                       IF isdev1
X                       ANDIFRUN dev1diag
X                       THENHYP device one is faulty
X                       !
X                       IFRUN isdev2
X                       ANDIFRUN dev2diag
X                       THENHYP device two is faulty
X                       !
X                       IFRUN isdev3
X                       ANDIFRUN dev3diag
X                       THENHYP device three is faulty
X                       !
X                       IFNOTRUN dev1diag
X                       IFNOTRUN dev2diag
X                       IFNOTRUN dev3diag
X                       IFRUN isdev1
X                       ANDRUN isdev2
X                       THENRUN diag12
X                       !
X                       !
X
X
X        George W. Hageman            --7--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X                       !
X                       IFRUN diag12
X                       THENHYP device two is suspect remove and re-run test
X                       !
X                       IFNOTRUN diag12
X                       THENHYP device one is suspect, remove and re-run test 
X                       !
X
X             Note:
X
X             You   have  probably  noticed  that  the  FOLLOWING  STRINGS 
X             associated  with  the  RUN KEYWORDS and  the  IF,  AND  etc. 
X             KEYWORDS are interchangeable.   This is due to fact that the 
X             TRUTH  of  a  string is kept as a pointer  into  the  string 
X             buffer,  and therefore have no information concerning  their 
X             nature and only have meaning when used in conjunction with a 
X             KEYWORD.  If you are sure that the string "isdev1" will have 
X             its   truth  determined  earlier  by  running  the   routine 
X             "isdev1",   Then you may use it as a regular string in later 
X             rules,  however,  if its truth has not been determined  then 
X             the  inference  engine  will ask you for the  truth  of  the 
X             statement  "isdev1" rather than running the routine.   To be 
X             sure  use  the "RUN" form for the strings  which  relate  to 
X             executeables,   they will only be run once as it is,  so you 
X             don't really have to keep them straight.
X
X                  Notice also that if the first rule is not proved by the 
X             fact  that  isdev1  turns up being TRUE  and  therefore  the 
X             antecedent  statement  is FALSE due to the reverse sense  of 
X             the IFNOTRUN KEYWORD,  then the others will not be run until 
X             they are encountered in other rules containing them.   So be 
X             safe and use the RUN forms of the KEYWORDS if you intend the 
X             execution of an object.
X         
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X        George W. Hageman            --8--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        USAGE:
X
X             Ok,  now  that we know what all the key words are,  how does 
X        one go about using an inference engine, and more specifically how 
X        does one use this inference engine and what for?  
X             
X             People  usually use inference engines  as part of  what  are 
X        known  as expert systems.   Expert systems are a study associated 
X        with  a  branch  of  software  engineering  known  as  Artificial 
X        Intelligence (AI).    (I am probably going to get some heat  from 
X        that  statement).    Expert systems are supposed to mimic the way 
X        in which human experts deal with a particular physical or  mental 
X        problem.    A  clear  example  is an expert  system  which  could 
X        isolate  a  fault within a complex computer system as well as  or 
X        almost  as well as its human counterpart.    It is apparent  that 
X        the  computer  expert  system  would lack  much  of  the  tactile 
X        resources  the human expert would have,  but the computer  expert 
X        could still be of great value when coupled with a novice computer 
X        user.    In  this way the computer expert would rely on the human 
X        to  perform  actions  and observations  the  computer  expert  is 
X        incapable  of  doing.   The computer expert and the  novice  then 
X        could form a team which might perform as well as the human expert 
X        alone and at a probably much lower per/hour labor rate.
X
X             Unfortunately,  in  order to develop an expert  system,  one 
X        must  either  be  an expert in the area one wants to  develop  an 
X        expert system for or have a ready access to one.   Assuming  that 
X        you are or have found one,  the following is a description of how 
X        one  would  use  the inference engine and the  rule  compiler  to 
X        produce an expert system.    Fortunately one of the sticker tasks 
X        of  developing  the inference engine has been done,  and all  you 
X        have  to do is develop the rule base,  compile it with  the  rule 
X        compiler  and  use  the resultant compiled rule-base  file  as  a 
X        parameter  when  you initiate the inference engine.   In  reality 
X        this is a much tougher job than developing the inference engine.
X
X             Expert   systems  generally  consist  of  three  parts,    a 
X        KNOWLEDGE BASE,  HUMAN INTERFACE,  and an INFERENCE ENGINE.   The 
X        following is a short description of each:
X
X        THE RULE or KNOWLEDGE BASE:
X
X             The  rules  consisting of ANTECEDENTS  and  CONSEQUENTS  are 
X        known  as  the  "KNOWLEDGE  BASE"  of  the  expert  system.    An 
X        additional part of the KNOWLEDGE BASE consists of the executeable 
X        files  and  their  shared data files.   The  way  the  "knowledge 
X        engineer"  puts  these rules together determines how good  and/or 
X        effective  the resultant expert system becomes.    The  KNOWLEDGE 
X        BASE  is the smarts of the expert system,  and the basic data  on 
X        which the inference engine is to operate upon.
X
X
X
X
X
X
X
X        George W. Hageman            --9--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        THE HUMAN INTERFACE:
X
X             The  expert system needs a way to ask the human  user  about 
X        the  TRUTH or FALSENESS of the antecedents contained in the  rule 
X        base.   This  is  contained  as a part of  the  inference  engine 
X        associated with this release.    These routines will ask the user 
X        whether or not FOLLOWING STRINGS are TRUE or FALSE.    Additional 
X        human interfaces may be contained in the executeable files.
X
X        THE INFERENCE ENGINE:
X
X             The inference engine released with this software is known as 
X        a backwards chaining inference engine.    It works by identifying 
X        consequents  and  attempting  to find rules to  prove  that  each 
X        consequent  is TRUE.   Once a consequent is proved  TRUE,  it  is 
X        remembered  as being TRUE.    Each antecedent which is determined 
X        either TRUE or FALSE is remembered so that the user does not have 
X        to  be  asked more than once to verify  a  particular  statement.  
X        Remember  that if a consequent is not proved TRUE,  then it  does 
X        not  necessarily  mean that it is FALSE.    For more  information 
X        consult  the "inference.str" file which is a  preliminary  pseudo 
X        code  description  of the inference engine.   It is  not  correct 
X        because   I  have  not  gone  back  and  up  dated  it  from  the 
X        implementation effort (Tom De Marco please forgive me) but, it is 
X        close  enough to provide a basis for understanding the  code.   I 
X        suggest  looking  at the code to understand  how  this  inference 
X        engine  works.    I have also left in all of the debug statements 
X        which I found helpful so by modifying the makefile to include the 
X        DEBUG FLAGS, you can observe the inference engine working.
X
X             This   inference  engine  attempts  to  prove  all  of   the 
X        consequents  from the top of the rule base through to the  bottom 
X        in a linear way if possible.  If however, an antecedent is really 
X        a  consequent  of a rule,  the inference engine will  attempt  to 
X        prove  that consequent even if it occurs later in the rule  base.  
X        In  this sense,  the inference engine will exhibit  some  forward 
X        chaining characteristics.
X
X             There  are  several good books on the development of  expert 
X        systems,  inference  engines  and the like.   I have  included  a 
X        bibliography  which contain the books I consulted to  build  this 
X        one.
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X        George W. Hageman            --10--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        BUILDING EXPERT SYSTEMS:
X
X             The  builders  of  expert systems  are  sometimes  known  as 
X        "knowledge engineers".    As the name implies, they deal with the 
X        manipulation  and  representation  of knowledge  leading  to  the 
X        development  of the Knowledge base the inference engine  operates 
X        upon.    In  order  to develop the knowledge base  the  knowledge 
X        engineer  needs either to be an expert in the area for which  the 
X        expert system is to be developed or, have ready access to a human 
X        expert in the field.   
X
X             The  inference engine in this distribution accepts knowledge 
X        in  many  ways.   The first of which are the rules  as  discussed 
X        above which are relatively simple logical or predicate statements 
X        about the TRUTH of well defined consequents.    The other is  the 
X        somewhat  complex  knowledge representation as contained  in  the 
X        executeable files which may be initiated by the inference engine.  
X        In  fact,  the inference engine and the rule_compiler each can be 
X        one  of these executeable files so you can have recursive  expert 
X        systems  if  you  have a mind to!    You  can  even  use  another 
X        executeable  to produce a text file of rules,  which then can  be 
X        operated  on  by  the  rule-compiler  and  then  fed  to  another 
X        inference  engine  producing  self-modifying or  learning  expert 
X        system.   So even though the inference engine is only 12K and the 
X        rule  compiler 10K these are sufficient enough to produce  rather 
X        powerful expert systems.
X
X             To  develop the knowledge base the knowledge engineer  first 
X        must  under  stand  the limitations which are acceptable  to  the 
X        expert  system  user.   For example,  if the user  of  an  animal 
X        identification  expert  system is not concerned with whether  the 
X        expert  can  differentiate  between a snake and a  lizard  or  is 
X        not interested in reptiles at all,  then the expert system can be 
X        simplified  by leaving out this knowledge.    Once the boundaries 
X        of  an expert system are well known,  the expert system  designer 
X        can concentrate on how to define the particulars.   This software 
X        release  contains  two examples of simple  expert  systems.   The 
X        first is the more or less classical animal identification expert, 
X        and  the second is a weather predictor expert which  demonstrates 
X        the usage of the IFRUN, ANDTHENRUN, etc. KEYWORDS.
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X        George W. Hageman            --11--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        ANIMALS:
X
X             This is a simple expert which can differentiate between only 
X        certain  types of animals -- Mammals and Birds.   And among these 
X        two groups it can only tell you that the animal you are  thinking 
X        about  is  either  a  Giraffe,  Zebra,  Cheeta,  Tiger,  Penguin, 
X        albatross,  Duck,  and  so on.   The example has been limited  to 
X        these  few  identifications  in  order to  greatly  simplify  the 
X        development  effort  and  to help  demonstrate  the  strategy  of 
X        developing the rules associated with this simple expert system.
X
X             The strategy associated with the development of a consistent 
X        set  of  rules  which  will identify a  particular  animal  given 
X        certain  physical  characteristics  to work with  is  DIVIDE  AND 
X        CONQUER.    The  idea being to use rules which build  a  decision 
X        tree  where each branch of the tree is formed by a rule which can 
X        decide  which direction to go at that junction.    Since  we  are 
X        obviously  dealing  with birds and mammals then we can build  our 
X        root  branch  or  the  grossest division  as  a  rule  which  can 
X        differentiate between birds and mammals.   Like:
X                  
X                  !
X                  IF animal gives milk
X                  ANDIF animal has hair
X                  THEN animal is mammal
X                  !
X                  IFNOT animal is mammal
X                  THEN animal is bird
X                  !
X                  .
X                  .
X
X             Notice  that if the animal is not a mammal we  automatically 
X        assume  that it is a bird since this is the domain of our  expert 
X        system  -- it  does not consider any other type of  animal  as  a 
X        possibility.   If  we  were to include perhaps reptiles then  the 
X        following might be used instead:
X
X                  !
X                  IF animal gives milk
X                  ANDIF animal has hair
X                  THEN animal is mammal
X                  !
X                  IFNOT animal is mammal
X                  AND animal has feathers
X                  AND animal lays eggs
X                  THEN animal is bird
X                  !
X                  IFNOT animal is mammal
X                  IFNOT animal is bird
X                  THEN animal is reptile
X                  !
X
X
X
X
X
X        George W. Hageman            --12--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X             Again  the last category is the default and needs no further 
X        definitions  because it is within the limitations of  the  expert 
X        system.
X
X
X             Further  refinement of the decision tree associated with the 
X        animals  expert should build on the knowledge gained  by  earlier 
X        branching.   So  one should use the knowledge that the animal has 
X        been  identified  as a bird to further  define  the  animal.   An 
X        example of the further definition of the type of mammal follows:
X
X                  !
X                  IF animal is mammal
X                  ANDIF animal eats meat
X                  ANDIF animal eats little vegetation
X                  THEN animal is carnivore
X                  !
X                  IFNOT animal is carnivore
X                  ANDNOT animal eats little vegetation
X                  THEN animal is vegetarian
X                  !
X
X             Finer  and  finer branching is achieved by this  divide  and 
X        conquer strategy until the leaves of the tree are reached,  These 
X        leaves  are  the  actual hypothesis which end the  search  for  a 
X        particular animal.  
X
X                  .
X                  .
X                  !
X                  IF animal is cat
X                  AND animal has tan color
X                  AND animal has stripes
X                  THENHYP animal is tiger
X                  !
X                  IF animal is cat
X                  AND animal has tan color
X                  AND animal has spots
X                  THENHYP animal is cheeta
X                  !
X                  .
X                  .
X
X             Notice  that  "animal  is cat" should be a  THEN  Consequent 
X        somewhere or else the inference engine will simply ask you if the 
X        "animal  is  cat" statement is TRUE or not -- which  may  not  be 
X        construed  as  a  particularly intelligent thing  for  an  expert 
X        system to do.   The intelligence represented by the expert system 
X        is directly related to the intelligence you give it.  However, it 
X        is possible to get very confused when there are a large number of 
X        rules for a particular expert system and mistakes in logic or the 
X        development  of  circular  logic occurs.   The first  will  cause 
X        incorrect  conclusions to be drawn and the second will cause  the 
X        inference engine to crash.   Circular logic causes the  inference 
X        engine to run out of stack space.  
X
X
X        George W. Hageman            --13--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X             A  circular argument is an argument which cannot be resolved 
X        because  its proof relies on another rule which in turn relies on 
X        proving the first statement before it can be proved such as in:
X
X                       !
X                       IF the second one is true
X                       THEN the first one is true
X                       !
X                       IF the first one is true
X                       THEN the second one is true
X                       !
X
X             Or to expand the concept:
X
X                       !
X                       IF the third one is true
X                       THEN the first one is true
X                       !
X                       IF the first one is true
X                       THEN the second one is true
X                       !
X                       IF the second one is true
X                       THEN the third on is true
X                       !
X
X             Usually,   when  the inference engine tells you that  "Stack 
X        overflow" has occurred -- this will be the problem.
X
X             Look at the animal file contained in this release,  see  how 
X        the  rules are built and as an exercise add another animal to  be 
X        differentiated  like a platypus,  and later add a whole class  of 
X        animals such as domestic farm animals, or even a division such as 
X        reptiles or insects.
X
X             You  will soon notice that your knowledge base will increase 
X        very  quickly  with each set of animals you want your  expert  to 
X        differentiate.   With this growth of the knowledge base come real 
X        difficulty  in keeping the rules correct  and  non-circular.    A 
X        strategy  for limiting the complexity for these rules is  to  use 
X        the IFRUN or THENRUN capability of this inference engine to fire-
X        up  a whole new inference engine which is an expert in one of the 
X        major branches of animals.   In this manner one only has to  make 
X        the  major decisions associated with a class of animals and  then 
X        run  the  expert system which knows how to  handle  the  specific 
X        class of animals.  
X
X
X
X
X
X
X
X
X
X
X
X
X        George W. Hageman            --14--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        For example:
X
X                  !
X                  IF animal has feathers
X                  AND animal lays eggs
X                  THEN animal is bird
X                  THENRUNHYP  inference.exe birds.cmp
X                  !
X                  IFNOT animal is bird
X                  AND  animal has hair
X                  AND  animal gives milk
X                  THEN animal is mammal
X                  THENRUNHYP inference.exe mammals.cmp
X                  !
X
X             The  files  "birds.cmp" and "mammals.cmp" are the  resultant 
X        compiled  files  from  the  text  knowledge  bases  "birds"   and 
X        "mammals"  which  the knowledge engineer would need  to  produce.   
X        But,  as stated above, these files would represent expert systems 
X        knowing  only the limited area of birds or mammals and  therefore 
X        can be greatly simplified.    NOTE:  inference.exe returns a TRUE 
X        value  if  it has proved anything while it ran,  and FALSE if  it 
X        could  not  prove anything.   With an IBM AT with 512  KBYTES  of 
X        memory  I  was able to load four copies of  an  inference  engine 
X        simultaneously.   A good way to see how many your system can deal 
X        with at the same time compile the following test:
X
X                       !
X                       IFRUN INFERENCE.EXE TEST.CMP
X                       THENHYP I am done
X                       ! 
X
X
X        If this file is named TEST, then compile it using the following:
X
X                  rulecomp test test.cmp
X
X        Then, run the inference engine with the test.cmp file as follows:
X
X                  inference test.cmp
X
X
X             Notice  that when any routine cannot be run for some  reason 
X        or another, an attempt is made to tell you what went wrong -- out 
X        of memory,  can't find it,  or some other reason, and the routine 
X        will  be assumed to have returned normally with a TRUE  predicate 
X        value.   This  is done so that the inference engine won't  simply 
X        die at some mysterious point.   The "I infer that : I am done" at 
X        the top was the last inference engine which could be loaded which 
X        could run but could not spawn a new process, the next one down is 
X        the  last one which could spawn one,  and any others  below  this 
X        line  plus  this  one will tell you the number of levels  of  the 
X        inference engine you can run on your system.  
X
X
X
X
X        George W. Hageman            --15--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        THE WEATHER EXPERT:
X
X             The  other  expert system knowledge base included with  this 
X        release  is  a weather predicting  expert.   This  expert  system 
X        demonstrates   the   use  of  executeable  files  to  expand  the 
X        abilities  of  the inference engine to deal with other  forms  of 
X        knowledge.   Look  a  the source files for  the  weather  expert, 
X        notice how each member of this set of routines uses a common file 
X        for  the  transfer of needed data.   The need of a file for  this 
X        transfer of data is done because it was the only standard form of 
X        data  transmission  which was not machine dependent according  to 
X        MS-DOS and UNIX operating systems.   Specific methods for dealing 
X        with this problem can be found for your machine which can greatly 
X        speed up this cumbersome data transfer method -- but it works.   
X
X             UNIX  and the Microsoft C compiler allows a routine to  exit 
X        with a value which will be returned to the parent process.   This 
X        method  is  used to allow each executeable to return  its  TRUTH-
X        VALUE  or  PREDICATE-VALUE back to  the  inference  engine.   The 
X        routine  runRoutine(consequent) performs this task.   If you look 
X        at  the  file runrouti.c you can see that this is done  with  the 
X        "exit(value) ;" statement.  The values used to indicate the TRUTH 
X        and  FALSEness  of  the  routine  are  "RETURN_ROUTINE_TRUE"  and 
X        "RETURN_ROUTINE_FALSE"   as   contained  in   the   header   file 
X        "routine.h".   This header file should be included in any routine 
X        which returns a TRUE/FALSE value to the inference engine.
X
X             The   strategy  for  using  this  method  of  expanding  the 
X        knowledge  base  is essentially the same as that for  the  animal 
X        type  expert  system,  except that it is noticed that the  simple 
X        form  of  the  knowledge base does not  have  the  capability  to 
X        perform  some  of  the functions  needed.   Cases  where  complex 
X        questions  or the manipulation of data are necessary must  resort 
X        to  the use of executeable files which can deal with them.   Such 
X        is the case for the weather predicting expert system.
X
X             The  weather expert must deal with such data  as  barometric 
X        pressure,  wind direction,  and the current rate of change of the 
X        barometric  pressure.    Additional data such as cloud conditions 
X        can  be  handled  by the predicate  functions  of  the  inference 
X        engine.     Again  the  strategy  is  to divide  and  conquer  by 
X        determining  which  information  needs  to  be  gathered  by  the 
X        executeable portion of the expert system,  what routines must  be 
X        used  to  convert  this data into a form which is usable  by  the 
X        inference engine i.e.  TRUE or FALSE,  and,  what information can 
X        be  gathered  directly  by the  inference  engine.    Once  these 
X        decisions are made then a decision tree can be built in a similar 
X        manner  to  the  one  built  for  the  animal  expert  but   also 
X        incorporating the executeable files where appropriate.    Look at 
X        the  file  "weather"  and  at each of the source  files  for  the 
X        executeable portion of the weather expert.  
X
X
X
X
X
X
X        George W. Hageman            --16--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X             Notice  that there is a "main" routine (even though  all  of 
X        the  programs  are  stand-a-lone programs and are have  the  name 
X        "main").   This routine is in the file "MESSAGE1.C", and provides 
X        the  data entry for all of the data needed by the expert  system.  
X        The  routine  explains to the user what  is  needed,  checks  for 
X        reasonable  responses,  and  writes the data to a disk  file  for 
X        later  use  by  the other routines associated with  this  system.  
X        These other routines,  when initiated,  have the task of  reading 
X        the  disk file produced by the "MESSAGE1.EXE" executeable,  check 
X        for  some conditions,  and return "ROUTINE_RETURN_TRUE",  if  the 
X        conditions are met and "ROUTINE_RETURN_FALSE" if they are not.
X
X             In  this  manner  the knowledge  engineer  can  make  expert 
X        systems   of  a  higher  complexity  and  usefulness  than  would 
X        otherwise be possible.
X
X        CONCLUSION:
X
X             I hope that this short definition of the inference engine is 
X        enough  to  get  you started into the fascinating  field  of  AI.   
X        There are some useful additions the enthusiastic programmer could 
X        make  to the inference engine to both enhance its usefulness  and 
X        its  ability to assist in the debugging of knowledge  basses.   A 
X        "Why"  function which shows the complete logical path which  lead 
X        to  the  question being asked,  showing each statement  as  being 
X        known or unknown and if known what its predicate value is,  would 
X        be very helpful.    Further,  the addition of different  KEYWORDS 
X        such  as  an  OR function,  might expand the  usefulness  of  the 
X        inference engine.  I may include these in my next release of this 
X        software  but If you have made such an addition -- I will include 
X        them in the next release and refund your $10.00 if you have  been 
X        so good as to send it to me.
X
X             Good  luck  and if you have any questions or would  like  to 
X        discuss this concept please drop me a line.
X
X                                 Thanks,
X
X                                      George W. Hageman
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X        George W. Hageman            --17--
X
X
X
X
X
X        INFERENCE          -- SOFTMAN ENTERPRIZES --        Dec. 30, 1985
X
X
X        BIBLIOGRAPHY:
X
X             The  following  books  and articles  were  helpful  in  the 
X        development of this inference engine.  
X
X             MVP-FORTH EXPERT SYSTEM TOOLKIT,  Jack Park,   Mountain View 
X        Press,  Inc.  P.O. Box 4656 Mountain View, CA 94040 Phone: (415)-
X        961-4130.
X
X             MVP-FORTH EXPERT-2 TUTORIAL,  Mitch Derick and Linda Derick, 
X        Mountain View Press.
X
X             INSIDE F83, C. H. Ting,  OFFETE ENTERPRISES, INC.  Available 
X        from Moutain View Press. 
X
X             Expert  Systems  and the Weather,  Jack  Park,   Dr.  Dobb's 
X        Journal, April 1984, pp. 24-28.
X
X             Programming in Prolog,  William F.  Clocksin and Christopher 
X        S. Mellish, Springer-Verlag 
X
X             LISP,  Patrick H.  Winston and Berthold Klaus and Paul Horn, 
X        Addison Weseley.
X
X             A  special  thankyou to Jack Park and the  MVP-FORTH  EXPERT 
X        SYSTEM  TOOLKIT.   Many  of  the ideas in this document  and  the 
X        development environment afforded by FORTH were the starting point 
X        for many of the ideas developed in this inference engine.  If you 
X        are into FORTH this is an excellent source of information on  the 
X        subject.  
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X        George W. Hageman            --18--
X
X
X
X
X----------------------  Cut Here --------------------------------
X
XGeorge Hageman  ( ...bmcg!asgb!benish!hageman )
X
XHappy New Year!
X
X
SHAR_EOF
exit



More information about the Comp.sources.unix mailing list