menzies.us/tmp/scant
(less, simpler, SE)

home | site map
new | credits | legal
© 2004 gpl2 by
tim@menzies.us


Advanced search

Fuzzy rules

The words figure and fictitious both derive from the same Latin root, fingere. Beware! --M.J. Moroney

Everything is vague to a degree you do not realize till you have tried to make it precise. --Bertrand Russell

... as the complexity of a system increase, our ability to make precise and yet significant statements about its behavior diminishes until a threshold is reached beyond which precision and significance (or relevance) become almost mutually exclusive properties. --Lofti Zadeh [Zadeh1973a]

Officially, there are rules. Unofficially, there are shades of gray. Fuzzy logic is one way to handle such shades of gray.

For example, consider the rule ``if not raining then play golf''. Strange to say, sometimes people play golf even in the rain. Sure, they may not play in a hurricane but a few drops of rain rarely stops the dedicated golfer.

Fuzzy logic tries to capture these shades of gray. Our golf rule means that we rush to play golf in the sunshine, stay home during hurricanes, and in between we may or may not play golf depending on how hard it is raining.

Fuzzy logic uses fuzzy sets. Crisp sets have sharp distinctions between true and falsehood (e.g. it is either raining or not). Fuzzy sets have blurred boundaries (e.g. it is barely raining). Typically, a membership function is used to denote our belief in some concept. For example, here are some membership functions for the belief that a number is positive. All these beliefs have the same membership function:

 1/(1+exp(-1*x*crisp))

where the crisp parameter controls how sharp is the boundary in our beliefs:

Note that for large crisp values the boundary looks like classical logic: at zero a number zaps from positive to negative. However, in the case of our golfing rule, there are shades of gray in between sunshine and hurricanes. We might believe in playing golf, even when there is a little rain around. For these situations, our beliefs are hardly crisp, as modeled by setting crisp to some value less than (say) one.

The crisp value lets us operationalize a set of linguistic variables or hedges such as ``barely'', ``very'', ``more or less'', ``somewhat,'' ``rather,'' ``sort of,'' and so on. In this approach, analysts debrief their local experts on the relative strengths of these hedges then add hedge qualifiers to every rule. Internally, this just means mapping hedges to crisp values. For example:

  definitely: crisp=10
  sort of:    crisp=0.5
  etc

Alternatively, analysts can ask the local experts to draw membership functions showing how the degrees of belief in some concept changes over its range. For example:

Sometimes, these can be mapped to mathematical functions as in the following:

Note that the function need not be represented mathematically. If the local experts draw any shape at all, we could read off values from that drawing and store them in an array. For example, an analyst can construct an array of values for various terms, either as vectors or matrices. Each term and hedge can be represented as (say) a 7-element vector or 7x7 matrix. Each element of every vector and matrix a value between 0.0 and 1.0, inclusive, in what might be considered intuitively a consistent manner. For example, the term ``high'' could be assigned the vector

     0.0 0.0 0.1 0.3 0.7 1.0 1.0

and ``low'' could be set equal to the reverse of ``high,'' or

     1.0 1.0 0.7 0.3 0.1 0.0 0.0

[TOC]

Zadeh Operators

The AND, OR, NOT operators of boolean logic exist in fuzzy logic, usually defined as the minimum, maximum, and complement; i.e.

    [1]  truth (not A)   = 1.0 - truth (A)
    [2]  truth (A and B) = minimum (truth(A), truth(B))
    [3]  truth (A or B)  = maximum (truth(A), truth(B))

When they are defined this way, the are called the Zadeh operators, because they were originally defined as such in Zadeh's original papers.

Here are some examples of the Zadeh operators in action:

 [1] truth (not A)   = 1.0 - truth (A)
 [2] truth (A and B) = minimum (truth(A), truth(B))
 [3]  truth (A or B)  = maximum (truth(A), truth(B))

Some researchers in fuzzy logic have explored the use of other interpretations of the AND and OR operations, but the definition for the NOT operation seems to be safe.

Note that if you plug just the values zero and one into the definitions [1],[2],[3], you get the same truth tables as you would expect from conventional Boolean logic. This is known as the EXTENSION PRINCIPLE, which states that the classical results of Boolean logic are recovered from fuzzy logic operations when all fuzzy membership grades are restricted to the traditional set {0, 1}. This effectively establishes fuzzy subsets and logic as a true generalization of classical set theory and logic. In fact, by this reasoning all crisp (traditional) subsets ARE fuzzy subsets of this very special type; and there is no conflict between fuzzy and crisp methods.

