I didn't read the book, but your code seems very elegant, more than I even thought possible. I've never programmed with fuzzy logic before, but I can understand your code - it reads naturally.<br><br clear="all">Fernando Henrique Sanches<br>
<br><br><div class="gmail_quote">On Wed, Oct 14, 2009 at 9:59 AM, Neal Alexander <span dir="ltr"><<a href="mailto:relapse.dev@gmx.com">relapse.dev@gmx.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
So i was reading "Programming Game AI by Example" by Mat Buckland (<a href="http://www.ai-junkie.com/books/toc_pgaibe.html" target="_blank">http://www.ai-junkie.com/books/toc_pgaibe.html</a>) and decided to rewrite his chapter on Fuzzy logic in haskell (from C++).<br>
<br>
My initial impression: its one of those scenarios where OOP grossly over complicates things<br>
<br>
<br>
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).<br>
<br>
<a href="http://code.haskell.org/%7Ehexpuem/fuzzyLogic/AI/Logic/Fuzzy/Example.hs" target="_blank">http://code.haskell.org/~hexpuem/fuzzyLogic/AI/Logic/Fuzzy/Example.hs</a><br>
<br>
An excerpt:<br>
<br>
<br>
------------------------------------<br>
<br>
weapons = [ Weapon "RocketLauncher" 9 rocketLauncher,<br>
Weapon "ShotGun" 13 shotgun,<br>
Weapon "AssaultRifle" 120 assaultRifle,<br>
Weapon "SniperRifle" 7 sniperRifle,<br>
Weapon "Knife" 1 knife ]<br>
<br>
chooseWeapon dist = maximum ratings<br>
where<br>
ratings = [ (f dist ammo, n) | Weapon n ammo f <- weapons ]<br>
<br>
------------------------------------<br>
<br>
shotgun :: Double -> Double -> Double<br>
shotgun dist ammo =<br>
<br>
unfuzz desireDom $ rules (fuzz distDom dist) (fuzz ammoDom ammo)<br>
<br>
where<br>
<br>
rules :: Fuzzy Distances -> Fuzzy Ammo -> FL Desirability<br>
rules distance ammo = do<br>
<br>
distance `is` SniperSuited =>> Pointless<br>
distance `is` Far =>> Undesirable<br>
distance `is` Medium =>> Undesirable<br>
distance `is` Melee =>> Undesirable<br>
<br>
(distance `fairly` Near) & (ammo `fairly` High) =>> VeryDesirable<br>
(distance `fairly` Near) & (ammo `fairly` Good) =>> Desirable<br>
(distance `fairly` Near) & (ammo `is` Low) =>> Undesirable<br>
<br>
------------------------------------<br>
<br>
Full code at <a href="http://code.haskell.org/%7Ehexpuem/fuzzyLogic/" target="_blank">http://code.haskell.org/~hexpuem/fuzzyLogic/</a>.<br>
<br>
Suggestions welcome - maybe it'd be useful to upload to hackage at some point.<br>
<br>
<br>
It only supports triangle, shoulder, and singleton memberships at the moment.<br>
<br>
<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>