
RiverSoftAVG Newsletter #5
June/July 2001 Hello and welcome to the fifth edition of RiverSoftAVG's newsletter! We must apologize for sending out this newsletter so late. As you will find out though, we have been working hard on the new release of the Inference Engine Component Suite. We also hope that you have been finding these newsletters welcome as well as helpful. This is our largest newsletter ever, packed full of information. This issue, the major topic is the discussion of a powerful new feature coming with IECS v2.0: Fuzzy Logic. We are very excited about this feature as it simplifies developing expert systems as well as making them more powerful. Also, in this issue, we have some great tips for storing your expert systems in a database and creating UIs from the expert system. A reminder, this newsletter and all previous ones are available for download from the web site at http://www.riversoftavg.com/articles_&_tips.htm Contents News: What's coming up? Get Fuzzy With Inference Engine Component Suite version 2.0 Tip: How to create and modify your UI from your IECS expert system Tip: How to store your IECS expert system structures in a database Free Download: IECS Delphi 6 Update Note: You received this newsletter because you are an owner of a RiverSoftAVG product. If you received this newsletter by mistake or if for any reason you wish to not receive any future mailings from RiverSoftAVG, just reply to this message informing us so. We apologize for any intrusion. News: What's coming up? Get Fuzzy with Inference Engine Component Suite Version 2.0 This edition, we are continuing our series about new features upcoming in version 2 of the Inference Engine Component Suite. Last time, we discussed the exciting database features; if you missed it, you can access the old newsletter at http://www.riversoftavg.com/newsletter2001AprMay.htm This month, we are going to talk about what I think is an even more exciting feature: fuzzy logic.
What is Fuzzy Logic?
It would probably be good to start off discussing
just what is Fuzzy Logic. Fuzzy Logic was actually invented by Lofti
Zadeh way back in 1965. Despite its name, Fuzzy Logic is not
the science of fuzzy thinking combined with fuzzy conclusions :). Fuzzy
Logic is a method for modeling and reasoning about imprecise or approximate
concepts. Traditional logic, where concepts must be true or false, zero
or one, has trouble at the boundaries of a problem: Is the water hot or
cold? It's neither and both, it is somewhat hot. Fuzzy logic
concerns itself with the boundaries of a concept, things can be "somewhat
true" and "sorta false". Fuzzy Logic reduces system
complexity by avoiding the complex mathematics and logic necessary to solve
problems at these boundary conditions using traditional, exists/not exists,
logic. It is able to approximate the behavior of problems that are
poorly understood or nonlinear.
Fuzzy Logic also exhibits another desirable
characteristic, it allows the user to represent or model the problem linguistically.
Fuzzy Logic allows reasoning about the problem in ways natural to the user: Is
the temperature hot? How long is the project duration? Using linguistic
variables (similar to regular variables like X and Y except they take
values like "cold" or "not very long"), Fuzzy Logic
provides a closer mapping between the actual, real world, problem and the
computer representation of the problem.
Altogether, Fuzzy Logic provides a mechanism for
reducing development time, modelling complex nonlinear systems, and
increasing comprehension and understanding of the solutions made..
What is its advantages and what is it used
for?
Fuzzy Logic, combined with expert systems, provide
the ability to make your solutions more intuitive and natural, with less
rules, and with greater readability and maintainability. It also makes
your systems more powerful by modelling problems hard to solve using
traditional techniques. Fuzzy Logic provides the following advantages
for developing systems:
Fuzzy Logic has wide applicability in most every
domain where a conventional expert system could be used (and beyond!).
Fuzzy Logic is a great match for process and control engineering problems,
business logic and risk analysis, and information decision support systems.
For example, Fuzzy Logic can solve problems to automatically brake trains to a
stop, back 18 wheeler trucks up to a loading dock, assess project risk,
calculate pricing for new products, balance a pendulum, control mixing of flow
of liquids, etc.
Fuzzy Set Theory
As opposed to numbers and discrete symbols, Fuzzy
Logic reasons based on fuzzy set theory. Unlike traditional sets though,
where a value may either be in (TRUE or one) or not in (False
or zero) a set (a member of a set), fuzzy sets are more flexible allowing
values to have a degree of membership in the set between zero and one.
More formally, fuzzy sets are "functions that map a value, which might be
a member of a set, to a number between zero and one, indicating its actual
degree of membership." Membership values of zero indicate that a
value is not in the set, while membership values of one indicate a value is
completely in the set.
To go back to something we discussed earlier,
fuzzy set theory allows us to define the terms or values that a linguistic
variable may take. For example, we may define a fuzzy type which is
based on Temperature. Any variable of this type may take fuzzy set
values of cold, warm, and hot. Cold, warm, and hot are the fuzzy sets
which define the terms of the type. Each fuzzy set defines a membership
function over the domain of temperature. If, for our example,
temperature has a range between 0 and 100 Celsius, Cold could be a fuzzy set
that defined any value under 10 Celcius as completely cold (one or True),
any value over 50 as not at all cold (zero), and any value in between having a
membership between zero and one. The graph below illustrates this point:
Fuzzy Variable: Temperature
Fuzzy Values: cold(*)
1.0000*******
0.9500 ** 0.9000 * 0.8500 * 0.8000 * 0.7500 * 0.7000 0.6500 * 0.6000 * 0.5500 0.5000 * 0.4500 0.4000 * 0.3500 * 0.3000 0.2500 * 0.2000 * 0.1500 * 0.1000 * 0.0500 ** 0.0000 **************************  0.00 20.00 40.00 60.00 80.00 100.00
Universe of Discourse: From 0.00 to 100.00
On the Left or y axis is the degree of membership
and on the bottom or xaxis is the universe of discourse, in this
case the temperature range. A temperature reading of 10 means that our
belief that the temperature is cold is 100%. As the temperature
increases, our belief that the temperature is cold decreases until we don't
believe it at all at 50 Celsius. Note that our definition of a linguistic
variable is slightly different from what is defined in The Fuzzy Systems
Handbook (discussed below). For the IECS, a fuzzy linguistic variable
can take a fuzzy set value, which is a fuzzy set modified by qualifiers.
Our fuzzy set value is what Earl Cox defines as a linguistic variable.
Fuzzy Sets can be combined into expressions using
"and" and "or" as well as modified by qualifiers, called hedges
in fuzzy logic nomenclature. Hedges modify the shape of a fuzzy
set in predictable ways. For example, the very hedge modifies a
fuzzy set by pushing all values less than one towards zero. This has the
effect of shrinking the boundary, the fuzzy portion, of the set closer to the
area that is completely in the set:
Fuzzy Variable: Temperature
Fuzzy Values: cold(*) very cold(+)
1.0000++++++*
0.9500 ++* 0.9000 +* 0.8500 +* 0.8000 * 0.7500 + * 0.7000 0.6500 + * 0.6000 * 0.5500 + 0.5000 * 0.4500 + 0.4000 * 0.3500 + * 0.3000 0.2500 + * 0.2000 + * 0.1500 + * 0.1000 * 0.0500 +++ ** 0.0000 +++++++++++++++++++++++++++++  0.00 20.00 40.00 60.00 80.00 100.00
Universe of Discourse: From 0.00 to 100.00
The curve defined by the plus signs in the graph
above defines the fuzzy set value, very cold. Hedges are very
powerful because they are predictable as well as intuitive.
Intuitively, if we think about a temperature being very cold we want the value
to be the prototypical value of cold, closer to cold. A temperature of
30 is certainly a member of cold, but it should definitely be less of a
member of very cold, which is what occurs. There are many other
hedges such as Not, Somewhat, Extremely, Above, Below, About, Near,
Positively, and Generally.
What does it mean to make fuzzy expressions using and
and or? Lofti Zadeh defined the AND operator for fuzzy
sets to mean taking the intersection of the two sets, or the minimum
values in the sets. The OR operator is the opposite, taking the union
of the two sets, or the maximum values in the sets. If we use
an example, it becomes clear. When we talk about is a value hot OR
cold, we can see it is very likely that a value could be hot or
coudl be cold, where only in the mid range between the two concepts does
the likelihood decrease. The maximum of the two sets captures this
meaning perfectly:
Fuzzy Variable: Temperature
Fuzzy Values: hot or cold(*)
1.0000*******
******
0.9500 ** ** 0.9000 * * 0.8500 * * 0.8000 * * 0.7500 * * 0.7000 0.6500 * * 0.6000 * * 0.5500 0.5000 * * 0.4500 0.4000 * * 0.3500 * * 0.3000 0.2500 * * 0.2000 * * 0.1500 * * 0.1000 * * 0.0500 ** ** 0.0000 ***  0.00 20.00 40.00 60.00 80.00 100.00
Universe of Discourse: From 0.00 to 100.00
Using fuzzy sets and hedges, it is possible to
write expressive statements about a subject, with the same shades of semantic
meaning used by experts, e.g.,
Fuzzy Inference
The above "rule" begs the question, how
does fuzzy sets and fuzzy logic work with inferencing in fuzzy expert systems?
With fuzzy expert systems, rules assert fuzzy facts to combine evidence
about a fuzzy fact. There is no such thing as duplicate fuzzy facts on
the fact list in a fuzzy expert system. Rather, fuzzy facts combine to
modify the shape of the fuzzy set in the one fuzzy fact. There are
actually two components to this procedure: how do fuzzy facts match with fuzzy
rule patterns and how does asserting fuzzy facts work?
We will look at the second question first.
Asserting fuzzy facts is no different than asserting regular facts, except for
one slight difference. When a fuzzy fact is already on the fact list of
an expert system, asserting another fuzzy fact of the same type causes the
intersection (minimum) of the two fuzzy facts to be asserted (the old
fuzzy fact on the fact list is removed).
How do fuzzy facts match with fuzzy rule patterns
and how does that affect rule execution? At first glance, it is not
obviously how a rule with a pattern that if the temperature is very hot
should match with a fact about the temperature is somewhat warm.
Obviously, it seems like they should match some even though it is not a
perfect match. In fact, they do. Fuzzy facts match with fuzzy rule
patterns based on the maximum value in the intersection of the fuzzy fact with
the fuzzy rule pattern fact. For example, the intersection (minimum) of
very hot and somewhat warm could look like this:
Fuzzy Variable: Temperature
Fuzzy Values: [very hot] and [somewhat warm](+)
1.0000
0.9500 0.9000 0.8500 0.8000 0.7500 0.7000 0.6500 +++ 0.6000 + 0.5500 + 0.5000 + 0.4500 + + 0.4000 0.3500 + + 0.3000 + 0.2500 + 0.2000 + + 0.1500 + + 0.1000 0.0500 +++ + 0.0000++++++++++++++++++++++++++++++  0.00 20.00 40.00 60.00 80.00 100.00
Universe of Discourse: From 0.00 to 100.00
In this case, the maximum of the
intersection is around 0.65. In fuzzy expert systems, this maximum, the
pattern match strength is usually compared to some threshhold, if the
strength is greater than the threshhold then the rule is activated and put on
the agenda. This number also becomes the strength of the rule
activation. Note that if a rule has multiple patterns, the
minimum of all the pattern match strengths determines if the rule is activated.
The rule activation strength is used to modify the shape of any fuzzy sets
that are asserted on the right hand side of a rule; this process is called
fuzzy correlation. Fuzzy correlation controls how the truth of a rule's
premise (IF portion) modifies the truth of the rule's consequents, e.g., fuzzy
facts asserted in a rule cannot have greater truth than the truth values of
the premise. Intuitively, this makes sense since you shouldn't
assert a fuzzy fact that has stronger possibility than its precedents.
Usually, the rule activation strength is either used to chop off any values of
a fuzzy set above the strength (which modifes the shape of the fuzzy set by
creating "plateaus") or multiplied against the values of the fuzzy
set (which preserves the shape of the fuzzy set but has the effect of
"shrinking" it).
For example, the following graph shows the fuzzy
set cold and how the fuzzy set cold (*) would be asserted with a cutoff
value of 0.65 (+):
Fuzzy Variable: Temperature
Fuzzy Values: cold(*) cold > 0.6500(+)
1.0000*******
0.9500 ** 0.9000 * 0.8500 * 0.8000 * 0.7500 * 0.7000 0.6500+++++++++++++* 0.6000 +* 0.5500 + 0.5000 + 0.4500 0.4000 + 0.3500 + 0.3000 0.2500 + 0.2000 + 0.1500 + 0.1000 + 0.0500 ++ 0.0000 ++++++++++++++++++++++++++  0.00 20.00 40.00 60.00 80.00 100.00
Universe of Discourse: From 0.00 to 100.00
Observe, how a plateau is created in cold once it
exceeds 0.65. The next graph shows the fuzzy correlation using the
product of cold and 0.65:
Fuzzy Variable: Temperature
Fuzzy Values: cold(*) cold * 0.6500(+)
1.0000*******
0.9500 ** 0.9000 * 0.8500 * 0.8000 * 0.7500 * 0.7000 0.6500++++++++ * 0.6000 ++ * 0.5500 ++ 0.5000 + * 0.4500 + 0.4000 + * 0.3500 + * 0.3000 0.2500 + * 0.2000 + * 0.1500 + * 0.1000 ++* 0.0500 ++* 0.0000 +++++++++++++++++++++++++++  0.00 20.00 40.00 60.00 80.00 100.00
Universe of Discourse: From 0.00 to 100.00
Ok, now we understand how fuzzy correlation
modifies asserted fuzzy facts, but there is one last step in asserting fuzzy
facts in a rule: Fuzzy Inference. The Fuzzy Inference method controls
how assertions are combined with facts already on the fact list. Note
that this is different from the fuzzy fact assertions we described above, that
was for an unconditional assertion outside of a rule, and was the intersection
or minimum of the two fuzzy facts. Inside a rule firing, we
take the union or maximum of the two fuzzy sets. This union inside a
rule is called the minmax method of Fuzzy Inference. Remember, though,
that the asserting fuzzy fact has been modified by the Fuzzy Correlation
method.
Defuzzifying Fuzzy Sets
There is one last piece to using fuzzy sets.
How do you get a representative value from a fuzzy set? Obviously, it is
hard to set a faucet to slow or a car to fast. We need
a specific value that hopefully best represents the fuzzy set in its domain.
This is a process called defuzzification.
The easiest method would be just to arbitrarily
pick a point from the fuzzy set, perhaps the point in the domain where the
fuzzy set reaches its maximum. This is easy, but is clearly not the
best. What if a fuzzy set looks like the following:
Fuzzy Variable: Temperature
Fuzzy Values: [(pi 30 5)] or [(pi 60 10)](*)
1.0000
*
*
0.9500 0.9000 0.8500 * * 0.8000 0.7500 0.7000 0.6500 * * * * 0.6000 0.5500 0.5000 0.4500 0.4000 0.3500 * * 0.3000 0.2500 0.2000 0.1500 * * * * 0.1000 0.0500 0.0000************* ******** ***************  0.00 20.00 40.00 60.00 80.00 100.00
Universe of Discourse: From 0.00 to 100.00
Which maximum point is the "best" for
the fuzzy set: 30 or 60? A better method would be to take the average of
the maximum points, in this case 45. This method is very common and is
called maximum defuzzification.
However, there are even better methods for
defuzzification. Consider the following fuzzy set:
Fuzzy Variable: Temperature
Fuzzy Values: [warm > 0.6500] or [very [extremely hot]](*)
1.0000
*****
0.9500 0.9000 * 0.8500 * 0.8000 0.7500 * 0.7000 0.6500 ***************** 0.6000 * * * 0.5500 * * 0.5000 * * 0.4500 * * * 0.4000 * * 0.3500 * * 0.3000 * 0.2500 * 0.2000 ** 0.1500 ** 0.1000 ** 0.0500 ** 0.0000*  0.00 20.00 40.00 60.00 80.00 100.00
Universe of Discourse: From 0.00 to 100.00
The maximum defuzzification method would give a
value of 90. Clearly, though, the bulk of this fuzzy set is in the hump
in the middle. It would seem that there must be a better defuzzification
method which would factor in all parts of a fuzzy set. A common
defuzzification in control domains is the moment, or center of gravity,
defuzzification method because it accounts for the entire fuzzy set shape and
tends to smooth out the fuzzy region. This method finds the center of
gravity where half of the "weight" of the fuzzy set is on the left
and half on the right. The moment defuzzification method figures out the
area under the shape of the fuzzy set and gets the center point. For the
fuzzy set above, the moment defuzzification method returns 61.39.
Fuzzy Logic in the IECS
Ok, there ends our primer on fuzzy logic. Now, how is fuzzy logic implemented in the IECS? Version 2 has extended the CLIPS syntax to accomodate fuzzy logic. Fuzzy Types are defined using a new def construct, similar to deftemplate or defrule, called deftype. The deftype construct accepts the name of the type, followed by its base types (in this case FUZZY), and then the definition of the type. To define the Temperature type used above, the user would write:
(deftype Temperature extends (type FUZZY)
(range 0.0000 100.0000) (units Celcius) (values (term hot (s 50.0000 90.0000)) (term warm (pi 50.0000 50.0000)) (term cold (z 10.0000 50.0000)) ))
Now, to actually use the Temperature type, we need to define it in a
deftemplate slot, using the optional type modifier:
(deftemplate water
(slot temp (type Temperature)) )
After defining the deftemplate, we can create facts normally using the fuzzy
sets and hedges:
(water (temp hot or [very cold and not
warm]))
(water (temp not somewhat hot))
(water (temp about 30))
Rules can also be written:
(defrule waterhot
(water (temp ?temp&hot) ) => (printout t "Water is hot " (momentdefuzzify ?temp)))
The IECS also provides very powerful inferencing capabilities based on Fuzzy
Logic. Not only does it calculate the strength of the rule activations,
but provides both methods of Fuzzy Correlation: product and minimum. It
also supports FIVE different Fuzzy Inference methods: minmax, sum, mean,
product, and boundedsum.
Finally, it provides both defuzzification methods: maximum and center of
gravity. To get a scalar value for a fuzzy set, you use either the
maximumdefuzzify or momentdefuzzify functions:
(momentdefuzzify (createfuzzyvalue
Temperature [warm > 0.6500] or [very [extremely hot]]))
This article was a quick primer for Fuzzy Logic in the IECS. Hopefully,
you have learned some new things, and on the way has made you excited about
this powerful new feature. For more information on Fuzzy Logic, you may
want to check out The Fuzzy Systems Handbook by Earl Cox. Even though
the source code for the book is in C++, the practical examples are a great
help in understanding and designing fuzzy systems. The IECS' fuzzy logic
also implements almost all the features discussed in the book.
Numerous expert systems and demos will also be included with version 2.0 to
help you work with and understand this powerful feature.
The Inference Engine Component Suite provides an
amazing array of functions. Using TUserFunction components, you can
create additional functions that allow your expert systems to do practically
anything, from complex math to building user interfaces. But did
you know that you can use some builtin functions to build and manipulate your
forms and userinterfaces from within your expert system?
The object package provides some powerful
functions for creating and destroying objects and components, as well as reading
and writing to published properties. The object package provides the
following functions: createcomponent, createobject, and freeobject for
creating components or objects and then freeing them; getcomponent and
getcomponents for retrieving a component or a list of child components of another
component; getproperties, getproperty, and setproperty for reading and
writing properties; executeaction for executing the OnExecute event of a
TAction component; and getclass and isclass for manipulating class type
information.
So how would you create a component using the
IECS? Well, first let us look at what you would need to do to create a
TButton component at runtime (as opposed to designtime). To create a
component, you need to call the constructor of component passing in a Owner,
which is usually the form:
Button1 := TButton.Create(Self);
However, if you just write the above code you
don't see anything! What is missing?! Well, we forgot that a
component *must* have a parent to be created on:
Button1 := TButton.Create(Self);
Button1.Parent := Self;
Much better! However, if we execute the following code, it creates a
standard button all the way in the topleft corner, no caption or text, and no
code to execute when the user clicks the button. We obviously need a few
more things. How would we do it at design time? We would set
the Top and Text properties and define an OnClick event handler.
Alternatively (and actually much better), we would use a TAction component
setting its Text property and creating an OnExecute event handler, and then
assign the Button's Action property to our action.
Let's assume we have created the TAction component at designtime.
Therefore, to create and hook up our button at runtime, our code would look
like this:
Button1 := TButton.Create(Self);
Button1.Parent := Self;
Button1.Top := 20;
Button1.Action := Action1;
The IECS allows you to do the exact same thing in
your expert system. The createcomponent function is just what we need.
Here is the online help ('(ppdeffunction
createcomponent)') for the function:
;; createcomponent function
;; Creates and returns a component based on the first argument's class. The second argument is the owner of the component. The optional third argument specifies the parent of the component ;; syntax: (createcomponent <TComponentClassexpression> <TComponentexpression> [<TWinControlexpression>])
So what do we need? We need the class (TButton)
we wish to create and the form (as both owner and parent). The getclass
function can be used to return the class and the getcomponent function will
return the form. The getclass function will return a class expression
for the symbol passed to it. However, there is a slight wrinkle in that
it can only return classes that have been registered for the appliction.
You will need to call Delphi's RegisterClass method in your form's OnCreate
event to make the class available:
// Must register the classes we are creating
RegisterClass( TButton );
The getcomponent function returns the child component
of the form where the TInferenceEngine component resides matching the name
given to it. To just return the form where the TInferenceEngine is
located, calling getcomponent without any arguments will do. So, the
following CLIPS code will create a button and make it appear on the form:
(createcomponent (getclass TButton)
(getcomponent) (getcomponent))
How do we set the other properties of the button?
Well, as you might have guessed, the setproperty function was made to do just
that job. The setproperty function uses Delphi's RunTime Type
Information (RTTI) to write to any writable, published property
of an object. I am not going to discuss RTTI or published properties.
For the purposes of our discussion, f you can see the property of a component
at designtime, it is published.
The setproperty function needs the component to
modify though. We could use the getcomponent function to get the
component that we just created, but since we don't know its name, it is
probably better if we just bind the created component to a variable in the
expert system. Using the bind function, we will bind the component to a
variable called ?Button1 and then set the properties:
(bind ?Button1 (createcomponent (getclass
TButton) (getcomponent) (getcomponent)))
(setproperty ?Button1 Top 20)
(setproperty
?Button1 Action (getcomponent Action1))
Voila! We are done. Instant GUIs :)
Using the above technique, we can easily create almost any UI that uses
actions. The trickiest part is remembering to register all the classes
and creating the actions.
Occasionally, you may find yourself needing to be
able to store your expert system structures (facts, rules, etc) in a database,
and of course restoring them too. You may need to do this to support
version control, to support multiple runtime experts, or for whatever.
The IECS does not directly support database operations, though automatic
fact database support will appear in version 2.0 (Please see last month's
newsletter for more details). But what about the other structures,
especially rules? Of course, you could just save the entire expert
system to an ASCII file (using the SaveToFile method of the
TInferenceEngine component) and then put the file in a blob field, but
I think it would be nicer if we had better control and granularity than that.
Fortunately, there is a better way!
The IECS provides ToString properties for every
major structure (rules, facts, fact templates, fact sets, etc) in your expert
system. We can take these strings that are created and store them in our
database fields. Later, we can use the parsing methods of the
TInferenceEngine component to enter them back into the expert system.
For our example, we are going to write code to
store the rules of our expert system into a database. We are going to
assume you know how to create tables and fields. To support storing your
rules with version history, first fire up your database and create a table
with at least 3 fields: VERSION_NO (float or integer), RULE_NAME (String), and
RULE (String). The VERSION_NO and RULE_NAME fields will make up our key,
together they will provide a unique key for every rule and for every version.
To store a rule, you would write the following
code (assuming table is open, etc):
Table1.FieldByName('VERSION_NO').AsFloat
:= 1.0; // Version number could also be automatically
generated by the DB
Table1.FieldByName('RULE_NAME').AsString := Rule.RuleName; Table1.FieldByName('RULE').AsString := Rule.ToString;
To restore your rule later, you just need to parse
the RULE field. Of course, the fact templates must have been defined
first:
InferenceEngine1.ParseRule( Table1.FieldByName('RULE').AsString );
That's it! Quick and easy database storage.
Free Download: IECS Delphi 6 Update The Inference Engine Component Suite has been updated for Borland's Delphi 6! With this latest update, the IECS supports Delphi 4, 5, or 6. You can find the update at http://www.riversoftavg.com/patches.htm 
Send mail to
webmasterNO@SPAMRiverSoftAVG.com with questions or comments about this web
site.
