Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: Ants Sandbox - an ants simulator (ants-sandbox.io)
367 points by tulustul on July 10, 2022 | hide | past | favorite | 61 comments
I was inspired to make a web based ants simulator after watching this video https://www.youtube.com/watch?v=81GQNPJip2Y

Any feedback appreciated.

Best viewed on Chromium based browsers. Firefox is slow for some reason and Safari is not tested as I don't have a Mac.




One thing that is missing from this simulation is that (at least some) ant species walk straight back to their nest once they found some food. They do this (as far as we know) by accumulating each step they take into what is called the "home vector", effectively integrating the whole path they took. That way they always know which way is "back to the nest". There is a vast amount of papers on the topic. Some (personal) highlights:

If you put the ants on stilts they will follow the same home vector, but because each step on stilts is effectively longer they will overshoot the target: https://www.researchgate.net/figure/Fig4-Odometry-by-stride-...

If the ant is placed close to the nest after gathering food, it will still follow its home vector ... away from the nest: https://royalsocietypublishing.org/doi/abs/10.1098/rspb.2004...

Crazy little buggers these ants.


Hah, that reminds me of a robotics competition I took part in at school.

Robots were placed in an arena, and the objective was to gather QR-marked boxes, and bring them back "home".

There were also QR codes placed at known locations around the arena, and the intention was that you could use them for navigation. However, the camera systems were pretty flakey (especially under unpredictable lighting conditions), so we wanted to avoid using them as much as possible. So, we put rotary encoders on our wheels, and integrated the readings to calculate a "home vector" for the return journey, just like you described.

At the time, I wasn't aware that this was an ant-inspired technique - but it was very effective, and we won the competition. Thanks, ants!


The crazy thing is how good the ants are at this. Especially considering their brain size.

With robots it works ok, but over time small errors accumulate. After travelling for a while the home error will be off.

In contrast desert ants are able to do this trick after travelling for hundreds of meters.


I’m sure the ants have some kind of error correction, and even if it is a one-off robot, you can apply a personal error correction, but it will be specific to that robot. There is no “general” amount of error because every motor/encoder/circuit will provide different levels of error.


You can't just "error correct" accumulated random errors. The only way to be accurate is to never lose precision in the first place (which makes the ants especially impressive).


thants


Bless you ants.

Blants. [0]

[0] https://www.youtube.com/watch?v=gaI6kBVyu00 - Look Around You


Gives me nostalgia for playing Sim Ant when I was young.

https://en.wikipedia.org/wiki/SimAnt


Sim Ant and Sim Earth are both begging for remakes!


Yup, I loved both those games. SimEarth in particular would be fantastic for a remake... lots of stuff you can learn about climate change from that game. Maxis did such excellent work building the systems that you could probably work from the original sim logic and build something massive in Unity mostly focusing on the graphics.


I don't understand why there's no true SimCity remake (not this Mobile game crap). It's a surefire commercial success.


Even the spiritual successor Cities Skylines is getting old at this point. There's a few other city-building games that almost nail it but the devs always add in something that makes it too micro-managey like production chains.


Personally I like the production chains in Cities Skylines. I feel they add purpose to the design of the road network and handling of traffic problems. I just wish the AI was a ton better (there are a few mods that help though).


In that vein, I remember SimLife, which had a whole textbook dedicated to it, showing how to model real world things like predator/prey cycles.

https://www.amazon.com/SimLife-Official-Strategy-Guide-Secre...


Very nice! There's a long history of this kind of simulation, going back to Alan Turing (or before). I used to work doing this kind of thing in the 90s, as part of the Artificial Life community. There's a lot of papers from back then if you're looking for other ideas for agent based simulations or complex systems. For that matter the ALife community itself still exists.


I remember A. K. Dewdney introducing "Wa-Tor" in Scientific American in the 80's. I was fascinated by those explorations.

(And SimAnt is of course the more obvious parallel to this particular simulation.)

https://en.wikipedia.org/wiki/Wa-Tor

https://en.wikipedia.org/wiki/SimAnt


Yeah, there was a discussion of emergence in Gödel, Escher, Bach[0] published 1979.

"Aunt Hillary" was the super-organism created from the actions of the indivdual ants, IIRC.