[TOC]

Example

Assume that we have some fuzzy sub membership functions for combination of TALL and OLD things defined as follows:

 function tall(height) {
  if (height < 5 ) return Zero;
  if (height <=7 ) return (height-5)/2;
  return 1;
 }
 function old(age) {
   if (age <  18) return Zero;
   if (age <= 60) return (age-18)/42;
   return 1;
 }
 function a(age,height)   { return FAND(tall(height),old(age)) }
 function b(age,height)   { return FOR(tall(height), old(age)) }
 function c(height)       { return FNOT(tall(height)) }
 function abc(age,height) { 
        return FOR(a(age,height),b(age,height),c(height)) }

(The functions FNOR, FAND, FOR are the Zadeh operators and are defined below.)

For compactness, we'll call our combination functions:

    a = X is TALL and X is OLD
    b = X is TALL or X is OLD
    c = not (X is TALL)
    abc= a or b or c

Here's the OLDness functions:

Here's the TALLness function:

Here's (c); i.e. the not TALLness function:

Here's (a): TALL and OLD

Here's (b): TALL or OLD

Here's (abc): a or b or c

[TOC]

In practice

Methodology, a fuzzy logic session looks like this:

Fuzzification:
Using membership functions, describe a situation.
Rule evaluation:
Apply fuzzy rules (e.g. using the Zadeh operators)
Defuzzification:
Obtaining the crisp or actual results:
  • Apply some threshold to declare than any fuzzy belief over (e.g.) 0.2 is true and all others are false.
  • Translate the resulting beliefs back through the membership functions to get a linguistic summary of the conclusion; e.g. happy is sort of true.
  • Return the centroid of the computed membership function. This process can be very complex (using full integrals) or, as the following example shows, very simple.

    Suppose we have an output fuzzy set AMOUNT which has three members: ZERO, MEDIUM, and HIGH. We assign to each of these fuzzy set members a corresponding output value, say 0 for Zero, 40 for MEDIUM, and 100 for HIGH. A Defuzzification procedure might then be:

     return (ZERO*0 + MEDIUM*40 + HIGH*100)/(0+40+100)

[TOC]

Source Code

zadeh.awk

share/bin/zadeh.awk::

Fuzzy Not

Compliment of passed arguments

 function FNOT(a) { return 1 - a}

Fuzzy And

Minimum of passed arguments

 function FAND(a,b,c,d,e,f,g,h,    argv,argc,ii,temp) {
   argc=args(argv,a,b,c,d,e,f,g,h);
   temp=1;
   for(ii=1;ii<=argc;ii++) 
        temp=least(temp,argv[ii]);
   return temp;
 }

Fuzzy Or

Minimum of passed arguments

 function FOR(a,b,c,d,e,f,g,h,     argv,argc,ii,temp) {
   argc=args(argv,a,b,c,d,e,f,g,h);
   temp=Zero;
   for(ii=1;ii<=argc;ii++) 
        temp=most(temp,argv[ii]);
   return temp;
 }

fuzzyeg.awk

ref/eg/fuzzyeg.awk::

 BEGIN{
   olds  =Root "/oldness.dat";  printf "">olds; 
   talls =Root "/tallness.dat"; printf "">talls;
   as    =Root "/aness.dat";    printf "">as;
   bs    =Root "/bness.dat";    printf "">bs;
   cs    =Root "/cness.dat";    printf "">cs;
   abcs  =Root "/abcness.dat";  printf "">abcs;
   Amin=5; Amax=65;  Astep=5;
   Hmin=3; Hmax=7.5; Hstep=1/12;
   tallness();
   oldness();
   aness();
   bness();
   cness();
   abcness();
 }
 function tallness(  h) {
   for (h=Hmin; h<=Hmax; h+= Hstep) 
     printf("%5.2f  %5.2f\n",h, tall(h))>>talls;
 }
 function oldness(  a) {
   for (a=Amin; a<=Amax; a+= Astep) 
     printf("%5.2f  %5.2f\n",a, old(a))>>olds;
 }
 function aness( a,h) {
   for (a=Amin; a<=Amax; a+= Astep) 
           for (h=Hmin; h<=Hmax; h+= Hstep) 
                printf("%5.2f %5.2f %5.2f\n",a,h,a(a,h))>>as
 } 
 function bness( a,h) {
   for (a=Amin; a<=Amax; a+= Astep) 
           for (h=Hmin; h<=Hmax; h+= Hstep) 
                printf("%5.2f %5.2f %5.2f\n",a,h,b(a,h))>>bs
 }
 function cness( h) {
   for (h=Hmin; h<=Hmax; h+= Hstep) 
     printf("%5.2f %5.2f\n",h, c(h))>>cs;
 }
 function abcness( a,h) {
   for (a=Amin; a<=Amax; a+= Astep) 
           for (h=Hmin; h<=Hmax; h+= Hstep) 
                printf("%5.2f %5.2f %5.2f\n",a,h,abc(a,h))>>abcs
 }

