File: setfis.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 (271 lines) | stat: -rw-r--r-- 10,757 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
## 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{fis} =} setfis (@var{fis}, @var{property}, @var{property_value})
## @deftypefnx {Function File} {@var{fis} =} setfis (@var{fis}, @var{in_or_out}, @var{var_index}, @var{var_property}, @var{var_property_value})
## @deftypefnx {Function File} {@var{fis} =} setfis (@var{fis}, @var{in_or_out}, @var{var_index}, @var{mf}, @var{mf_index}, @var{mf_property}, @var{mf_property_value})
##
## Set a property (field) value of an FIS structure and return the
## updated FIS.
##
## There are three forms of setfis:
##
## @multitable @columnfractions .20 .70
## @headitem Number of Arguments @tab Action Taken
## @item 3
## @tab  Set a property of the FIS structure. The properties that may
##       be set are: name, type, andmethod, ormethod, impmethod,
##       addmethod, defuzzmethod, and version.
## @item 5
## @tab  Set a property of an input or output variable of the FIS
##       structure. The properties that may be set are: name and range.
## @item 7
## @tab  Set a property of a membership function. The properties that
##       may be set are: name, type, and params.
## @end multitable
## @sp 1
## The types/values of the arguments are expected to be:
##
## @multitable @columnfractions .20 .70
## @headitem Argument @tab Expected Type or Value
## @item @var{fis}
## @tab  an FIS structure
## @item @var{property}
## @tab  a string; one of 'name', 'type', 'andmethod',
##       'ormethod', 'impmethod', 'addmethod', 
##       'defuzzmethod', and 'version' (case-insensitive)
## @item @var{property_value}
## @tab  a number (if property is 'version'); a string (otherwise)
## @item @var{in_or_out}
## @tab  either 'input' or 'output' (case-insensitive)
## @item @var{var_index}
## @tab  a valid integer index of an input or output FIS variable
## @item @var{var_property}
## @tab  a string; either 'name' or 'range'
## @item @var{var_property_value}
## @tab  a string (if var_property is 'name') or 
##       a vector range (if var_property is 'range')
## @item @var{mf}
## @tab  the string 'mf'
## @item @var{mf_index}
## @tab  a valid integer index of a membership function 
## @item @var{mf_property}
## @tab  a string; one of 'name', 'type', or 'params'
## @item @var{mf_property_value}
## @tab  a string (if mf_property is 'name' or 'type');
##       an array (if mf_property is 'params')
## @end multitable
## @sp 1
## Note that all of the strings representing properties above are case
## insensitive.
##
## @seealso{newfis, getfis, showfis}
## @end deftypefn

## Author:        L. Markowsky
## Keywords:      fuzzy-logic-toolkit fuzzy inference system fis
## Directory:     fuzzy-logic-toolkit/inst/
## Filename:      setfis.m
## Last-Modified: 13 Jun 2024

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

function fis = setfis (fis, arg2, arg3, arg4 = 'dummy', ...
                       arg5 = 'dummy', arg6 = 'dummy', arg7 = 'dummy')

  switch (nargin)
    case 3  fis = setfis_three_args (fis, arg2, arg3);
    case 5  fis = setfis_five_args (fis, arg2, arg3, arg4, arg5);
    case 7  fis = setfis_seven_args (fis, arg2, arg3, arg4, arg5, ...
                                     arg6, arg7);
    otherwise
            error ("setfis requires 3, 5, or 7 arguments\n");
  endswitch

endfunction

##----------------------------------------------------------------------
## Function: setfis_three_args
## Purpose:  Handle calls to setfis that have 3 arguments. See the
##           comment at the top of this file for more complete info.
##----------------------------------------------------------------------

function fis = setfis_three_args (fis, arg2, arg3)

  ## If not all of the arguments have the correct types, print an error
  ## message and halt.

  if (!is_fis (fis))
    error ("setfis's first argument must be an FIS structure\n");
  elseif (!(is_string (arg2) && ismember (tolower (arg2), ...
          {'name', 'type', 'andmethod', 'ormethod', 'impmethod', ...
           'aggmethod', 'defuzzmethod', 'version'})))
    error ("incorrect second argument to setfis\n");
  elseif (strcmp(tolower (arg2), 'version') && !is_real (arg3))
    error ("the third argument to setfis must be a number\n");
  elseif (!strcmp(tolower (arg2), 'version') && !is_string (arg3))
    error ("the third argument to setfis must be a string\n");
  endif

  ## Set the property (arg2) of the FIS to the property value (arg3).

  switch (tolower(arg2))
    case 'name'         fis.name = arg3;
    case 'version'      fis.version = arg3;
    case 'type'         fis.type = arg3;
    case 'andmethod'    fis.andMethod = arg3;
    case 'ormethod'     fis.orMethod = arg3;
    case 'impmethod'    fis.impMethod = arg3;
    case 'aggmethod'    fis.aggMethod = arg3;
    case 'defuzzmethod' fis.defuzzMethod = arg3;
  endswitch

endfunction

##----------------------------------------------------------------------
## Function: setfis_five_args
## Purpose:  Handle calls to setfis that have 5 arguments. See the
##           comment at the top of this file for more complete info.
##----------------------------------------------------------------------

