Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

The test of intelligent tinkering is to save all the parts. -- Aldo Leopold


devel / comp.lang.forth / Fuzzy systems in forth

SubjectAuthor
* Fuzzy systems in forthAhmed MELAHI
`* Re: Fuzzy systems in forthAhmed MELAHI
 `* Re: Fuzzy systems in forthMarcel Hendrix
  `* Re: Fuzzy systems in forthAla'a
   +* Re: Fuzzy systems in forthAhmed MELAHI
   |`* Re: Fuzzy systems in forthminforth
   | `* Re: Fuzzy systems in forthAhmed MELAHI
   |  `* Re: Fuzzy systems in forthAla'a
   |   `- Re: Fuzzy systems in forthAhmed MELAHI
   `* Re: Fuzzy systems in forthAhmed MELAHI
    `* Re: Fuzzy systems in forthAla'a
     `- Re: Fuzzy systems in forthAhmed MELAHI

1
Fuzzy systems in forth

<6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24602&group=comp.lang.forth#24602

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:620a:4d2:b0:76f:cd2:5d10 with SMTP id 18-20020a05620a04d200b0076f0cd25d10mr118857qks.5.1694777984157;
Fri, 15 Sep 2023 04:39:44 -0700 (PDT)
X-Received: by 2002:a05:6808:20a7:b0:3ad:aeed:7eeb with SMTP id
s39-20020a05680820a700b003adaeed7eebmr617471oiw.6.1694777983951; Fri, 15 Sep
2023 04:39:43 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Fri, 15 Sep 2023 04:39:43 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=154.121.55.228; posting-account=KJSw4AoAAACRkUCek5r_78mFj6sHzH4C
NNTP-Posting-Host: 154.121.55.228
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>
Subject: Fuzzy systems in forth
From: ahmed.melahi@univ-bejaia.dz (Ahmed MELAHI)
Injection-Date: Fri, 15 Sep 2023 11:39:44 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3511
 by: Ahmed MELAHI - Fri, 15 Sep 2023 11:39 UTC

Hi,
Here is a program that permits to create fuzzy systems.
Save this program as "fuzzy.fs" and include it in the programs where the fuzzy systems will be created.
Notice that you can create several fuzzy systems in the same program.

\ the code begins here:

\ Mamdani type fuzzy system with normalized output (in [-1, 1])

variable tnorm
variable tconorm
variable (in_fuzzify)
variable (out_fuzzify)
variable (rules)

: tnorm: ' tnorm ! ;
: tconorm: ' tconorm ! ;
: in_fuzzify: ' (in_fuzzify) ! ;
: out_fuzzify: ' (out_fuzzify) ! ;
: rules: ' (rules) ! ;

fvariable fs_output
: output: fs_output create , does> @ ;
: inputs: 0 ?do fvariable loop ;

\ we use membership functions: gaussian, triangular, trapezoidal, ... Just define them
\ gaussian membership function mf = mf_gauss(x;m,s) = exp(-0.5*((x-m)/s)^2)
: mf_gauss ( f: x m s -- f: md ) frot frot f- fswap f/ fdup f* 0.5e f* fnegate fexp ;

\ using prod as tnorm and probabilistic sum as tconorm,
\ we can use fmin and fmax for tnorm and tconorm respectively
: prod f* ;
: prob_sum fover fover f* f- f+ ; \ (u,v)---> u+v-u*v

\ use fmin or fmax already defined directly
\ : fmin fmin ;
\ : fmax fmax ;

\ to write rules
: fuzzy_propositions: 0 ?do 0e fvalue loop ;
: .and. tnorm @ execute ;
: .or. tconorm @ execute ;
: .if. 1e ;
: .then. tnorm @ execute ;
: )r tnorm @ execute ;
: r( ; immediate
: rt( 0e ;
: )rt tconorm @ execute ;
: .is. f@ ;

: infere (out_fuzzify) @ execute (rules) @ execute ;

10 value N/2
N/2 negate value N_begin
N/2 1+ value N_end
N/2 s>f 1/f fvalue out_step

: infere_defuzz ( f: -- z* )
0e 0e ( f: s_mfz s_mf )
N_end N_begin
do
fswap ( f: s_mf s_mfz )
i s>f out_step f* ( f: s_mf s_mfz z )
fs_output f! ( f: s_mf s_mfz )
infere ( f: s_mf s_mfz mf)
ftuck fs_output f@ f* f+ ( f: s_mf mf s_mfz)
frot frot f+ ( f: s_mfz s_mf)
loop
f/ ;

: (fuzzy_system) (in_fuzzify) @ execute infere_defuzz fs_output f! ;
: fuzzy_system: ['] (fuzzy_system)
create , tnorm @ , tconorm @ , (in_fuzzify) @ , (out_fuzzify) @ , (rules) @ ,
does> dup
cell+ dup @ tnorm !
cell+ dup @ tconorm !
cell+ dup @ (in_fuzzify) !
cell+ dup @ (out_fuzzify) !
cell+ @ (rules) !
@ execute ;

\ the code finishes here.

Enjoy,
Bye

Re: Fuzzy systems in forth

<4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24603&group=comp.lang.forth#24603

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:ac8:7d49:0:b0:412:1974:9730 with SMTP id h9-20020ac87d49000000b0041219749730mr31449qtb.5.1694778412895;
Fri, 15 Sep 2023 04:46:52 -0700 (PDT)
X-Received: by 2002:a05:6870:5b0f:b0:1d6:4344:88f6 with SMTP id
ds15-20020a0568705b0f00b001d6434488f6mr512990oab.0.1694778412691; Fri, 15 Sep
2023 04:46:52 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Fri, 15 Sep 2023 04:46:52 -0700 (PDT)
In-Reply-To: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=154.121.55.228; posting-account=KJSw4AoAAACRkUCek5r_78mFj6sHzH4C
NNTP-Posting-Host: 154.121.55.228
References: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com>
Subject: Re: Fuzzy systems in forth
From: ahmed.melahi@univ-bejaia.dz (Ahmed MELAHI)
Injection-Date: Fri, 15 Sep 2023 11:46:52 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 8366
 by: Ahmed MELAHI - Fri, 15 Sep 2023 11:46 UTC

Le vendredi 15 septembre 2023 à 11:39:45 UTC, Ahmed MELAHI a écrit :
> Hi,
> Here is a program that permits to create fuzzy systems.
> Save this program as "fuzzy.fs" and include it in the programs where the fuzzy systems will be created.
> Notice that you can create several fuzzy systems in the same program.
>
> \ the code begins here:
>
> \ Mamdani type fuzzy system with normalized output (in [-1, 1])
>
> variable tnorm
> variable tconorm
> variable (in_fuzzify)
> variable (out_fuzzify)
> variable (rules)
>
> : tnorm: ' tnorm ! ;
> : tconorm: ' tconorm ! ;
> : in_fuzzify: ' (in_fuzzify) ! ;
> : out_fuzzify: ' (out_fuzzify) ! ;
> : rules: ' (rules) ! ;
>
>
> fvariable fs_output
> : output: fs_output create , does> @ ;
> : inputs: 0 ?do fvariable loop ;
>
>
> \ we use membership functions: gaussian, triangular, trapezoidal, ... Just define them
> \ gaussian membership function mf = mf_gauss(x;m,s) = exp(-0.5*((x-m)/s)^2)
> : mf_gauss ( f: x m s -- f: md ) frot frot f- fswap f/ fdup f* 0.5e f* fnegate fexp ;
>
>
> \ using prod as tnorm and probabilistic sum as tconorm,
> \ we can use fmin and fmax for tnorm and tconorm respectively
> : prod f* ;
> : prob_sum fover fover f* f- f+ ; \ (u,v)---> u+v-u*v
>
> \ use fmin or fmax already defined directly
> \ : fmin fmin ;
> \ : fmax fmax ;
>
>
> \ to write rules
> : fuzzy_propositions: 0 ?do 0e fvalue loop ;
> : .and. tnorm @ execute ;
> : .or. tconorm @ execute ;
> : .if. 1e ;
> : .then. tnorm @ execute ;
> : )r tnorm @ execute ;
> : r( ; immediate
> : rt( 0e ;
> : )rt tconorm @ execute ;
> : .is. f@ ;
>
> : infere (out_fuzzify) @ execute (rules) @ execute ;
>
> 10 value N/2
> N/2 negate value N_begin
> N/2 1+ value N_end
> N/2 s>f 1/f fvalue out_step
>
> : infere_defuzz ( f: -- z* )
> 0e 0e ( f: s_mfz s_mf )
> N_end N_begin
> do
> fswap ( f: s_mf s_mfz )
> i s>f out_step f* ( f: s_mf s_mfz z )
> fs_output f! ( f: s_mf s_mfz )
> infere ( f: s_mf s_mfz mf)
> ftuck fs_output f@ f* f+ ( f: s_mf mf s_mfz)
> frot frot f+ ( f: s_mfz s_mf)
> loop
> f/ ;
>
> : (fuzzy_system) (in_fuzzify) @ execute infere_defuzz fs_output f! ;
> : fuzzy_system: ['] (fuzzy_system)
> create , tnorm @ , tconorm @ , (in_fuzzify) @ , (out_fuzzify) @ , (rules) @ ,
> does> dup
> cell+ dup @ tnorm !
> cell+ dup @ tconorm !
> cell+ dup @ (in_fuzzify) !
> cell+ dup @ (out_fuzzify) !
> cell+ @ (rules) !
> @ execute ;
>
> \ the code finishes here.
>
> Enjoy,
> Bye
Hi again,
Here an example how to use this program:

\ here begins the code

s" fuzzy.fs" included

\ example 1
\ fuzzy system FS1 with: 2 inputs x and y
\ 1 output z

2 inputs: x y
output: z

\ print x, y and z
: .xyz cr ." X = " x f@ f. cr ." Y = " y f@ f. cr ." Z = " z f@ f. ;

\ we use gaussian membership functions for the three fuzzy variables,
\ 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 ;

\ fuzzy propositions for rules
3 fuzzy_propositions: x_is_nb1 x_is_ze1 x_is_pb1 \ for input x
3 fuzzy_propositions: y_is_nb2 y_is_ze2 y_is_pb2 \ for input y
3 fuzzy_propositions: z_is_nb3 z_is_ze3 z_is_pb3 \ for output z

: in_fuzzify
x .is. nb1 to x_is_nb1
x .is. ze1 to x_is_ze1
x .is. pb1 to x_is_pb1

y .is. nb2 to y_is_nb2
y .is. ze2 to y_is_ze2
y .is. pb2 to y_is_pb2
;

: out_fuzzify
z .is. nb3 to z_is_nb3
z .is. ze3 to z_is_ze3
z .is. pb3 to z_is_pb3
;

\ the fuzzy rules are given as below
: rules_1
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_nb3 )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_nb2 .then. z_is_nb3 )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_pb2 .then. z_is_pb3 )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_pb3 )r .or.
r( .if. x_is_pb1 .and. y_is_pb2 .then. z_is_pb3 )r
)rt
;

