Posts Tagged: Ants


11
Dec 10

An F# Ant Colony Simulation in Silverlight 4.0 with Dynamic AI Loading

I’ve been enviously watching Phillip Trelford publish excellent F# games all week and tonight I just couldn’t stand it anymore.  I stayed in, rolled up my sleeves and ported the very same ant colony simulation I used in my CUFP workshop to Silverlight 4.0.

Install Microsoft Silverlight

Wow, just look at those little guys go at it.  Silverlight sure is pretty!

As you might have noticed by the big white “Click To Load Custom AI” message, you can also compile your own AI and battle it out with what I’ve included here.  To make things easy I’ve provided a ready to go solution.  Simply load it up, compile it and select the compiled DLL.

Once you’ve loaded your AI the game will immediately start.  Best of luck to you against my little monsters, so far they’ve been undefeated.  If you happen to come up with an ant-dominating example I hope you’ll post it here in the comments.  There were some really creative ideas in the CUFP workshop and I’d love to see what could be done with more than just four hours.

If your interested in the gory details of the simulation itself, I’ve put up a github repository with the entirety of the code.  And just in case you’re wondering, I’m still planning on running that contest.  Expect a ton of cool new AI features and a big focus on combat.

Cheers!

Update:  I’ve mucked with a few things.  Mainly, it will be a bit nicer when handling AI exceptions now.  While I was at it I tweaked some of the game world parameters and so you’ll need to re-download the example solution if you already have it.


14
Oct 10

A CUFP Tutorial, F# Day at the … Oh God, Ants!

Everyone was ready for a nice relaxing day at the beach when they showed up for my CUFP tutorial…

A Day at the Beach, Interrupted

So, after realizing how boring it would be teach thirty flavors of factorial to experienced functional programmers for four hours, I thought I’d spice things up a bit. AI is always fun and I do love ants, so why not a contest where attendees would write Ant AI to gather food? For a bit of additional motivation to get everyone over the post-lunch slump, I decided to give away one of my Visual Studio 2010 Ultimate Edition MSDN subscriptions as a prize.

Just look at those little guys go.

In thinking about how I would run this contest, it occurred to me that the AI better be pretty frickin easy to write. Four hours is not a lot of time to both learn a language and write something cool in it. Below is a modified (to fit nicely on my blog) version of the Ant AI for the red colony above, which is what I provided as a baseline sample for participants to test against.

match me with | HasFood | HasMaxFood -> match locations with | HasUnownedFood cells when not (here.HasPheromone me.Color) -> DropPheromone (here, 100) | NearHome homeCells -> match homeCells with | CanDrop dropCells -> DropFood dropCells.Head | CantDrop -> Move homeCells.Head | AwayFromHome allCells -> if not homeDirectionCell.ContainsAnt then Move homeDirectionCell else match randomEmptyLocation locations with | Some location -> Move location | None -> Nothing | HasNoFood -> match locations with | HasUnownedFood foodCells -> TakeFood (maxFood foodCells) | SmellsPheromones pheroCells -> Move (maxPhero pheroCells) | _ -> match randomEmptyLocation locations with | Some location -> Move location | None -> Nothing

We all had a great time. Most didn’t even take the given breaks other than to run out quickly to the bathroom or grab a coffee. When time finally ran out I was proud to have three finalists who stayed and watched their ants battle it out, one of whom completed the entire project in Mono/Linux (Mono on MacOS, not so much).

Making it this far was an accomplishment.

The three finalists: Frank Levine, Phil Clayton and Paul Greene.

Paul’s rather clever ants did what they could to block the opponent’s nest while gathering food. Phil, despite having some delays getting going due to my lack of experience with mono, made an excellent showing. However, in the end little could be done to stem the tide of Frank Levine’s pheromone gradient following ants.

As runner up, Phil will be getting a fresh copy of Professional F# 2.0 as soon as they hit my doorstop. Frank won the day and took home his very own MSDN Ultimate subscription.

Frank wins the day.

All of the tutorial feedback was great. To my surprise, those who had trouble getting my code running on their Macs seem to have had a fun time. For days afterward I received emails from people who kept working on the problem even after they went home from the conference!

Currently I’m working on putting together a larger version of this contest online. I hope to extend the model used in this to include combat, spawning new ants and maybe even decaying dead ants. Send me a message on twitter if you’re interested in helping to beta test the engine.

I want to thank all of the attendees for coming, having fun, and putting up with the various cross platform issues. I also want to thank Martin Logan and Anil Madhavapeddy for helping to make this session happen. Finally, I want to thank Paul Greene for reporting a behavior related bug he found afterward through experimentation. It’s fixed now in my local source, but not in the files you can sneakily grab from the Mono bug report above ;).

I hope all of you CUFP tutorialists will join me in the next iteration of this contest. It’s sure to be a blast.