by Steven Binns: Y13 Age ~18


Artificial life simulations model aspects of the behaviour of living organisms. Steven obtained the algorithm for ALifeSim from the book AI Application Programming by M. Tim Jones. You can download a useful pdf file containing similar material entitled Artificial Life – Ethology by Alan Dix and Manolis Sifalakis. They introduce the background and terminology clearly. The agents are herbivores (which eat plants) and carnivores (which eat herbivores only). For agents, reproduction requires a threshold energy and death occurs when energy decreases to zero. Interestingly, Dix and Sifalakis found that herding and ambushing strategies evolved; herbivores followed other herbivores and carnivores waited near plants for herbivores to arrive.

You can learn from the diagrams in the above paper and try to match the code to them. (In this program the grid folds back on itself in both the x and y directions so that an agent moving past an edge reappears at the opposite edge. Also, agents cannot occupy the same cell). The program is ambitious for a school student (with its object-oriented code, use of automatically named ini files and graphics using SDL) and we hope it will inspire further AI contributions. The neural network concept on which ALifeSim is based has applications such as pattern recognition and video games.

In a neural network (named after the neurons in a brain), inputs have adjustable weights applied to them to determine the output action and the outcomes of actions influence subsequent values of weights. The system thus learns through repetition. In this program, each of the four outputs (turn left, turn right, move forward and eat) receives 12 inputs (from the agent's perceptions of nearby organisms) that are weighted randomly at first. Each output also has a random bias added to it. The output with the greatest total is the chosen action. As the simulation continues, the oldest agents and those who have gained sufficient energy to reproduce pass on their weights and biases to the next generation so that successive generations gain in intelligence. You can watch a simulation from scratch and compare it with one with a few hundred thousand "learning" steps to see if you think that learning has been effective.

This screenshot shows part of a simulation window with plants (green), herbivores (blue) and carnivores (red). The black line on each carnivore and herbivore indicates the direction it is facing.



You can feed the results of one simulation (the "brains") into the start of another. The brains will then be tested in a different random environment. These .ini files are named by date and time and are easier to load if you give them short names. Usefully, Steven provides commands to open the relevant folders.


To view the program, download, unzip it and double click on ALifeSim.exe. The zip file also contains both source code files. To see the motion graphic, type the command run. To run the program and obtain the results in files without viewing a graphic, type runnogui. Runs to completion generate files storing the network weights of herbivores (in the Networks\Herbivores folder) and carnivores (in the Networks\Carnivores folder). If you want to load them into a new run, it is easier if you change the filenames. We supply sample files named l and c. Open these .ini files with a text editor to see the weights applied to the twelve inputs to generate the four output totals.

There are menu options for changing many parameters. The source code on the following page shows the menu structure, which we summarise here.

networks (n)
  open herbivore folder (oh)
  open carnivore folder (oc)
  load herbivore (lh, then enter filename)
  load carnivore (lc, then enter filename)  
  return (r)
parameters (p)
  steps (s)
  fast steps (fs)
  plants (p)
  herbivores (h)
  carnivores (c)
  grid (g)
  energy (e)
  food energy (fe)
  reproduction energy (re)
  return (r)
Programming - a skill for life!

Seven programs including ALifeSim, Invader, My_first_sdl2 and PlatformDemo by Steven Binns