\ create the fuzzy system FS1
tnorm: prod
tconorm: prob_sum
in_fuzzify: in_fuzzify
out_fuzzify: out_fuzzify
rules: rules_1
fuzzy_system: FS1

\ ------------------------------------------------------------
\ -------------------------------------------------------------
\ example 2
\ fuzzy system FS2 with: 1 input x2
\ 1 output z2

1 inputs: x2
output: z2

\ print x2 and z2
: .xz_2 cr ." X2 = " x2 f@ f. cr ." Z2 = " z2 f@ f. ;

\ we use gaussian membership functions for the three fuzzy variables,
\ for input x2
: nb1_2 -1e 0.25e mf_gauss ;
: pb1_2 1e 0.25e mf_gauss ;

\ for output z2
: nb3_2 -1e 0.25e mf_gauss ;
: pb3_2 1e 0.25e mf_gauss ;

\ fuzzy propositions for rules
2 fuzzy_propositions: x2_is_nb1_2 x2_is_pb1_2 \ for input x2
2 fuzzy_propositions: z2_is_nb3_2 z2_is_pb3_2 \ for output z2

: in_fuzzify_2
x2 .is. nb1_2 to x2_is_nb1_2
x2 .is. pb1_2 to x2_is_pb1_2
;

: out_fuzzify_2
z2 .is. nb3_2 to z2_is_nb3_2
z2 .is. pb3_2 to z2_is_pb3_2
;

\ the fuzzy rules are given as below
: rules_2
rt(
r( .if. x2_is_nb1_2 .then. z2_is_pb3_2 )r .or.
r( .if. x2_is_pb1_2 .then. z2_is_nb3_2 )r
)rt
;

\ create the fuzzy system FS2
tnorm: fmin
tconorm: fmax
in_fuzzify: in_fuzzify_2
out_fuzzify: out_fuzzify_2
rules: rules_2
fuzzy_system: FS2

\ examples of results
1 [if]
0e x f! 0e y f! FS1 .xyz cr \ print x y and the result which is in z
-1e x f! -1e y f! FS1 .xyz cr \ print x y and the result which is in z
1e x f! 1e y f! FS1 .xyz cr \ print x y and the result which is in z
-1e x2 f! FS2 .xz_2 cr \ print x2 and the result which is in z2
0e x2 f! FS2 .xz_2 cr \ print x2 and the result which is in z2
1e x2 f! FS2 .xz_2 cr \ print x2 and the result which is in z2
[THEN]

\ timing
variable FS
: FS_EXEC FS @ execute ;
: timing_10000: cr ' FS ! timer-reset 10000 0 do FS_EXEC loop .elapsed ." for 10000 times." cr ;
timing_10000: FS1
timing_10000: FS2

\ here the code finishes
The program was tested with gforth, vfxforth and iforth version 4.0
the timing was interesting:
gforth:
FS1: 20 micro seconds
FS2: 06 micro seconds
vfxforth:
FS1: 11 micro seconds
FS2: 9.5 micro seconds
iForth:
FS1: 14 micro seconds
FS2: 05 micro seconds

Notice that FS1 and FS2 works in the same program.
Enjoy
Bye

Re: Fuzzy systems in forth

<06f1344c-6372-47d7-b173-49d2c73b85b1n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24605&group=comp.lang.forth#24605

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:620a:2289:b0:76d:77d2:e74e with SMTP id o9-20020a05620a228900b0076d77d2e74emr51077qkh.2.1694807020591;
Fri, 15 Sep 2023 12:43:40 -0700 (PDT)
X-Received: by 2002:a05:620a:270b:b0:773:ae41:d88f with SMTP id
b11-20020a05620a270b00b00773ae41d88fmr57969qkp.0.1694807020384; Fri, 15 Sep
2023 12:43:40 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Fri, 15 Sep 2023 12:43:40 -0700 (PDT)
In-Reply-To: <4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2001:1c05:2f18:6d00:e53f:e2c5:eac:328e;
posting-account=-JQ2RQoAAAB6B5tcBTSdvOqrD1HpT_Rk
NNTP-Posting-Host: 2001:1c05:2f18:6d00:e53f:e2c5:eac:328e
References: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com> <4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <06f1344c-6372-47d7-b173-49d2c73b85b1n@googlegroups.com>
Subject: Re: Fuzzy systems in forth
From: mhx@iae.nl (Marcel Hendrix)
Injection-Date: Fri, 15 Sep 2023 19:43:40 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3847
 by: Marcel Hendrix - Fri, 15 Sep 2023 19:43 UTC

On Friday, September 15, 2023 at 1:46:54 PM UTC+2, Ahmed MELAHI wrote:
> Le vendredi 15 septembre 2023 à 11:39:45 UTC, Ahmed MELAHI a écrit :
[..]
> The program was tested with gforth, vfxforth and iforth version 4.0
> the timing was interesting:
> gforth:
> FS1: 20 micro seconds
> FS2: 06 micro seconds
> vfxforth:
> FS1: 11 micro seconds
> FS2: 9.5 micro seconds
> iForth:
> FS1: 14 micro seconds
> FS2: 05 micro seconds
>
> Notice that FS1 and FS2 works in the same program.

