You get a bonus - 1 coin for daily activity. Now you have 1 coin

Simple expert system

Lecture



Example of writing a program Simple Expert System in ProLog language. To begin with, the equal sign in the ProLog is used not as an equating operator, but as a unification operator. Unification is the casting of expressions to the right and left of the unification sign to a single value. In this case, unrelated values ​​from one expression will be filled with values ​​from another expression. Thus, if two variables are involved in the unification operation, and each is associated with its own value, then they are compared. If one variable is not bound, then the value is equalized. And if expressions with unrelated parts are involved in unification, these parts take values ​​from the same parts of another expression. At the same time, ProLog tries to bring both expressions to the same value. Examples

Unification as equating.

  Goal 
  A = 5, 
  A = B 
  write (A, “,”, B). 

Here, in the first line, the unification of the unbound variable with the value 5. The result is the value of the variable A associated with the constant 5. In the second line, the unification of the unbound variable B and the associated A will cause the value of the variable A to be transferred to the variable B. The ProLog will display:

5.5

Unification as a comparison

  Goal 
  A = 5, 
  B = “yes” 
  A = B. 

Here, two fully related values ​​will be involved in the unification, which means that a simple comparison of values ​​takes place. The result of the ProLog:

No

This suggests that the unification is not successful, that is, the values ​​of variable A and B are unequal.

Unification as a cast to a single value

Now let's see what happens if there are only some parts of the values ​​in the variables that are unrelated. A list of values ​​is well suited for this. If any element of the list is unbound, then the unification mechanism will try to bind it using values ​​from another expression.

  domains 
  Word = string 
  Words = reference Word * 
  predicates 
  unification (Words A, Words B) 
  clauses 
  unification (Words, Words): - !. 
 
  Goal 
  A = [“yes”, “no”, C], 
  B = [“yes”, _, “exactly”], 
  unification (A, B), 
  write (A, “,”, B, “:”, C). 

Here ProLog will bring both values ​​to one generalized value, using linked values ​​from one expression to relate unrelated values ​​in another expression. The result of the ProLog:

[“Yes”, “no”, “exactly”], [“yes”, “no”, “exactly”]: “exactly”

As you can see, the unification was successful, which resulted in the same value for both expressions. However, if variable A is assigned a different value [“yes”, “no”, _, _], the unification will be unsuccessful.

It should be noted that to enable the mechanism of unification of incomplete values, it is necessary to explicitly declare that the values ​​of a variable can be partially related (to be links, not values) using the reference keyword. This is done when declaring a value type in the program’s domains section. Another note. When the constant “list of elements” appears in the ProLog program, the first element must be associated with a value. This is necessary so that the ProLog can determine the type of the remaining list items. In our example, in both constants the “list of elements” the first element is associated with the value “yes”. If the list is passed as a parameter of a predicate call, then this condition is not necessary due to the fact that the predicate parameters are already explicitly described and belong to a specific type. For example, one could write a unification predicate ([_, “yes”], C) that would not cause a ProLog error.

Another interesting feature of the reference keyword is that you can unify unrelated variables. Then their references to the domain will be the same. And if in the future one variable is associated with a value, then the second variable will be automatically linked. An example using the “unification” predicate described above.

  Goal 
  Unification (A, B), 
  A = [“yes”, “no”], 
  write (B). 

Result Prolog:

["well no"]

It can be seen that clearly no value for variable B was assigned. Nevertheless, the meaning appeared in it.

Simple Expert System based on the unification of ProLog values.

Now We can create a more complex and interesting example of using the unification of ProLog values. We will make an expert system on knowledge of the world in a language close to the natural language of man. Let every knowledge consist of only one sentence. Each word of a sentence can be of a different type: noun, verb, adjective, ... Each word has a semantic connection in another word. The announcement of such a word will look like this:

  Domains 
  Word = string 
  WordPred = reference noun (Word, WordPred); 
  Glag (Word, WordPred);  adj (Word, Word Entrance); 
  pre (Word, WordPred);  Nar (Word, WordPred); 
  is empty 

Here “ empty ” means the end of a sentence. As you can see, each word of a sentence is connected with another word of a sentence, so that the meaning of knowledge would be expressed semantically with the help of these links.

Now we describe the knowledge base and the type of facts in it.

  database - knowledge 
  knowledge (SlovoPredl) 

Using the unification of values ​​of the ProLog We get the simplest search for knowledge. The search predicate will look like this:

  predicates 
  nondeterm response (WordPress) 
 
  clauses 
  Answer (WordPost): - 
  knowledge (SlovoPredl). 

EVERYTHING! I note that you can do without the predicate at all, asking the search for facts directly in the knowledge base. The nondeterm keyword indicates that, in the case of a rollback, you need to iterate over all possible answers, if any.

Now you can create a knowledge buzzer for the expert system and ask questions. Let the knowledge base be stored in the “knowledge.txt” file and contain the facts:

  knowledge (noun ("lion", gob ("lives", before ("in", noun ("savanna", empty))))). 
  knowledge (noun ("antelope", gob ("lives", before ("in", noun ("savanna", empty))))). 
  knowledge (noun ("zebra", gob ("lives", before ("in", noun ("savanna", empty))))). 
  knowledge (noun ("lion", gob ("hunts", before ("on", noun ("animals", empty))))). 
  knowledge (noun ("tiger", gob ("hunts", before ("on", noun ("animals", empty))))). 
  knowledge (noun ("tiger", gob ("lives", before ("in", noun ("forest", empty))))). 
  knowledge (noun ("hare", gob ("lives", before ("in", noun ("forest", empty))))). 
  knowledge (noun ("hare", gob ("eats", noun ("grass", empty)))). 
  knowledge (noun ("deer", gob ("eats", noun ("grass", empty)))). 
  knowledge (noun ("hare", gob ("eats", noun ("bark", empty)))). 

Now you can ask a question to the expert system, for example, who lives in the savannah. It will look like this:

 
  Goal : -% call ProLog solver 
  consult ("knowledge.txt", knowledge),% knowledge base load 
  % definition of question words 
  WhatDoes = "live", Where = "savannah", 
  % build question 
  A = noun (Who, Glag (WhatDoes, Pre (Prev, noun (Where, Additions)))) 
  % search for answers 
  Answer (A) 
  % print answers 
  nl, write (Who, "", WhatDoes, "", Prev, "", Where, "", Extensions), 
  fail. 

Prologue response:

  the lion lives in the savannah is empty 
  the antelope lives in the savannah is empty 
  the zebra lives in the savannah is empty 

If we need to find out what the lion is doing, then we need to specify the following lines instead of the words of the query, the assembly of the query and the output to the screen:

  Who = "lion", 
  A = noun (Who, the Glob (WhatDoes, Additions), 
  nl, write (Who, "", WhatDoes, "", Add-ons), 

ProLogue response will be:

  the lion lives before ("in", noun ("savanna", empty)) 
  lion hunts before ("on", noun ("animals", empty)) 

Of course, you can make an automatic assembly of the question and the output of the knowledge found without the derivation of the internal types of the words of knowledge, so as to completely switch to the natural language of communication with the expert system. But that is another task. In general, on the basis of the unification mechanism, a very strong and intelligent expert system can be built. At the same time using natural language both input and output.

created: 2014-09-23
updated: 2021-03-13
132498



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Expert systems

Terms: Expert systems