[0] https://en.wikipedia.org/wiki/G%C3%B6del%2C_Escher%2C_Bach


The simulation works fine on Firefox on Android. But touching the map causes it to fly out of view and I can never get it back. Might benefit from a Centre View button for navigation.


Also get this on Firefox on Android. Would love to restart the step but it looks like you would have to cancel the whole tour and start over.

Really enjoyed it though! Love the retro vibe.


Can confirm


My only feedback is that this is amazing. I would gladly pay for a fleshed out version of this. SimAnt was an old favorite of mine.


My dog is currently running a similar study from the balcony to her dog food bowl.

Every day I move the bowl, every night they find it, and every morning I spend battling soldier ants.

The tour was really enjoyable to watch through, thanks for sharing this!


Have a little fun with it. Chalk can disrupt the ant’s route finding. At least for a little while.

https://homeguides.sfgate.com/kind-chalk-used-keep-ants-away...


I got super into messing with the parameters of the ants, and after watching a randomized map (along with tampering with it mid-game to see what would happen) I decided I wanted to see what would happen if I pit them against each other. I set up a map between different extremes that gave them all of the same resources and obstacles, and would over time force them to interact to increasing degrees.

I want to preface this by saying I didn't make some of the decisions I made for any particular reason. This whole thing was purely for fun and not intended to be perfect. Things like the placement of the colonies and what the map ended up looking like in the end just felt right. Some of the decisions were deliberate, like the quality of the food is based on where it is on the map, and the likelihood of encountering another colony to get it.


The experiment:

I copied an old warcraft arcade map initially meant for tower defense, made some adjustments to add additional resources and interaction points, as well as some walls to not have an all-out war immediately, and some extra long tunnels with big payoffs so the less aggressive ants. The map was originally made for 8 players, but to create each extreme maximum-medium-minimum combo I only needed 6 colonies, plus the med-med-med colony, so I ended up with 7 colonies. I just duplicated the all-mediums colony for the sake of balance on the 8-colony map.

I wrote out each combo, and used a randomizer to assign each to a number, then placed them on the map in counter-clockwise order starting in the upper-left, as seen in image 2 of the imgur album. The list of colonies and their randomly assigned max-med-min combos are at the bottom of the post.

This is twice the number of colonies as the default, so I just halved the starting ants to 50 just so we can get a slower and more interesting start. I felt like this would keep the colonies from being obliterated before we get to see them all have a chance. I kept the maximum at 500, however, because I thought the ending would be more interesting.

As I said before, I manually drew the food in with varying values based on where it is on the map, the likelihood of encountering another colony to get it, distance travelled to get it, or how small the payoff was in size. You can check image 3 for the map with the food values listed.

Imgur album: https://imgur.com/a/hU1noNy

TL;DR:

Winner: Colony 4 Total ants: 4685 Starved ants: 0 Killed: 4185 Kills: 1976 Stored food: 187,308 Total food obtained: 512,782

Start time: 07/10/22 8:59pm End time: 07/11/22 12:34am Total runtime: 3 hours, 35 minutes Run speed: 25x

In the end, it came back to the old tactic of starvation as a very good method to kill armies.

-------------------------------------

Colonies:

Colony #1 Aggressiveness: 1 Mean ant Energy: 1 Freedom: 0.05

Colony #2 Aggressiveness: 0.1 Mean ant Energy: 5 Freedom: 0.1

Colony #3 Aggressiveness: 0.5 Mean ant Energy: 5 Freedom: 0.05

Colony #4 Aggressiveness: 0.5 Mean ant Energy: 10 Freedom: 0.003

Colony #5 Aggressiveness: 0.5 Mean ant Energy: 1 Freedom: 0.1

Colony #6 Aggressiveness: 0.5 Mean ant Energy: 5 Freedom: 0.05

Colony #7 Aggressiveness: 1 Mean ant Energy: 5 Freedom: 0.003

Colony #8 Aggressiveness: 0.1 Mean ant Energy: 10 Freedom: 0.05


Outcomes: Part 1

Colony 8 was the first to die, which was not surprising, considering 8 was low aggression and their closest neighbor was Colony 1 which was high aggression, medium freedom and low energy. Shortly after, Colony 1 became the first colony to reach the cap of 500 ants. This was no doubt due to no longer having much conflict, and also obtaining their closest neighbor's resources.