I like the naming much better now (personal preference).

And indeed, the timing is fascinating:

[1] iForth x64 server 1.32 (console), Aug 24 2023, 23:22:14.
[2] Stuffed iForth at $0109DB00 [entry: $01100000]
[3] Having a Windows terminal.
[4] Console is active.
[5] Sound devices are internal.

iForth version 6.9.109, generated 18:39:31, September 27, 2021.
x86_64 binary, native floating-point, extended precision.
Copyright 1996 - 2021 Marcel Hendrix.
[6] Use --- iForth.prf
Creating --- Locate support Version 2.01 ---
Creating --- Several utilities Version 3.53 ---
AMD Ryzen 7 5800X 8-Core Processor
TICKS-GET uses os time & PROCESSOR-CLOCK 4192MHz
Do: < n TO PROCESSOR-CLOCK RECALIBRATE >

in fuzzy2
9.2 us for 1 execution.
3.2 us for 1 execution.

The best runs from 30 or so:
9.1 us for 1 execution.
1.5 us for 1 execution.

The fact that rules_1 is consisting of pure code (everything
inlined) might be too cache-unfriendly.

Your rules look cute, but I think I can read them better
as pure Forth, and then it would be faster still.

\ the fuzzy rules are given as below
: rules_1
0e
1e x_is_nb1 prod y_is_nb2 prod z_is_nb3 prod prob_sum
1e x_is_nb1 prod y_is_ze2 prod z_is_nb3 prod prob_sum
1e x_is_nb1 prod y_is_pb2 prod z_is_ze3 prod prob_sum
1e x_is_ze1 prod y_is_nb2 prod z_is_nb3 prod prob_sum
1e x_is_ze1 prod y_is_ze2 prod z_is_ze3 prod prob_sum
1e x_is_ze1 prod y_is_pb2 prod z_is_pb3 prod prob_sum
1e x_is_pb1 prod y_is_nb2 prod z_is_ze3 prod prob_sum
1e x_is_pb1 prod y_is_ze2 prod z_is_pb3 prod prob_sum
1e x_is_pb1 prod y_is_pb2 prod z_is_pb3 prod
prob_sum
;

FORTH> in fuzzy3
X = 0.000000 Y = 0.000000 Z = 0.000000
X = -1.000000 Y = -1.000000 Z = -0.869768
X = 1.000000 Y = 1.000000 Z = 0.869768
X2 = -1.000000 Z2 = 0.830292
X2 = 0.000000 Z2 = 0.000000
X2 = 1.000000 Z2 = -0.830292
1.9 us for 1 execution.
2.6 us for 1 execution.
ok

-marcel

Re: Fuzzy systems in forth

<0134b349-6150-4810-b4cf-b3d93fa85ec8n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24886&group=comp.lang.forth#24886

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:620a:2703:b0:774:1c2f:ac3e with SMTP id b3-20020a05620a270300b007741c2fac3emr299670qkp.9.1697042734698;
Wed, 11 Oct 2023 09:45:34 -0700 (PDT)
X-Received: by 2002:a4a:5554:0:b0:57b:eecc:98fd with SMTP id
e81-20020a4a5554000000b0057beecc98fdmr9769634oob.0.1697042734371; Wed, 11 Oct
2023 09:45:34 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 11 Oct 2023 09:45:33 -0700 (PDT)
In-Reply-To: <06f1344c-6372-47d7-b173-49d2c73b85b1n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2001:8f8:1c6d:3147:8925:afbd:e73c:2218;
posting-account=mkOgdwoAAAAGDUBcY0S_1Gob1TR-ZY2T
NNTP-Posting-Host: 2001:8f8:1c6d:3147:8925:afbd:e73c:2218
References: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>
<4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com> <06f1344c-6372-47d7-b173-49d2c73b85b1n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <0134b349-6150-4810-b4cf-b3d93fa85ec8n@googlegroups.com>
Subject: Re: Fuzzy systems in forth
From: amalawi@gmail.com (Ala'a)
Injection-Date: Wed, 11 Oct 2023 16:45:34 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 7239
 by: Ala'a - Wed, 11 Oct 2023 16:45 UTC

I was having fun learning FL, still do, and thought to share what I understood till now. so here is my take as a newbie to FL. I hope i did not make too many mistakes (especially any conceptual ones :) ). The formatting my be messed up.

