File: mamdani_tip_demo.m

package info (click to toggle)
octave-fuzzy-logic-toolkit 0.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,024 kB
  • sloc: makefile: 147
file content (120 lines) | stat: -rw-r--r-- 4,107 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
## Copyright (C) 2011-2025 L. Markowsky <lmarkowsky@gmail.com>
##
## This file is part of the fuzzy-logic-toolkit.
##
## The fuzzy-logic-toolkit is free software; you can redistribute it
## and/or modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 3 of
## the License, or (at your option) any later version.
##
## The fuzzy-logic-toolkit is distributed in the hope that it will be
## useful, but WITHOUT ANY WARRANTY; without even the implied warranty
## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with the fuzzy-logic-toolkit; see the file COPYING.  If not,
## see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Script File} {} mamdani_tip_demo
## Demonstrate the use of the Octave Fuzzy Logic Toolkit to read and evaluate a
## Mamdani-type FIS stored in a file.
##
## The demo:
## @itemize @bullet
## @item
## reads the FIS structure from a file
## @item
## plots the input and output membership functions
## @item
## plots each of the two FIS outputs as a function of the inputs
## @item
## plots the output of the 4 individual rules for (Food-Quality, Service) = (4, 6)
## @item
## plots the aggregated fuzzy output and the crisp output for
## (Food-Quality, Service) = (4, 6)
## @item
## displays the FIS rules in symbolic format in the Octave window
## @end itemize
##
## @seealso{cubic_approx_demo, heart_disease_demo_1, heart_disease_demo_2, investment_portfolio_demo, linear_tip_demo, sugeno_tip_demo}
## @end deftypefn

## Author:        L. Markowsky
## Keywords:      fuzzy-logic-toolkit fuzzy tests demos
## Note:          This example is based on an assignment written by
##                Dr. Bruce Segee (University of Maine Dept. of ECE).
## Directory:     fuzzy-logic-toolkit/inst
## Filename:      mamdani_tip_demo.m
## Last-Modified: 4 Jun 2024

## Read the FIS structure from a file.
fis = readfis ('mamdani_tip_calculator.fis');

## Plot the input and output membership functions.
plotmf (fis, 'input', 1);
plotmf (fis, 'input', 2);
plotmf (fis, 'output', 1);
plotmf (fis, 'output', 2);

## Plot the Tip and Check + Tip as functions of Food-Quality
## and Service.
gensurf (fis, [1 2], 1);
gensurf (fis, [1 2], 2);

## Calculate the Tip and Check + Tip using
## (Food-Quality, Service) = (4, 6).
[output, rule_input, rule_output, fuzzy_output] = ...
  evalfis ([4 6], fis, 1001);

## Plot the first output (Tip) of the individual fuzzy rules
## on one set of axes.
x_axis = linspace (fis.output(1).range(1), ...
                   fis.output(1).range(2), 1001);
colors = ['r' 'b' 'm' 'g'];
figure ('NumberTitle', 'off', 'Name', ...
        'Output of Fuzzy Rules 1-4 for Input = (4, 6)');

for i = 1 : 4
    y_label = [colors(i) ";Rule " num2str(i) ";"];
    plot (x_axis, rule_output(:,i), y_label, 'LineWidth', 2);
    hold on;
endfor

ylim ([-0.1, 1.1]);
xlabel ('Tip', 'FontWeight', 'bold');
grid;
hold;

## Plot the first aggregated fuzzy output and the first crisp output
## (Tip) on one set of axes.
figure('NumberTitle', 'off', 'Name', ...
       'Aggregation and Defuzzification for Input = (4, 6)');
plot (x_axis, fuzzy_output(:, 1), "b;Aggregated Fuzzy Output;", ...
      'LineWidth', 2);
hold on;
crisp_output = evalmf(x_axis, output(1), 'constant');
y_label = ["r;Crisp Output = " num2str(output(1)) "%;"];
plot (x_axis, crisp_output, y_label, 'LineWidth', 2);
ylim ([-0.1, 1.1]);
xlabel ('Tip', 'FontWeight', 'bold');
grid;
hold;

## Show the rules in symbolic format.
puts ("\nMamdani Tip Calculator Rules:\n\n");
showrule (fis, 1:columns(fis.rule), 'symbolic');

%!test
%! fis = readfis ('mamdani_tip_calculator.fis');
%! food_service = [1 1; 5 5; 10 10; 4 6; 6 4; 7 4];
%! tip = evalfis (food_service, fis, 1001);
%! expected_result = ...
%!   [10.0000    1.1000
%!    15.0000    1.1500
%!    20.0000    1.2000
%!    15.0000    1.1500
%!    15.0000    1.1500
%!    16.4708    1.1647];
%! assert(tip, expected_result, 1e-4);