fuzzy logic example

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

fuzzy logic example

Postby bluatigro » May 04, 2012 13:04

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 = nd
end sub
declare function fuzzy( x as single , f as fuzz ) as single
declare function fuzmass( h as single , f as fuzz ) as single
declare function fuzpoint( f as fuzz ) as single
declare function max( a as single , b as single ) as single
declare function min( a as single , b as single ) as single
declare sub plot( x as integer , y as integer , z as integer )

''ceate the nesery fuzzyobjects
dim as fuzz driverisyoung , driverismidle , driverisold
dim as fuzz carisslow , carisaverage , carisfast
dim as fuzz payislow , payismidle , payishigh
''fill fuzzyobjects whit values
driverisyoung.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 , driverold
dim as single carslow , caraverage , carfast
dim as single rischigh , riscmidle , risclow , a , b , speed , age
dim as single ml , mm , mh , pl , pm , ph , pay
screen 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 speed
do
loop while inkey = ""
end
sub 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 sub
function 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 uit
end function
function 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 )*h
end function
function 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 function
function max( a as single , b as single ) as single
  if a > b then return a
  return b
end function
function min( a as single , b as single ) as single
  if a < b then return a
  return b
end function

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 3 guests