# Spatial Decision Support Systems/Fuzzy Controller/Fuzzy Sets
## Definition: Fuzzy sets

The input variables in a fuzzy control system are in general mapped by sets of membership functions similar to this, known as "fuzzy sets". The process of converting a crisp input value to a fuzzy value is called "fuzzification".
A control system may also have various types of switch, or "ON-OFF", inputs along with its analog inputs, and such switch inputs of course will always have a truth value equal to either 1 or 0, but the scheme can deal with them as simplified fuzzy functions that happen to be either one value or another (see Wikipedia:fuzzy set)

### Membership Functions

Given "mappings" of input variables into membership functions and truth values, the microcontroller then makes decisions for what action to take, based on a set of "rules", each of the form:
In this example, the two input variables are "temperature" and "humidity" that have values defined as fuzzy sets. The output variable, "mosquito abundance" is also defined by a fuzzy set that can have values like "low" or "medium" etc.
In general a membership function is mapping from a domain $\Omega$ into the real number between 0 and 1 (i.e. the interval $[0,1]\subset \mathbb {R}$)
    $\mu :\Omega \longrightarrow [0,1]$
For the temperature example the domain can be defined as the set of real numbers $\Omega :=\mathbb {R}$, so that the membership function $\mu _{temp}:\Omega \rightarrow [0,1]$ could take all temperatures in degrees Celsius as input variable.
    $\mu _{temp}:\mathbb {R} \longrightarrow [0,1]\qquad x\mapsto {\frac {1}{1+{\frac {(x-m)^{2}}{s}}}}$
Other type of membership functions are sigmoid functions like: $\mu _{a,\delta }(x)={\begin{cases}0&x\leq a-\delta \\2({\frac {x-a+\delta }{2\delta }})^{2}&a-\delta <x\leq a\\1-2({\frac {a-x+\delta }{2\delta }})^{2}&a<x\leq a+\delta \\1&x>a+\delta \end{cases}}$ The S-curve (sigmoid function) expresses an increasing truth of the property in the value range [0,1] for an increasing value $x\in \Omega$. The letter S is used because of the shape of the graph of $\mu$. Depending on the application, a decreasing membership can be expressed by a corresponding Z-curve: $Z_{a,\delta }(x):=1-S_{a,\delta }(x)$

### Learning Tasks

* Install the OpenSource Software Geogebra
* Create two value sliders for $m\in \mathbb {R}$ and $s>0$.
* Enter the following membership function with the input bar of geogebra:
* change the value sliders for $m\in \mathbb {R}$ and $s>0$ and explore the meaning of the variables $m$ and $s$.
* Analyse the following mappings with geogebra and check if these functions can be used as membership functions in Fuzzy Logic with $x\in \mathbb {R} \qquad$:
    $\mu _{1}(x)=arctan(x)$ Geogebra: mu_1(x)=atan(x)
    $\mu _{2}(x)={\frac {arctan(x)}{2}}$ Geogebra: mu_2(x)=atan(x)/2
    $\mu _{3}(x)={\frac {arctan(x)+1}{2}}$ Geogebra: mu_3(x)=(atan(x)+1)/2

### Rules applied on fuzzy values

This rule by itself is very puzzling since it looks like it could be used without bothering with fuzzy logic, but remember that the decision is based on a set of rules:
* All the rules that apply are invoked, using the membership functions and truth values obtained from the inputs, to determine the result of the rule.
* This result in turn will be mapped into a membership function and truth value controlling the output variable.
* These results are combined to give a specific ("crisp") answer, the actual brake pressure, a procedure known as "defuzzification".
This combination of fuzzy operations and rule-based "inference" describes a "fuzzy expert system".
Traditional control systems are based on mathematical models in which the control system is described using one or more differential equations that define the system response to its inputs. Such systems are often implemented as "PID controllers" (proportional-integral-derivative controllers). They are the products of decades of development and theoretical analysis, and are highly effective.
If PID and other traditional control systems are so well-developed, why bother with fuzzy control? It has some advantages. In many cases, the mathematical model of the control process may not exist, or may be too "expensive" in terms of computer processing power and memory, and a system based on empirical rules may be more effective.
Furthermore, fuzzy logic is well suited to low-cost implementations based on cheap sensors, low-resolution analog-to-digital converters, and 4-bit or 8-bit one-chip microcontroller chips. Such systems can be easily upgraded by adding new rules to improve performance or add new features. In many cases, fuzzy control can be used to improve existing traditional controller systems by adding an extra layer of intelligence to the current control method.

### Fuzzy control in detail

