Giter Club home page Giter Club logo

gameoflife's Introduction

Game Of Life

Conway’s game of life is a famous simulation of a grid system, in which the cells are either ‘alive’ or ‘dead’. This grid is set up before the simulation and then requires no further input from the user. After starting, a cell changes its status based on the status of its neighbors. More information can be found online.

I wanted to apply the Game of Life to an ecological topic, for instance to simulate the interaction between two different species. One of the famous interactions in population dynamics is the competition between multiple types (or species) of organisms for the same resource. This competition can be for food, water, territory etc. The ‘winner’ in this competition could be decided by a species’ advantageous trait, for instance being more adapted to the environment, having a higher rate of reproduction, being more resistant towards disease or predation etc. However, it is not always the case that one species completely out-competes the other. Quite often we can find multiple species with overlapping resource requirements living in the same habitat.

Implementation

The game of Life with two different species was implemented using the C programming language. I used existing files graphics.h and graphics.c in order to display the simulation to the user. After running a simulation, the program saves the output in a file called PopulationGrowth.txt. The data in this file can be displayed by running the ReadPopulationGrowth.py script. The location of the data is hardcoded into this script.

This project was part of a course in High Performance programming. The goal of this project was to create a program and then to use a series of optimization techniques, like script optimization, compiler flag optimization and parallelization to decrease the run time as much as possible.

Manual

The user can compile the program using the make command in the terminal, which activates the Makefile. The program is stored in a file called game which can be run in the terminal. When this command is given the user will need to specify a number of parameter values, in the shape of ./game nrRows nrCols nrTime nrThreads graphics graphicsTerminal. These parameters define the size of the grid, the run time of the program, the number of threads (for parallel optimization) and whether thesimulation is visualized in a separate screen or printed out in the terminal. Example: ./game 100 100 50 3 1 0 simulates a 100 by 100 grid using the graphics.h package, but doesn’t output in the terminal. Be warned, outputting in the terminal is only recommended for very small grids. This type of visualization does however have the benefit of allowing the user to look up the distribution in the grid however he/she wants. Simulations using graphics.h should not be done for grids larger than150x150, since it will become very difficult to see the circles. At the end of the visualization with the graphix simulation, press q to close the simulation window and finish the program. During the simulation the time is displayed in the terminal.

Apart from the parameter values which are set by the user when calling the program, there are a number of other important parameters set in the code. The percentage probability of a cell being occupied by a certain species is denoted as probabilityLifeA for species A and probabilityLifeB for species B. These probabilities are used when initializing the grid. These parameters are initially set to 10. Note: when both species have a sufficient likelihood of occupying a certain cell, the one with the highest likelihood is chosen. Another parameter is WaitingTime, which defines the time in microseconds between time steps in the simulation with graphic.h. If the user wants to increase the time to look at the distribution of alive cells in the grid, he/she can increase this parameter. All three of these parameters are defined as macros in the GameFunctions.h file and can easily be altered by the user. For instance, if the user doesn’t want species B to occur, he/she can set the probabilityLifeB parameter to zero.

The rules for changing a cell’s status based on it's neighbors are implemented in the UpdateCell function, which can be found in the GameFunctions.c file from lie 126 to line 146. This code can also be altered by the user. These rules are as follows:

  • A dead cell will become ‘occupied’ by a certain species if it is surrounded by three living neighbors of that same species. When there are both three neighbors of species A and B, the cell will become occupied by species A, making A the stronger competitor of the two.
  • Any living cell with more than a total of three live neighbors, no matter what species these belong to, will die.
  • A living cell which is surrounded by two or three individuals of the same species continues on living.
  • A living cell with less then two neighbours of the same species will perish.

When reading the code it is important to remember that a status of 0 refers to a dead cell, a status of 1 refers to a cell occupied by species A and status of 2 refers to a occupation by species B. Finally I recommend sticking to simulations of square grids when visualizing with graphics.h, since when using rectangular grids the cells get compressed.

Use command make clean in order to remove all the compiled files and executables and anything else that was created while using the game executable.

Discussion

Although the program is a fun simulation tool, is it obviously too limited to simulate complex ecological systems. I was however suprised to find that a sufficiently long run time displays patterns which will be familiar to any population ecologist. The ExampleGraph.png for instance shows an initial decrease in both populations. This is due to Game of Life with its random placement of individuals over the total grid, with many of them not being surrounded by any neighbors of the same species and therefore dying off. However, after this inital decrease in size, a clear competition pattern is shown, with one species growing in a steady manner, and the other species being outcompeted.

ExampleGraph.png

gameoflife's People

Contributors

erikjan22 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.