The next to go was 6, which was an all-medium build pitted against 7, who I suspected at the start would be the winner, with max aggression, medium energy, and the most focus (lowest freedom). 6 put up a good fight, having killed just under 800 ants before succumbing to 7.

Colony 3, the other all-medium build, was the next colony to reach the population cap. I thought this juxtaposition was interesting, although not terribly surprising since their closest neighbor was 2, a low aggression and high-freedom build.

Colony 5, a medium-aggression build, struggled to stay afloat, which I thought was surprising, since their neighbor was Colony 4, the other medium-aggression build. The only difference between these two was that 4 has high energy, and 5 has high freedom. The major factor that seems to have given 4 the upper-hand is that they had a much heavier flow of ants to the store of 70-quality food placed behind their starting point. This was good news to me, since I placed those long skinny halls back there specifically to (hopefully) benefit high-energy (and possibly low-freedom) ants that could make the journey, and wasn't sure if I had made them long or difficult enough.

It took a while, but Colony 5's numbers dwindled at the same rate that 4's climbed. Colony 4 was the 3rd to reach population cap while 5 was the 3rd to fall very shortly thereafter.

At this point it was mostly 1 who was making a run for the center food store, Followed by colony 7. Both colonies kept trying to enter only from the left side, which led to some pretty intense conflict, seeing as these were the two max-aggressive colonies. Just like the conflict between colony 4 and 5 before them, the only difference between these colonies was that colony 1 had higher freedom, and colony 7 had higher energy.

From what you can see in the pathing made by each colony, it looks as though 1's high freedom caused a group of ants to start swarming down near 7's exit, where they patrolled around in circles like some sort of death squad, picking off the steady stream of 7's ants as they tried to either get to or from the center food store.

Meanwhile, 3 was slowly picking away at 2, slowly building up in the same manner that 4 and 5 did. 3 Had a strong flow to the high-value store down the hall, which 2 had yet to develop in any meaningful way. Although, I believe the upper hand was gained by 3 by means of having medium aggression over 2's low aggression, in addition to one thing threatening them both: They bad both lasted so long to this point that their food stores depleted to nothing, aside from the large store down the long hallway, which meant venturing out to the center food store where Colony 1 and 4 had already developed a decent flow.


Part 2

Colony 1's circling death squads shifted around the top of the center food store, intercepting 2 and 3's workers at their exit, resulting in a 3-way battle.

Finally, Colony 2 was the 4th colony to fall, which left 1 colony in each quadrant of the map. the 2 high-aggression builds, one of the all-medium builds, and one high energy build.

Colony 1 seemed to keep the pressure on 3 and 7, who stayed between 200 and 300 ants for the next good chunk of time. However, it was stretching itself too thin, as it dropped from a healthy 450-500 ants down to 300 or so and stayed there while it kept sending out death squads to the others.

Colony 4, on the other hand, maintained a healthy 500 ants as it focused most of it's workers toward the food store down the long hallway, only sending a relatively small amount out toward the center food store. Those that did go to the center were generally not attacked, since 7 and 3 were acting as meat-shields for them. Even if they did get attacked, their aggression was medium and they were able to fight back as needed. It seemed that 4 simply needed to hold out for the other 3s mutual destruction, and then pick off whatever remained.

1, 3, and 7 kept battling on. Their numbers dropping to ~250, ~200, and ~100, respectively. Colony 1 and 3 eventually got a semi-decent flow heading to the store down the long hallway, and most colonies moved away from the center food store aside from Colony 4, who was now running low on their own hallway store.

After a long-fought battle, Colony 7 finally fell to 1's death squads, and 3 was shortly behind.

Just as they fell, Colony 4 finally finished off their hallway food store. This caused the majority of the workers to fracture into 3 groups: Most were smart enough to venture into their neighbor's untouched hallway food store. They have maximum energy and could easily make the trip past where the old colony stood, into their hallway, and back. Another smaller group shifted toward the middle, into danger, but to the closer and more valuable food store, while the rest were stragglers that kept going down their own hallway to a food store that was no longer there.

It's important at this point to note that, at this time, Colony 4 was comprised entirely of workers, 500 of them. Colony 1 was comprised almost entirely of soldiers. Of the 260 ants, 222 of them were soldiers with only 38 workers.