Fuzzy controllers are very simple conceptually. They consist of an input stage, a processing stage, and an output stage. The input stage maps sensor or other inputs, such as switches, thumbwheels, and so on, to the appropriate membership functions and truth values. The processing stage invokes each appropriate rule and generates a result for each, then combines the results of the rules. Finally, the output stage converts the combined result back into a specific control output value.
The most common shape of membership functions is triangular, although trapezoidal and bell curves are also used, but the shape is generally less important than the number of curves and their placement. From three to seven curves are generally appropriate to cover the required range of an input value, or the "universe of discourse" in fuzzy jargon.
As discussed earlier, the processing stage is based on a collection of logic rules in the form of IF-THEN statements, where the IF part is called the "antecedent" and the THEN part is called the "consequent". Typical fuzzy control systems have dozens of rules.
Consider a rule for a thermostat:
This rule uses the truth value of the "temperature" input, which is some truth value of "cold", to generate a result in the fuzzy set for the "heater" output, which is some value of "high". This result is used with the results of other rules to finally generate the crisp composite output. Obviously, the greater the truth value of "cold", the higher the truth value of "high", though this does not necessarily mean that the output itself will be set to "high" since this is only one rule among many. In some cases, the membership functions can be modified by "hedges" that are equivalent to adverbs. Common hedges include "about", "near", "close to", "approximately", "very", "slightly", "too", "extremely", and "somewhat". These operations may have precise definitions, though the definitions can vary considerably between different implementations. "Very", for one example, squares membership functions; since the membership values are always less than 1, this narrows the membership function. "Extremely" cubes the values to give greater narrowing, while "somewhat" broadens the function by taking the square root.
In practice, the fuzzy rule sets usually have several antecedents that are combined using fuzzy operators, such as AND, OR, and NOT, though again the definitions tend to vary: AND, in one popular definition, simply uses the minimum weight of all the antecedents, while OR uses the maximum value. There is also a NOT operator that subtracts a membership function from 1 to give the "complementary" function.
There are several ways to define the result of a rule, but one of the most common and simplest is the "max-min" inference method, in which the output membership function is given the truth value generated by the premise.
Rules can be solved in parallel in hardware, or sequentially in software. The results of all the rules that have fired are "defuzzified" to a crisp value by one of several methods. There are dozens, in theory, each with various advantages or drawbacks.
The "centroid" method is very popular, in which the "center of mass" of the result provides the crisp value. Another approach is the "height" method, which takes the value of the biggest contributor. The centroid method favors the rule with the output of greatest area, while the height method obviously favors the rule with the greatest output value.
The diagram below demonstrates max-min inferencing and centroid defuzzification for a system with input variables "x", "y", and "z" and an output variable "n". Note that "mu" is standard fuzzy-logic nomenclature for "truth value":
Notice how each rule provides a result as a truth value of a particular membership function for the output variable. In centroid defuzzification the values are OR'd, that is, the maximum value is used and values are not added, and the results are then combined using a centroid calculation.
Fuzzy control system design is based on empirical methods, basically a methodical approach to trial-and-error. The general process is as follows:
* Document the system's operational specifications and inputs and outputs.
* Document the fuzzy sets for the inputs.
* Document the rule set.
* Determine the defuzzification method.
* Run through test suite to validate system, adjust details as required.
* Complete document and release to production.
As a general example, consider the design of a fuzzy controller for a steam turbine. The block diagram of this control system appears as follows:
The input and output variables map into the following fuzzy set:
—where:
The rule set includes such rules as:
In practice, the controller accepts the inputs and maps them into their membership functions and truth values. These mappings are then fed into the rules. If the rule specifies an AND relationship between the mappings of the two input variables, as the examples above do, the minimum of the two is used as the combined truth value; if an OR is specified, the maximum is used. The appropriate output state is selected and assigned a membership value at the truth level of the premise. The truth values are then defuzzified. For an example, assume the temperature is in the "cool" state, and the pressure is in the "low" and "ok" states. The pressure values ensure that only rules 2 and 3 fire:
The two outputs are then defuzzified through centroid defuzzification:
The output value will adjust the throttle and then the control cycle will begin again to generate the next value .

### Building a fuzzy controller

Consider implementing with a microcontroller chip a simple feedback controller:
A fuzzy set is defined for the input error variable "e", and the derived change in error, "delta", as well as the "output", as follows:
If the error ranges from -1 to +1, with the analog-to-digital converter used having a resolution of 0.25, then the input variable's fuzzy set (which, in this case, also applies to the output variable) can be described very simply as a table, with the error / delta / output values in the top row and the truth values for each membership function arranged in rows beneath:
Suppose this fuzzy system has the following rule base:
These rules are typical for control applications in that the antecedents consist of the logical combination of the error and error-delta signals, while the consequent is a control command output. The rule outputs can be defuzzified using a discrete centroid computation:
Now, suppose that at a given time we have:
Then this gives:
Plugging this into rule 1 gives:
-- where:
* mu(1): Truth value of the result membership function for rule 1. In terms of a centroid calculation, this is the "mass" of this result for this discrete case.
* output(1): Value (for rule 1) where the result membership function (ZE) is maximum over the output variable fuzzy set range. That is, in terms of a centroid calculation, the location of the "center of mass" for this individual result. This value is independent of the value of "mu". It simply identifies the location of ZE along the output range.
The other rules give:
The centroid computation yields:
If you have problems figuring out the centroid equation, remember that a centroid is defined by summing all the moments (location times mass) around the center of gravity and equating the sum to zero. So if $X_{0}$ is the center of gravity, $X_{i}$ is the location of each mass, and $M_{i}$ is each mass, this gives:
In our example, the values of mu correspond to the masses, and the values of X to location of the masses
(mu, however, only 'corresponds to the masses' if the initial 'mass' of the output functions are all the same/equivalent. If they are not the same, i.e. some are narrow triangles, while others maybe wide trapizoids or shouldered triangles, then the mass or area of the output function must be known or calculated. It is this mass that is then scaled by mu and multiplied by its location X_i).
This system can be implemented on a standard microprocessor, but dedicated fuzzy chips are now available. For example, Adaptive Logic INC of San Jose, California, sells a "fuzzy chip", the AL220, that can accept four analog inputs and generate four analog outputs. A block diagram of the chip is shown below: