Skip to content

A model of an ant colony which aims to improve their nest making abilities through the power of a genetic algorithm which modifies pheromone secretion and detection.

Notifications You must be signed in to change notification settings

CT-Clark/Antymology

 
 

Repository files navigation

Assignment 3: Antymology

In this project I attempt to simulate ant colonies which aim to create the largest possible nests. This is done within the paradigm of genetic algorithms by implementing pheromone excretion and detection based on genes.

Record3

Disclaimer

The source code for the world has been forked from https://github.com/DaviesCooper/Antymology. The ant model use is courtesy of Serena Schimert whose github can be found at https://github.com/SerenaSchimert. The concept for the pheromone interactions is heavily inspired by: Wilensky, U. (1997). NetLogo Ants model. http://ccl.northwestern.edu/netlogo/models/Ants. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

Agents

Each agent is an ant which moves around and interacts with their world by secreting pheromones.

UI & Interface

Currently the interface consists of a simple text readout to display the current number of nest blocks. The camera can be controlled through the WASD keys to go forward, strafe left, back, and strafe right respectively, and Q and E keys to "increase and decrease height" respectively. Pressing and holding the middle mouse button allows the camera to be rotated. Left clicking on a location within the map produces a stone block, whereas right clinking removes the block at the location indicated. These controls are unchanged from Cooper's original implementation.

In order to run the simulation simply load the scene, set the seed if desired, and press play!

If specific initial genomes are to be desired then you will need to acess the WorldManager.cs script and change fields near the top that I have specifically highlighted. Reasons for doing this include wanting to recreate the best colony simulated so far, for fun, etc.

The colours and types of blocks are as follows: Red blocks are nest blocks, black blocks are container blocks, light green blocks are mulch blocks, dark green blocks are grass blocks, grey blocks are stone blocks, purple blocks are acidic blocks, and finally every other block is an air block. (Air blocks are not visible.)

After running the simulation for at least 1 generation statistics about that generation are then saved in a text file called "AnymologyResults". A word of warning, if running this simulation multiple times it will overwrite any previously saved data in that file. Present on this github is a longer training session with some results.

Ant Behaviour

Health

Ants have health which starts at 100 and decreases by 0.1f each fixed frame. They can refill their health to a maximum of 100 by consuming mulch blocks. To consume a mulch block for health no other ants may be occupying the same space as the ant munching on the mulch. After an ant consumes mulch the mulch block is removed from the world. Ants which stand on an acidic block have their health drained twice as quickly. Ants may also give some of their health to other ants. In my simulation they only give this health to the queen ant. This exchange is a zero-sum exchange. If they cannot give the full amount of exchange health to the queen, they will only give a partial amount and then return to foraging.

Movement

Ants may move into any air block orthogonal to themselves and with a heigh difference no greater than two.

Digging

My ants are capable of digging up blocks. They must be over top of the block, and once dug the block is removed from the world. In order to dig a block the block must not be an air block or a container block. Additionally, the ants don't want to become trapped, therefore they also inspect the space before they dig to ensure there's still a spot to move to after they dig.

Pheromones

The bulk of the ants decision making process is powered by pheromones. Ants who have consumed a mulch block will drop food pheromones on their way back to the nest. These ants find their way back to the nest by following the pheromones the queen produces. They will prefer to step into a space with a higher concentration of food or queen pheromone depending on their role. Additionally, if any ant (including the queen) steps on an acid block they will produce an acid pheromone. Ants will subtract this acid pheromone when considering the goodness of a potential square. Finally, the nest itself produces pheromones. These nest pheromones are used exclusively by the queen in order to ocnstruct new nest blocks. This allows the queen to move around and locate the exterior parts of the nest.

Roles

Queen

There is a single queen who is exclusively responsible for producing nest blocks. This queen is coloured yellow in my simulation. Additionally, this queen cannot forage for her own food so must be supported by other ants in the colony. When the queen ant produces a nest block she loses 1/3 of her maximum health, i.e., ~33. The queen does not spawn new ants, contrary to her real life counterpart.

When spawning nest blocks the queen will look for blocks on the edges of the nest and then place a block next to a preexisting nest block. She prefers to place these blocks lower so that the nest can grow upwards. The first block she places however will be when her health threshold has surpass a certain amount and she is also standing over a grass/mulch block. She will dig that block up and replace it with a nest block. If a mulch block is dug up this way she will not gain health from it.

image

In the above image we can see a more mature colony a little closer. The queen ant is yellow.

Forager

Ants who have not yet consumed a block of mulch are considered to be in the state of foraging. In this state the assembled the possible orthoginal blocks around them for movement and then, based on pheromone concentrations, decide which block to move to. Once the ant is above a mulch block they will consume it by using the dig action and then there state will change to tending the queen.

Tenders

Ants who have consumed a mulch block then make it their goal to tend to the queen. They will traverse the map making decisions based on the pheromones the queen excretes in order to find her. Once they are adjacent to her they will transfer some of their health to her and become foragers once more.

