Hi again,
Here is an example of creating a fuzzy system with 2 inputs and 1 output.
\ here the code begins
s" fuzzy.fs" included
\ example
\ fuzzy system with: 2 inputs x and y
\ 1 output z
fvariable x
fvariable y
' output alias z
fvariable zz \ to save output
\ print x, y and zz
: .xyz cr ." X = " x f@ f. cr ." Y = " y f@ f. cr ." Z = " zz f@ f. ;
\ using prod as tnorm and probabilistic or as tconorm, we can use fmin and fmax for tnorm and tconorm respectively
: tnorm1 f* ;
: tconorm1 fover fover f* f- f+ ; \ (u,v)---> u+v-u*v
' tnorm1 as tnorm
' tconorm1 as tconorm
\ here, we use gaussian membership functions for the three fuzzy variables,
\ we can use other functions: triangular, trapezoidal, ... Just define them
\ membership degree md = mf_gauss(x;m,s) = exp(-((x-m)/s)^2)
: mf_gauss ( f: x m s -- f: md ) frot frot f- fswap f/ fdup f* fnegate fexp ;
\ for input x
: nb1 -1e 0.2e mf_gauss ;
: ze1 0e 0.2e mf_gauss ;
: pb1 1e 0.2e mf_gauss ;
\ for input y
: nb2 -1e 0.2e mf_gauss ;
: ze2 0e 0.2e mf_gauss ;
: pb2 1e 0.2e mf_gauss ;
\ for output z
: nb3 -1e 0.2e mf_gauss ;
: ze3 0e 0.2e mf_gauss ;
: pb3 1e 0.2e mf_gauss ;
\ the fuzzy rules are given as below
: rules1
rt(
r( if x is nb1 and y is nb2 then z is nb3 )r or
r( if x is nb1 and y is ze2 then z is ze3 )r or
r( if x is nb1 and y is pb2 then z is ze3 )r or
r( if x is ze1 and y is ze2 then z is ze3 )r or
r( if x is ze1 and y is nb2 then z is ze3 )r or
r( if x is ze1 and y is pb2 then z is ze3 )r or
r( if x is pb1 and y is nb2 then z is ze3 )r or
r( if x is pb1 and y is ze2 then z is ze3 )r or
r( if x is pb1 and y is pb2 then z is pb3 )r
)rt
;
' rules1 as rules
\ the fuzzy sustem
: FS (fuzzy_system) zz f! ;
\ examples of results
0e x f! 0e y f!
FS .xyz \ print x y and the result which is in zz
cr
-1e x f! -1e y f!
FS .xyz
cr
1e x f! 1e y f!
FS .xyz
cr
\ here the code finishes
the timing using gforth :
\ mean time of execution
: timing_1000 utime 1000 0 do FS loop utime d>f d>f f- 1e-3 f* 1e-3 f* cr ." mean execution time: " f. ." ms. " ;
timing_1000
gives about 0.9 ms
note that the fuzzy system is FS
Bye