|Fuzzy Logic Toolbox|
www.kxcad.net Home > CAE Software Index > MATLAB Index >
|On this page…|
Now, you are going to work through a similar tipping example, only we'll be building it using the graphical user interface (GUI) tools provided by Fuzzy Logic Toolbox. Although it is possible to use Fuzzy Logic Toolbox by working strictly from the command line, in general it is much easier to build a system graphically. There are five primary GUI tools for building, editing, and observing fuzzy inference systems in Fuzzy Logic Toolbox:
Fuzzy Inference System (FIS) Editor
Membership Function Editor
These GUIs are dynamically linked, in that changes you make to the FIS using one of them, can affect what you see on any of the other open GUIs. You can have any or all of them open for any given system.
In addition to these five primary GUIs, the toolbox includes the graphical ANFIS Editor GUI, which is used for building and analyzing Sugeno-type adaptive neural fuzzy inference systems. The ANFIS Editor GUI is discussed in Sugeno-Type Fuzzy Inference.
The FIS Editor handles the high-level issues for the system: How many input and output variables? What are their names? Fuzzy Logic Toolbox does not limit the number of inputs. However, the number of inputs may be limited by the available memory of your machine. If the number of inputs is too large, or the number of membership functions is too big, then it may also be difficult to analyze the FIS using the other GUI tools.
The Membership Function Editor is used to define the shapes of all the membership functions associated with each variable.
The Rule Editor is for editing the list of rules that defines the behavior of the system.
The Rule Viewer and the Surface Viewer are used for looking at, as opposed to editing, the FIS. They are strictly read-only tools. The Rule Viewer is a MATLAB based display of the fuzzy inference diagram shown at the end of the last section. Used as a diagnostic, it can show (for example) which rules are active, or how individual membership function shapes are influencing the results. The Surface Viewer is used to display the dependency of one of the outputs on any one or two of the inputs—that is, it generates and plots an output surface map for the system.
This section began with an illustration similar to the following one describing the main parts of a fuzzy inference system, only the next one shows how the three editors fit together. The two viewers examine the behavior of the entire system.
The five primary GUIs can all interact and exchange information. Any one of them can read and write both to the workspace and to a file (the read-only viewers can still exchange plots with the workspace and save them to a file). For any fuzzy inference system, any or all of these five GUIs may be open. If more than one of these editors is open for a single system, the various GUI windows are aware of the existence of the others, and, if necessary, updates related windows. Thus, if the names of the membership functions are changed using the Membership Function Editor, those changes are reflected in the rules shown in the Rule Editor. The editors for any number of different FIS systems may be open simultaneously. The FIS Editor, the Membership Function Editor, and the Rule Editor can all read and modify the FIS data, but the Rule Viewer and the Surface Viewer do not modify the FIS data in any way.
We'll start with a basic description of a two-input, one-output tipping problem (based on tipping practices in the U.S.).
Given a number between 0 and 10 that represents the quality of service at a restaurant (where 10 is excellent), and another number between 0 and 10 that represents the quality of the food at that restaurant (again, 10 is excellent), what should the tip be?
The starting point is to write down the three golden rules of tipping, based on years of personal experience in restaurants.
1. If the service is poor or the food is rancid, then tip is cheap.
2. If the service is good, then tip is average.
3. If the service is excellent or the food is delicious, then tip is generous.
Assume that an average tip is 15%, a generous tip is 25%, and a cheap tip is 5%.
Obviously the numbers and the shape of the curve are subject to local traditions, cultural bias, and so on, but the three rules are generally universal.
Now that you know the rules and have an idea of what the output should look like, begin working with the GUI tools to construct a fuzzy inference system for this decision process.
The following discussion tells you how to build a new fuzzy inference system from scratch. If you want to save time and follow along quickly, you can load the prebuilt system by typing
This command loads the FIS associated with the file tipper.fis (the .fis is implied) and launches the FIS Editor. However, if you load the prebuilt system, you will not build rules or construct membership functions.
The FIS Editor displays general information about a fuzzy inference system. There is a simple diagram at the top that shows the names of each input variable on the left, and those of each output variable on the right. The sample membership functions shown in the boxes are just icons and do not depict the actual shapes of the membership functions:
Below the diagram is the name of the system and the type of inference used. The default, Mamdani-type inference, is what is described so far and what you continue to use for this example. Another slightly different type of inference, called Sugeno-type inference, is also available. This method is explained in Sugeno-Type Fuzzy Inference.
Below the name of the fuzzy inference system, on the left side of the figure, are the pop-up menus that allow you to modify the various pieces of the inference process.
On the right side at the bottom of the figure is the area that displays the name of either an input or output variable, its associated membership function type, and its range.
The latter two fields are specified only after the membership functions have been.
Below that region are the Help and Close buttons that call up online help and close the window, respectively. At the bottom is a status line that relays information about the system.
To start this system from scratch, type
at the MATLAB prompt. The generic untitled FIS Editor opens, with one input, labeled input1, and one output, labeled output1. For this example, you construct a two-input, one output system. A second yellow box labeled input2 appears. The two inputs in your example are service and food. The one output is tip. You want to change the variable names to reflect these designations:
Select Edit > Add variable > Input.
Click the yellow box input1. This box is highlighted with a red outline.
Edit the Name field from input1 to service, and press Enter.
Click the yellow box input2. This box is highlighted with a red outline.
Edit the Name field from input2 to food, and press Enter.
Click the blue box output1.
Edit the Name field from output1 to tip, and press Enter.
Select File > Export > To Workspace.
Enter the Workspace variable name tipper, and click OK.
The diagram is updated to reflect the new names of the input and output variables. There is now a new variable in the workspace called tipper that contains all the information about this system. By saving to the workspace with a new name, you also rename the entire system. Your window looks something like the following diagram.
Leave the inference options in the lower left in their default positions for now. You have entered all the information you need for this particular GUI. Next, define the membership functions associated with each of the variables. To do this, open the Membership Function Editor.
You can open the Membership Function Editor in one of three ways:
Within the FIS Editor window, select Edit > Membership Functions..
Within the FIS Editor window, double-click the blue icon called tip.
At the command line, type mfedit.
The Membership Function Editor shares some features with the FIS Editor. In fact, all of the five basic GUI tools have similar menu options, status lines, and Help and Close buttons.
The Membership Function Editor is the tool that lets you display and edit all of the membership functions associated with all of the input and output variables for the entire fuzzy inference system.
When you open the Membership Function Editor to work on a fuzzy inference system that does not already exist in the workspace, there are no membership functions associated with the variables that you defined with the FIS Editor.
On the upper-left side of the graph area in the Membership Function Editor is a "Variable Palette" that lets you set the membership functions for a given variable.
To set up the membership functions associated with an input or an output variable for the FIS, select a FIS variable in this region by clicking it.
Next select the Edit pull-down menu, and choose Add MFs .. A new window appears, which allows you to select both the membership function type and the number of membership functions associated with the selected variable. In the lower-right corner of the window are the controls that let you change the name, type, and parameters (shape), of the membership function, after it is selected.
The membership functions from the current variable are displayed in the main graph. These membership functions can be manipulated in two ways. You can first use the mouse to select a particular membership function associated with a given variable quality, (such as poor, for the variable, service), and then drag the membership function from side to side. This action affects the mathematical description of the quality associated with that membership function for a given variable. The selected membership function can also be tagged for dilation or contraction by clicking on the small square drag points on the membership function, and then dragging the function with the mouse toward the outside, for dilation, or toward the inside, for contraction. This action changes the parameters associated with that membership function.
Below the Variable Palette is some information about the type and name of the current variable. There is a text field in this region that lets you change the limits of the current variable's range (universe of discourse) and another that lets you set the limits of the current plot (which has no real effect on the system).
The process of specifying the input membership functions for this two input tipper problem is as follows:
Select the input variable, service, by double-clicking on it. Set both the Range and the Display Range to the vector [0 10].
Select Remove All MFs from the Edit menu. This removes all existing Membership Functions from the Membership Function Editor.
Select Add MFs. from the Edit menu. The following window opens:
Use the tab to choose gaussmf for MF Type and 3 for Number of MFs. This choice adds three Gaussian curves to the input variable service.
Click once on the curve with the left-most hump. Change the name of the curve to poor. To adjust the shape of the membership function, either use the mouse, as described previously, or type in a desired parameter change, and then click on the membership function. The desired parameter Params listing for this curve is [1.5 0]. The two inputs of Params represent the standard deviation and center for the Gaussian curve.
Name the curve with the middle hump, good. The desired parameter listing for this curve is [1.5 5]. Name the curve with the rightmost hump, excellent. The desired parameter for this curve is [1.5 10].
Select the input variable, food, by clicking on it. Set both the Range and the Display Range to the vector [0 10].
Select Add MFs. from the Edit menu and add two trapmf curves to the input variable food.
Click once directly on the curve with the leftmost trapezoid. Change the name of the curve to rancid. To adjust the shape of the membership function, either use the mouse, as described previously, or type in a desired parameter change, and then click on the membership function. The default parameter listing for this curve is [0 0 1 3].
Name the curve with the rightmost trapezoid, delicious, and reset the associated parameters if desired.
Next, create the membership functions for the output variable, tip. To create the output variable membership functions, use the Variable Palette on the left, selecting the output variable, tip. The inputs ranged from 0 to 10, but the output scale is going to be a tip between 5% and 25%.
Use triangular membership function types for the output. First, set the Range (and the Display Range) to [0 30], to cover the output range. Initially, the cheap membership function has the parameters [0 5 10], the average membership function are[10 15 20], and the generous membership function are [20 25 30]. Your system should look similar to the following figure.
Now that the variables have been named and the membership functions have appropriate shapes and names, you can enter the rules. To call up the Rule Editor, go to the Edit menu and select Rules, or type ruleedit at the command line.
Constructing rules using the graphical Rule Editor interface is fairly self evident. Based on the descriptions of the input and output variables defined with the FIS Editor, the Rule Editor allows you to construct the rule statements automatically, From the GUI, you can:
Create rules by selecting an item in each input and output variable box, and one Connection item and clicking Add Rule. You can choose none as one of the variable qualities to exclude that variable from a given rule and choose not under any variable name to negate the associated quality.
Delete a rule by selecting the rule and clicking Delete Rule.
Edit a rule by changing the selection in the variable box and clicking Change Rule.
Specify weight to a rule by typing in a desired number between 0 and 1 in Weight. If you do not specify the weight, it is assumed to be unity (1).
Similar to those in the FIS Editor and the Membership Function Editor, the Rule Editor has the menu bar and the status line. The menu items allow you to open, close, save and edit a fuzzy system using the five basic GUI tools. From the menu, you can also:
Set the format for the display by selecting Options > Format.
Set the language by selecting Options > Language.
You can access information about the Rule Editor by clicking Help and close the GUI using Close.
To insert the first rule in the Rule Editor, select the following:
poor under the variable service
rancid under the variable food
The or radio button, in the Connection block
cheap, under the output variable, tip.
The resulting rule is
|1. If (service is poor) or (food is rancid) then (tip is cheap) (1)|
The numbers in the parentheses represent weights.
Follow a similar procedure to insert the second and third rules in the Rule Editor to get
|1. If (service is poor) or (food is rancid) then (tip is cheap) (1)|
|2. If (service is good) then (tip is average) (1)|
|3. If (service is excellent) or (food is delicious) then (tip is generous) (1)|
To change a rule, first click on the rule to be changed. Next make the desired changes to that rule, and then click Change rule. For example, to change the first rule to
|1. If (service not poor) or (food not rancid) then (tip is not cheap) (1)|
Select the not check box under each variable, and then click Change rule.
The Format pop-up menu from the Options menu indicates that you are looking at the verbose form of the rules. Try changing it to symbolic. You will see
|1. (service==poor) | (food==rancid) => (tip=cheap) (1)|
|2. (service==good) => (tip=average) (1)|
|3. (service==excellent) | (food==delicious) => (tip=generous) (1)|
There is not much difference in the display really, but it is slightly more language neutral, because it does not depend on terms like if and then. If you change the format to indexed, you see an extremely compressed version of the rules.
|1 1, 1 (1) : 2|
|2 0, 2 (1) : 1|
|3 2, 3 (1) : 2|
This is the version of the rules that the machine deals with.
The first column in this structure corresponds to the input variables.
The second column corresponds to the output variable.
The third column displays the weight applied to each rule.
The fourth column is shorthand that indicates whether this is an OR (2) rule or an AND (1) rule.
The numbers in the first two columns refer to the index number of the membership function.
A literal interpretation of rule 1 is "If input 1 is MF1 (the first membership function associated with input 1) or if input 2 is MF1, then output 1 should be MF1 (the first membership function associated with output 1) with the weight 1.
The symbolic format does not consider the terms, if, then, and so on. The indexed format doesn't even bother with the names of your variables. Obviously the functionality of your system doesn't depend on how well you have named your variables and membership functions. The whole point of naming variables descriptively is, as always, making the system easier for you to interpret. Thus, unless you have some special purpose in mind, it is probably be easier for you to continue with the verbose format.
At this point, the fuzzy inference system has been completely defined, in that the variables, membership functions, and the rules necessary to calculate tips are in place. Now, look at the fuzzy inference diagram presented at the end of the previous section and verify that everything is behaving the way you think it should. You can use the Rule Viewer, the next of the GUI tools we'll look at. From the View menu, select Rules.
The Rule Viewer displays a roadmap of the whole fuzzy inference process. It is based on the fuzzy inference diagram described in the previous section. You see a single figure window with 10 plots nested in it. The three plots across the top of the figure represent the antecedent and consequent of the first rule. Each rule is a row of plots, and each column is a variable. The rule numbers are displayed on the left of each row. You can click on a rule number to view the rule in the status line.
The first two columns of plots (the six yellow plots) show the membership functions referenced by the antecedent, or the if-part of each rule.
The third column of plots (the three blue plots) shows the membership functions referenced by the consequent, or the then-part of each rule.
Notice that under food, there is a plot which is blank. This corresponds to the characterization of none for the variable food in the second rule.
The fourth plot in the third column of plots represents the aggregate weighted decision for the given inference system.
This decision will depend on the input values for the system. The defuzzified output is displayed as a bold vertical line on this plot.
The variables and their current values are displayed on top of the columns. In the lower left, there is a text field Input in which you can enter specific input values. For the two-input system, you will enter an input vector, [9 8], for example, and then pressEnter. You can also adjust these input values by clicking on any of the three plots for each input. This will move the red index line horizontally, to the point where you have clicked. Alternatively, you can also click and drag this line in order to change the input values. When you release the line, (or after manually specifying the input), a new calculation is performed, and you can see the whole fuzzy inference process take place:
Where the index line representing service crosses the membership function line "service is poor" in the upper-left plot determines the degree to which rule one is activated.
A yellow patch of color under the actual membership function curve is used to make the fuzzy membership value visually apparent.
Each of the characterizations of each of the variables is specified with respect to the input index line in this manner. If you follow rule 1 across the top of the diagram, you can see the consequent "tip is cheap" has been truncated to exactly the same degree as the (composite) antecedent—this is the implication process in action. The aggregation occurs down the third column, and the resultant aggregate plot is shown in the single plot appearing in the lower right corner of the plot field. The defuzzified output value is shown by the thick line passing through the aggregate fuzzy set.
You can shift the plots using left, right, down, and up. The menu items allow you to save, open, or edit a fuzzy system using any of the five basic GUI tools.
The Rule Viewer allows you to interpret the entire fuzzy inference process at once. The Rule Viewer also shows how the shape of certain membership functions influences the overall result. Because it plots every part of every rule, it can become unwieldy for particularly large systems, but, for a relatively small number of inputs and outputs, it performs well (depending on how much screen space you devote to it) with up to 30 rules and as many as 6 or 7 variables.
The Rule Viewer shows one calculation at a time and in great detail. In this sense, it presents a sort of micro view of the fuzzy inference system. If you want to see the entire output surface of your system—the entire span of the output set based on the entire span of the input set— you need to open up the Surface Viewer. This viewer is the last of our five basic GUI tools in Fuzzy Logic Toolbox, and you open it by selecting Surface from the View menu.
Upon opening the Surface Viewer, you see a three-dimensional curve that represents the mapping from food and service quality to tip amount. Because this curve represents a two-input one-output case, you can see the entire mapping in one plot. When we move beyond three dimensions overall, we start to encounter trouble displaying the results.
Accordingly, the Surface Viewer is equipped with drop-down menus X (input):, Y (input): and Z (output): that let you select any two inputs and any one output for plotting. Below these menus are two input fields X grids: and Y grids: that let you specify how many x-axis and y-axis grid lines you want to include. This capability allows you to keep the calculation time reasonable for complex problems.
Clicking Evaluate initiates the calculation, and the plot is generated after the calculation is complete. To change the x-axis or y-axis grid after the surface is in view, change the appropriate input field, and press Enter. The surface plot is updated to reflect the new grid settings.
The Surface Viewer has a special capability that is very helpful in cases with two (or more) inputs and one output: you can grab the axes, using the mouse and reposition them to get a different three-dimensional view on the data.
The Ref. Input field is used in situations when there are more inputs required by the system than the surface is mapping. You can edit this field to explicitly set inputs not specified in the surface plot.
Suppose you have a four-input one-output system and would like to see the output surface. The Surface Viewer can generate a three-dimensional output surface where any two of the inputs vary, but two of the inputs must be held constant because computer monitors cannot display a five-dimensional shape. In such a case, the input is a four-dimensional vector with NaNs holding the place of the varying inputs while numerical values indicates those values that remain fixed. A NaN is the IEEE symbol for not a number.
The menu items allow you to open, close, save and edit a fuzzy system using the five basic GUI tools. You can access information about the Surface Viewer by clicking Help and close the GUI using Close.
This concludes the quick walk-through of each of the main GUI tools. For the tipping problem, the output of the fuzzy system matches your original idea of the shape of the fuzzy mapping from service to tip fairly well. In hindsight, you might say, "Why bother? I could have just drawn a quick lookup table and been done an hour ago!" However, if you are interested in solving an entire class of similar decision-making problems, fuzzy logic may provide an appropriate tool for the solution, given its ease with which a system can be quickly modified.
When you save a fuzzy system to a file, you are saving an ASCII text FIS file representation of that system with the file suffix .fis. This text file can be edited and modified and is simple to understand. When you save your fuzzy system to the MATLAB workspace, you are creating a variable (whose name you choose) that acts as a MATLAB structure for the FIS system. FIS files and FIS structures represent the same system.
Note If you do not save your FIS to a file, but only save it to the MATLAB workspace, you are not able to recover it for use in a new MATLAB session.
If you want to include customized functions as part of your use of Fuzzy Logic Toolbox, you must follow a few guidelines. You may substitute customized functions for the AND, OR, aggregation, and defuzzification methods, provided your customized functions work in a similar way to max, min, or prod in MATLAB. That is, they must be able to operate down the columns of a matrix.
In MATLAB, for a matrix x, min(x) returns a row vector containing the minimum element from each column. For N-D arrays, min(x) operates along the first non-singleton dimension. The function min(x,y), on the other hand, returns an array the same size as x and y populated with the smallest elements from x or y. Either one can be a scalar. Functions such as max, prod, and mean operate in a similar manner.
In Fuzzy Logic Toolbox, the implication method performs an element by element matrix operation, similar to the min(x,y) function in MATLAB, as in
a=[1 2; 3 4]; b=[2 2; 2 2]; min(a,b) ans = 1 2 2 2
After you have defined your custom function using the procedure described in the next section, use the FIS Editor to substitute your custom function for a standard function using the following procedure:
Open the FIS Editor by typing fuzzy at the command line prompt.
In the lower-left panel, locate the method you want to replace.
In the pull-down menu, select Custom. A dialog box appears.
Enter the name of your custom function, and click OK.
Your custom function then replaces the standard function in all subsequent operations.
You can create your own membership functions using an M-file. The values these functions can take must be between 0 and 1. There is a limitation on customized membership functions in that they cannot use more than 16 parameters.
To define a custom membership function named custmf:
Create an M-file for a function, custmf.m, that takes values between 0 and 1, and depends on 16 parameters at most.
Choose the Add Custom MF item in the Edit menu on the Membership Function Editor GUI.
Enter your custom membership function M-file name, custmf, in the M-file function name text box.
Enter the vector of parameters you want to use to parameterize your customized membership function in the text box next to Parameter list.
Give the custom membership function a name different from any other membership function name you use in your FIS.
The following sample code for a custom membership function, testmf1, depends on eight parameters between 0 and 10.
function out = testmf1(x, params) for i=1:length(x) if x(i)<params(1) y(i)=params(1); elseif x(i)<params(2) y(i)=params(2); elseif x(i)<params(3) y(i)=params(3); elseif x(i)<params(4) y(i)=params(4); elseif x(i)<params(5) y(i)=params(5); elseif x(i)<params(6) y(i)=params(6); elseif x(i)<params(7) y(i)=params(7); elseif x(i)<params(8) y(i)=params(8); else y(i)=0; end end out=.1*y';
You can try naming this file testmf1.m and loading it into the Membership Function Editor using the parameters of your choice.
|Fuzzy Inference Systems||Working from the Command Line|