[Haskell-cafe] Fuzzy Logic / Linguistic Variables

Neal Alexander relapse.dev at gmx.com
Wed Oct 14 08:59:54 EDT 2009

So i was reading "Programming Game AI by Example" by Mat Buckland 
(http://www.ai-junkie.com/books/toc_pgaibe.html) and decided to rewrite 
his chapter on Fuzzy logic in haskell (from C++).

My initial impression: its one of those scenarios where OOP grossly over 
complicates things

Heres an example taken from the book: An agent needs to decide what 
weapon to use based on his distance from the target and the amount of 
ammo each has. The result is in the desirability domain (0-100).


An excerpt:


weapons = [ Weapon "RocketLauncher" 9 rocketLauncher,
             Weapon "ShotGun" 13 shotgun,
             Weapon "AssaultRifle" 120 assaultRifle,
             Weapon "SniperRifle" 7 sniperRifle,
             Weapon "Knife" 1 knife ]

chooseWeapon dist = maximum ratings
     ratings = [ (f dist ammo, n) | Weapon n ammo f <- weapons ]


shotgun :: Double -> Double -> Double
shotgun dist ammo =

   unfuzz desireDom $ rules (fuzz distDom dist) (fuzz ammoDom ammo)


     rules :: Fuzzy Distances -> Fuzzy Ammo -> FL Desirability
     rules distance ammo = do

       distance `is` SniperSuited =>> Pointless
       distance `is` Far          =>> Undesirable
       distance `is` Medium       =>> Undesirable
       distance `is` Melee        =>> Undesirable

       (distance `fairly` Near) & (ammo `fairly` High) =>> VeryDesirable
       (distance `fairly` Near) & (ammo `fairly` Good) =>> Desirable
       (distance `fairly` Near) & (ammo `is` Low)      =>> Undesirable


Full code at http://code.haskell.org/~hexpuem/fuzzyLogic/.

Suggestions welcome - maybe it'd be useful to upload to hackage at some 

It only supports triangle, shoulder, and singleton memberships at the 

More information about the Haskell-Cafe mailing list