File: aggregate_output_sugeno.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 (152 lines) | stat: -rw-r--r-- 5,760 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
## 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 {Function File} {@var{fuzzy_output} =} aggregate_output_sugeno (@var{fis}, @var{rule_output})
##
## Given the:
## @itemize @bullet
## @item @var{fis.aggMethod:}
## the aggregation method for the given @var{fis}
## @item @var{rule_output:}
## a matrix of the singleton output of each (rule, FIS output) pair
## @end itemize
##
## Return:
## @itemize @bullet
## @item @var{fuzzy_output:}
## a vector of structures containing the aggregated output for each FIS output
## @end itemize
##
## @var{rule_output} is a 2 x (Q * M) matrix, where Q is the number of rules
## and M is the number of FIS output variables. Each column of @var{rule_output}
## gives the (location, height) pair of the singleton output for one
## (rule, FIS output) pair:
##
## @verbatim
##                     Q cols            Q cols                  Q cols 
##                ---------------   ---------------         ---------------
##                out_1 ... out_1   out_2 ... out_2   ...   out_M ... out_M
##     location [[                                                         ]
##       height  [                                                         ]]
## @end verbatim
##
## The return value @var{fuzzy_output} is a vector of M structures,
## each of which has an index i and a matrix of singletons that form the
## aggregated output for the ith FIS output variable.
## For each FIS output variable, the matrix of singletons is a 2 x L matrix
## where L is the number of distinct singleton locations in the fuzzy output
## for that FIS output variable. The first row gives the (distinct) locations,
## and the second gives the (non-zero) heights:
##
## @verbatim
##                singleton_1  singleton_2 ... singleton_L
##     location [[                                        ]
##       height  [                                        ]]
## @end verbatim
##
## Because aggregate_output_sugeno is called only by the private
## function evalfis_private, it does no error checking of the argument values.
## @end deftypefn

## Author:        L. Markowsky
## Keywords:      fuzzy-logic-toolkit fuzzy inference system fis
## Directory:     fuzzy-logic-toolkit/inst/private/
## Filename:      aggregate_output_sugeno.m
## Last-Modified: 26 Jul 2024

##----------------------------------------------------------------------

function fuzzy_output = aggregate_output_sugeno (fis, rule_output)

  fuzzy_output = [];
  num_outputs = columns (fis.output);
  num_rules = columns (fis.rule);

  ## For each FIS output, aggregate the slice of the rule_output matrix,
  ## then store the result as a structure in fuzzy_output.

  for i = 1 : num_outputs
    unagg_output = rule_output(:, (i-1)*num_rules+1 : i*num_rules);
    aggregated_output = aggregate_fis_output (fis.aggMethod, ...
                                              unagg_output);
    next_agg_output = struct ('index', i, ...
                              'aggregated_output', aggregated_output);
    if (i == 1)
      fuzzy_output = next_agg_output;
    else
      fuzzy_output = [fuzzy_output, next_agg_output];
    endif
  endfor
endfunction

##----------------------------------------------------------------------
## Function: aggregate_fis_output
## Purpose:  Aggregate the multiple singletons for one FIS output.
##----------------------------------------------------------------------

function mult_singletons = aggregate_fis_output (fis_aggmethod, ...
                                                 rule_output)

  ## Initialize output matrix (multiple_singletons).

  mult_singletons = sortrows (rule_output', 1);

  ## If adjacent rows represent singletons at the same location, then
  ## combine them using the FIS aggregation method.

  for i = 1 : rows (mult_singletons) - 1
    if (mult_singletons(i, 1) == mult_singletons(i+1, 1))
      switch (fis_aggmethod)
        case 'sum'
          mult_singletons(i + 1, 2) = mult_singletons(i, 2) + ...
                                      mult_singletons(i + 1, 2);
        otherwise
          mult_singletons(i + 1, 2) = str2func (fis_aggmethod) ...
                                      (mult_singletons(i, 2), ...
                                       mult_singletons(i + 1, 2));
      endswitch
      mult_singletons(i, 2) = 0;
    endif
  endfor

  ## Return the transpose of the matrix after removing 0-height
  ## singletons.

  mult_singletons = (remove_null_rows (mult_singletons))';
    
endfunction

##----------------------------------------------------------------------
## Function: remove_null_rows
## Purpose:  Return the argument without the rows with a 0 in the
##           second column.
##----------------------------------------------------------------------

function y = remove_null_rows (x)
  y = [];
  for i = 1 : rows (x)
    if (x(i, 2) != 0)
      if (isequal (y, []))
        y = x(i, :);
      else
        y = [y; x(i, :)];
      endif
    endif
  endfor
endfunction