Other metrics to note at this moment:

--Colony 1-- Killed: 3741 Starved: 0 Kills: 10367 Food stored: 10

--Colony 4-- Killed: 4066 Starved: 0 Kills: 1930 Food stored: 24014


Part 3

Before long, the numbers started to rapidly tank for both colonies, but one slowed and began to regrow. Colony 1 was now at -1 food stored, and 68 starved ants, and rising. Colony 4 began to quickly replenish their fallen, including adding in new soldiers.

In the span of about 30 seconds (at 25x speed) Colony 1 dropped to 181 ants. Colony 4 dropped down to 399, then climbed back up to 437. 4 was making more soldiers, although it seemed that they may not be needed. 30 more seconds and Colony 1 was down to 119 remaining, with 145 dead by starvation(all soldiers). Colony 4 was back up to 453.

Finally, Colony 4 was back up to 500 ants. Of which only one was a soldier, but that was probably more than necessary against 1's 30 total ants, 24 of whom were workers.

This is where things slowed considerably. Colony 4 was entirely focused on getting food efficiently from their side of the map. Colony 1 was just barely failing to replenish their food as fast as they died. Their food store wavered between 1 and -9, only one ant dying by starvation every 10 seconds or so. The more that died, the more they could keep their heads above water, especially if it was a soldier and not a worker who died.

It was at 22 total ants, 18 workers and 4 soldiers, that Colony 1 was starting to stay in the green. Meanwhile, Colony 4 had not only replenished what food store they lost during this initial fighting (~6500) but they were now above where they started, at 25955.

Colony 1 sent over 2 of their 4 soldiers, who dispatched a decent number of workers (who were immediately replaced) before dying. Shortly after, a third soldier starved to death, and the final soldier died trying to kill Colony 4's workers, in a 500 vs 1 fight.

Things screeched to a grinding halt at this point. Colony 4 was too focused on farming to send any ants to kill any of the remaining 18 worker ants, and Colony 1's workers were so inefficient that they only barely just made status quo, but never had the surplus needed to replenish their numbers. After about 3 solid minutes, a handful of 1's workers spiraled too far from home and starved, another handful walked right into Colony 4's workers and were killed. Down to 7.

Looking at their pathing, it was obvious that the ones who went off to die were the most "freedom" expressing. Running all over the place rather than going directly to and from any of the food stores they could reach. This left only the most efficient workers behind to keep going. I waited a minute or two before just letting it run and glancing over periodically to watch the death throws of Colony 1. I'm a big proponent of "it isn't over til the fat lady sings" and I didn't plan to end this thing until that number reached 0 and Colony 4 officially reigned supreme.

After about 6 minutes of nothing, until I saw they had gone from 7 to 8!

... They made a soldier. It seemed fine, though, as their food store kept going up, and was now in the low 100s. Another couple minutes, and another solider. Let's hope their overly-aggressive nature doesn't cause them to foolishly make more soldiers than their workers can feed. A worker ventured too far into 4's territory and is killed, along with another soldier, so they birth another soldier, who is also killed, then replaced again.

This happens a few times, a soldier born, then killed by 4's workers. It's no problem for 4, who now has a food store of a whopping 91,816, which is more than half of all of the food Colony 1 has ever accumulated combined.

Over the next 23 minutes, Colony 1 keeps making the mistake of birthing more soldiers rather than workers, and having them die, with the occasional worker who is never replaced. Then it finally happened. With just 1 worker remaining, Colony 1 spawned a soldier ant, then the worker wandered into Colony 4's workers and was killed. The food dropped to 0 and the soldier promptly starved to death.


If you made it this far, I hope you enjoyed, and thanks for reading! If you're interested in the screenshots I took periodically as the sim went on, or the ones in the end showing the stat breakdowns and graphs of how stuff turned out, let me know. If anyone cares I'd be willing to crop and organize them and post 'em up as an imgur album. :)


Love to see people playing with it like this :) Can you post the exported map somewhere? I am eager to tinker with it too. Screenshots would also be nice :)


When I touch the screen on iPad, everything goes black. Edit: it’s usable if I only use the gui element to zoom out one step and don’t scroll or zoom.

I was thinking: what if workers also fight? I think they generally do in real life.


