Artful code

One of the things I want to achieve through the blog is the ability to compose a small project in an evening. Rather than just have ideas that never get implemented into even the most basic of MVP, the blog would encourage that I move to build my ideas quickly.

Moving back to javascript has helped with that as the setup required to be able to get something visual working on an HTML5 canvas is much smaller than the equivalent elsewhere. Javascript was the language I started learning when I first learnt to code. It was the easiest to run because every computer already had a browser and notepad.exe.

One of the things I like to do is create something akin to computational art and that is what I would argue I have done in this week's post. Artificial intelligence has been a long time interest of mine and one element of that is the interaction of agents. In this post, I have created a system of creatures which live in a world inspired by agar.io.

If you are not familiar with the game it is based around circular cells which the user controls. There is static food, which you could argue represents the vegetation. The player starts as a small cell and as you eat you grow. Once you are a certain size bigger than another player you can swallow them too. If you do they have lost and will have to start as the basic cell again.

The aim of the game is to become as large as possible. There are a couple of downsides to your growth however, one is that you cannot move as quickly, the other is that you lose some of your sizes over time, the larger you are the harder it is to keep your size.

To combat the speed reduction you can divide in two, this, however, leads you more vulnerable to be eaten yourself.

I thought it would be a great game, one to create something pretty in an evening, but also a great environment to run genetic algorithms.

Genetic algorithms are a technique for optimizing parameters using the concepts of evolution. The idea being the entities which survive go on to breed and so whatever set of parameters they represent continue to propagate with greater success in the future.

I am not the first person to consider agario a good platform for genetic algorithms however I thought it would be interesting to see how the agents would evolve when the dividing possible in the game was akin to mitosis.

This way they would have to evolve the payoff between size and the amount they bread. The lower the size before dividing, the more they would propagate but the more at risk of being eaten they would be.

The results have been really interesting with some agents evolving to what I like to think of as insects. They bred almost after eating a single unit of vegetation and their population escalates quickly. There are others who trundle along pretty much allowing their food to accidentally fall into contact with them.


I have found myself becoming mesmerized by the animation and watching it for hours. Another interesting observation I have made is that the small agents who bread more seem to evolve to not be able to see. That is the distance within which another agent has to be for them to be included in either their fear or greed calculation tends to 0. I can’t fathom why this is.


The speed in which I developed this means there are quite a few issues which I’m sure you will find, but for an evening of work, I’m happy with the stability.

Demo

Interesting problems to overcome

There were quite a few issues with the edges of the map. First, if there was no wrap around then any agents who were scared would naturally move to the corners. This made the animation less pleasing to look at so instead I moved to a wrapped surface. Wrapping the position is a trivial affair of adding a modulo width and modulo height to the x and y. However, calculating the minimum distance between points is a little more tricky. It turns out the best way is to check for the smallest distance placing a pane of the world in each direction as explained well in this article I found.


Comments

Popular posts from this blog

An exploration in number systems

Structural engineering with cardboard