args.awk

share/bin/args.awk::

AWK functions can take a variable number of arguments and the ones not seen in the argument call are unintialized. args places the initialize arguments into a list argv and sets argv[0] to the size of the list.

 function args(argv,a,b,c,d,e,f,g,h) {
   if (a) {push(argv,a)} else {return argv[0]};
   if (b) {push(argv,b)} else {return argv[0]};
   if (c) {push(argv,c)} else {return argv[0]};
   if (d) {push(argv,d)} else {return argv[0]};
   if (e) {push(argv,e)} else {return argv[0]};
   if (f) {push(argv,f)} else {return argv[0]};
   if (g) {push(argv,g)} else {return argv[0]};
   if (h) {push(argv,h)} else {return argv[0]};
   return argv[0];
 }

Of course, this call gets confused between uninitialized, zero, and empty string. So we define a variable Zero to model ``very low lack of belief''.

 BEGIN { Zero=10**-32;}
 function FALSE(x) {return (x==Zero)}
 function TRUE(x)  {if (x >Zero) { return 1} else {return Zero}}

fuzzyeg

ref/eg/fuzzyeg::

 var="$SCANT/ref/var"
 share="$SCANT/share/bin"
 gawk   -f $share/lib.awk \
        -f $share/zadeh.awk \
        -f $share/args.awk \
        -f fuzzyeg.awk \
        -f fuzzyegfunctions.awk\
        -v Root="$var"
 gnuplot<<-EOF
        set terminal png small color
        set nokey
        set size 0.5,0.5
        set yrange [-0.05:1.05]
        set ylabel "belief"
        set title "oldness"
        set xlabel "age"
        set output "$var/oldness.png"
        plot "$var/oldness.dat" with linesp
 EOF
 gnuplot<<-EOF
        set terminal png small color
        set nokey
        set size 0.5,0.5
        set yrange [-0.05:1.05]
        set ylabel "belief"
        set title "tallness"
        set xlabel "height"
        set output "$var/tallness.png"
        plot "$var/tallness.dat" with linesp
 EOF
 gnuplot<<-EOF
        set terminal png small color
        set nokey
        set size 0.5,0.5
        set ylabel "belief"
        set title "aness (old and tall)"
        set xlabel "age"
        set ylabel "height"
        set zlabel  "belief"
        set output "$var/aness.png"
        splot "$var/aness.dat" 
 EOF
 gnuplot<<-EOF
        set terminal png small color
        set nokey
        set size 0.5,0.5
        set ylabel "belief"
        set title "bness (old or tall)"
        set xlabel "age"
        set ylabel "height"
        set zlabel  "belief"
        set output "$var/bness.png"
        splot "$var/bness.dat" 
 EOF
 gnuplot<<-EOF
        set terminal png small color
        set nokey
        set size 0.5,0.5
        set yrange [-0.05:1.05]
        set ylabel "belief"
        set title "cness (not tallness)"
        set xlabel "height"
        set ylabel "belief"
        set output "$var/cness.png"
        plot "$var/cness.dat" with linesp
 EOF
 gnuplot<<-EOF
        set terminal png small color
        set nokey
        set size 0.5,0.5
        set ylabel "belief"
        set title "abcness (aness or bness or cness)"
        set xlabel "age"
        set ylabel "height"
        set zlabel  "belief"
        set output "$var/abcness.png"
        splot "$var/abcness.dat" 
 EOF
 chmod a+r $var/*.png

[TOC]

References

Brule85
Fuzzy Systems- A Tutorial (1985) by James Brule; http://www.austinlinks.com/Fuzzy/tutorial.html
FAQ
Fuzzy logic FAQ http://www-2.cs.cmu.edu/Groups/AI/html/faqs/ai/fuzzy/part1/faq-doc-2.html
Zadeh1973a
Outline of a New Approach to the Analysis of Complex Systems and Decision Processes (1973) by L. A. Zadeh; IEEE Transactions on Systems, Man and Cybernetics, number 1, January, pages 28-44.

[TOC]