• Re: Forth sourcecode for fuzzy control of ball balancing?

    From Ahmed MELAHI@21:1/5 to All on Mon Sep 11 09:40:38 2023
    Le lundi 11 septembre 2023 à 16:36:18 UTC, Ahmed MELAHI a écrit :
    Le mercredi 2 janvier 2019 à 11:15:48 UTC, minf...@arcor.de a écrit :
    Am Mittwoch, 2. Januar 2019 12:11:10 UTC+1 schrieb minf...@arcor.de:
    Am Mittwoch, 2. Januar 2019 11:52:30 UTC+1 schrieb Manuel Rodriguez:
    Fuzzy control is a grammar driven control technique which allows
    to realized sophisticated control systems. In the literature the standard example of a ball on a plate is often mentioned. The idea is to define some linguistic variables for example acceleration, position,
    distance-to-trajectory and some rules like moveleft, moveup and stop. Then
    a control program is created which is realizing the fuzzy set in reality.

    Without any doubt the best programming language for implementing fuzzy control is Forth. It allows to define the fuzzy rules as words and thanks it's interactivity capability it's great for interactive testing
    the system. My question is: Are sourcecode examples available in which Forth was used for fuzzy control?

    With due respect: this seems a Forth-centric tunnel view.

    In reality you need sensors, actuators and a controller eg PLC. Their toolstack
    determines what you have in hand. And it is to 99.999% not in Forth.

    When you're lucky you can develop and simulate using a fuzzy control toolbox. Eg.
    https://www.mathworks.com/products/fuzzy-logic.html
    p.s. I forgot:

    see page 6ff in
    http://www.forth.org/fd/FD-V18N6.pdf
    Hi,
    Here is a little program that permits to create fuzzy systems.
    Save this program as fuzzy.fs and include it in the file where a fuzzy system will be created.

    \ Here begins the code
    \ Mamdani type fuzzy system with normalized output (in [-1, 1])
    vocabulary fuzzy fuzzy definitions
    ' is alias as
    defer tnorm
    defer tconorm
    defer rules
    : and tnorm ;
    : or tconorm ;
    : if 1e ;
    : is f@ ;
    : then tnorm ;
    : )r tnorm ;
    : r( ;
    : rt( 0e ;
    : )rt or ;
    fvariable output
    100 value N/2 N/2 negate value -N/2
    : s_z*f 0e -N/2 N/2 do i s>f 1e-1 f* output f! rules output f@ f* f+ -1 +loop ;
    : s_f 0e -N/2 N/2 do i s>f 1e-1 f* output f! rules f+ -1 +loop ;
    : (fuzzy_system) s_z*f s_f f/ ;

    \ Here the code finishes.


    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ahmed MELAHI@21:1/5 to All on Mon Sep 11 09:36:15 2023
    Le mercredi 2 janvier 2019 à 11:15:48 UTC, minf...@arcor.de a écrit :
    Am Mittwoch, 2. Januar 2019 12:11:10 UTC+1 schrieb minf...@arcor.de:
    Am Mittwoch, 2. Januar 2019 11:52:30 UTC+1 schrieb Manuel Rodriguez:
    Fuzzy control is a grammar driven control technique which allows
    to realized sophisticated control systems. In the literature the standard example of a ball on a plate is often mentioned. The idea is
    to define some linguistic variables for example acceleration, position, distance-to-trajectory and some rules like moveleft, moveup and stop. Then
    a control program is created which is realizing the fuzzy set in reality.

    Without any doubt the best programming language for implementing fuzzy control is Forth. It allows to define the fuzzy rules as words and thanks it's interactivity capability it's great for interactive testing the system. My question is: Are sourcecode examples available in which Forth was used for fuzzy control?

    With due respect: this seems a Forth-centric tunnel view.

    In reality you need sensors, actuators and a controller eg PLC. Their toolstack
    determines what you have in hand. And it is to 99.999% not in Forth.

    When you're lucky you can develop and simulate using a fuzzy control toolbox. Eg.
    https://www.mathworks.com/products/fuzzy-logic.html
    p.s. I forgot:

    see page 6ff in
    http://www.forth.org/fd/FD-V18N6.pdf

    Hi,
    Here is a little program that permits to create fuzzy systems.
    Save this program as fuzzy.fs and include it in the file where a fuzzy system will be created.

    \ Here begins the code
    \ Mamdani type fuzzy system with normalized output (in [-1, 1])
    vocabulary fuzzy fuzzy definitions
    ' is alias as
    defer tnorm
    defer tconorm
    defer rules
    : and tnorm ;
    : or tconorm ;
    : if 1e ;
    : is f@ ;
    : then tnorm ;
    : )r tnorm ;
    : r( ;
    : rt( 0e ;
    : )rt or ;
    fvariable output
    100 value N/2 N/2 negate value -N/2
    : s_z*f 0e -N/2 N/2 do i s>f 1e-1 f* output f! rules output f@ f* f+ -1 +loop ; : s_f 0e -N/2 N/2 do i s>f 1e-1 f* output f! rules f+ -1 +loop ; : (fuzzy_system) s_z*f s_f f/ ;

    \ Here the code finishes.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marcel Hendrix@21:1/5 to Ahmed MELAHI on Mon Sep 11 11:13:41 2023
    On Monday, September 11, 2023 at 6:40:41 PM UTC+2, Ahmed MELAHI wrote:
    [..]
    timing_1000

    gives about 0.9 ms
    note that the fuzzy system is FS

    It takes 0.63ms in iForth, that is a bit unexpected...
    I wonder what makes it so slow (or so fast for gForth)?

    I can understand that you want to give the words the exact
    names that you feel are right, but an overloaded IS, AND, OR,
    IF, THEN, output ( not used, see zz ), and then tnorm1 and
    tcnorm1 instead of and1 and or1 so that you could write
    "r( if x is nb1 and y is nb2 then z is nb3 )r", was a bit *too*
    cute for me :--)

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ahmed MELAHI@21:1/5 to All on Mon Sep 11 11:42:55 2023
    Le lundi 11 septembre 2023 à 18:20:21 UTC, Marcel Hendrix a écrit :
    On Monday, September 11, 2023 at 8:13:45 PM UTC+2, Marcel Hendrix wrote:
    On Monday, September 11, 2023 at 6:40:41 PM UTC+2, Ahmed MELAHI wrote: [..]
    Sorry, I should have started by saying that this is a very interesting piece of work.

    -marcel
    Hi,
    Thanks,
    I wrote this program a long ago, and when I saw this thread I wanted to share it with you all.
    Here we can see that approximately one screen of code can program or create fuzzy systems.
    I don't know if Mr. Bernd Paysan is interrested to add it to his one screen collection!!!
    Concerning the overloading of the words " and, or, if,..." I've already eluded this by using french words like:
    IF <--> SI,
    THEN <--> ALORS
    AND <--> ET
    OR <--> OU
    IS <--> EST
    etc...

    so that a fuzzy rule can be written like this:
    r( SI x EST NB ET y EST NB ALORS z est NB)r
    for example.
    Concerning the speed: in gforth 0.9 ms and in iforth 0.63ms and 0.63 is less than 0.9 so iforth win.

    Bye

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marcel Hendrix@21:1/5 to Marcel Hendrix on Mon Sep 11 11:20:18 2023
    On Monday, September 11, 2023 at 8:13:45 PM UTC+2, Marcel Hendrix wrote:
    On Monday, September 11, 2023 at 6:40:41 PM UTC+2, Ahmed MELAHI wrote: [..]

    Sorry, I should have started by saying that this is a very interesting piece
    of work.

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)