image

In the above image we can see a newer nest with the colony of ants having gorged themselves on mulch. The green ants are returning to the queen, while the red ones are attemping to follow in the footsteps of the green ants.

Genetic Algorithm/Simulation

This simulation runs as follows:

  1. Two genomes are randomly created. This genome has the following properties: 1a) foodPheromoneAmount - This is the amount of pheromones to drop after a mulch block has been consumed but before transfering health to the queen. 1b) acidPheromoneAmount - This is the amount of pheromones to drop after this ant has stepped on an acidic block. 1c) queenPheromoneAmount - This is the amount of pheromones the queen continually drops in ordr to lead ants back to the nest. 1d) foodPheromoneDetectionLimit - If this concentration is too high it means that an ant who has consumed mulch recently has just passed through. The ant does not want to follow that ant back to the nest, but back to the food source, so this limit is introduced. 1e) nestPheromoneDetectionLimit - This concentration is so that the queen builds around the perimeter of the nest, rather than directly staying near the central nest block. Higher concentrations lead to more concentrated deposits of nest blocks. 1f) maximumhealthToTransfer - This dictates what the maximum health an ant is able to transfer to the queen is. Too high and the ant may die prematurely. Too low and the queen doesn't get enough food. 1g) nestHealthThreshold - This dictates at which health level the queen is comfortable removing 1/3 of the maximum health in order to construct a nest block. Too low and the queen may not recover that health in time, too high and time is wasted not constructing nest blocks.

  2. Crossover occurs. Elements from each genome are randomly picked and then used to construct a new genome. This new genome contains genes from both parents. For example the foodPheromoneAmount may be used from the first genome whilst acidPheromoneAmount is picked from the second. With the exception of the initial generation the two best genomes are selected from each generation to breed.

  3. Mutation possibly occurs. This helps prevent the gene pool from becoming stagnant. About 20% of the time genes are increased or decreased within certain parameters randomly.

  4. The ant colony is created. The simulation will run with each ant fullfilling its own duties until no ants in the colony remain. This occurs for a number of colonies until the generation has run its course.

  5. The ant colony goes about its business collecting mulch, feeding the queen, and building nest blocks until all of the colony has perished.

  6. Each colony's performance is evaluated. The evaluatin function is simply the largest number of nest blocks placed. The simulation then selects the genomes of the best two colonies and repeats this process. Eventually larger and larger nest block amounts should occur.

  7. Then the world manager resets the world state to an identical collection of blocks so that each colony is working with a similar environment.

Bugs(Ha)/TODO/Analysis of things which aren't working quite how I'd like them to

Currently the ants fall from the sky in a randomly determined clumping at a particular point on the map. This was easier to implement, but runs into trouble if that particular seed generates a map where they fall onto the top of a container block from which they cannot escape. To fix this - Please - change the seed to a different map where they're not doomed from the start. If some ants don't make then that's okay, it's a sacrifice I'm willing to make, but most should survive for it to be an interesting simulation. it would be nice to spawn the ants in a clump on the ground instead.

I'm not quite sure how great of an effect the different genomes have upon the simulation, additionally the initial gene properties are confined within a given random space, but perhaps that space should be expanded (Or restricted). The issue with expanding that space is simply that it opens up the possibility of having these genes quite far off any usable data. I've massaged them into a ball park, especially with inspiration from Wilensky's netlogo model, but they're still potentially off considering the difference in implementation between the two models.

Often large sections of ants which have fed will wander off as a group away from the nest, and as a result die. (I've experimented with allowing them to eat mulch if they're below a certain health limit, but that just makes a long algorithm even longer.) This leaves a small portion of the total ants actually taking care of the queen. I assume this is due to the ability for them to move just as quickly as the queen's pheromones, ahead of them in fact, compounded by the limited reach of the pheromones, and unlike Wilensky's model I have not preprogrammed pheromones into the map. Perhaps this can be fixed by adding a different sort of diffusion from the next blocks whereby they perpetually diffuse but don't evaporate to simulate a homing beacon of sorts. In the wild the ants use visual cues as well, so raycasting and possibly hitting nest blocks may also be an option in the future.

Although I've gone over the movement selection algorithm with a fine toothed comb, and there is an element of randomness applied when all movement spaces are equally as good, often ants still seem to just move back and forth, or seem stuck in one spot. Perhaps this is because of the imitation of unity and not updating its frame for fixed updates. But this could also be caused by improper pheromone detection balancing as mentioned above.

Most of the ant genome creation and manipulation is being done in the WorldManager.cs script. In this project I have treated this world script as a script for the world of the simulation, rather than just the manager for creating the terrain. It would be better to seperate much of the biology/numbers from this script and place them inside their own scripts for modularity. Additionally the configuration manager has not been touched, but it would be better to switch a number of settings which don't change and aren't relevant to the terrain into the configuration manager.

About

A model of an ant colony which aims to improve their nest making abilities through the power of a genetic algorithm which modifies pheromone secretion and detection.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%