Artificial Life Simulation

Home / Machine Learning / Artificial Life Simulation

Recently, I made an educational simulation project on what is known as Particle Life to showcase how complexity can arise from simplicity. Particle Life is like Conway’s game of life but in Conway’s game of life the effect of the particles are confined to their surrounding neighbors only while in these simulations particles have effects on each other over longer distances. Also, the interactions rely on Newtonian like attraction/repulsion forces among the interacting particles.

A diverse set of self-organizing patterns. All emerged from four particle types interacting with each other.

In our case, four particle types (green, red, white, yellow) are interacting with each other. Each has a different attraction and repulsion property toward the other particle types. They start randomly and with time they aggregate together and move around giving rise to some interesting formations and behaviors. This produces some interesting self-organizing patterns where it looks life-like cells eating, chasing, or merging with each other.

I modified the algorithm to make it much simpler removing collision detection and distance squaring which has improved the performance and allowed testing thousands of particles in real-time. I also added the ability to explore various parameters in real-time. This has allowed me to discover some never-seen before patterns to emerge from some very simple rules. This simulation changed my mind that, after all, life like patterns are not so difficult to emerge especially during the early earth days where the primordial soup was filled with the necessary ingredients.

One thing to be noted is, the simulation here involved unidirectional attraction and repulsion too. This asymmetry, which doesn’t exist at the atomic level, leads to some interesting chasing behavior. However, one can easily imagine the emergence of the chasing behavior from symmetrical forces if we simulate a very large number of particles in a much larger space. For example, we might have a large protein that has more negative ions on one side than the other or we can imagine a closed space with a negatively charged membrane, a positive particle will move towards the membrane and might be followed by a negative charge imitating chasing. We, as a local observer, might model the chasing with asymmetrical force without the need to model the membrane. This is good news, because it means we can model complex biochemical reactions to some degree of accuracy with much less computational resources.

Complex patterns emerging from simple relations is an interesting topic but as a Neuroscientist, I am interested in the reversal process where our brain tries to untangle the complexities and re-model the relationships among its neuronal connections. Our brains ability to model these relations is what allows us to imagine and predict.

Human brain is able to model the relations among its neuronal connections

Simulation demos and a walk-through tutorial is available in this video:

You can find the project source code on GitHub:

You can also play with a live online demo here:

The JavaScript code is as simple as this:

<canvas id="life" width="500" height="500"></canvas>
  //Hunar Ahmad @ brainxyz
  m = document.getElementById("life").getContext("2d");
  draw = (x, y, c, s) => {
    m.fillStyle = c;
    m.fillRect(x, y, s, s);
  atoms = [];
  atom = (x, y, c) => {
    return { x: x, y: y, vx: 0, vy: 0, color: c };
  random = () => {
    return Math.random() * 400 + 50;
  create = (number, color) => {
    group = [];
    for (let i = 0; i < number; i++) {
      group.push(atom(random(), random(), color));
    return group;
  rule = (atoms1, atoms2, g) => {
    for (let i = 0; i < atoms1.length; i++) {
      fx = 0;
      fy = 0;
      for (let j = 0; j < atoms2.length; j++) {
        a = atoms1[i];
        b = atoms2[j];
        dx = a.x - b.x;
        dy = a.y - b.y;
        d = Math.sqrt(dx * dx + dy * dy);
        if (d > 0 && d < 80) {
          F = (g * 1) / d;
          fx += F * dx;
          fy += F * dy;
      a.vx = (a.vx + fx) * 0.5;
      a.vy = (a.vy + fy) * 0.5;
      a.x += a.vx;
      a.y += a.vy;
      if (a.x <= 0 || a.x >= 500) { a.vx *= -1; }
      if (a.y <= 0 || a.y >= 500) { a.vy *= -1; }
  yellow = create(200, "yellow");
  red = create(200, "red");
  green = create(200, "green");
  update = () => {
    rule(green, green, -0.32);
    rule(green, red, -0.17);
    rule(green, yellow, 0.34);
    rule(red, red, -0.1);
    rule(red, green, -0.34);
    rule(yellow, yellow, 0.15);
    rule(yellow, green, -0.2);
    m.clearRect(0, 0, 500, 500);
    draw(0, 0, "black", 500);
    for (i = 0; i < atoms.length; i++) {
      draw(atoms[i].x, atoms[i].y, atoms[i].color, 5);

This project was inspired by Jeffery Ventrella’s Clusters I don’t have access to Ventrella’s code but I guess the main difference of this project with the other particle life projects is that I didn’t implement collision detection and this made simulating thousands of particles possible in real-time.

Also, I added GUI controls to change the parameters in real-time this allows easy fine-tuning & exploration, hence, I was able to find some never-seen-before patterns emerge form some extremely simple models of relations. The code here is probably an order of magnitude simpler than any other Artificial Life codes out there because I started this code solely as an educational material for non-programmers and general audience to prove the point that complexity can arise from simplicity.

Another famous of example of complexity arising from simplicity is the Mendelbrot set:

Related topics: #artificial #game #simulator, Particle Life Simulation, Primordial Soup – Evolution, Conway’s game of life, Cellular automata, Fractals, Self organizing patterns, Mandelbrot, JavaScript programming.

Similar Posts

Leave a Reply

Your email address will not be published.