Whether you’re building a massive, complex planet for a game or modelling the inner workings of your endangered species reserve, you want the world you’re building to feel rich and real. One of the best ways to do that is by allowing it to grow and change organically. This means coding evolution.
Here in the Improbable Laboratories, we decided to give the evolutionary powers of SpatialOS a test drive with ev0: an artificial life model. In our instance, we tested the progress of virtual organisms against a fitness function.
A fitness function measures how well an individual can achieve a goal. You test a fitness function by creating a population that can “learn” to achieve their goal over multiple generations. Fitness functions can be difficult to define; you have to know what task you want the population to optimise and how to measure it.
So how do you write an evolutionary algorithm? Well, if you’re like most people with Internet access and the desire to play God, you’ll turn to Wikipedia.
Step One: Generate the initial population of individuals randomly. (First generation)
Step Two: Evaluate the fitness of each individual in the population using the defined fitness function (e.g. health, achievements, score, etc.)
Step Three: Repeat the following regenerational steps until termination:
1. Select the best-fit individuals for reproduction. (Parents) 2. Breed new individuals through crossover and mutation operations to give birth to offspring. 3. Add the new individuals into the population (either completely replace, or mix in.) 4. Go to Step Two
Don’t worry though – you don’t have to define a complicated fitness function within your model. Instead, you can create an environment with the mechanics of evolution: think birth, death, competition, mutation. If an individual doesn’t eat, it doesn’t mate and its genes drop out of the population on its death. This way, the ecosystem takes care of defining the fitness function on its own as it evolves.
These basic mechanisms for reproduction and death are all you need to create game creatures that adapt to their environment over generations. God games like Spore, Creatures or Cubivore have attempted these before–but the persistence enabled by SpatialOS allows you to take evolution to the next level. The implications for persistent creature populations are pretty awesome, and could help you build a game that remains interesting and dynamic for a long period of time. For instance, you could fill your game with creatures that are well suited for a local area, whose survival is inherently tied to the world around them. Imagine if these creatures could adapt to changes in the map and to interactions with players. Your whole world would shift in response to gameplay over time!
The creatures in ev0 are simple beasts. They care about only two things, food and sex, in that order. As their limbs move through the water they push the fluid surrounding them, propelling them through space but also using up energy. To replenish their energy, they have to eat. If they run out of energy they die. Fortunately, food is plentiful at the start when we begin to run the model and is injected continuously into the environment throughout the duration of the run. If a creature eats enough, it will eventually have enough energy to mate.
The ev0 organisms reproduce sexually. Each parent donates a certain fraction of their energy and genetic material to their new child. One thing that we wanted to play with in ev0 was the development of different species. We programmed our creatures to prefer mating with more genetically similar creatures, and creatures that were a shorter distance away.
This mate preference promotes a really robust creature population in the world. It becomes far more likely that life will take hold; any early-stage viable creatures generated are attracted to mates that are also likely to be viable.
Most evolution models are limited by the area and number of creatures that can be processed in a single computing environment. With SpatialOS, not only were we able to run our model, but we were also able to take snapshots of specific moments in an evolution cycle and fork backwards from them to examine different possibilities for emergent traits.
When you try to model evolution in a limited space, with a small number of creatures you run into problems. A lot of genes that get passed on protect inefficient, clunky attributes like limbs that drag or jittery movement. Often, a large number of your starting creatures will die pretty swiftly. When you’re developing on SpatialOS, the number of creatures you can have and the size of your space are limitless. With a big enough sample, you’re bound to hit some of the reproductive sweet spots in your population’s space.
What’s more, the interactions that lead creatures to mate and reproduce are complicated, and are a result of the right cocktail of intricate behaviours. SpatialOS negotiates the results of these complex interacting behaviours behind the scenes, so you don’t have to worry about programming the outcomes that emerge in every interaction. All you need to do is program a simple set of behaviours that apply to your entities and watch what happens when they come together at massive scale. Another really powerful feature of SpatialOS that you can play with here is the Snapshot tool; this allows you to capture tons of data about your simulation at any given moment (great for academics) and allows you to return to a specific moment in time and try things differently. For example, you can introduce a predator element or a different food source at a certain juncture and see how it impacts the development of your creatures. Imagine the possibilities of tiny change on an entire population! With SpatialOS you can make this a reality.