File: writefis.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 (315 lines) | stat: -rw-r--r-- 10,468 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
## 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} {} writefis (@var{fis})
## @deftypefnx {Function File} {} writefis (@var{fis}, @var{filename})
## @deftypefnx {Function File} {} writefis (@var{fis}, @var{filename}, @var{dialog})
##
## Save the specified FIS currently in the Octave workspace to a file
## named by the user.
##
## There are three forms of writefis:
##
## @multitable @columnfractions .20 .70
## @headitem Number of Arguments @tab Action Taken
## @item 1
## @tab  Open a dialog GUI to help the user choose a directory and name
##       for the output file.
## @item 2
## @tab  Do not open a dialog GUI. Save the FIS to a file in the
##       current directory with the specified @var{filename}. If the
##       specified @var{filename} does not end in '.fis', append '.fis'
##       to the @var{filename}.
## @item 3
## @tab  Open a dialog GUI with the specified @var{filename} in the
##       'filename' textbox of the GUI. If the specified @var{filename}
##       does not end in '.fis', append '.fis' to the @var{filename}.
## @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 satisfying is_fis (see private/is_fis.m)
## @item @var{filename}
## @tab  a string; if the string does not already end with the extension
##       ".fis", then ".fis" is added
## @item @var{dialog}
## @tab  the string 'dialog' (case insensitive)
## @end multitable
## @sp 1
## Note:
## The GUI dialog requires zenity to be installed on the system.
##
## Known error:
## When using the file dialog, if the user clicks "Cancel" instead of
## saving the file, an error message is generated.
##
## @seealso{readfis}
## @end deftypefn

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

function writefis (fis, filename = 'filename.fis', dialog = 'dummy')

  ## If writefis was not called with between 1 and 3 arguments, or if
  ## the argument values were of the wrong type, print an error message
  ## and halt.

  if (!(nargin >= 1 && nargin <= 3))
    error ("writefis requires between 1 and 3 arguments\n");
  elseif (!is_fis (fis))
    error ("writefis's first argument must be an FIS structure\n");
  elseif ((nargin >= 2) && !is_string (filename))
    error ("writefis's second argument must be a string\n");
  elseif ((nargin == 3) && ...
          !(is_string (dialog) && strcmpi (dialog, 'dialog')))
    error ("writefis's third argument must the string 'dialog'\n");
  endif

  ## Open the output file.

  use_gui = (nargin != 2);
  fid = open_output_file (filename, use_gui);

  ## Write the [System], [Input<number>], [Output<number>], and [Rules]
  ## sections of the output file.

  write_system_section (fid, fis);
  write_input_sections (fid, fis);
  write_output_sections (fid, fis);
  write_rules_section (fid, fis);

  ## Close the output file.

  fclose (fid);

endfunction

##----------------------------------------------------------------------
## Function: open_output_file
## Purpose:  Open the output file. Return the fid if successful.
##           Otherwise, print an error message and halt.
##----------------------------------------------------------------------

function fid = open_output_file (filename, use_gui)

  ## If the filename is not empty, and if the last four characters of
  ## the filename are not '.fis', append '.fis' to the filename.

  fn_len = length (filename);
  if (((fn_len >= 4) && ...
       !strcmp(".fis",filename(fn_len-3:fn_len))) || ...
      ((fn_len > 0) && (fn_len < 4)))
    filename = [filename ".fis"];
  endif

  ## If writefis was called with 1 or 3 arguments, use a dialog to
  ## choose an output filename.

  if (use_gui)
    system_command = sprintf ("zenity --file-selection --filename=%s \
                              --save --confirm-overwrite; \
                              echo $file", filename);
    [dialog_error, filename] = system (file=system_command);
    if (dialog_error)
      error ("error selecting file using dialog\n");
    endif
    filename = strtrim (filename);
  endif

  ## Open output file. 

  [fid, msg] = fopen (filename, "w");
  if (fid == -1)
    if (use_gui)
      system ('zenity --error --text "Error opening output file."');
    endif
    error ("error opening output file: %s\n", msg);
  endif

endfunction

##----------------------------------------------------------------------
## Function: write_system_section
## Purpose:  Write [System] section of the output file.
##----------------------------------------------------------------------

function write_system_section (fid, fis)

  fprintf (fid, "[System]\n");
  fprintf (fid, "Name='%s'\n", fis.name);
  fprintf (fid, "Type='%s'\n", fis.type);
  fprintf (fid, "Version=%.1f\n", fis.version);
  fprintf (fid, "NumInputs=%d\n", columns(fis.input));
  fprintf (fid, "NumOutputs=%d\n", columns(fis.output));
  fprintf (fid, "NumRules=%d\n", columns(fis.rule));
  fprintf (fid, "AndMethod='%s'\n", fis.andMethod);
  fprintf (fid, "OrMethod='%s'\n", fis.orMethod);
  fprintf (fid, "ImpMethod='%s'\n", fis.impMethod);
  fprintf (fid, "AggMethod='%s'\n", fis.aggMethod);
  fprintf (fid, "DefuzzMethod='%s'\n", fis.defuzzMethod);