Unfortunately, I don't posses any Apple device so I cannot fix it.

You're right, workers do fight in the real life. It's one of many simplifications in the simulation. Real world is much more complex


You might want to take a look at Epiphany[1]. It's just Safari (Webkit, actually), but without the need for Apple devices.

[1]: https://gitlab.gnome.org/GNOME/epiphany


Looks like the same thing happens on an Android phone as well.


Just curious, can't device emulation on Chrome and other browsers help reproduce the issue?


Negatory, there's literally no way other than owning an apple device. I have a stack of bugs in a FLOSS app I can't fix for safari because I don't have a mac anymore.

I guess I could try to boot a hackintosh but it's not worth the effort to me.


May I ask - in the early stages, when a wandering ant encounters its first food pheromone trail, is there a logic that dictates which direction it chooses to then follow? It has two options - towards the source of the food, or away from. The latter resulting in a wasted journey home before a hoped-for bounce back.

- ed - it seems to me that they always go towards the food, is all.

- ed - ed : Great work, btw! SimAnt was a big favourite as a kid, so this is right up my street! :)


What happens when there is a circular path. Do all ants follow it until they die?


Yes. The system breaks down locally.

https://en.wikipedia.org/wiki/Ant_mill

- ed - d'oh, sorry - I have no idea how this works in the simulation, but I guess This. :)


Individual ants might be smarter than you give them credit for: they've been known to pass the mirror test.

https://www.scinapse.io/papers/2180773430


Would you mind describing the mechanism responsible for the ants optimizing the path? I would assume that once a path is established it become self-reinforcing as more ants follow it and drop their pheromones.

Is it that the ants that randomly begin taking the shortest path complete trips more quickly, thereby increasing the pheromone concentration along the shorter path? Still, it seems difficult to overcome the sheer amount of pheromones being laid across the longer but more popular path...


This is great to see. Years ago I created a basic simulation using the same principles of pheromone gradients, one for the nest and one for food. https://youtu.be/VsHc91IhzdI

It a fascinating example of emergent behavior from simple rules.

I've been wanting to find time to revisit the concept and produce an updated version, your simulation seems to include everything I wanted to do and more.


Really love this, it can create such amazing visuals (before the ants erase them with the "no food left" signal!)

Fun to read through the TS/Vue/PixiJS source as well, thanks for open sourcing it. Nicely built!

Could imagine this being a fun base upon which to build a game like Liquid War https://www.gnu.org/software/liquidwar6/


I think it crashed my phone, froze between tutorial steps and when I looked back my phone, a galaxy note 10+ had restarted.


Sorry but this completely fails on iOS. Can't touch the controls, zoom in or out, etc.


Works for me.


I was trying to create a circular mill with rock placement without luck. https://www.youtube.com/watch?v=LEKwQxO4EZU


Brings back memories of my final year dissertation at University nearly 20 years ago, "An emergent model of an Ant colony" (in VRML and Javascript).

Incredibly fascinating and great to see this.


I don't know why, but it seems to malfunction on Microsoft Edge for some reason, where the ant looks like a circular brown blob.


This is awesome. Way to go.

It'd be cool to get the same kinda effect with Ml Agents and Unity. I should work on that.


The tours are awesome! The last one (with soldiers) stops working after the first Next tap (iOS, Safari).


Doesn't work well on Firefox Android. Zooming in causes the map to fly away who knows where.


cool, but this needs WebGL or something as I'm getting 8 fps


It does use WebGL. I can run 15k ants in 60FPS on my machine. What browser and spec do you have?


Yea I think something was borked with my Linux Nvidia driver. Works at 60fps in Windows.


Super cool!


[deleted]


How does the draw function work? I can't seem to draw food or rocks when I try. Latest MS Edge on Windows 10.


You should be able to draw with right mouse button. Is this how you try it? Does it work on other browsers?


I don't have a right mouse button. On OSX, I use ctrl+click as an equivalent (normally) to the right mouse button.

But it might be because of my convoluted setup (using the touchpad on Linux on a Macbook to control OSX on another Mac using barrier) so I'm not sure if it's actually relevant, don't sweat it.


Yeah, right-click drawing is not the most intuitive. I found it purely by accident.


Ah! In fact, it does. Thanks!




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: