    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: An Introduction to Fuzzy Logic</title>
        <link>http://accu.org/index.php/journals/998</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">CVu Journal Vol 12, #3 - May 2000 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="http://accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c77/">CVu</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c126/">123</a>
                    (22)
<br />

                                            <a href="http://accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c65/">Programming</a>
                    (488)
<br />

                                            <a href="http://accu.org/index.php/journals/c126-65/">Any of these categories</a>

                    -                        <a href="http://accu.org/index.php/journals/c126+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;An Introduction to Fuzzy Logic</h1>
<p><strong>Author:</strong>&nbsp;Administrator</p>
<p>
<strong>Date:</strong> 03 May 2000 13:15:36 +01:00 or Wed, 03 May 2000 13:15:36 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>One of the Cambridge colleges has a relationship with a college
in Japan, and as a result has a building partly owned by the
Japanese college. This building contains a very modern,
air-conditioned concert hall, which like many concert halls has
numerous lights. During the course of a concert, these lights were
frequently adjusted in a continuous manner, never suddenly jumping
from one level to another. Naturally I imagined a skilled operator
sitting behind a bank of variable resistors, but it turned out to
be computer-controlled - the computer would gently slide the
lighting into whatever configuration was requested at a small
touch-sensitive console. But the rate of change was never constant
- someone had obviously designed a complex mathematical model for
it. Or had they?</p>
<p>Fuzzy logic is a method of quickly designing classification
systems by capturing the intuition of the designer. It was made
popular in embedded control systems in Japan because
&quot;classification&quot; can be interpreted as &quot;deciding what to do next&quot;
(you classify the inputs into sets that correspond to best
actions), and it is also possible to have continuous output
variables (like &quot;rate of change&quot;). But fuzzy logic is not limited
to control systems - it can be used anywhere where there are rules
of the form &quot;if this condition holds then that output is preferred&quot;
and where it is unclear how to resolve conflicts of interest
between the rules.</p>
<p>Consider a single continuous input variable. Sometimes different
actions should be taken depending on whether it is large or small
(or near to certain values), and the traditional means of doing
this (apart from working out complex equations) is by thresholding,
ie.</p>
<pre class="programlisting">
void decide(int input) {
  if(input&lt;threshold1) action1();
  else if (input&lt;threshold2) action2();
  else action3();
}
</pre>
<p>The problem with this is that you have to decide where to put
the thresholds, and the system might &quot;change its mind&quot; too often
around those points; also it is difficult to reconcile with other
conditions on other inputs that might have conflicting interests.
In fuzzy logic, the code would look more like the following:</p>
<pre class="programlisting">
void decide(int input1, int input2) {
  int vote1=0, vote2=0, vote3=0;
  vote1 += functionA(input1);
  vote2 += functionB(input1);
  vote3 += functionC(input1);
  vote1 += functionD(input2);
  vote2 += functionE(input2);
  vote3 += functionF(input2);
  /* Other functions on other inputs go here */
  if(vote1&gt;vote2 &amp;&amp; vote1&gt;vote3) action1();
  else if(vote2&gt;vote3) action2();
  else action3();
}
</pre>
<p>Needless to say, it is a biased voting system - each function
determines how many &quot;votes&quot; a particular action should get given
the value of an input, and the action with the most votes wins. If
the system is to be prevented from &quot;changing its mind&quot; too rapidly,
the action previously taken could also be voted for (indeed,
previous actions can be included with the inputs and any amount of
logic done on them). In fuzzy logic theory, these &quot;votes&quot; are
normalised so that all the numbers are between 0 and 1, but in an
embedded system you would usually want to use integer arithmetic.
(By the way, I'm not using standard fuzzy logic terminology because
I think it's easier to think of as a voting system.) The functions
themselves can be anything (and can of course take more than one of
the inputs), but they are usually quite simple, for example:</p>
<pre class="programlisting">
int functionA(int i) {
  if (condition(i)) return 2;
  else return 0;
}
int functionB(int i) {
  int difference=i-midVal;
  if (difference&lt;0) difference=-difference;
  if (difference&gt;maxVote) return 0;
  else return maxVote-difference;
}
</pre>
<p>The first of these simply casts two votes if a certain condition
is true (rules can be weighted by changing the number of votes they
can cast). The second is a triangle function - it casts &quot;maxVote&quot;
votes if the input is exactly &quot;midVal&quot;, with a decreasing number of
votes for inputs &quot;around&quot; that value; the sharpness of decrease can
be adjusted by multiplying or dividing (or bit shifting)
&quot;difference&quot; by a constant. It is usually easier to specify things
this way than to choose thresholds, and other variations on it
exist, for example:</p>
<pre class="programlisting">
int functionC(int i) {
  if (i&gt;=minVal) return maxVote;
  else {
    int difference=minVal-i;
    if(difference&gt;maxVote) return 0;
    else return maxVote-difference;
  }
}
int functionD(int i) {
  int difference;
  if (i&gt;=minVal &amp;&amp; i&lt;=maxVal) return maxVote;
  else if(i&lt;minVal) difference=minVal-i;
  else difference=i-maxVal;
  if (difference&gt;maxVote) return 0;
  else return maxVote-difference;  
}
</pre>
<p><tt class="function">functionC</tt> is a &quot;fuzzy&quot; threshold (a
maximum number of votes are cast if the input exceeds the
threshold, but allowances are made for inputs just below it), and
functionD is a trapezoid with two thresholds. More complex
functions can occur, but there is rarely much point in going to the
trouble of complex floating-point equations like Gaussians.
Functions can of course overlap each other, so a particular input
value might get 30 votes for one action and 20 for another, but it
would help if there were no possible inputs that result in no votes
for anything.</p>
<p>Sometimes functions of more than one input will be needed, with
rules of the form &quot;input 1 is in this region and input 2 is in that
region&quot;. To do this as a single fuzzy function would be complex;
instead, fuzzy functions could be written for each input, and their
results combined, like someone saying &quot;I'll vote for this if both
my advisors say I should&quot;. Fuzzy logic usually accomplishes this by
taking the minimum of the &quot;advisors'&quot; recommended votes, thus:</p>
<pre class="programlisting">
int someFunc(int i1,int i2) {
  int votes1=advice1(i1), votes2=advice2(i2);
  return MIN(votes1,votes2);
}
</pre>
<p>The AND logic can be changed to OR logic by taking the maximum
instead of the minimum. This can be seen as a generalisation of
single-bit Boolean logic (where AND is minimum and OR is maximum)
to votes.</p>
<p>Negative votes are rarely used, since &quot;prefer not to do action
X&quot; can be re-written as &quot;prefer to do anything but action X&quot;. For
this and other reasons, you may need functions that can distribute
their votes across several actions; this can be achieved by keeping
all the votes in an array and letting the functions change it.</p>
<p>If the application simply tells the user what category the
inputs match, it may be useful to list a number of them with an
indication of how many votes were given to each. If the application
is supposed to be slightly non-deterministic, this can be achieved
by giving out a small number of random votes.</p>
<p>Continuous outputs are also possible. Suppose that the &quot;actions&quot;
in the above examples are not completely different, for
example:</p>
<pre class="programlisting">
void action1() { set_value(20); }
void action2() { set_value(35); }
void action3() { set_value(50); }
</pre>
<p>In this case, it might be sensible to set the value to something
in between the given points, depending on how many votes were cast
for each one. For example, to take the arithmetic mean:</p>
<pre class="programlisting">
int chooseValue(int n,int* values,int* votes) {
  int sum=0,totalVotes=0,i;
  for(i=0; i&lt;n; i++) {
    sum+=values[i]*votes[i];
    totalVotes+=votes[i];
  }
  return sum/totalVotes;
}
</pre>
<p>This explains why negative votes are avoided - strange things
can happen if the above has to deal with them. The type of integer
used should be sufficiently large to prevent overflow; implementing
a running mean instead will be subject to rounding error.</p>
<p>Another function that is sometimes used adds extra weight to the
first few votes a value gets, so the more votes it gets, the less
each one counts (by a simple ramp function). This can be thought of
as building a triangle around the value and cutting it off at the
height of the number of votes; its effect is to reduce the
dominance of values that tend to be voted for a lot, which may or
may not be desirable.</p>
<p>Another method, <tt class="literal">Product-Max</tt>, is to draw
appropriately-sized triangular distributions around the values
voted for and take the centroid of the resulting shape, where
overlaps are combined with OR rather than added. How this can be
implemented in an embeddable way is left as an exercise to the
reader!</p>
<p>The examples in this article are small, and can easily be done
without fuzzy logic. Fuzzy logic comes into its own when you have
perhaps hundreds of rules and have to find a compromise between all
of them. As well as control and classification systems, it could
conceivably be used in optimising algorithms for compromising
between different optimisations, typesetting and layout algorithms
for compromising between different constraints, and so on.</p>
<p>Fuzzy logic is probably not good enough for safety-critical
systems. The rules are designed intuitively, but intuition can
break down in more unusual cases. The Japanese concert hall had
this problem - apparently, giving a certain input from a certain
configuration causes the whole hall to be rapidly plunged into
darkness, and this can trap the unwary (as it did several times
during their 1998 concert). At least it wasn't a nuclear reactor
cooling system.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