VOCABULARY FuzzyLogic21 ALSO FuzzyLogic21 DEFINITIONS
\ Assume trapizoidal inputs
\ Assume range is bounded by p1 and p4
\ Assume singleton outputs
\ no verification of rules being complete (covering all cases) is done in this implementation
: lingvar VARIABLE ; \ lingustic variable (input or output is a variable here)
: member ( p1 p2 p3 p3 --) CREATE >R SWAP ROT , , , R> , ; \ define input m.ship. function, simple without linking lingvar
: p1 @ ; : p2 CELL+ @ ; : p3 2 CELLS + @ ; : p4 3 CELLS + @ ;
: pts ( mem -- p1 p2 p3 p4 ) >R R@ p1 R@ p2 R@ p3 R> p4 ;
: singleton ( val -- ) CONSTANT ; \ 'member' word could be used with p1, but seemed a waste!
: seg1 ( mem -- p1 p2 ) >R R@ p1 1+ R> p2 1- ; \ ascending line
: seg2 ( mem -- p2 p3 ) >R R@ p2 R> p3 ; \ horizontal line
: seg3 ( mem -- p3 p4 ) >R R@ p3 1+ R> p4 1- ; \ desc. line
1000 VALUE res \ resolution of the 1==1000 value membership
: interpo ( xm y1 y2 -- ym ) >R TUCK - res * SWAP R> - / ABS ; \ interpolate. used for both asc. and desc. lines.
: ]clip[ ( val p1 p2 -- clipped ) ROT DUP >R -ROT 1+ WITHIN ABS R> * ; \ val or zero if out of range
: fuzz ( crisp mem -- fz1 fz2 fz3 ) \ trapizoidal interpolation. is it possible to improve this?
2DUP seg1 ]clip[ IF 2DUP seg1 interpo ELSE 0 THEN -ROT
2DUP seg2 ]clip[ IF res ELSE 0 THEN -ROT
2DUP seg3 ]clip[ IF seg3 SWAP interpo ELSE 2DROP 0 THEN ;

DEFER aggregate
: maximize ( val var -- ) TUCK @ MAX SWAP ! ;
: minimize ( val var -- ) TUCK @ MIN SWAP ! ;
: sum ( val var -- ) +! ;
' maximize IS aggregate
: defuz ( fuz-val lingvar singleton -- ) ROT res */ SWAP aggregate ;

\ Rules compilation
SYNONYM ` POSTPONE
DEFER <IS> IMMEDIATE
DEFER <AND> IMMEDIATE
: nop ;
: compile-at ` @ ;
: compile-fuzz-min ` fuzz ` + ` + ` min ; ' compile-fuzz-min IS <AND>
: compile-<and>-defuz ` defuz ` DUP ;
: antecedent-scope ['] compile-at IS <IS> ['] compile-fuzz-min IS <AND> ;
: consequence-scope ['] nop IS <IS> ['] compile-<and>-defuz IS <AND> ;
: <IF> ` res ` 1+ antecedent-scope ; IMMEDIATE \ max fuz is res, thus 'res 1+' will do with MIN
: <THEN> ` <AND> ` DUP ` IF ` DUP consequence-scope ; IMMEDIATE
: <END> ` defuz ` THEN ` DROP ; IMMEDIATE
: .fired ." Rule " . ." fired! " ;

\
\ Example - car brakes
\ lingvar speed \ in KM\H
0 10 40 50 member low
40 60 80 100 member medium
80 100 320 325 member high

lingvar distance \ in meters
-1 0 20 50 member close
30 60 80 100 member near
80 110 1000 1005 member far

lingvar brake \ measured in N or poundal (this example is illustrative rather than giving experience/measured forces)
0 singleton light
10 singleton moderate
30 singleton hard

: brake-rules
0 brake !
<IF> distance <IS> close <AND> speed <IS> high <THEN> 1 .fired brake <IS> hard <END>
<IF> distance <IS> close <AND> speed <IS> medium <THEN> 2 .fired brake <IS> hard <END>
<IF> distance <IS> close <AND> speed <IS> low <THEN> 3 .fired brake <IS> moderate <END>
<IF> distance <IS> near <AND> speed <IS> high <THEN> 4 .fired brake <IS> hard <END>
<IF> distance <IS> near <AND> speed <IS> medium <THEN> 5 .fired brake <IS> moderate <END>
<IF> distance <IS> near <AND> speed <IS> low <THEN> 6 .fired brake <IS> light <END>
<IF> distance <IS> far <THEN> 7 .fired brake <IS> light <END>
;

: control-brake ( -- b ) brake-rules brake @ ;

CR CR .( Car Brakes example) CR
2 distance !
CR 15 speed ! control-brake .
CR 70 speed ! control-brake .
CR 100 speed ! control-brake .
CR
50 distance !
CR 45 speed ! control-brake .
CR 90 speed ! control-brake .
CR
50 distance !
CR 15 speed ! control-brake .
CR 70 speed ! control-brake .
CR 100 speed ! control-brake .
CR
200 distance !
CR 15 speed ! control-brake .
CR 70 speed ! control-brake .
CR 100 speed ! control-brake .

\
\ Tipping Examples
\ lingvar service \ level
-2 -1 0 5 member poor
0 5 5 10 member acceptable
5 10 11 12 member amazing

lingvar quality \ of food
-2 -1 0 5 member bad
0 5 5 10 member decent
5 10 11 12 member great

lingvar tip \ percentage
0 singleton low
13 singleton medium
25 singleton high

variable count
: tipping-rules
0 tip ! 0 count !
<IF> service <IS> amazing <THEN> 1 .fired count ++ tip <IS> high <END>
<IF> quality <IS> great <THEN> 2 .fired count ++ tip <IS> high <END>
<IF> service <IS> acceptable <THEN> 3 .fired count ++ tip <IS> medium <END>
<IF> service <IS> poor <AND> quality <IS> poor <THEN> 4 .fired count ++ tip <IS> low <END>
; ' sum is aggregate
: calc-tip tipping-rules tip @ count @ / ;

CR CR .( Tipping example)
CR 6 quality ! 9 service ! calc-tip .
CR 6 quality ! 8 service ! calc-tip .
CR 9 quality ! 4 service ! calc-tip .
CR 3 quality ! 3 service ! calc-tip .

\
\ Example - fan control
\ lingvar temperature
-21 -20 20 40 member cool
20 40 40 50 member warm
40 60 120 121 member hot

lingvar fan-speed
30 singleton slow
60 singleton medium
90 singleton fast

: fan-rules
0 fan-speed !
<IF> temperature <IS> cool <THEN> 1 .fired fan-speed <IS> slow <END>
<IF> temperature <IS> warm <THEN> 2 .fired fan-speed <IS> medium <END>
<IF> temperature <IS> hot <THEN> 3 .fired fan-speed <IS> fast <END>
;

: control-fan fan-rules fan-speed @ ;
: clip-input cool p1 hot p4 WITHIN NOT ABORT" Input out of range!" ;
: control2 clip-input temperature ! control-fan ;

CR CR .( Fan Speed control)
: test 80 0 DO CR I 3 .r space I temperature ! control-fan space 3 .r LOOP ; test

Re: Fuzzy systems in forth

<02579c55-8e03-408a-abf1-eded0aa071ben@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24887&group=comp.lang.forth#24887

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:622a:199d:b0:412:2107:7f1d with SMTP id u29-20020a05622a199d00b0041221077f1dmr324435qtc.7.1697050252032;
Wed, 11 Oct 2023 11:50:52 -0700 (PDT)
X-Received: by 2002:a05:6808:218c:b0:3a3:d677:9a8d with SMTP id
be12-20020a056808218c00b003a3d6779a8dmr11417350oib.0.1697050251749; Wed, 11
Oct 2023 11:50:51 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 11 Oct 2023 11:50:51 -0700 (PDT)
In-Reply-To: <0134b349-6150-4810-b4cf-b3d93fa85ec8n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=154.121.62.164; posting-account=KJSw4AoAAACRkUCek5r_78mFj6sHzH4C
NNTP-Posting-Host: 154.121.62.164
References: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>
<4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com> <06f1344c-6372-47d7-b173-49d2c73b85b1n@googlegroups.com>
<0134b349-6150-4810-b4cf-b3d93fa85ec8n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <02579c55-8e03-408a-abf1-eded0aa071ben@googlegroups.com>
Subject: Re: Fuzzy systems in forth
From: ahmed.melahi@univ-bejaia.dz (Ahmed MELAHI)
Injection-Date: Wed, 11 Oct 2023 18:50:52 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 8675
 by: Ahmed MELAHI - Wed, 11 Oct 2023 18:50 UTC

Le mercredi 11 octobre 2023 à 16:45:36 UTC, Ala'a a écrit :
> I was having fun learning FL, still do, and thought to share what I understood till now. so here is my take as a newbie to FL. I hope i did not make too many mistakes (especially any conceptual ones :) ). The formatting my be messed up.
>
> VOCABULARY FuzzyLogic21 ALSO FuzzyLogic21 DEFINITIONS
> \ Assume trapizoidal inputs
> \ Assume range is bounded by p1 and p4
> \ Assume singleton outputs
> \ no verification of rules being complete (covering all cases) is done in this implementation
> : lingvar VARIABLE ; \ lingustic variable (input or output is a variable here)
> : member ( p1 p2 p3 p3 --) CREATE >R SWAP ROT , , , R> , ; \ define input m.ship. function, simple without linking lingvar
> : p1 @ ; : p2 CELL+ @ ; : p3 2 CELLS + @ ; : p4 3 CELLS + @ ;
> : pts ( mem -- p1 p2 p3 p4 ) >R R@ p1 R@ p2 R@ p3 R> p4 ;
> : singleton ( val -- ) CONSTANT ; \ 'member' word could be used with p1, but seemed a waste!
> : seg1 ( mem -- p1 p2 ) >R R@ p1 1+ R> p2 1- ; \ ascending line
> : seg2 ( mem -- p2 p3 ) >R R@ p2 R> p3 ; \ horizontal line
> : seg3 ( mem -- p3 p4 ) >R R@ p3 1+ R> p4 1- ; \ desc. line
> 1000 VALUE res \ resolution of the 1==1000 value membership
> : interpo ( xm y1 y2 -- ym ) >R TUCK - res * SWAP R> - / ABS ; \ interpolate. used for both asc. and desc. lines.
> : ]clip[ ( val p1 p2 -- clipped ) ROT DUP >R -ROT 1+ WITHIN ABS R> * ; \ val or zero if out of range
> : fuzz ( crisp mem -- fz1 fz2 fz3 ) \ trapizoidal interpolation. is it possible to improve this?
> 2DUP seg1 ]clip[ IF 2DUP seg1 interpo ELSE 0 THEN -ROT
> 2DUP seg2 ]clip[ IF res ELSE 0 THEN -ROT
> 2DUP seg3 ]clip[ IF seg3 SWAP interpo ELSE 2DROP 0 THEN ;
>
> DEFER aggregate
> : maximize ( val var -- ) TUCK @ MAX SWAP ! ;
> : minimize ( val var -- ) TUCK @ MIN SWAP ! ;
> : sum ( val var -- ) +! ;
> ' maximize IS aggregate
> : defuz ( fuz-val lingvar singleton -- ) ROT res */ SWAP aggregate ;
>
> \ Rules compilation
> SYNONYM ` POSTPONE
> DEFER <IS> IMMEDIATE
> DEFER <AND> IMMEDIATE
> : nop ;
> : compile-at ` @ ;
> : compile-fuzz-min ` fuzz ` + ` + ` min ; ' compile-fuzz-min IS <AND>
> : compile-<and>-defuz ` defuz ` DUP ;
> : antecedent-scope ['] compile-at IS <IS> ['] compile-fuzz-min IS <AND> ;
> : consequence-scope ['] nop IS <IS> ['] compile-<and>-defuz IS <AND> ;
> : <IF> ` res ` 1+ antecedent-scope ; IMMEDIATE \ max fuz is res, thus 'res 1+' will do with MIN
> : <THEN> ` <AND> ` DUP ` IF ` DUP consequence-scope ; IMMEDIATE
> : <END> ` defuz ` THEN ` DROP ; IMMEDIATE
> : .fired ." Rule " . ." fired! " ;
>
> \
> \ Example - car brakes
> \
> lingvar speed \ in KM\H
> 0 10 40 50 member low
> 40 60 80 100 member medium
> 80 100 320 325 member high
>
> lingvar distance \ in meters
> -1 0 20 50 member close
> 30 60 80 100 member near
> 80 110 1000 1005 member far
>
> lingvar brake \ measured in N or poundal (this example is illustrative rather than giving experience/measured forces)
> 0 singleton light
> 10 singleton moderate
> 30 singleton hard
>
> : brake-rules
> 0 brake !
> <IF> distance <IS> close <AND> speed <IS> high <THEN> 1 .fired brake <IS> hard <END>
> <IF> distance <IS> close <AND> speed <IS> medium <THEN> 2 .fired brake <IS> hard <END>
> <IF> distance <IS> close <AND> speed <IS> low <THEN> 3 .fired brake <IS> moderate <END>
> <IF> distance <IS> near <AND> speed <IS> high <THEN> 4 .fired brake <IS> hard <END>
> <IF> distance <IS> near <AND> speed <IS> medium <THEN> 5 .fired brake <IS> moderate <END>
> <IF> distance <IS> near <AND> speed <IS> low <THEN> 6 .fired brake <IS> light <END>
> <IF> distance <IS> far <THEN> 7 .fired brake <IS> light <END>
> ;
>
>
> : control-brake ( -- b ) brake-rules brake @ ;
>
> CR CR .( Car Brakes example) CR
> 2 distance !
> CR 15 speed ! control-brake .
> CR 70 speed ! control-brake .
> CR 100 speed ! control-brake .
> CR
> 50 distance !
> CR 45 speed ! control-brake .
> CR 90 speed ! control-brake .
> CR
> 50 distance !
> CR 15 speed ! control-brake .
> CR 70 speed ! control-brake .
> CR 100 speed ! control-brake .
> CR
> 200 distance !
> CR 15 speed ! control-brake .
> CR 70 speed ! control-brake .
> CR 100 speed ! control-brake .
>
> \
> \ Tipping Examples
> \
> lingvar service \ level
> -2 -1 0 5 member poor
> 0 5 5 10 member acceptable
> 5 10 11 12 member amazing
>
> lingvar quality \ of food
> -2 -1 0 5 member bad
> 0 5 5 10 member decent
> 5 10 11 12 member great
>
> lingvar tip \ percentage
> 0 singleton low
> 13 singleton medium
> 25 singleton high
>
> variable count
> : tipping-rules
> 0 tip ! 0 count !
> <IF> service <IS> amazing <THEN> 1 .fired count ++ tip <IS> high <END>
> <IF> quality <IS> great <THEN> 2 .fired count ++ tip <IS> high <END>
> <IF> service <IS> acceptable <THEN> 3 .fired count ++ tip <IS> medium <END>
> <IF> service <IS> poor <AND> quality <IS> poor <THEN> 4 .fired count ++ tip <IS> low <END>
> ;
> ' sum is aggregate
> : calc-tip tipping-rules tip @ count @ / ;
>
> CR CR .( Tipping example)
> CR 6 quality ! 9 service ! calc-tip .
> CR 6 quality ! 8 service ! calc-tip .
> CR 9 quality ! 4 service ! calc-tip .
> CR 3 quality ! 3 service ! calc-tip .
>
>
> \
> \ Example - fan control
> \
> lingvar temperature
> -21 -20 20 40 member cool
> 20 40 40 50 member warm
> 40 60 120 121 member hot
>
> lingvar fan-speed
> 30 singleton slow
> 60 singleton medium
> 90 singleton fast
>
>
> : fan-rules
> 0 fan-speed !
> <IF> temperature <IS> cool <THEN> 1 .fired fan-speed <IS> slow <END>
> <IF> temperature <IS> warm <THEN> 2 .fired fan-speed <IS> medium <END>
> <IF> temperature <IS> hot <THEN> 3 .fired fan-speed <IS> fast <END>
> ;
>
> : control-fan fan-rules fan-speed @ ;
> : clip-input cool p1 hot p4 WITHIN NOT ABORT" Input out of range!" ;
> : control2 clip-input temperature ! control-fan ;
>
> CR CR .( Fan Speed control)
> : test 80 0 DO CR I 3 .r space I temperature ! control-fan space 3 .r LOOP ; test

Hi,
I tried to read your program. I see that you have used integers. Me I prefer floats.
You can find a version of fuzzy logic in forth in "forth dimension magazine" Volume 18 N° 6 (written by Rick VanNorman).
You can find it at : http://forth.org/fd/contents.html.

I tested your program, it worked for the 1st and 3rd examples, but the 2nd example does'nt work because the word ++ is not defined in the gforth I use..

In executing the fuzzy system program (fuzzy logic regulator) we are interested in the output not the rules that were fired, but for purposes of debugging we can add the display of the fired rules, and just for this.

Have fun and good discoveries

Re: Fuzzy systems in forth

<936f9de0-4ad1-41fd-8797-e1ea157b5165n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24890&group=comp.lang.forth#24890

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:622a:120b:b0:412:8bb7:25a6 with SMTP id y11-20020a05622a120b00b004128bb725a6mr354699qtx.4.1697097726148;
Thu, 12 Oct 2023 01:02:06 -0700 (PDT)
X-Received: by 2002:a05:6870:c78e:b0:1e9:a13c:ffba with SMTP id
dy14-20020a056870c78e00b001e9a13cffbamr977008oab.9.1697097725922; Thu, 12 Oct
2023 01:02:05 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mixmin.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 12 Oct 2023 01:02:05 -0700 (PDT)
In-Reply-To: <02579c55-8e03-408a-abf1-eded0aa071ben@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2003:f7:1f1d:b5f1:4804:18cf:a570:d2c;
posting-account=AqNUYgoAAADmkK2pN-RKms8sww57W0Iw
NNTP-Posting-Host: 2003:f7:1f1d:b5f1:4804:18cf:a570:d2c
References: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>
<4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com> <06f1344c-6372-47d7-b173-49d2c73b85b1n@googlegroups.com>
<0134b349-6150-4810-b4cf-b3d93fa85ec8n@googlegroups.com> <02579c55-8e03-408a-abf1-eded0aa071ben@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <936f9de0-4ad1-41fd-8797-e1ea157b5165n@googlegroups.com>
Subject: Re: Fuzzy systems in forth
From: minforth@arcor.de (minforth)
Injection-Date: Thu, 12 Oct 2023 08:02:06 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: minforth - Thu, 12 Oct 2023 08:02 UTC

Ahmed MELAHI schrieb am Mittwoch, 11. Oktober 2023 um 20:50:53 UTC+2:
> Le mercredi 11 octobre 2023 à 16:45:36 UTC, Ala'a a écrit :
> I tried to read your program. I see that you have used integers. Me I prefer floats.

Practically all real-life applications of fuzzy logic are in control of technical systems,
eg motors or chemical reactors.

There you often don't have floats available. So binary FL has its important place.

Re: Fuzzy systems in forth

<e8e2a422-b02c-4cc0-bb4e-67be15d9bf14n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24891&group=comp.lang.forth#24891

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:ac8:7d10:0:b0:406:94da:5abd with SMTP id g16-20020ac87d10000000b0040694da5abdmr357203qtb.12.1697098826845;
Thu, 12 Oct 2023 01:20:26 -0700 (PDT)
X-Received: by 2002:a05:6830:84b:b0:6ba:169f:f425 with SMTP id
g11-20020a056830084b00b006ba169ff425mr7395517ott.2.1697098826675; Thu, 12 Oct
2023 01:20:26 -0700 (PDT)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!newsfeed.xs3.de!callisto.xs3.de!gandalf.srv.welterde.de!news.mixmin.net!news.neodome.net!feeder1.feed.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 12 Oct 2023 01:20:26 -0700 (PDT)
In-Reply-To: <936f9de0-4ad1-41fd-8797-e1ea157b5165n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=154.121.62.164; posting-account=KJSw4AoAAACRkUCek5r_78mFj6sHzH4C
NNTP-Posting-Host: 154.121.62.164
References: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>
<4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com> <06f1344c-6372-47d7-b173-49d2c73b85b1n@googlegroups.com>
<0134b349-6150-4810-b4cf-b3d93fa85ec8n@googlegroups.com> <02579c55-8e03-408a-abf1-eded0aa071ben@googlegroups.com>
<936f9de0-4ad1-41fd-8797-e1ea157b5165n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e8e2a422-b02c-4cc0-bb4e-67be15d9bf14n@googlegroups.com>
Subject: Re: Fuzzy systems in forth
From: ahmed.melahi@univ-bejaia.dz (Ahmed MELAHI)
Injection-Date: Thu, 12 Oct 2023 08:20:26 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2190
 by: Ahmed MELAHI - Thu, 12 Oct 2023 08:20 UTC

Le jeudi 12 octobre 2023 à 08:02:07 UTC, minforth a écrit :
> Ahmed MELAHI schrieb am Mittwoch, 11. Oktober 2023 um 20:50:53 UTC+2:
> > Le mercredi 11 octobre 2023 à 16:45:36 UTC, Ala'a a écrit :
> > I tried to read your program. I see that you have used integers. Me I prefer floats.
> Practically all real-life applications of fuzzy logic are in control of technical systems,
> eg motors or chemical reactors.
>
> There you often don't have floats available. So binary FL has its important place.
Agreed.

But for simulations of fuzzy logic control systems, I prefer floats.

Re: Fuzzy systems in forth

<de601e5e-cc30-4298-8fd2-091e92b88a0bn@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24894&group=comp.lang.forth#24894

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:620a:14f:b0:775:810b:2ec with SMTP id e15-20020a05620a014f00b00775810b02ecmr402540qkn.1.1697117226671;
Thu, 12 Oct 2023 06:27:06 -0700 (PDT)
X-Received: by 2002:a05:6808:18a3:b0:3ae:2710:cf87 with SMTP id
bi35-20020a05680818a300b003ae2710cf87mr12290078oib.7.1697117226417; Thu, 12
Oct 2023 06:27:06 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 12 Oct 2023 06:27:05 -0700 (PDT)
In-Reply-To: <e8e2a422-b02c-4cc0-bb4e-67be15d9bf14n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2001:8f8:1c6d:3147:cd3a:9e65:d82b:ad25;
posting-account=mkOgdwoAAAAGDUBcY0S_1Gob1TR-ZY2T
NNTP-Posting-Host: 2001:8f8:1c6d:3147:cd3a:9e65:d82b:ad25
References: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>
<4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com> <06f1344c-6372-47d7-b173-49d2c73b85b1n@googlegroups.com>
<0134b349-6150-4810-b4cf-b3d93fa85ec8n@googlegroups.com> <02579c55-8e03-408a-abf1-eded0aa071ben@googlegroups.com>
<936f9de0-4ad1-41fd-8797-e1ea157b5165n@googlegroups.com> <e8e2a422-b02c-4cc0-bb4e-67be15d9bf14n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <de601e5e-cc30-4298-8fd2-091e92b88a0bn@googlegroups.com>
Subject: Re: Fuzzy systems in forth
From: amalawi@gmail.com (Ala'a)
Injection-Date: Thu, 12 Oct 2023 13:27:06 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2625
 by: Ala'a - Thu, 12 Oct 2023 13:27 UTC

On Thursday, October 12, 2023 at 12:20:27 PM UTC+4, Ahmed MELAHI wrote:
> Le jeudi 12 octobre 2023 à 08:02:07 UTC, minforth a écrit :
> > Ahmed MELAHI schrieb am Mittwoch, 11. Oktober 2023 um 20:50:53 UTC+2:
> > > Le mercredi 11 octobre 2023 à 16:45:36 UTC, Ala'a a écrit :
> > > I tried to read your program. I see that you have used integers. Me I prefer floats.
> > Practically all real-life applications of fuzzy logic are in control of technical systems,
> > eg motors or chemical reactors.
> >
> > There you often don't have floats available. So binary FL has its important place.
> Agreed.
>
> But for simulations of fuzzy logic control systems, I prefer floats.

Hi,

sure both are fine. my goal was concept realization without distraction with FP details. Moreover, if you dabble with Forth in micro controllers, most are FP-less (if that was a word!) but again on PC it is doable in both.

++ word is incrementing by one
: ++ ( a -- ) 1 SWAP +! ;

Regards,

Re: Fuzzy systems in forth

<3f5c2749-c096-4786-b0db-554f9b8cbce0n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24896&group=comp.lang.forth#24896

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:620a:143:b0:774:f02:7e59 with SMTP id e3-20020a05620a014300b007740f027e59mr405912qkn.11.1697120152894;
Thu, 12 Oct 2023 07:15:52 -0700 (PDT)
X-Received: by 2002:a05:6808:2001:b0:3ac:b428:844d with SMTP id
q1-20020a056808200100b003acb428844dmr12040575oiw.8.1697120152678; Thu, 12 Oct
2023 07:15:52 -0700 (PDT)
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 12 Oct 2023 07:15:52 -0700 (PDT)
In-Reply-To: <de601e5e-cc30-4298-8fd2-091e92b88a0bn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=154.121.62.164; posting-account=KJSw4AoAAACRkUCek5r_78mFj6sHzH4C
NNTP-Posting-Host: 154.121.62.164
References: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>
<4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com> <06f1344c-6372-47d7-b173-49d2c73b85b1n@googlegroups.com>
<0134b349-6150-4810-b4cf-b3d93fa85ec8n@googlegroups.com> <02579c55-8e03-408a-abf1-eded0aa071ben@googlegroups.com>
<936f9de0-4ad1-41fd-8797-e1ea157b5165n@googlegroups.com> <e8e2a422-b02c-4cc0-bb4e-67be15d9bf14n@googlegroups.com>
<de601e5e-cc30-4298-8fd2-091e92b88a0bn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3f5c2749-c096-4786-b0db-554f9b8cbce0n@googlegroups.com>
Subject: Re: Fuzzy systems in forth
From: ahmed.melahi@univ-bejaia.dz (Ahmed MELAHI)
Injection-Date: Thu, 12 Oct 2023 14:15:52 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Ahmed MELAHI - Thu, 12 Oct 2023 14:15 UTC

Le jeudi 12 octobre 2023 à 13:27:08 UTC, Ala'a a écrit :
> On Thursday, October 12, 2023 at 12:20:27 PM UTC+4, Ahmed MELAHI wrote:
> > Le jeudi 12 octobre 2023 à 08:02:07 UTC, minforth a écrit :
> > > Ahmed MELAHI schrieb am Mittwoch, 11. Oktober 2023 um 20:50:53 UTC+2:
> > > > Le mercredi 11 octobre 2023 à 16:45:36 UTC, Ala'a a écrit :
> > > > I tried to read your program. I see that you have used integers. Me I prefer floats.
> > > Practically all real-life applications of fuzzy logic are in control of technical systems,
> > > eg motors or chemical reactors.
> > >
> > > There you often don't have floats available. So binary FL has its important place.
> > Agreed.
> >
> > But for simulations of fuzzy logic control systems, I prefer floats.
> Hi,
>
> sure both are fine. my goal was concept realization without distraction with FP details. Moreover, if you dabble with Forth in micro controllers, most are FP-less (if that was a word!) but again on PC it is doable in both.
>
Agreed.

> ++ word is incrementing by one
> : ++ ( a -- ) 1 SWAP +! ;
>
Now, Example 2 works.

> Regards,
Salutations

Re: Fuzzy systems in forth

<08a9477b-0828-4249-8930-2e8d7cc8c943n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24898&group=comp.lang.forth#24898

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:620a:1915:b0:773:ad95:aa16 with SMTP id bj21-20020a05620a191500b00773ad95aa16mr394710qkb.4.1697130618633;
Thu, 12 Oct 2023 10:10:18 -0700 (PDT)
X-Received: by 2002:a05:6870:9126:b0:1e9:af97:9fa3 with SMTP id
o38-20020a056870912600b001e9af979fa3mr1391479oae.5.1697130618387; Thu, 12 Oct
2023 10:10:18 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 12 Oct 2023 10:10:17 -0700 (PDT)
In-Reply-To: <0134b349-6150-4810-b4cf-b3d93fa85ec8n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=154.121.62.164; posting-account=KJSw4AoAAACRkUCek5r_78mFj6sHzH4C
NNTP-Posting-Host: 154.121.62.164
References: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>
<4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com> <06f1344c-6372-47d7-b173-49d2c73b85b1n@googlegroups.com>
<0134b349-6150-4810-b4cf-b3d93fa85ec8n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <08a9477b-0828-4249-8930-2e8d7cc8c943n@googlegroups.com>
Subject: Re: Fuzzy systems in forth
From: ahmed.melahi@univ-bejaia.dz (Ahmed MELAHI)
Injection-Date: Thu, 12 Oct 2023 17:10:18 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 8549
 by: Ahmed MELAHI - Thu, 12 Oct 2023 17:10 UTC

Le mercredi 11 octobre 2023 à 16:45:36 UTC, Ala'a a écrit :
> I was having fun learning FL, still do, and thought to share what I understood till now. so here is my take as a newbie to FL. I hope i did not make too many mistakes (especially any conceptual ones :) ). The formatting my be messed up.
>
> VOCABULARY FuzzyLogic21 ALSO FuzzyLogic21 DEFINITIONS
> \ Assume trapizoidal inputs
> \ Assume range is bounded by p1 and p4
> \ Assume singleton outputs
> \ no verification of rules being complete (covering all cases) is done in this implementation
> : lingvar VARIABLE ; \ lingustic variable (input or output is a variable here)
> : member ( p1 p2 p3 p3 --) CREATE >R SWAP ROT , , , R> , ; \ define input m.ship. function, simple without linking lingvar
> : p1 @ ; : p2 CELL+ @ ; : p3 2 CELLS + @ ; : p4 3 CELLS + @ ;
> : pts ( mem -- p1 p2 p3 p4 ) >R R@ p1 R@ p2 R@ p3 R> p4 ;
> : singleton ( val -- ) CONSTANT ; \ 'member' word could be used with p1, but seemed a waste!
> : seg1 ( mem -- p1 p2 ) >R R@ p1 1+ R> p2 1- ; \ ascending line
> : seg2 ( mem -- p2 p3 ) >R R@ p2 R> p3 ; \ horizontal line
> : seg3 ( mem -- p3 p4 ) >R R@ p3 1+ R> p4 1- ; \ desc. line
> 1000 VALUE res \ resolution of the 1==1000 value membership
> : interpo ( xm y1 y2 -- ym ) >R TUCK - res * SWAP R> - / ABS ; \ interpolate. used for both asc. and desc. lines.
> : ]clip[ ( val p1 p2 -- clipped ) ROT DUP >R -ROT 1+ WITHIN ABS R> * ; \ val or zero if out of range
> : fuzz ( crisp mem -- fz1 fz2 fz3 ) \ trapizoidal interpolation. is it possible to improve this?
> 2DUP seg1 ]clip[ IF 2DUP seg1 interpo ELSE 0 THEN -ROT
> 2DUP seg2 ]clip[ IF res ELSE 0 THEN -ROT
> 2DUP seg3 ]clip[ IF seg3 SWAP interpo ELSE 2DROP 0 THEN ;
>
> DEFER aggregate
> : maximize ( val var -- ) TUCK @ MAX SWAP ! ;
> : minimize ( val var -- ) TUCK @ MIN SWAP ! ;
> : sum ( val var -- ) +! ;
> ' maximize IS aggregate
> : defuz ( fuz-val lingvar singleton -- ) ROT res */ SWAP aggregate ;
>
> \ Rules compilation
> SYNONYM ` POSTPONE
> DEFER <IS> IMMEDIATE
> DEFER <AND> IMMEDIATE
> : nop ;
> : compile-at ` @ ;
> : compile-fuzz-min ` fuzz ` + ` + ` min ; ' compile-fuzz-min IS <AND>
> : compile-<and>-defuz ` defuz ` DUP ;
> : antecedent-scope ['] compile-at IS <IS> ['] compile-fuzz-min IS <AND> ;
> : consequence-scope ['] nop IS <IS> ['] compile-<and>-defuz IS <AND> ;
> : <IF> ` res ` 1+ antecedent-scope ; IMMEDIATE \ max fuz is res, thus 'res 1+' will do with MIN
> : <THEN> ` <AND> ` DUP ` IF ` DUP consequence-scope ; IMMEDIATE
> : <END> ` defuz ` THEN ` DROP ; IMMEDIATE
> : .fired ." Rule " . ." fired! " ;
>
> \
> \ Example - car brakes
> \
> lingvar speed \ in KM\H
> 0 10 40 50 member low
> 40 60 80 100 member medium
> 80 100 320 325 member high
>
> lingvar distance \ in meters
> -1 0 20 50 member close
> 30 60 80 100 member near
> 80 110 1000 1005 member far
>
> lingvar brake \ measured in N or poundal (this example is illustrative rather than giving experience/measured forces)
> 0 singleton light
> 10 singleton moderate
> 30 singleton hard
>
> : brake-rules
> 0 brake !
> <IF> distance <IS> close <AND> speed <IS> high <THEN> 1 .fired brake <IS> hard <END>
> <IF> distance <IS> close <AND> speed <IS> medium <THEN> 2 .fired brake <IS> hard <END>
> <IF> distance <IS> close <AND> speed <IS> low <THEN> 3 .fired brake <IS> moderate <END>
> <IF> distance <IS> near <AND> speed <IS> high <THEN> 4 .fired brake <IS> hard <END>
> <IF> distance <IS> near <AND> speed <IS> medium <THEN> 5 .fired brake <IS> moderate <END>
> <IF> distance <IS> near <AND> speed <IS> low <THEN> 6 .fired brake <IS> light <END>
> <IF> distance <IS> far <THEN> 7 .fired brake <IS> light <END>
> ;
>
>
> : control-brake ( -- b ) brake-rules brake @ ;
>
> CR CR .( Car Brakes example) CR
> 2 distance !
> CR 15 speed ! control-brake .
> CR 70 speed ! control-brake .
> CR 100 speed ! control-brake .
> CR
> 50 distance !
> CR 45 speed ! control-brake .
> CR 90 speed ! control-brake .
> CR
> 50 distance !
> CR 15 speed ! control-brake .
> CR 70 speed ! control-brake .
> CR 100 speed ! control-brake .
> CR
> 200 distance !
> CR 15 speed ! control-brake .
> CR 70 speed ! control-brake .
> CR 100 speed ! control-brake .
>
> \
> \ Tipping Examples
> \
> lingvar service \ level
> -2 -1 0 5 member poor
> 0 5 5 10 member acceptable
> 5 10 11 12 member amazing
>
> lingvar quality \ of food
> -2 -1 0 5 member bad
> 0 5 5 10 member decent
> 5 10 11 12 member great
>
> lingvar tip \ percentage
> 0 singleton low
> 13 singleton medium
> 25 singleton high
>
> variable count
> : tipping-rules
> 0 tip ! 0 count !
> <IF> service <IS> amazing <THEN> 1 .fired count ++ tip <IS> high <END>
> <IF> quality <IS> great <THEN> 2 .fired count ++ tip <IS> high <END>
> <IF> service <IS> acceptable <THEN> 3 .fired count ++ tip <IS> medium <END>
> <IF> service <IS> poor <AND> quality <IS> poor <THEN> 4 .fired count ++ tip <IS> low <END>
> ;
> ' sum is aggregate
> : calc-tip tipping-rules tip @ count @ / ;
>
> CR CR .( Tipping example)
> CR 6 quality ! 9 service ! calc-tip .
> CR 6 quality ! 8 service ! calc-tip .
> CR 9 quality ! 4 service ! calc-tip .
> CR 3 quality ! 3 service ! calc-tip .
>
>
> \
> \ Example - fan control
> \
> lingvar temperature
> -21 -20 20 40 member cool
> 20 40 40 50 member warm
> 40 60 120 121 member hot
>
> lingvar fan-speed
> 30 singleton slow
> 60 singleton medium
> 90 singleton fast
>
>
> : fan-rules
> 0 fan-speed !
> <IF> temperature <IS> cool <THEN> 1 .fired fan-speed <IS> slow <END>
> <IF> temperature <IS> warm <THEN> 2 .fired fan-speed <IS> medium <END>
> <IF> temperature <IS> hot <THEN> 3 .fired fan-speed <IS> fast <END>
> ;
>
> : control-fan fan-rules fan-speed @ ;
> : clip-input cool p1 hot p4 WITHIN NOT ABORT" Input out of range!" ;
> : control2 clip-input temperature ! control-fan ;
>
> CR CR .( Fan Speed control)
> : test 80 0 DO CR I 3 .r space I temperature ! control-fan space 3 .r LOOP ; test

Hi again,

I took another look at your program, I have a question:

the formula used for defuzzification must be:

y* = ( sum(mfi(x) * yi))/(sum(mfi(x)))

the summation is over i = 1...R, R is the number of rules
and
where x is the input (or inputs),
yi the singleton used in the consequence of the i th rule
mfi is the membership function resulting from the antecedent or the i th rule
an y* is the output

have you used this formula, because I can't see the division by (sum(mfi(x))).

Best regards

Re: Fuzzy systems in forth

<2d135b4a-36a1-4902-bb76-c0724f28d86en@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24899&group=comp.lang.forth#24899

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:622a:1a8b:b0:412:26ce:857f with SMTP id s11-20020a05622a1a8b00b0041226ce857fmr424266qtc.12.1697137104647;
Thu, 12 Oct 2023 11:58:24 -0700 (PDT)
X-Received: by 2002:a05:6808:144d:b0:3ae:7044:fdce with SMTP id
x13-20020a056808144d00b003ae7044fdcemr13799625oiv.7.1697137104419; Thu, 12
Oct 2023 11:58:24 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 12 Oct 2023 11:58:23 -0700 (PDT)
In-Reply-To: <08a9477b-0828-4249-8930-2e8d7cc8c943n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2001:8f8:1c6d:3147:cd3a:9e65:d82b:ad25;
posting-account=mkOgdwoAAAAGDUBcY0S_1Gob1TR-ZY2T
NNTP-Posting-Host: 2001:8f8:1c6d:3147:cd3a:9e65:d82b:ad25
References: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>
<4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com> <06f1344c-6372-47d7-b173-49d2c73b85b1n@googlegroups.com>
<0134b349-6150-4810-b4cf-b3d93fa85ec8n@googlegroups.com> <08a9477b-0828-4249-8930-2e8d7cc8c943n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <2d135b4a-36a1-4902-bb76-c0724f28d86en@googlegroups.com>
Subject: Re: Fuzzy systems in forth
From: amalawi@gmail.com (Ala'a)
Injection-Date: Thu, 12 Oct 2023 18:58:24 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2043
 by: Ala'a - Thu, 12 Oct 2023 18:58 UTC

Hi Ahmed,

Your observation about the missing division is a near hit :)

Kindly correct me if I am wrong. There is aggregation and then Defuzzification. Aggregation result is sets, Defuzzification result is crisp values. Both of these are kind of intermingled (implemented) in 'defuz' and 'aggregate' words. The default of the deffered 'aggregate' is 'maximize'. however, for the tipping example it was 'sum' and then with the help of 'count' variable I cheated and did (my) average by dividing by the sum of count rather than sum of fired rules' antecedents values.

Regards,

Re: Fuzzy systems in forth

<197b7760-5f9e-441d-8177-8649daf963ddn@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24910&group=comp.lang.forth#24910

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:620a:40d2:b0:76e:f8bb:ff6 with SMTP id g18-20020a05620a40d200b0076ef8bb0ff6mr443170qko.0.1697185078804;
Fri, 13 Oct 2023 01:17:58 -0700 (PDT)
X-Received: by 2002:a05:6808:208a:b0:3ae:11ee:b66f with SMTP id
s10-20020a056808208a00b003ae11eeb66fmr13937387oiw.3.1697185078537; Fri, 13
Oct 2023 01:17:58 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Fri, 13 Oct 2023 01:17:58 -0700 (PDT)
In-Reply-To: <2d135b4a-36a1-4902-bb76-c0724f28d86en@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=154.121.93.164; posting-account=KJSw4AoAAACRkUCek5r_78mFj6sHzH4C
NNTP-Posting-Host: 154.121.93.164
References: <6ef090be-9f2c-4546-b779-77348f9c7454n@googlegroups.com>
<4a2c2978-a189-43f4-892c-5c88cf994646n@googlegroups.com> <06f1344c-6372-47d7-b173-49d2c73b85b1n@googlegroups.com>
<0134b349-6150-4810-b4cf-b3d93fa85ec8n@googlegroups.com> <08a9477b-0828-4249-8930-2e8d7cc8c943n@googlegroups.com>
<2d135b4a-36a1-4902-bb76-c0724f28d86en@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <197b7760-5f9e-441d-8177-8649daf963ddn@googlegroups.com>
Subject: Re: Fuzzy systems in forth
From: ahmed.melahi@univ-bejaia.dz (Ahmed MELAHI)
Injection-Date: Fri, 13 Oct 2023 08:17:58 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3652
 by: Ahmed MELAHI - Fri, 13 Oct 2023 08:17 UTC

Le jeudi 12 octobre 2023 à 18:58:26 UTC, Ala'a a écrit :
> Hi Ahmed,
>
> Your observation about the missing division is a near hit :)
>
> Kindly correct me if I am wrong. There is aggregation and then Defuzzification. Aggregation result is sets, Defuzzification result is crisp values. Both of these are kind of intermingled (implemented) in 'defuz' and 'aggregate' words. The default of the deffered 'aggregate' is 'maximize'. however, for the tipping example it was 'sum' and then with the help of 'count' variable I cheated and did (my) average by dividing by the sum of count rather than sum of fired rules' antecedents values.
>
> Regards,

Hi,
Your program is ok for just these use cases (by construction, your trapezoidal functions sums to 1 (full grade) for any input x) and the result is good because you divide by 1 (full grade) (ie sum(mfi(x))=fullgrade).
but in general, in fuzzy logic, we haven't that condition fulfilled all the time and the membership functions are with different forms and parameters and perhaps can be tuned by learning algorithms (see neuro-fuzzy systems and control).

another remark:
for the example n°1, in the word brake-rules, these fuzzy propositions are caluclated and recalculated again and again at runtime. but we need just one calulation and use the result several times (we haven't to recalculate them):
distance <IS> close, calculated 3 times
distance <IS> near, calculated 3 times
....
(N.B. I have used the same recalcualtions in the first psot of the fuzzy system program see:
https://groups.google.com/g/comp.lang.forth/c/KkXtmylnibE
) but I mdified it to avoid these recalculations in this thread.

I think, the best way is to separate the stages: fuzzification, inference (fuzzy conjunction, fuzzy implication and fuzzy aggregation), then defuzzification at the end.
the inference use the rule base to get a function (distribution) (here i noted it mfi(x) for the i th rule.
the deffuzification calculate a crisp (numeric) output using this distribution. And there several methods.

Bye, Enjoy.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor