A journey, exploring genetic algorithms, in the attempt to create a digital world where virtual dinosaurs are free to explore and evolve.
Have you ever wanted to create a world,
where dinosaurs of your own imagination are free to run wild?
Well, I have!
I decided to put my fascination of prehistoric creatures and computer systems skills to the test to create the ultimate genetic algorithm revision experiment.
Chapter 3 - Testing the Environment
The code for many of my new projects will be visible on GitHub, with explanations of the code on designated pages. In this section, I will instead show the results of the testing and provide explanations of what is shown.
The first step was to create a world in itself. A place where water is available for both plants and animals to consume. Somewhere where rocks may exist and importantly, soil for plants to grow in.
A grid was produced, of which its dimensions were the environment's boundaries. It was thought best to start small so testing would be easier, and the complexity of the environment could be increased in increments. Each grid space was referred to as a 'plot'. A plot is part of the environment, which acts independently to all others. Each plot would have a 'form', a state of being that maps directly to the table shown previously. Every plot has a counter for its water content and plant resources (such as minerals consumed by plants), which is vital in updating its form.
When a plot has considerable water content above a set threshold, the form turns changes to represent a water source, signifying it to be either a part of a lake or ocean. If the water content is too little, or the land is barren of suitable resources, it produces desert land. Rock land also exists but is mostly unused. No plants can grow in the rock land, but if the water concentration is too high, it can adapt and erode into a lake form.
If the water and plant resource levels are suitable for plant life, then plants will grow, grass being the easiest to grow and trees requiring the most resources to spawn. Depending on the plant life, water and resources will be extracted from the plot, as too by evaporation, but also replenished depending on how close to a water source it may be.
These actions of the plots were achieved by following simple rules within an Object Oriented approach. Each plot is considered its own object, an instance of the plot class, which can be referred to and run individually alongside all other plots. The creation of the plots was also made easier as a series of loops would create instances of the class for each grid space in the environment.
Overall, each grid space works independently, as small plots of land most likely do in real life, with the size of the environment being dynamic, and changes to the size will be more easily achievable in the future. Most importantly, if a plot's characteristics need to be changed, changes to the code in the class will update all later instantiated objects, making them follow the new rules.
The image below shows how the first outline for the environment appeared.
Early on, it was apparent that this project was going to get very complicated very fast.
During my revision of Genetic Algorithms at university, I wanted to take my attention away from mathematical and electronic applications and explore more biological options
Genetic Algorithms are a mathematical approach of exploring, evaluating and developing solutions to a problem inspired by nature.
Genetic algorithms allow possible solutions to be found and developed very quickly compared to randomly searching for an answer, especially if there are too many combinations to explore. Problems that may otherwise take up to a year to test every combination may be able to be solved in a day or less using Genetic Algorithms.
An example (soon to be) shown on this site is circuit design, where a problem is given, but a solution is not known. Possible solutions generated by the application are further developed to produce better solutions until an optimum solution is found or other criteria are met.
Genetic Algorithms are inspired by evolution, such as that found in nature, including components such as gene sharing, genetic mutation and natural selection. A recent trip to a fossil shop on holiday inspired me to apply this to one of the most famous and apparent cases of evolution in history, Dinosaurs.
Chapter 1 - The Concept
With this project, it was hard to know where to start. As apparent with many of my other skill-building projects, I tend to start with a general idea and a quick sketch and allow my hand and mind to their thing, often with limited initial research to see how far I can go with what I know so far. However, with a project of a scale such as this, a more planned course was needed.
Overall I knew what I wanted - To create some form of a virtual world where a selection of randomised dinosaurs can roam free and be put to the test, allowing the fittest to survive and pass their genes to the next generation along with their unique characteristics.
In the future, I aspire to create this within the Unity Game Engine, to have 3D representations of my creation show how the dinosaurs act and change over time. However, I knew this was the wrong place to concentrate on as the dinosaurs' look, feel, and behaviour are not the premise of this experiment in the fundamental levels of a proof of concept and would most likely be a currently unnecessary and considerably time-consuming part.
To start, I knew that I would likely have a few ASCII characters bleep and bloop across my screen representing the dinosaurs, with some form of basic simulator evaluating their performance.
The important part was how to score the dinosaurs' performance and what obstacles and needs were to be presented to make it possible. To start, I decided to use an environment to produce some form of tournament. For each generation, the dinosaurs are subjected to an environment in which they have to survive; those who last the longest with the highest survival rate would be selected for the next generation. Additionally, these aims could be changed depending on what specifics I want the system to evolve over, such as producing creatures that use the least amount of water in a scarce environment or having the deadliest fighting creatures possible.
It was here I found an even more fundamental problem. Before I could score any dinosaur chromosome, I first needed the environment to score it over, and the variables involved continued to grow.
Chapter 2 - Creating the Universe
For the dinosaurs to be scored on how well they found food, there must be some food to find. This includes the likes of trees and smaller vegetation such as grass, even though grass did not form until millions of years after the extinction of the dinosaurs.
Considering this, it is also important to note that the vegetation itself also needs resources such as water and minerals, some of which may be from manure from other wildlife. Some creatures ate insects, which ate other insects that ate plants, with the plants consuming them after they biodegrade.
This led to the realisation of an unfortunate cycle.
Dinosaurs need plants for food and oxygen, and plants need dinosaurs for food and carbon dioxide. Some plants may need other plants to survive, and there are dinosaurs that need other dinosaurs to survive.
This means rather than a blank plane for creatures to roam, there needs to be a full ecosystem of water, viable land for plants, and some form of equilibrium between the absorbing, processing and release of gasses between plants and animals in order for either to survive. It also means that the land would have to be scaped in a form to accommodate its needs and be dynamic to change itself.
Thus was a realisation that the further I try to find the start, the further it seems to be.
Before I even considered the dinosaurs, it was decided that some form of environment needed to exist, with all of the key fundamentals in place and basic functionalities.
A list of the fundamental items to be in the universe was created, and an ASCII symbol was given for its representation.
Chapter 4 - First Life
Now was the time to bring life to this little universe. In this case, life was the grass and trees needed to populate the environment to produce the oxygen and food required by the later introduced creatures. Though, at this stage, with no other life, the concept of regulating the air was not added.
This stage of the project was to provide the plots with the ability to add plant life to the environment. This meant each plot was given the ability to monitor its conditions and deduce whether the requirements for life were reached. If there was enough water and other resources, trees would grow; if not, grass would grow; if resources were too diminished even for grass, then nothing would grow; if the land was barren for too long, those plots would result in desert land. If a plant exists, but the conditions are too bad, then they can also die also.
Additionally, the amount that the plants have grown, such as the height of trees, is also omitted, as it is not currently relevant. However, one added characteristic was the time to grow; plants do not grow instantly, even if we don't care how much they have afterwards.
The concept of 'Cycles' was introduced. Cycles are the units of time representing a complete iteration of the program's main loop, where all the plots' relevant calculations are performed. These cycles do not represent a fixed time unit such as days or months but are used in a similar method to track the progress of the environment over time.
The plants and trees require few but different amounts of cycles to grow, so the plot must hold its viability state during this time. This counter will reset if a plot becomes unviable for vegetation to grow. This ability allows plots of land first to become grass, then trees, and thrive whilst they are in suitable conditions. This method also stops trees from instantly growing out of nowhere for quick flashes during intermittent instances of viability, just as in nature.
The images below demonstrate how when there is enough water, the overall plant life thrives, with large quantities of trees concentrated around lakes and fields full of grass. They also reveal how once water and other resources start to diminish, so does the plant life out of reach of relevant life-sustaining sources.
Chapter 5 - Changes to the Environment
(What's with all the Oceans?)
One crucial factor about Earth's environment is its weather. The weather does not just pose negatively to life, but is also a key part of its survival.
During early testing, as also shown below (left), it was found that all cases would eventually lead to the extinction of all life after a long enough duration. This was expected as once all the water is drained from the soil and rivers, all the water would eventually deplete to nothing, and no plant life can survive. To overcome this, rain was introduced.
By having a variable to control how much water is evaporated from the ground eventually, this factor can be changed depending on season. Currently, rain appears at random points in time and replenishes the ground under it with more water. In the picture below (right), rain is displayed as a cluster of forward slashes "///", accompanied by a list of grid coordinates of the watered areas.
Through further testing, now two outcomes would appear. If the rain occurred too frequently, the land would eventually resort to a total water plane, where the water levels would become too high. Additionally, due to a lack of rain, the soil would also turn into a desert wasteland if the plants were draining the soil too quickly. However, when the simulation was run long enough, it was apparent that even with low amounts of rain, a total water plane was also common, if not too common; what's with all the oceans?
Surprisingly, I noticed that I forgot to program a rule where the water kills the plants if the water concentration was too high, which at first is what appeared to have happened, considering no plant life was left, only water. However, even with the absence of this rule, raising water levels would still result in an ocean. This rule was added shortly after, changing the form to water if the concentration exceeded a set threshold, similar to the rocks.
The hypothesis was that possibly the form of water had a priority over that of plant life; this was not the case. A closer inspection, monitoring the cycles in shorter steps (from displaying the world once for every 1000 cycles to only 10), revealed that at first, the plants would die out due to an absence of not only water but mineral resources. Even with low amounts of rain, if the plants die due to lacking water availability, the desert may eventually regain viability once enough rain was absorbed over a long enough period, repeating a cycle of running out and collecting water with plants in between. However, when the minerals were gone, the plants would die out permanently, leaving the environment as a flat plane and the water with no significant drain mechanism causing the ground to flood out.
This signified that even though the water resources may be replenished, other vital materials, such as those from fertilisers and organic decomposition, were not. Now it was time for the Dinos!