endfunction

##----------------------------------------------------------------------
## Function: write_input_sections
## Purpose:  For each FIS input, write [Input<number>] section to
##           output file.
##----------------------------------------------------------------------

function write_input_sections (fid, fis)

  num_inputs = columns (fis.input);

  for i = 1 : num_inputs
    num_mfs = columns (fis.input(i).mf);

    fprintf (fid, "\n[Input%d]\n", i);
    fprintf (fid, "Name='%s'\n", fis.input(i).name);
    fprintf (fid, "Range=%s\n", ...
             strrep (mat2str (fis.input(i).range),","," "));
    fprintf (fid, "NumMFs=%d\n", num_mfs);
    for j = 1 : num_mfs
      fprintf (fid, "MF%d='%s':'%s',%s\n", j, ...
               fis.input(i).mf(j).name, fis.input(i).mf(j).type, ...
               params2str (fis.input(i).mf(j).params));
    endfor
  endfor

endfunction

##----------------------------------------------------------------------
## Function: write_output_sections
## Purpose:  For each FIS output, write [Output<number>] section to
##           output file.
##----------------------------------------------------------------------

function write_output_sections (fid, fis)

  num_outputs = columns (fis.output);

  for i = 1 : num_outputs
    num_mfs = columns (fis.output(i).mf);

    fprintf (fid, "\n[Output%d]\n", i);
    fprintf (fid, "Name='%s'\n", fis.output(i).name);
    fprintf (fid, "Range=%s\n", ...
             strrep(mat2str(fis.output(i).range),","," "));
    fprintf (fid, "NumMFs=%d\n", num_mfs);
    for j = 1 : num_mfs
      fprintf (fid, "MF%d='%s':'%s',%s\n", j, ...
               fis.output(i).mf(j).name, fis.output(i).mf(j).type, ...
               params2str (fis.output(i).mf(j).params));
    endfor
  endfor

endfunction

##----------------------------------------------------------------------
## Function: write_rules_section
## Purpose:  Write [Rules] section to output file.
##----------------------------------------------------------------------

function write_rules_section (fid, fis)

  num_inputs = columns (fis.input);
  num_outputs = columns (fis.output);
  num_rules = columns (fis.rule);

  fprintf (fid, "\n[Rules]\n");

  for i = 1 : num_rules
    next_ant = fis.rule(i).antecedent;
    next_con = fis.rule(i).consequent;
    next_wt = fis.rule(i).weight;
    next_connect = fis.rule(i).connection;

    ## Print membership functions for the inputs.
    if (num_inputs > 0)
      if (is_int (next_ant(1)))
        fprintf (fid, "%d", next_ant(1));
      else
        fprintf (fid, "%.2f", next_ant(1));
      endif
    endif
    for j = 2 : num_inputs
      if (is_int (next_ant(j)))
        fprintf (fid, " %d", next_ant(j));
      else
        fprintf (fid, " %.2f", next_ant(j));
      endif
    endfor
    fprintf(fid, ", ");

    ## Print membership functions for the outputs.
    for j = 1 : num_outputs
      if (is_int (next_con(j)))
        fprintf (fid, "%d ", next_con(j));
      else
        fprintf (fid, "%.2f ", next_con(j));
      endif
    endfor

    ## Print the weight in parens.
    if (is_int (next_wt))
      fprintf (fid, "(%d) : ", next_wt);
    else
      fprintf (fid, "(%.4f) : ", next_wt);
    endif

    ## Print the connection and a newline.
    fprintf (fid, "%d\n", next_connect);
  endfor

endfunction

##----------------------------------------------------------------------
## Function: params2str
## Purpose:  Convert membership function parameters to string
##           representation.
##----------------------------------------------------------------------

function str = params2str (params)
  if (length (params) < 2)
    str = ['[' num2str(params) ']'];
  else
    str = strrep (mat2str (params), ",", " ");
  endif
endfunction

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

## Test input validation
%!error <writefis requires between 1 and 3 arguments>
%! writefis()
%!error <writefis's first argument must be an FIS structure>
%! writefis(1)
%!error <writefis's second argument must be a string>
%! writefis(fis, 2)
%!error <writefis's third argument must the string 'dialog'>
%! writefis(fis, 'temp.fis', 'abc')
%!error <writefis: function called with too many inputs>
%! writefis(1, 2, 3, 4)