Exsys Corvid Core Advanced Tutorial

advertisement
Exsys Corvid Core Advanced Tutorial
!
Backward Chaining and Confidence Variables
!
Before starting this tutorial it is highly recommended that you review the
introductory online Corvid Core Tutorials. Those provides a more detailed
explanation of the development environment fundamentals, along with
illustrating Corvid Core basics and concepts.
!!
Selecting an Appropriate Problem
Exsys Corvid is designed to help you describe the logical steps in a decision-making process in a way that allows the
knowledge to be delivered to others as if they were interacting with a human expert. This is actually very similar to
the way you would explain how to solve a problem to a person.
!
The first step in building a Corvid Core application is to select a problem that can be broken into logical steps or
pieces. As you become more experienced with Corvid, you will learn advanced ways to approach complex problems,
but for your first system select a simple problem that can be described in a few dozen rules.
!
The problem should be able to be solved using logic that can be explained to a person using statements in the form:
"IF …. THEN…", or "SINCE …. I KNEW THAT……". The "IF", or "SINCE", part may involve several conditions that
are combined. For example:
!
IF it is raining THEN wear a raincoat.
IF the car will not start AND the gas gage is on empty THEN the car is out of gas.
SINCE the light was on I KNEW the computer was getting power.
!
SINCE it is Saturday AND it is sunny I KNEW Fred was probably playing golf.
Describing a problem in this way puts it in the correct form for input into Corvid. Notice that the last example used
"probably". As you will see, Corvid Core systems have many ways to handle probability. One of the best uses of Corvid is
for solving problems that involve selecting the "best" solution when there are multiple possible solutions.
!
!
!
!
Sample Problem – What’s the Best Way to Drive to Work?
In this tutorial, you will build a small Corvid system to make a decision. The sample problem is a simple one, but will
still demonstrate many of the fundamental features of Corvid, especially Confidence Variables.
!!
You will decide how to drive from home to work. There are 3 routes to consider:
1
1. The Highway - This is the fastest way to go, as long as there are no traffic problems. If there is heavy
traffic, it is very slow and few exits. It also does not have any gas stations, and you do not want to run out of
gas.
!
!
2. The City Streets - This is a slower, but reliable way to go. There are plenty of gas stations that are always
open. Even if there is a traffic problem, there are plenty of alternate roads, so it is a very reliable way to go.
!!
!
3. The Coastal Road - This is a scenic road along the coast. It is a very pretty and nice drive in good
weather, but difficult in bad weather or at night. It takes about the same amount time as going on the city
streets. It has no gas stations. When practical, it is the preferred way to go.
Putting it in IF/THEN Rules
To start, state the main problem-solving rules in IF / THEN form. These should be simple statements about different
aspects of the decision. They do not need to be combined. The Corvid's Inference Engine will automatically combine
the various factors into an overall system. At this point, just state them as simple statements in English (or whatever
language you prefer) and Algebra - don’t worry about Corvid syntax yet. Technically these are called “heuristics”
!
!
1. The only route that has gas stations is the City Streets.
IF you need to get gas
THEN City Streets are the only option
!
2. If driving conditions are bad or it is night, the Coast Road is difficult and should not be taken.
!
!
IF driving conditions are bad
THEN do not take the Coast Road
IF it is nighttime
THEN do not take the Coast Road
!!
3. The highway is the fastest way to go, and if you are in a hurry, this is the best route. However, if there is a traffic
problem, it becomes the slowest way.
!
IF you need to get to work quickly
AND there are no traffic problems on the highway
THEN take the highway
!
!
!
!!
IF you need to get to work quickly
AND there are traffic problems on the highway
THEN do not take the highway
4. If you have time, the way you prefer to go is the Coast Road.
IF you do not need to get to work quickly
THEN the Coast Road is preferred
These 5 rules describe the logic of the decision-making process. They are in an IF/THEN form that will make them
easy to input in Corvid.
!
2
Corvid Variables
All the logic in Corvid Core is defined using Corvid Variables. Variables are the building blocks that Corvid uses to
build rules and describe the logic. When the system is run, variables used in the IF part of rules will tested. To do
this, they will need to be assigned a value. The value may come from:
!
! !
!
•
•
•
Directly asking the system end user to provide a value
Being derived from other rules
External sources
There are 6 types of variables, but for this system we only need 2 types.
List Variables
This is a simple multiple-choice list. It is the most commonly used Corvid variable and is the first choice to use
whenever possible. It is made up of a statement, and 2 or more possible values. Like all variables, it has a name
such as:
!
Traffic_problems_on_highway
!
and a Prompt used to ask the user for input or display results:
!
Does the highway have traffic problems?
!
In this case, the variable can have simple values of "YES" and "NO". When building the rules, an IF statement will be
made using one, or some, of the values. For example:
!
Traffic_problems_on_highway:
!
YES
To determine if that statement is true, the end user will be asked a question and presented with the possible values to
select among. Their answer will determine if the IF statement is true or false.
!
!
!
!!
A list variable can also have many values. For example, the statement could be:
The state you live in is… Each of the 50 US states is a possible value.
List variables make it easy to write logic that covers all the possible values, and automatically creates a user interface
that limits the values that the user can select.
Confidence Variables
These variables are usually one of multiple possible options that the system will select among. They can be assigned
a "Confidence Value" that determines if they are an appropriate, or inappropriate recommendation based on the end
user's input. Corvid provides many ways to work with confidence values to select the best recommendation(s) to
give.
!
!
Often the logic for a decision has a form similar to:
!
!
IF … THEN X is a good choice
IF … THEN X is likely to be the cause
IF … THEN X should not be used
IF … THEN X is better than Y
In these cases, X and Y should be Confidence Variables
3
Confidence variables are generally the best type of variable to use if there are multiple possible items, and each item
is being selected or rejected based on multiple rules and factors.. (As you get to more advanced systems, this is not
always true, but it is a good approach for many systems.)
!
!!
!Confidence Values
Corvid provides many ways to work with Confidence variables, especially when there are various independent factors
that must be combined, and where some of the factors are more important than others.
Confidence Variables are given "confidence values" in the rules. The numeric value assigned indicates if that choice
is "good" or "bad" based on the logic of the rule and the end user's input. A specific Confidence variable may be
assigned multiple values by many rules. These values are combined from all the rules that fire to determine the
overall confidence value for the variable - which determines if that variable is the best recommendation.
!
This may sound complicated at first, but it is actually pretty simple. Corvid provides many ways to combine
confidence values, but the simplest is just to sum them up. In the simplest approach, think of the value as "points"
added to or taken from the “score” for the item or recommendation. In a rule, the Confidence Variables are assigned
a value in the THEN part. This is indicated in the rule by the name of the Confidence variable in square brackets, and
an " = " followed by the value to assign. For example, if a system is trying to help decide what to wear, and it has a
Confidence Variable "Jacket", there could be rules:
!
!
IF it is below 50 degrees THEN [Jacket] = 10
IF it is raining THEN [Jacket] = 15
The first rule means that if it is below 50 degrees, assign a confidence value of 10 to the Confidence Variable
"Jacket", or give "Jacket" 10 points. Likewise, if it was raining, the second rule would assign a value of 15, or add
another 15 points. If it was both cold and raining, the system would fire both rules, the overall confidence for "Jacket"
would be 10+15 or 25 points. If there were other rules that fired, which assigned a value to "Jacket", their values
would be added to the overall sum.
!
!
!
There could also be a rule,
IF it is below 10 degrees
THEN [Jacket] = -100 AND [Coat] = 50
which would mean if it is very cold (below 10 degrees), then a jacket is not appropriate, so it is given a large negative
value (-100) to greatly reduce its overall confidence value and eliminate it. This would be a deduction of 100 points.
At the same time, another Confidence Variable "Coat" is given a value of 50.
!
At the end of a run, the Confidence Variable(s) with the highest overall point score will be displayed in the results as
the "Best" option(s) based on the input. However, remember there are other ways to combine confidence values
besides summing them up. (There are many other ways to calculate and use Confidence variables, but this simple
approach works well for many systems. See the full Exsys Corvid Core manual for a description of the various other
techniques and formulas for computing Confidence Variables.)
!
The actual values to assign can be any number or an expression using other variables that evaluates to a number.
They should be scaled appropriately relative to each other. If a rule should slightly increase the likelihood of a
particular Confidence Variable, it can be given a few points. If it is a more significant factor, it could increase it by
many points. Rules that indicate that a Confidence Variable is not appropriate should assign similar negative values
to decrease its likelihood. Adding very large values will assure that a variable is in the recommendations, and large
negative values will eliminate a variable from the results - provided the values are scaled appropriately.
!
This simple approach allows competing factors to combine to give an overall best recommendation, even when a
particular variable may not be perfect in all respects.
4
!
Corvid provides a wide range of other mathematical approaches to combining the individual values assigned to
Confidence Variables. You can even use different methods for each individual Confidence Variable. This is very
useful when building more complex systems.
!!
“Confidence” and “Probability”
Confidence variables have many uses and support various ways to combine the values assigned to a Confidence
variable. While true statistical probability formulas are supported, in practice these are not often used because most
real world problems do not have the needed underlying statistical data or strictly follow the rules of probability.
Unless you are building a system related to dice, cards, coin tosses or some problem where there is very good
statistical data, you are going to use “confidence” rather than true “probability”.
!
This is not bad, and confidence factors can be used in vastly more situations than true probability. This is what
experts use everyday to say “I think the cause of the problem is X”, or “I think X is a better choice than Y”. These are
not true probability statements, they are expressions of confidence in a particular outcome based on expert
experience and knowledge - not mathematical probability.
!
The most common way to use confidence variables is to combine the implications of several mutually independent
factors. That is the way it will be used in this system. It considers weather, need for gas and time to drive as
independent factors that combine to an overall recommendation. In some cases, one or the other of these may
become the controlling factor and govern the decision - but in most cases they combine to build an overall
recommendation.
!
With Corvid and Confidence variables, it is easy to just build separate rules or logic trees for each independent factor
and the Corvid Inference Engine will combine them automatically. It would be possible to combine them all into a
single very large tree, but it would be complicated and have a great deal of repetition as independent factors had to
be repeated on each branch. If one factor changed, it would have to be changed in multiple places. With the Corvid
approach, each factor is a small independent section of logic that gets incorporated when needed. If that logic
changes, just change it in the one place it is described and it will automatically carry through the system.
!
Corvid provides various way to combine the values assigned to a Confidence variable. Some problems need to
combine them one way, others another. One may base a decision on the highest value assigned a variable, others
the average value. The system used in this system is the “sum” approach where the values assigned are added
together. This lets various rules add or subtract “points”, with the overall highest score determining the “best” option
to recommend. This simple approach works very well for a wide range of problems.
!
!
!
Variables in the Sample System
The sample system rules needs several items of data to make the decision:
!
!
Are the driving conditions bad?
Do you need to get to work quickly?
Are there traffic problems on the highway?
Do you need gas?
Is it nighttime?
To start a system, it is best to create “high level” rules that describe the logic in the most general terms. These are
rules that can generally be written fairly easily, but may use criteria that are somewhat subjective and not ideal
5
questions to ask the end user. For example, an different user’s opinion of what are “bad” driving conditions varies
and they may not know exactly how much gas they need, which will vary with different cars.
!
One of the most powerful features of Corvid is that it uses Backward Chaining. We can write high level rules in these
general terms and then add additional rules later to derive the ambiguous values from other rules. The Corvid
inference engine will automatically use the additional rules when needed. This makes building systems much faster
and easier.
!!
!
!
!
Here we will start by just building the high level rules.
Corvid uses variables for each item of data. This provides a way to build the rule, and defines the questions that will
be asked of the system user.
1. Driving conditions
Selecting the best route depends on the driving conditions. This is a multiple-choice list and easy to implement as a
List variable. Corvid requires that each variable have a name. To make the system simple, the variable "Driving
conditions" will have only 2 values: "Bad" and "Good".
!
2. Get to work quickly?
In the same way, the rules use information on how quickly you need to get to work. This can also be defined as a List
variable - "Need to get to work quickly" with values: "Yes" and "No".
!
3. Traffic problem on highway?
The question of traffic problems on the highway can be handled the same way as a List variable. "Traffic problems on
highway" with values: "Yes" and "No".
!
!
!
4. Do you need gas?
To start, this will just be a simple Yes/No question, but it will get expanded later.
5. Is it nighttime?
This will just be a simple Yes/No question asked of the end user.
5. Routes
In addition to the List variables that are needed in the IF parts of the rules, there are the 3 possible routes that the
system will select among. Since each of these is independent of the others, and more than one may be possible,
each should be a separate Confidence Variable. In the sample system, the 3 possible routes are the options to
decide among, and they appear only in the THEN part of the rules. Each route will be a Confidence Variable:
!
!!
!!
!!
!!
!!
!
Highway
City Streets
Coast Road
6
!
The Rules Using the New Variables
!
Restating the sample form of the rules in the system, but now using Corvid :
!
IF You Need Gas: Yes
THEN City Streets are the only possibility
IF
You Need Gas: Yes
THEN
[City_Streets] = 100
[Highway] = -100
[Coast_Road] = -100
!
The only route that has gas stations is the City Streets. Taking the other routes without enough gas is not an
acceptable recommendation should be eliminated. This rule, assigns a high confidence to the City Streets variable
making sure it will have an overall high score. Large negative values are given to the other two routes to eliminate
them regardless of other values they may get from other rules.
IF You Need Gas: No
THEN any route can be taken
!
IF
You Need Gas: No
THEN
[City_Streets] = 10
[Highway] = 10
[Coast_Road] = 10
!
When there is enough gas, any of the routes can be taken. This rule will assure that all the routes will be given the
same number of initial points. The actual amount of points is kept small, since there will be other rules that will
decide which route is actually taken.
!
!
!
IF driving conditions are bad
THEN do not take the Coast Road
IF
Driving conditions: Bad
THEN
!!
!
[Coast_Road] = -100
IF Nighttime:Yes
THEN do not take the Coast Road
IF
Nighttime:Yes
THEN
[Coast_Road] = -100
!
The Coast Road should be eliminated in bad conditions or at night , so these rules assign a large negative number.
We have made these 2 separate rules. It would be possible to make a single rule with a more complex IF condition
using OR, but it is better to have clear simple rules. The Corvid Inference Engine will automatically handle these
7
different rules. Bad conditions are not as much of a problem on the other roads, so the rules do not assign anything
to those variables. It is NOT necessary to have every rule assign a value to each Confidence Variable. Only assign
values when there is a logical reason to do so.
!
!!
IF you need to get to work quickly
AND there are no traffic problems on the highway
THEN take the highway
IF
Need to get to work quickly: YES
Traffic problems on highway: NO
THEN
[Highway] = 20
!
This rule recommends the highway for speed when there is no traffic problem, but it only assigns a value of 20. This
is because it is a rule that recommends the highway, but other factors have to be considered, such as gas. If this rule
fires, but there is not enough gas in the car, the system should not recommend the highway. The value of 20 will
push the "Highway" variable up in the recommendations, but will not override a possible -100 from other factors. It is
important to keep confidence factors scaled relative to each other.
!
!
IF you need to get to work quickly
AND there are traffic problems on the highway
THEN do not take the highway
IF
Need to get to work quickly: YES
Traffic problems on highway: YES
THEN
[Highway] = -100
!
In this case, the Highway should be eliminated so it can safely be given -100.
!!
!
IF you do not need to get to work quickly
THEN the Coast Road is preferred
IF
Need to get to work quickly: NO
THEN
!
[Coast_Road] = 20
This rule reflects the preference for the "Coast Road", but adds a lower value since there might be other (weather or
gas) reasons that would eliminate it.
!
!!
!!
!!
The system now has the rules defined in terms of variables, and in a form that will make them easy to add into
Corvid. Now to build the system.
8
!
Building the System with ExsysCorvid Core
!
!
In your Applications folder, double click on “Exsys
Corvid Core”, or use a shortcut if you have created one.
This will open the main Corvid Core window.
!!
Under the “File” menu, select “New Project”
!
!
!
Give it the name “DrivingToWork”.
It is legal to include spaces in the name, but since this
will eventually become part of a URL, spaces would
have to be encoded and make a URL that is more
difficult to read. Names without spaces are
recommended.
!
!!
!!
Adding Variables
All Corvid systems are built with variables. Since the rules for
the system have already been sketched out, we know the
variables we will need. It is not required to add all variables at
the start of a system, but it makes building the rules go faster.
Additional variables can always be added later if needed.
!
!
!!
!!
!!
!!
To add a variable, click the “New Variable” button.
9
!
This will display the New Variable window.
Each new variable must have a name and a "type".
The name will be used to refer to the variable
throughout the system. The name must be unique not already used as the name for another variable in
the system. It should be fairly short, but adequately
descriptive to be able to recognize what it means. A
cryptic name may make sense now, but not in 6
months when you go in to update the system.
!
Here, enter the name “Driving conditions”. The name
is not allowed to have spaces in it, but Corvid will
automatically convert the space (and any other illegal
character) to an underscore character, so generally it is
easier to just type the name in with spaces.
!
There are 6 types of variables in Corvid and there is a
button for each type. This is a Multiple Choice List
variable, so click the “Multiple Choice List” button.
!
Each variable type has its specific options for that type.
All variables allows the Prompt text to be changed. By
default, this is initially set to the name of the variable.
This can be changed to any text you prefer. If the
variable will be asked of the end user, this can be more
of a question form, along with any details that the user
may need to be able to answer question. Here, the
Prompt has been changed to “The driving conditions
are”
!
The window also allows adding the list of values that
will be associated with the variable. There will be 2
values - “good” and “bad”. Enter “good” in the edit box
and click the “Add” button or press the return key.
!!
!!
The value entered will be added to the list below.
!!
!!
!!
!!
!
Enter the second value “bad” and click “Add” again or
press the return key.
10
!!
!!
!!
!!
!!
!
Both values have now been added to the value list.
The options on the right side of the window allow setting the type of
control that will be used when asking the user for input, and other
options.
!
!!
!!
!!
!
!!
!!
!
Here the default values are OK, so just click “Done”
Now in exactly the same way add the other List variables:
Need to get to work quickly
Name: Need to get to work quickly
Prompt: Do you need to get to work as
quickly as possible?
Values: “Yes” and “No”
!!
!!
!!
!
Traffic problems on highway
!!
!
Name: Traffic problems on highway
Prompt: Are there traffic problems on
the highway?
Values: “Yes” and “No”
11
!!
!
Do you need gas?
Name: Need Gas
Prompt: Do you need gas?
Values: “Yes” and “No”
Is it nighttime?
!!
!!
!
!!
!
Name: Nighttime
Prompt: Is it nighttime?
Values: “Yes” and “No”
!
!!
!!
!
!!
!!
!Confidence Variables
!
!
!!
!!
!!
!!
!!
!!
All that remains is to add the 3 Confidence variables for
the possible routes that the system will decide among.
Click the “New Variable” button. Enter the name:
“Highway” and click the “Confidence” button.
12
This open the window for Confidence variables. The
Prompt can remain “Highway”. The option for how
confidence values will be combined has many options,
but the default “Sum” method will work well for the rules
we have designed, so just click the Done button.
!!
!!
!!
!!
!!
!!
!
Now, in exactly the same way add the other 2 Confidence variables
for the other routes:
!
!
!!
City Streets
Coast Road
These should both have the Prompt be the same as the name and
use the “Sum” method to combine values.
The system now has the 7 variables we need and we can start to
build the rules.
!!
!
!!
!
!
What is a Node?
Corvid decision-making logic is described and built using "nodes". A node can generally be thought of as a statement
in the IF, or THEN part of a rule. For example, in the rule:
!
!
IF
THEN
Need to get to work quickly: YES
Traffic problems on highway: NO
Highway = 20
Has 3 nodes, 2 IF nodes: "Need to get to work quickly: YES" and "Traffic problems on highway: NO", and a THEN
node "Highway = 20". Nodes can be structured to apply to all the nodes below them. This allows nodes to be used
to build a logic tree diagram, which both makes it easier to view the overall structure of the logic and helps to make
sure all possible cases are covered.
!
13
!
For example, the sample system has these 3 rules:
IF
THEN
!!
IF
THEN
!
!!
!!
!
!
IF
THEN
Need to get to work quickly: YES
Traffic problems on highway: YES
Highway = -100
Need to get to work quickly: YES
Traffic problems on highway: NO
Highway = 20
Need to get to work quickly: NO
Coast Road = 20
In a tree diagram, this would look like:
!
!
The tree diagram makes it easier to see the overall structure of the rules. In Corvid Core the logic of a system is built
in Logic Blocks, which hold the tree diagrams and other logical information. The following equivalent Logic Block tree
will be created:
!!
Each line in the Corvid Core tree is a rule. The + signs are insertion points where nodes can be added or inserted.
The overall structure is easy to follow and matches the logic tree above.
!!
!
It is easy to build this Logic Block.
14
Make sure the Logic Block tab is selected to open the
Logic Block window.
!
Since this is the first Logic Block in the system, it will be
named “Logic Block 1” and have only a single insertion
point. This insertion point will be orange, indicating it is
the “Active Insertion Point” and where nodes will be
added.
!
Initially only an IF node(s) can be added, and the “IF
(Test)” button at the bottom of the window will be
selected.
!
!!
!
The first node in the logic diagram we want to build uses the “Need to
get to work quickly” variable and has a branch for each of the possible
“Yes” and “No” values.
!
!
!!
!!
!!
!!
!!
!
!!
Click on the “Need_to_get_to_work_quickly” variable in the variable
list on the left.
This is a List variable, and Corvid will display its 2 possible values in
the Node Builder panel at the bottom of the window.
We want to add a separate node for each of the 2 values.
There are various ways to build nodes that select and
combine values, but the most common situation is that a
node is needed for each value. To do this click the “Each”
button just right of the value list.
!!
!!
!!
!!
This will build a node for each value in the “Node to Add” list on the right.
15
To add these nodes to the Logic Block, click the “Add Nodes To Block”
button at the bottom of the panel.
!
The nodes will be added to the Logic Block. Since they were added at
the same time, and are based on the same variable, the 2 nodes
added have a gray background between them.
!
Notice that the orange active insertion node has moved to the right and
is now right of the “Need_to_get_to_work_quickly = Yes” node. We
can move the active insertion point, but this is where the next node(s)
would be added. Since the logic diagram needs to add nodes at this
point, that is the easiest way to continue.
!!
!!
The next nodes in the logic diagram cover the possible values of “traffic
problems on highway”. This has 2 possible values.
!
!
!
!!
!!
Make sure the active insertion point is to the right of the
“Need_to_get_to_work_quickly = Yes” node.
Click on the “Traffic problems on highway variable in the Variables list
to select it. This will display its 2 values in the node builder value list.
Click the “Each” button to build a node for each value.
Now click the “Add Nodes to Block” button at the bottom of the panel to
add the 2 nodes to the Logic Block.
!!
!!
!!
!!
!!
!!
!!
!!
!!
!
16
This adds thew nodes to the Logic Block tree.
!
!
Notice the active insertion point has again shifted right.
We have now built all the IF nodes needed for the logic diagram and only
need to add the THEN parts. To add a THEN node, click the “THEN
(Assign)” button.
!
This will change the background of the Node Builder panel to light blue.
This indicates that THEN nodes are being built. Building IF and THEN
nodes is quite similar, but remember that the blue background indicates
THEN nodes. The THEN nodes in the Logic Block will also have a blue
background.
!
Leaving the active insertion point to the right of “Traffic problems on the
highway = Yes” node, the logic diagram indicates a THEN assignment of
-100 point for the “Highway” Confidence variable.
!!
!!
!
!
Click the “Highway” variable in the Variable list on the left.
This will display “[Highway] =“ in the Node Builder window.
THEN nodes assign a value to a variable. In expressions,
variables always have their names in square brackets. Corvid
fills in the name of the variable and the “=“ . All you need to do
is to fill in the value to assign (or an expression that evaluates to
a numeric value)
!!
!!
!!
!!
!!
!
!!
!!
!!
Fill in the assignment value “-100” and click the “Add Node To
Block” button at the bottom of the window.
This will add the THEN node to the Logic Block.
17
Now, in the same way, add the node “Highway = 20” for the
case where there is no traffic on the highway.
!
!
!!
!
!!
!!
!!
!!
!
!
!!
!
Click the insertion point to the right of the “Traffic problems on
highway = No” node to make it the active insertion point.
Click the “Highway” variable in the variables list. This will build
the “[Highway] =“ part of the assignment node.
Enter the assignment value “20” and click the “Add Node To
Block” button.
This adds the new THEN node to the Logic Block
In the same way, set the active insertion point to the right of the
“Need to get to work quickly = No” node and click the “Coast Road”
variable to select it.
!!
!!
!!
!!
!
!!
!!
!!
!!
!
Enter the assignment value of “20” and click the “Add Node to
Block” button.
18
This finishes this Logic Block. It now matches
the logic diagram we started with.
!!
Each line in the Logic Block is a rule. We can
check this by clicking on the last insertion point
on any line. The nodes that make up the rule
will be highlighted. The rule’s IF nodes will be
shown with white text on maroon. The THEN
nodes are white text on teal blue.
!!
!
In addition, the Rule View window will show the text of the selected rule in If/Then
form.
You can step through the rules in a Logic Block by selecting a rule and using the
“Prev” and “Next” buttons to look at each rule the Logic Block will generate. This is
a good way to check that the Logic Block exactly describes the rules you want.
!!
!!
!!
!!
!!
!
!!
!
!
That covers 3 of the rules in the system, but there are a few more.
Rules that have IF nodes based on different values for the same variables can be grouped in the same logic block.
This is actually not required, but makes the logic of a system easier to see and to build complete rule sets.
Two of the other rules we want to put in the system are:
IF
Need Gas: Yes
THEN
!
[City_Streets] = 100
[Highway] = -100
[Coast_Road] = -100
IF
Need Gas: No
THEN
!!
[City_Streets] = 10
[Highway] = 10
[Coast_Road] = 10
19
These use the same “Need Gas” variable for the IF nodes,
so it make sense to put them together in a Logic Block.
Click the “New” button at the top of the Logic Block
panel.
!
!
This will create a new blank Logic Block. A system can
have as many Logic Blocks as needed.
Each new Logic Block will be named “Logic Block #”,
where # is the number of the logic block. This is fine, but
when a system has many logic blocks, it is better to give
them names so it is easier to find them later. Here we will
change the name to “Gas”.
!
!!
!
!!
!
!
!
!
!
!
!
!!
!
!!
!
With the new, empty Logic Block open, click the “Change
Name” button.
Enter a name of “Gas” and click “OK”
Build the IF node using the “Need Gas” variable.
Just as before, build a node for each value.
Now to add the THEN nodes.
Make sure the Active Insertion Point is to the right of “Need Gas = Yes”.
Click the “THEN (Assignment)” button at the bottom of the panel to switch
to THEN mode.
Click the “City Streets” variable in the Variables list and enter the
assignment value of “100”.
Click the “Add Nodes to Block” button to add the node to the Logic Block.
This will add the first THEN node. The rule
we want has multiple THEN assignments,
which is easy to do in Corvid. The Active
Insertion Point will have moved right. Just
click on the next variable to use in the THEN
part, which is “Highway”.
20
Give it a value of “-100” and
add it to the block.
!
Then do the same thing for the
3rd assignment setting “Coast
Road” to “-100”
!!
!
Now, click the insertion point right of the “Need gas = No” node and build out the THEN nodes for the other rule:
IF
Need Gas: No
THEN
[City_Streets] = 10
[Highway] = 10
[Coast_Road] = 10
!
This finishes this Logic Block:
!
That almost finishes the logic, but we still have 2 more small rules:
!
IF
Driving conditions: Bad
THEN
!!
[Coast_Road] = -100
IF
Nighttime:Yes
THEN
[Coast_Road] = -100
!
These rules do not have the same IF nodes, so we cannot
group them in a Logic Block. There is no requirement that
Logic Blocks be complex or tree structured. A Logic Block can
be a single rule, which is what we will use here.
!
!
!
Create a new Logic Block by clicking the “New” button. Name
it “Bad Driving Conditions”.
Click the “Driving Conditions” variable to select it. This will
display its value list.
In this case, only the “bad” value is needed to build a rule.
There is no rule associated with the “good” value and it has no
21
logical consequences in the system. Corvid does not require
that all values be used, and they should not be used when
there is no logical reason.
!
(Note: In some cases where the system logic is not fully
known, it can be good to build branches for all values, even
though they may not initially have THEN nodes. These serve
as reminders that these options are possible. These can then
be researched later to see if there is logic that should be
associated with them. If not, they can be left with no THEN
nodes. Branches without THEN nodes are legal, but will not
generate rules for the inference engine to use.)
!
Here we know that only the “bad” value is needed, so select it
and click the “Selected” button. This will build a node with only
the selected value(s) in the node. It will NOT build a node for
each value as the “Each” button did.
!!
!!
!!
Click the “Add Nodes To Block” button to add the single node to the block
Now, just add the associated THEN node “[Coast_Road] = -100” to build
the rule. This is done the same as where there are multiple rules in the
block.
!!
!!
!
!!
!!
!!
!!
!!
!
Now we have the Corvid form of the rule.
The Rule View window confirms that this is the rule we wanted.
Now in the same way, build the last rule in the system:
IF
Nighttime:Yes
THEN
[Coast_Road] = -100
!
22
!
!
Command Block
That is all the rules, but there is one last thing to do before we can run the system.
When a new Corvid Core system is started, the Command Block is set to “FORWARD ALL ALLOW_DERIVE”. This
command runs all the rules in forward chaining mode, with backward chaining allowed. This would actually work for
this system, but we have really designed it to use backward chaining to derive the Confidence variables. This will
become more important as we enhance the system later.
!
!
We will change the Command Block.
Click the “Command Block” tab to select it and display the
Command Block panel.
!!
!!
Click the top command in the command list to select it. This is the
command we want to change. We will build a new command and
replace it.
!!
!!
!
!!
!!
!
We want to use Backward Chaining which is based on variables,
so click on the “Var “ tab.
We want to derive each Confidence variable. In the drop down list
at the top, select “All Confidence Variables”.
Individual variables can be selected individually, but there are
options for All Confidence and All Collection variables since these
are common approaches to building Corvid systems. We could
alternatively add 3 commands for the 3 Confidence variables and
the system would run the same. This is just more convenient.
!!
!
!
Click the “Derive” radio button. This builds a DERIVE command
which is the easiest way to start backward chaining. This command
will use any and all relevant rules in the system to set the value for
the variable(s) specified - here, all Confidence variables.
!
23
!!
This builds the DERIVE CONF command at the bottom of the panel.
Click the “Replace” button to replace the command we selected
earlier in the command list.
!!
!
The Command List now has the DERIVE CONF command at the
top. We can leave the RESULTS command for now, though it will
be replaced by a more customized on later.
Running the System
!!
!
That is all that is needed and the system can be run.
!
We could use either the applet or servlet runtime, but here we will
use the servlet runtime. (Using the servlet runtime is often easier
and allows fielded systems to run on iPhones and iPads.)
!
!
!
Just select the “Servlet Runtime” in the “Run Using” drop down and
click the “Run” triangle to start the system.
If Apache Tomcat is running, the system will immediately start. You can tell if Tomcat is running by the
icon in the upper right corner of the Corvid window. It will be a green dot if Tomcat is running.
If Tomcat is not running, the icon in the upper right will be a red circle and line. In this case, Tomcat
must be started before a system can be run with either
the servlet or applet runtime. You can start Tomcat by
clicking on the red circle and line icon, or just by trying
to run the system.
!
If Tomcat needs to be started, you will see a warning
message, with a button labeled “Display Tomcat
startup.sh Script”. this button is the easiest way to start
Tomcat.
!
Click the button and a Finder window will open with the “startup.sh”
script highlighted. Double click on the highlighted script. (Do not
click on any other scripts. They do various things to control Tomcat
- but are NOT what you want to use to start it.)
!!
!!
!!
!
24
Running the startup.sh script will open a terminal window on a Mac that displays
the commands used to start Tomcat. You can generally just ignore this window,
however, if there is an error in starting Tomcat the messages in the window may
be helpful.
!!
!
!
!
!
Once Tomcat is running and the icon in the upper right is a green dot, just click
the “Run” triangle to run the system.
The servlet runtime will generate a screen in a Safari browser
window with the first question the system needs to ask.
Answer the questions in various ways to test the logic of the
system.
In this case, the input was:
!
Get to work quickly: Yes
Driving Conditions: Good
Traffic problems on highway: No
Need gas: No
Nighttime: No
The advice was to take the “Highway” with a score of 30 while “City
Streets” and “Coast Road” got scores of 10. this is exactly what
we expected from the logic.
!
!
!
The system runs properly, but there are many things that can be
done to improve it.
First, the results screen presents too much information and is
confusing.
This is because in the Command Block, we used the default
generic RESULTS command that displays the value of all variables
set by the system with very simple formatting. This is OK for initial
testing of the system logic, but should generally be changed to
something more end user friendly.
!
To change this, we need to change the generic RESULTS
command in the Command Block to one that uses a custom screen
to display the advice.
!
Click the Command Block tab.
Click the RESULTS command in the command list to select it.
Click the “Results” tab at the top of the command builder panel
Under “Custom”, click “New” to create a new custom screen.
!
Corvid will ask for the name of the new custom screen file. Give it
a name you can recognize. Corvid will automatically give it a .rpt
extension. Here we will call it “DrivingToWorkAdviceScreen”.
Click OK
!
25
!
!
!
!
!
!
!!
!
!!
!!
!!
!!
!
!!
!
!
!
!
!
!
!!
This opens the window for building custom screens. The first line
should be a text title.
Click the “Text” tab at the top
Enter the text “The recommended way(s) to get to work is:”
Set the Format to 14 pt, bold, blue
Click the “insert” button to add the command to the screen
command list.
The screen command list should now have this first TEXT
command in it.
The next command will add the Confidence variables that received
a high score.
Click the “Variables” tab.
In the “Type of Variable” drop down, select “All Confidence
Variables”
In the “Sort Confidence” drop down, select “Descending”
In the “Only include if value is greater than” edit box, enter “0”
Set the Format to italic, black
Click the “Add Last” button to add this command to the list
26
The command list now has the 2 commands we need. However, these commands have to be written to a file that the
system will use. ` Click the “Done - Save commands to file” button. This is a very important step. Unless the
commands are saved, they will not be used.
!
Now, back in the Command Block panel, the new DISPLAY
command we built is displayed, but it has not been added as a
command list. Make sure the RESULTS command is selected in the
command list. Click the “Replace” button.
!!
!!
!
!
!!
!!
!!
The Command Block should now include the DISPLAY command.
Now run the system again with the same input as before.
The results are now formatted and limited to the
recommendations. The recommended routes are arranged in
order. They include the confidence value they received. (If the
confidence value was not wanted, it could have been left off by
selecting the “Only display Prompt” checkbox when we built the
custom screen command to display the Confidence variables.)
!!
!!
!!
!!
!!
27
Enhancing the System
The system runs correctly and gives good advice, but some of the questions could be improved to make a better end
user interface. A good system should always ask questions that the intended end user can easily and unambiguously
answer, and should not ask any unnecessary questions.
!
Corvid makes it easy to expand the logic using Backward Chaining. Backward Chaining is one of the most powerful
capabilities in Exsys Corvid, and a good understanding of it is fundamental to building many advanced expert
systems. Backward Chaining allows larger problems to be broken into small, easily defined sections, which are
automatically used by the system when, and if, they are needed.
!
Even though Backward Chaining is very powerful, it is quite easy to use - in fact, unless it is turned off, it just
happens. If the system needs to know the value of a variable, and it has a rule that allows Corvid to derive the value
for that variable, it will automatically execute the relevant rule(s) to obtain the value. This can happen over many
rules and is recursive for as many layers as needed. You simply add rules that assign values to variables that are
tested in other rules. The Backward Chaining rules can be put anywhere in the system. When it needs them, Corvid
will find and use the rules.
!
Here, the “Do you need gas?” question is a bit ambiguous. Suppose you have 2 cars and they need different
amounts of gas for the drive. For this system, we will say the options are a SUV that needs 3/8 of a tank, and a
hybrid sedan that needs 1/4 tank. Normally, we take the sedan with its better milage, but in snow or rain, we use the
SUV.
!
!
Even though this sounds like a somewhat complicated addition to the system, with backward chaining it is easy.
First add a new List variable:
Name: Car_To Use
Prompt: Car to use: (as you will see, we will not actually ask the user for this value)
Values:
SUV
Hybrid sedan
!
Also add another List variable to ask the gas gage reading in the selected car. This could be done with a numeric
variable, but we would then require the end user to put in an exact value and would have to handle the issues that
they might enter “3/8”, rather than “.375”. That level of precision is not needed in the logic. All we really care about is
if the amount is less than 1/4 tank, between 1/4 and 3/8 or more than 3/8. We can get the information we need much
easier with a list var that will be easier for the end user to answer.
!
!
Add a List variable:
!
Name: Amount of Gas
Prompt: The amount of gas in the [[Car_to_use.value]]
Values:
Less than 1/4 tank
1/4 or more, but less than 3/8 tank
3/8 tank or more
Notice that the Prompt includes “[[Car_to_use.value]]”. This will embed the car we are going to use in the Prompt.
Any variable can be embedded in any Corvid string by putting the name in double square brackets. Here we only
want the value, so the .value property is used. This is a VERY powerful technique for making dynamic systems. We
only want to know about the gas in the car we will use, so embedding the name of the car is important to make a
clear question.
!
Also, we don’t have to worry about when or how the value of the embedded variable will be obtained. Embedded
variables use backward chaining to set their values. If the value of Car_to_use is known when this question is asked
the value will just be inserted. If it is not known, Corvid will immediately, automatically do whatever is needed to get
the value. This could call for testing/firing other rules or asking the end user.
28
!
Add a new Logic Block named “Enough Gas” that uses these 2 new variables to set the value for the variable
Need_Gas. This is built the same as the earlier Logic Blocks and should look like:
Note: When building the IF nodes for the Amount_of_gas variable, multiple values can be added to the same node
by selected more than one value and clicking the “Selected” button to add the values as a single node.
!
Now we have given the system a way to determine if the user needs gas. But we have not given it way to decide
which car to use. We could just ask the end user which car they want to take, but we want the system to recommend
the higher MPG hybrid sedan unless the weather conditions call for the SUV.
!
!
We will decide on the car to use base on the weather, so add a new List variable for the weather (we will also use this
in other rules)
!
Name: Weather
Prompt: The weather is
Values:
Clear
Raining
Snowing or recently snowed with snow on the roads
Fog
With these variables we can add a new Logic Block named “Weather” and build rules that say:
!
!!
IF it is raining or snowing or there is snow on the roads
THEN The car to use is the SUV
IF It is clear or foggy
THEN The car to use is the hybrid sedan
We don’t have to worry about where this Logic Block is. Backward Chaining will automatically use the rules when
they are needed.
!!
!!
!
!
29
!
That’s all that is needed. One of the big advantages of using
an Inference Engine like Corvid is that we can expand the
system just be giving it additional rules and “heuristics” to
include in the decision-making process. Unlike traditional
programming, we don’t have to explicitly link these new rules
into the logic. The Inference Engine and backward chaining
will find and use them when needed.
!!
!
Now let’s run the system to see what effect the additions have.
Answer the questions that you need to get to work quickly and
there are no traffic problems on the highway.
Now instead of being asked if we have enough gas, as it did
before, we are asked about the weather. This is because it
need to know the weather to know what car we will use, which
it needs to determine how much gas is enough. All this
happened automatically just by adding the rules.
!
!
Select that the weather is raining. As we know from the rules
we added, this will select the SUV.
The next question is the one about the gas gage reading.
Notice that the “SUV” has been inserted into the prompt asking
the question. This came from the embedded variable we
added to the prompt. If we had selected “Clear” in the weather
question, it would have inserted “Hybrid sedan”.
!!
!!
!!
!
Select that we have “1/4 or more, but less than 3/8 tank”. This
is not enough gas for the SUV. Answer the other questions
with any value. The rules will force the “City Streets” selection.
!!
!!
!!
!!
!!
30
Since the system now choses the car to use, we should
add this to the advice at the end to let the user know.
!
!
Open the Command Block.
Double click on the DISPLAY command to open it in the
edit portion of the panel. (or click to select it and click
“Edit” below the command list.)
!
!!
!!
!!
In the command builder portion, click the “Edit” button
below the “File to Use”
This will open the screen command builder window.
!
!
Click the “Variables” tab at the top.
Put the cursor in the “Specific Variable” edit box at the top and click Ctrl-V (Hold the Control key down and press V)
This will open a popup window with all the variables in the system. Select “Car_to_use” and click the “Insert” button
on the popup.
!
Then click the “Add Last” button to add this as the last screen command.
31
Click the “Done - Save Commands to File” button (This is important - don’t just close the window or the new
command will not be in the file.)
!!
Return to the Command Block. This does not need to be
changed. It already has the DISPLAY command with the
custom screen file. We have made a change to the file,
but the DISPLAY command using that file does not need to
be changed.
!!
!!
!!
!!
!
!!
!
!
!!
!!
!!
!!
!!
!!
!!
!
Now run the system again.
The advice at the end includes the car that should be used.
That is getting better, but there are still a few ways to improve it. We both ask the weather and the driving conditions.
Once we know the weather, we can derive the driving conditions. Add a new Logic Block named “Driving Conditions”
and use the Weather variable to set the value of Driving Conditions.
!!
!!
With this new Logic Block the system will only ask about the weather and can derive the value for Driving Conditions.
32
!
!
!
!!
!!
!!
!!
!!
!
!
One last thing we could add is a link to a traffic web site to the question about if there are “Traffic problems on the
highway”. This is easy to do.
Double click the “Traffic_problems_on_the_highway” variable in the
variable list to open it in the editor.
In the Prompt text, add a standard HTML link to a website with traffic
information.
Change the “ Are there traffic problems on the highway?” to:
Are there traffic problems on the highway? <a href="http://www.....com" target="_blank"> Click for info </a>
where the link is a link to a traffic info site for the area. Systems run with the Corvid Servlet Runtime can include any
HTML tags since they are displayed in a browser. However, even the Corvid Applet Runtime recognizes some simple
HTML, including links like this.
!
When the system is run, the question now has a link that
will display the current traffic conditions to allow the user
to answer the question.
!!
!!
!!
You’re Done! Next Steps
That finishes this sample system. This shows how even a seemingly simple problem can rapidly become more
complex one all the factors are worked in. Trying to build a system to perform this logic by traditional programming
techniques, while certainly possible, would take far longer, be much more complex and difficult to maintain than
building the same system with Exsys Corvid Core.
!
Exsys Corvid has many more features and capabilities. However, with just the main elements covered here, you can
build very effective and powerful expert systems. Of the thousands of expert systems that have been built with Exsys
tools, most use only the logical structures and concepts covered in this introduction. It is recommended you build and
complete your first systems using this approach. Later you can familiarize yourself with other features, integration,
customization and deployment options.
!!
EXSYS, Inc.
6565 America’s Pkwy. NE, Suite 200
Albuquerque, NM 87110 U.S.A.
info@exsys.com
www.exsys.com
33
Download