paint-brush
Evolving agents to solve XOR in Pixling Worldby@fredriknoren
132 reads

Evolving agents to solve XOR in Pixling World

by Fredrik NorénOctober 18th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Training a neural <a href="https://hackernoon.com/tagged/network" target="_blank">network</a> to perform <a href="https://hackernoon.com/tagged/xor" target="_blank">XOR</a> is basically the Hello World of machine learning, and since Pixlings (agents) in my evolution simulator all have neural networks for brains, I figured it should be pretty straightforward implement it in my game. This blog post will show you how to do it (don’t worry, it’s really easy). If you want to follow along you can create an account at <a href="https://pixling.world" target="_blank">https://pixling.world</a>, it’s free and runs completely in the browser so no need to install anything. You can also see a finished version of this <a href="https://pixling.world/KXHEQJnqlGCFmMALGgHlb" target="_blank">here</a>.

Company Mentioned

Mention Thumbnail
featured image - Evolving agents to solve XOR in Pixling World
Fredrik Norén HackerNoon profile picture

Training a neural network to perform XOR is basically the Hello World of machine learning, and since Pixlings (agents) in my evolution simulator all have neural networks for brains, I figured it should be pretty straightforward implement it in my game. This blog post will show you how to do it (don’t worry, it’s really easy). If you want to follow along you can create an account at https://pixling.world, it’s free and runs completely in the browser so no need to install anything. You can also see a finished version of this here.

The general outline of what we’ll do is that we will give each Pixling an XOR “problem” each step, and if they solve the problem we will reward them by letting them reproduce. If they answer incorrectly we will kill them. This gives them plenty of incentive to get really good at XOR really quickly.

A problem to solve for our Pixlings

We’ll be using what’s called “Environments” to create the XOR problem for the Pixlings. Each environment has a single floating point value for each cell in the world, so if we create two of them and randomize the values to be either 0 or 1 we get the two values the Pixlings need to perform XOR on. They will then have to give an answer by using one out of two abilities we will give them (more on this later), so for instance if the values of the environments for a Pixling are 0 and 1, they should use the True ability if they don’t want to be killed, as 0 xor 1 is 1.

Hit “Create World” if you haven’t already and then hit the green edit button in the top and create an environment named “A” (or whatever you want, just remember it later). In the eachStep section we will first add a “SetValue” with an If switch that sets the value to 0 or 1 depending on the outcome of a random number. This will create an environment where each cell is randomly 1 or 0 each step. You can set the “displayMaxValue” to 1 to make it easier to see the values, or change “visibility” to hide the environment from rendering entirely as it can get a bit annoying with the flashing colors after a while.

This is the configuration we’re going for. You can check that it’s working by hitting run in the panel to the right.

Duplicate this environment so you have two inputs to the network. You can duplicate it by right clicking the little square next to its name in the sidebar to the left and choosing “Duplicate” (surprise surprise). Hit Update to save these changes.

Tools to solve the problem

Next we will create two Abilities for the Pixlings named “True” and “False”. We’ll expect them to choose one or the other depending on the inputs of their current cell, and if they choose right we’ll reward them. Go to the Abilities editor and add a new Ability. In the Actions section we want an “If else” statement that switches on whether they guessed right or not, so for the “True” ability you want something that looks like this:

If the Pixlings use this ability in the correct situation (when they’re on a cell where A != B) they’ll be rewarded by getting to Reproduce. If they use the ability in the wrong situation they die though.

Duplicate the ability to create the “False” ability, except for that one the comparison should be == instead of !=.

Almost done! Next we just need to create a new Pixling that we can use to clone into the world. Go to the “Pixling zoo” tab and add a new Pixling. Set its abilities to the two abilities you just created. We also want our world a bit larger than the default so change it to at least 300x300 in the Advanced tab. Hit Update to write your changes to your world.

Let’s run it!

Great, you’re ready to kick of the simulation now! If it’s not already running hit the play button in the top and set the Speed to at least Hyper. Then click the “Spawn Pixlings” button (the icon is a little Pixling baby) in the “Pixling zoo” panel (this one is different from the one in the Edit world dialog). The click in the world to spawn a few Pixlings. You can change the “Radius” setting at the bottom to spawn many at the same time.

Use this button to spawn new Pixlings.

Initially they have completely random weights so chances are they’ll die pretty quickly. If you spawn a bunch you should get some that perform ok at least though, and fairly quickly you’ll have a world full of Pixlings, like this:

Pixlings are colored to make it easier to see which “species” they belong to. Each time they reproduce their color is slightly changed, so you can easily see which ones are closely related and not. Video of this here:

But it’s not filling up. Why? Well turns out we have a very basic problem with our world. The default neural networks only have one layer and we need at least two to perform XOR. Hit the edit world button again and browse to the Pixling zoo tab, where you can change the brainNLayers setting to two. Run it a while longer and you should end up with something that looks like this:

Much better. Video here:

I.e. virtually no space left in the world. Which means the Pixlings are pretty great at performing XOR! (Remember, they are punished with death if they get it wrong even once, so they have a lot of incentives to be good at it). And that’s it, if you’ve followed along you should now have your own species of XORing Pixlings!

One last note

One odd thing I ran into while building this was that smaller networks seemed to perform worse (i.e. take longer to evolve). I’m guessing this is because a larger network have many more ways to solve this problem, whereas a small one only have a small number of “solutions” so it takes longer to find those.

Finally, this was just one small, and fairly technical, demo of what you can build in Pixling World. But there’s a lot more you can do with this. You can create anything from ecosystems with plants, herbivores and carnivores to complex weather dynamics or just plain beautiful simulations. It’s up to you! To learn more you can read about how I’m implementing it, or check out the wiki.

Hope you enjoyed!

/Fredrik