function fis = setfis_five_args (fis, arg2, arg3, arg4, arg5)

  ## If not all of the arguments have the correct types, print an error
  ## message and halt.

  if (!is_fis (fis))
    error ("setfis's first argument must be an FIS structure\n");
  elseif (!(is_string (arg2) && ...
            ismember (tolower (arg2), {'input','output'})))
    error ("setfis's second argument must be 'input' or 'output'\n");
  elseif (!is_var_index (fis, arg2, arg3))
    error ("setfis's third argument must be a variable index\n");
  elseif (!(is_string (arg4) && ...
            ismember (tolower (arg4), {'name', 'range'})))
    error ("setfis's fourth argument must be 'name' or 'range'\n");
  elseif (strcmp (arg4, 'name') && !is_string (arg5))
    error ("incorrect fifth argument to setfis\n");
  elseif (strcmp (arg4, 'range') && !is_real_matrix (arg5))
    error ("incorrect fifth argument to setfis\n");
  endif

  ## Set the input or output variable property (arg4) to the
  ## value (arg5).

  switch (tolower (arg2))
    case 'input'
      switch (tolower (arg4))
        case 'name'  fis.input(arg3).name = arg5;
        case 'range' fis.input(arg3).range = arg5;
      endswitch
    case 'output'
      switch (tolower (arg4))
        case 'name'  fis.output(arg3).name = arg5;
        case 'range' fis.output(arg3).range = arg5;
      endswitch
  endswitch

endfunction

##----------------------------------------------------------------------
## Function: setfis_seven_args
## Purpose:  Handle calls to setfis that have 7 arguments. See the
##           comment at the top of this file for more complete info.
##----------------------------------------------------------------------

function fis = setfis_seven_args (fis, arg2, arg3, arg4, arg5, ...
                                  arg6, arg7)

  ## If not all of the arguments have the correct types, print an error
  ## message and halt.

  if (!is_fis (fis))
    error ("setfis's first argument must be an FIS structure\n");
  elseif (!(is_string (arg2) && ...
            ismember (tolower (arg2), {'input','output'})))
    error ("setfis's second argument must be 'input' or 'output'\n");
  elseif (!is_var_index (fis, arg2, arg3))
    error ("setfis's third argument must be a variable index\n");
  elseif (!(is_string (arg4) && strcmp (tolower (arg4), 'mf')))
    error ("setfis's fourth argument must be 'mf'\n");
  elseif (!is_mf_index (fis, arg2, arg3, arg5))
    error ("setfis's fifth arg must be a membership function index\n");
  elseif (!(is_string (arg6) && ismember (tolower(arg6), ...
           {'name', 'type', 'params'})))
    error ("incorrect sixth argument to setfis\n");
  elseif (ismember (tolower (arg6), {'name', 'type'}) && ...
          !is_string (arg7))
    error ("incorrect seventh argument to setfis\n");
  elseif (strcmp (tolower (arg6), 'params') && !is_real_matrix (arg7))
    error ("incorrect seventh argument to setfis\n");
  endif

  ## Set the membership function property (arg6) to the value (arg7).

  switch (tolower (arg2))
    case 'input'
      switch (tolower (arg6))
        case 'name'   fis.input(arg3).mf(arg5).name = arg7;
        case 'type'   fis.input(arg3).mf(arg5).type = arg7;
        case 'params' fis.input(arg3).mf(arg5).params = arg7;
      endswitch
    case 'output'
      switch (tolower (arg6))
        case 'name'   fis.output(arg3).mf(arg5).name = arg7;
        case 'type'   fis.output(arg3).mf(arg5).type = arg7;
        case 'params' fis.output(arg3).mf(arg5).params = arg7;
      endswitch
  endswitch

endfunction

%!shared fis
%! fis = readfis ('mamdani_tip_calculator.fis');

%!test
%! fis = setfis(fis, 'defuzzMethod', 'mom');
%! assert(fis.defuzzMethod, 'mom');

## Test input validation
%!error <setfis requires 3, 5, or 7 arguments>
%! setfis()
%!error <setfis requires 3, 5, or 7 arguments>
%! setfis(1)
%!error <setfis requires 3, 5, or 7 arguments>
%! setfis(1, 2)
%!error <setfis requires 3, 5, or 7 arguments>
%! setfis(1, 2, 3, 4)
%!error <setfis requires 3, 5, or 7 arguments>
%! setfis(1, 2, 3, 4, 5, 6)
%!error <setfis: function called with too many inputs>
%! setfis(1, 2, 3, 4, 5, 6, 7, 8)
%!error <setfis's first argument must be an FIS structure>
%! setfis(1, 2, 3, 4, 5, 6, 7)
%!error <setfis's second argument must be 'input' or 'output'>
%! setfis(fis, 2, 3, 4, 5, 6, 7)
%!error <setfis's third argument must be a variable index>
%! setfis(fis, 'input', 3, 4, 5, 6, 7)
%!error <setfis's fourth argument must be 'mf'>
%! setfis(fis, 'input', 1, 4, 5, 6, 7)
%!error <setfis's fifth arg must be a membership function index>
%! setfis(fis, 'input', 1, 'mf', 5, 6, 7)
%!error <incorrect sixth argument to setfis>
%! setfis(fis, 'input', 1, 'mf', 1, 6, 7)
%!error <incorrect seventh argument to setfis>
%! setfis(fis, 'input', 1, 'mf', 1, 'name', 7)