home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Unsorted BBS Collection
/
thegreatunsorted.tar
/
thegreatunsorted
/
programming
/
misc_programming
/
fuzzgen.doc
< prev
next >
Wrap
Text File
|
1992-06-26
|
21KB
|
470 lines
F U Z Z G E N
Fuzzy Logic Source Code Generator
Evaluation Version 1.0
U S E R G U I D E
INTRODUCTION:
As programmers, many of us have heard the terms NEURAL NETWORKS
and FUZZY LOGIC bandied about over the last couple of years. The
odd thing is that very few of us that write code on a daily basis
really have a handle on what these things mean.
Fuzzy Logic is a decision making method for computers. It has
taken many years for the merits of Fuzzy Logic to become clear.
One of the reasons is the name: people hear "Fuzzy Logic" and
immediately assume that it is imprecise. Actually, it is the
other way around -- Fuzzy Logic allows FOR imprecision in the
input in making a precise decision.
The idea that Fuzzy Logic is suited only to microcontroller
applications such as flow controllers and such is also quite
misleading. It is suitable for a wide variety of applications.
Fuzzy Logic also allows for perspective to change without having
to completely recode massive programs. For instance, if you have
a program like a battle simulation that takes factors such as
the effect of heat into account on the troops, the perspective
of HOT changes depending on the origin of the troops. Swedish or
Russian troops may see 80 degrees F as hot, whereas troops from
countries nearer the equator see 100 degrees as hot and 80
degrees as mild. Being able to make this decision in a "Fuzzy"
manner means that you can read in the control variables as needed
and still guarantee the integrity of a decision made by the
program.
FuzzGen is a program intended to introduce you to Fuzzy concepts
and allow you to create Fuzzy decision sets for your own programs.
It allows you to graphically model the decision process to aid in
visualising a decision. It also produces source code in PASCAL,
BASIC, or C so that you can incorporate Fuzzy decision making in
your programs. It is intended to save time so that you do not need
to keep coding IF - THEN - ELSE blocks to make decisions in your
programs. Essentially, FuzzGen is a CASE tool that is additionally
educational in that it will introduce many of you to new concepts.
BASIC CONCEPTS:
Fuzzy Logic is a little like the "new math" some of us were taught
(starting in the late 60's and early 70's in most districts) in
school. Fuzzy Logic uses the concept of SETS and membership. What
this means is that a data point is seen as either being a set
member or not, and more importantly, HOW MUCH of a member. Using
triangular and trapezoidal shapes to model data allows you to plot
a data point on a slope. Rather than exclusively dealing with YES/NO,
you can deal with "65%" or "32%" membership. In essence, you can
say that data point X is a Y% member of data set Z. This allows you
to have much more control over input data variance, as we can
immediately see exactly where input data lies.
This allows for a different way to code than what you're used to.
(By the way, we'll be using code fragments done in C to illustrate
points as we go along.) For instance, here's how you may code for
a variable input in standard ways:
if(INPUT_DATA <= .1)
we_do_this();
else if(INPUT_DATA > .1 && INPUT_DATA <= .2)
we_do_that();
else if(INPUT_DATA > .2 && INPUT_DATA <= .3)
do_something_else();
// AND SO ON...
Fuzzy Logic tends to compartmentalise things. Therefore, instead
of a big IF-ELSE construct, you could use the following:
int i, SetNumber;
double testval, setpctg = 0;
for(i = 0; i < NumOfSets; i++){ // loop
testval = InSet(i, INPUT_DATA); // is data in the set?
if(testval > setpctg){ // if so,
setpctg = testval; // reset high pctg marker
SetNumber = i; // and record which set.
}
}
do_whatever_we_need_done(SetNumber);
What the above code does is record the set with the highest
membership percentage so that we have a variable that shows what
set the data is in and also what action to take. At first blush,
this may not seem any better than the "old" way using the large
IF-ELSE construct. However, if you stop and think about it, you
have more control with the second method. Sure, we could change
the test points to variables in both cases, but the second method
does not need to be recompiled if you change your mind about the
number of tests to make. This is provided by variable NumOfSets.
Now, using the Fuzzy method, you can read your variable list in
PLUS you can read in how many tests to take. Sure, you could do
this yourself with IF-ELSE constructs, but this is easier.
The next step is to make it simpler to model a decision so that
it is simple to see what is happening.
GRAPHICS AND DECISION VISUALISATION:
FuzzGen uses a 2 axis graph to allow you to see data sets and
their relationships. The Y axis of the graph is used to show
percentage and ranges from 0 - 100. The X axis is used to show
the ordinal range of input data values. See figure 1.
| 100
|
|
| <---------------- Percentage
|
| Ordinal Data
| |
| |
|_0_________________________________________________________
4 20
-=-=-=-=-=-= 4-20 ma current input =-=-=-=-=-
fig 1
Also, to be able to properly plot data values into sets, we use
shapes to classify them. Common shapes (supported by FuzzGen) are
Triangular, Trapezoidal, and Square (Boolean.) There is infinite
variety possible using the Triangular and Trapezoidal shapes.
As an example, we'll now graph out a simple Fuzzy Logic demo that
shows how to use the graphics and shapes to allow a change of
perspective. The subject is temperature as seen by a Polar Bear
and as seen by someone's grandmother.
______________________60 82_________
100 | \ / |
| \ / |
P | \ / |
C | \ / |
T | C O L D \ / H O T |
G | \ / |
| \/ |
| /\ |
0 |____________________________/__\_______________|
0 65 70 100
-=-=-=-=TEMPERATURE AS SEEN BY GRANDMA =-=-=-=-
fig 2
So, in this scenario, if you wanted to find out what Grandma
would think of, say, 62 degrees, you would plot along the X
axis to 62 degrees and find out where the decision shape
intersected the decision shapes at. In this case here, Grandma
would see 62 degrees as 75% COLD and not HOT at all. (It didn't
intersect the HOT decision at all.) The COLD decision set uses
a Trapezoidal shape: anything from 0 - 60 is seen as 100% COLD,
and at 60 degrees it starts tapering down to 70. HOT also uses
a trapezoidal shape.
_____10 34___________________________
100 | \ / |
| \ / |
P | \ / |
C | \ / |
T | MILD \ / H O T |
G | \ / |
| \/ |
| /\ |
0 |__________/__\_________________________________|
0 20 25 100
-=-=-=-=TEMPERATURE AS SEEN BY A POLAR BEAR =-=-=-=-
fig 3
On the other hand, we have in this scenario the EXACT SAME
decision shapes used, but now we see the SAME RANGE of the
temperature as seen by a Polar Bear. Note that the bear sees
anything under 10 degrees as MILD, and anything above this as
getting warmer. Downright HOT starts at 34 degrees.
In these two cases we see the same data being looked at by use
of Fuzzy Logic decisions to base COLD or HOT on the perspective
of the viewer. While this is a very simple example, it should
serve to explain the basic FuzzGen modeling of Fuzzy Logic.
USING FUZZGEN:
As the program starts up, it will allow you to either get Help,
Open a data file, or start a New file. Once you get a filename
started, you are allowed to SETUP the GRAPH.
STEP 1: SET UP THE GRAPH BASICS
In the graph setup, you may want to put a label on the X axis
so that viewing of the graph is a little clearer. You can also
optionally put tickmarks and Major Tickmarks along the X axis
so that it is easier to see where a decision is intersected.
You'll also need to specify the unit range of input data, which
is required. For instance, if your input data is in degrees
Farenheight, your range may be (like our example) 0 - 100. On
the other hand, you may want to create code that reads a hardware
current loop device from 4 - 20 milliamps. In other words, the
range used is up to you and your application. The last thing to
do is tell FuzzGen how many decisions it will need to work with.
You may use up to 10 in this evaluation version.
All of these settings can be changed later, such as changing your
mind about how many decisions to work with and do on.
STEP 2: ADD DECISION SHAPES
You'll need to access SETUP | DATA SETS. This window allows you
to choose a general shape by clicking the associated option
button. You can "fine-tune" the triangular and trapezoidal shapes
by clicking the picture underneath the option button. Each click
will cause the picture to cycle through the possible shapes.
As you click the option buttons denoting shape, the data input
boxes denoting major hinge points of the shape (i.e. endpoints
and apexes) will change to reflect the number of inputs this
shape will need: Boolean - 2, Trapezoidal - 4, Triangular - 3.
These data input boxes allow for full double-precision data, so
you're not limited to integer based decision-making.
Once you decide on a basic shape, you can enter the data. FuzzGen
may sometimes disallow your input and warn you to that effect. If
this happens, look at the basic shape picture to see if it allows
you to do what you want. In general, the shapes that do not have
a straight side have the ability to place the apex hinge points
anywhere you want.
Next, you should pick a "Tag colour" by clicking the little box
with a colour in it. This allows you to pick a colour so that
when the graph is drawn it is easier to associate the particular
decision shape with a colour.
Once these items are in order, you should click the RECORD button
to record the input. You can then select a new decision to enter
or edit data for by clicking the LEFT-RIGHT buttons in the lower
right. Above the buttons is a label showing you what decision
you are working on (numbered) such as "1 of 4."
At any time, you can access a SHAPE HELP by clicking the HELP
button. The SHAPE HELP window shows you the basics of working
with the shapes. As you get more accustomed to working with
FuzzGen, you'll rely on this a little less.
Once you have essentially defined the decsion shapes, we can then
proceed to the next step.
STEP 3: MODELING
You should now exit from the SETUP DATA SETS window by clicking
the CANCEL button. On the main FuzzGen window, click the APPLY
button. Your graph and basic data shapes are shown. You may at
this time elect to go back to SETUP GRAPH or SETUP DATA SETS to
fine tune your model. In this sense, FuzzGen is interactive in
that you can make an adjustment and come back to click the APPLY
button to see what the change looks like.
STEP 4: GENERATE SOURCE CODE
The LANGUAGE menu allows you to select a source language to use
by clicking the language of choice so that it becomes "checked."
If you are satisfied that your application is correctly done (i.e.
the graph seems to reflect what you're after) and the language
source is correct, go back to the LANGUAGE menu and select the
MAKE CODE menu item. This will output a file with the appropriate
extension added to the name of your data file. For example, if
you are working with filename DUMMY, here's the source files that
would be generated depending on your language of choice:
C ------- DUMMY.C
PASCAL -- DUMMY.PAS
BASIC --- DUMMY.BAS
Unless you have any second thoughts about the decision modeling,
you are pretty much done with FuzzGen at this point. You should
SAVE the data file you are working with for later recall. In the
FILE menu, select SAVE or SAVE AS. SAVE AS will ask you for a new
name to save under (other than the one you used). It will save in
the current data file directory.
UNDERSTANDING FUZZGEN SOURCE CODE:
Essentially, the code example we used earlier was idealised so
that you could see how highly optimised Fuzzy code would work.
FuzzGen code is slightly different in that we don't know what
you'll want to do to optimise it. FuzzGen output source code
is fully commented such that you should be able to be able to
read it without difficulty. We think that you'll want code you
can read cleanly rather than highly optimised code that is more
difficult to decipher.
As such, here's a short description of what a typical source
file would look like:
It starts out with a series of constants, which are basically
the hinge points of the different decision sets. It uses a
coding for the names so that you can tell what decision uses
what constants. Here's an example:
d1low = decision #1, left (low) endpoint
d1hi = decision #1, right (high) endpoint
ap1l = APEX #1 LEFT = decision #1 left apex
So in a C source output, it would use #defines to set up the
initial constants:
#define d1low 0
#define d1hi 20
etc...
The constant declaration is followed by the source for each of
the decisions. They are all named decision-n and you pass the
inputs (including the data to test) as part of the parameter
list. In C:
double Decision1(double TESTDATA, double leftend,
double leftapex, double rightapex,
double rightend)
For immediate implementation, you'd simply feed a decision with
the previously defined constants as follows:
double percentage;
percentage = Decision1( DATA, d1low, ap1l, ap1r, d1hi);
This allows you to test each decision independantly.
Our recommendation is that once you're satisfied that the
routines work as you want, that you combine them into 1 single
function as follows:
double FuzzDecision( int DecisionNum, double TestData)
{
switch (DecisionNum){
case 1:
BODY OF FUZZGEN DECISION 1 CODE
case 2:
BODY OF FUZZGEN DECISION 2 CODE
....
}
This will allow you to make your decision loop much faster as
in the example code we had at the beginning of this guide.
FUZZGEN DISTRIBUTION:
FuzzGen evaluation version 1.0 is a program that will hopefully
give you enough of a taste of Fuzzy programming that you'll want
to give serious consideration to ordering FuzzGen II, which is
the enhanced retail version of this product.
FuzzGen II Features:
* Printing! You can print graphics copies of your decision logic
to aid in documenting programs.
* Notes File to attach to the decision logic so that you can
document the entire decision process, assumptions, etc.
* More decision sets for those ultra-tough projects.
* Printed documentation with some more tricks, techniques and
hints.
FuzzGen is a shareware product, meaning that it has been released
in a functional format for BBS and disk vendor distribution. It is
as much of an advertising ploy for Alston Software Labs as much as
it is useful in and of itself.
LEGAL STUFF:
Shareware distribution sometimes is misunderstood. For instance,
FuzzGen is NOT public domain. It is a copyrighted product wholly
owned by Alston Software Labs, and licensees (as in standard retail
software) may use FuzzGen to create source code. Although we do not
charge any sort of royalty license to use FuzzGen generated source
code in your programs, you are strictly forbidden to use Fuzzgen
code in your programs if you haven't paid for the product. Sorry,
but any money you paid to get a copy of this program from a vendor
or other source did not give you any rights to FuzzGen.
INDUSTRY STANDARD DISCLAIMER:
FuzzGen evaluation version 1.0 is supplied as-is with no warranty
whatsoever with regard to merchanability or fitness for purpose.
Alston Software Labs assumes no responsibility for any consequenses
of use or misuse of this program.
HOW TO ORDER:
Product purchase comes in 2 forms: You may elect to "register"
FuzzGen as-is for $15, at which point we'll send you a fully
licensed copy of FuzzGen minus the "Evaluation Copy" tags present
in this copy. For $25, you may purchase FuzzGen II, which contains
the added features outlined above. Either payment method will
license you to be a registered user with rights to use the source
code produced by FuzzGen (or FuzzGen II) in your programs without
any royalty.
Please use WINDOWS NOTEPAD or a similar editor to open ORDER.FRM
to be able to print the order form / invoice. You'll need to call
or Fax us for Site License information.
IN CLOSING:
We sincerely hope you'll be able to learn something from FuzzGen,
even if you don't order it. We encourage you to pass this product
on to associates, employees, and other interested parties.
(providing, of course, that you charge no fee for it.)
If you have suggestions for improvement of FuzzGen, we'd sure like
to hear from you. Our aim is to produce a truly useful Fuzzy Logic
generator at the lowest possible end-user cost, and we're always
looking for ways to improve our product line. Speaking of product
lines, you can see what other ASL products are available by
browsing the CATALOG.DOC file using NOTEPAD or a similar tool.
Alston Software Labs
1320 Standiford Ave #242
Modesto CA 95350 USA
Phone (209) 522 - 8666
FAX (209) 522 - 8666