## fuzzy logic example

bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

### fuzzy logic example

Code: Select all

`type fuzz  dim as single a , b , c , d  declare sub fill( na as single , nb as single , nc as single , nd as single )end type sub fuzz.fill( na as single , nb as single , nc as single , nd as single )  this.a = na  this.b = nb  this.c = nc  this.d = ndend subdeclare function fuzzy( x as single , f as fuzz ) as singledeclare function fuzmass( h as single , f as fuzz ) as singledeclare function fuzpoint( f as fuzz ) as singledeclare function max( a as single , b as single ) as singledeclare function min( a as single , b as single ) as singledeclare sub plot( x as integer , y as integer , z as integer )''ceate the nesery fuzzyobjectsdim as fuzz driverisyoung , driverismidle , driverisolddim as fuzz carisslow , carisaverage , carisfastdim as fuzz payislow , payismidle , payishigh''fill fuzzyobjects whit valuesdriverisyoung.fill 0 , 0 , 18 , 25 driverismidle.fill 18 , 25 , 50 , 70 driverisold.fill 50 , 70 , 120 , 120 carisslow.fill 0 , 0 , 120 , 140 carisaverage.fill 120 , 140 , 170 , 200 carisfast.fill 170 , 200 , 400 , 400 payislow.fill 0 , 1 , 20 , 40 payismidle.fill 20 , 40 , 60 , 80 payishigh.fill 60 , 80 , 99 , 100 dim as single driveryoung , drivermidle , driverolddim as single carslow , caraverage , carfastdim as single rischigh , riscmidle , risclow , a , b , speed , agedim as single ml , mm , mh , pl , pm , ph , payscreen 20 , 32  for speed = 400 to 0 step -1    for age = 0 to 120      ''fuzzyfy input      driveryoung = fuzzy( age , driverisyoung )      drivermidle = fuzzy( age , driverismidle )      driverold = fuzzy( age , driverisold )      carslow = fuzzy( speed , carisslow )      caraverage = fuzzy( speed , carisaverage )      carfast = fuzzy( speed , carisfast )      ''ceate a rule set      ''if car is fast      ''or car is average and( not driver is midle )then risc is high      rischigh = max( carfast , min( caraverage , 1 - drivermidle ) )      ''if car is slow and not( driver is midle )      ''or car is average      ''or driver is midle and car is fast then risk is midle      a = min( carslow , 1 - drivermidle )      b = min( carfast , drivermidle )      riscmidle = max( a , max( caraverage , b ) )      ''if car is slow then risc is low      risclow = carslow      ''defuzzyfycation      ml = fuzmass( risclow , payislow )      mm = fuzmass( riscmidle , payismidle )      mh = fuzmass( rischigh , payishigh )      pl = fuzpoint( payislow )      pm = fuzpoint( payismidle )      ph = fuzpoint( payishigh )      pay = ( ml * pl + mm * pm + mh * ph ) / ( ml + mm + mh + 1e-10 )      plot speed , age , pay    next age  next speeddoloop while inkey = ""endsub plot( x as integer , y as integer , z as integer )  dim as integer r , g , b  r = z * 2.5  if ( int( x / 10 ) + int( y / 10 ) ) mod 2 then g = 255  b = 255 - z * 2.5  line ( 300 + y / 2 + x , 350 + y ) _  - ( 300 + y / 2 + x , 350 + y - z ) , rgb( r , g , b )end subfunction fuzzy( x as single , f as fuzz ) as single''what is membervalue of x in fuzzy-object\$  dim uit as single  if f.b <= x and x <= f.c then uit = 1  if f.a < x and x < f.b then    uit = ( x - f.a ) / ( f.b - f.a )  end if  if f.c < x and x < f.d then    uit = 1 - ( x - f.c ) / ( f.d - f.c )  end if  return uitend functionfunction fuzmass( h as single , f as fuzz ) as single''calulate mass of toped fuzzy-object\$    return ( (f.c-f.b) + (f.b-f.a)/2 + (f.d-f.c)/2 )*hend functionfunction fuzpoint( f as fuzz ) as single''calulate value of led point  dim as single ml , mm , mr , pl , pm , pr  ml = ( f.b - f.a ) / 2  mm = f.c - f.b  mr = ( f.d - f.c ) / 2  pl = ( f.b + f.a ) / 2  pm = ( f.c + f.b ) / 2  pr = ( f.d + f.c ) / 2  return ( ml * pl + mm * pm + mr * pr ) / ( ml + mm + mr )end functionfunction max( a as single , b as single ) as single  if a > b then return a  return bend functionfunction min( a as single , b as single ) as single  if a < b then return a  return bend function`