Giter Site home page Giter Site logo

mpdcampbell / nat-selection Goto Github PK

View Code? Open in Web Editor NEW
5.0 4.0 3.0 48.38 MB

A natural selection simulation where blobs compete and evolve depending on environment

License: MIT License

C++ 99.96% C 0.04%
evolution-simulation educational primer natural-selection

nat-selection's Introduction

nat-selection

This is a recreation of the natural selection simulation demonstrated in a youtube video, where creatures compete and their traits evolve depending on environment pressure. Primer has his own github repository, but none of his code was used here. The idea was to copy the simulation "rules" set out in the video and independently recreate it in C++. The simulation runs in its entirety offscreen, then gnuplot is used (via the gnuplot-iostream api) to produce plots of how the population number and the creature traits changed. Additionally, a 2D animation of the simulation is displayed, using the olcPixelGameEngine library, as each generated frame is passed to ffmpeg to create a local video.

Example Graphs

Average change plot for a single simulation Average change plot for 10 repeat simulation runs Trait distribution for a given simulation

Example Animations

Blob creature art


Simulation Rules

The simulation creates a squared grid map, with "blob" creatures living at the edges, and randomly populates it with food. Each day the blob creatures leave home to hunt for food. If a blob eats one food object and makes it home, it will survive to the next day. If a blob eats two "food" and makes it home then they reproduce asexually. Each blob has three traits to help it hunt food:

  • Size - A blob can eat another blob so long as the prey is 20% smaller than it.
  • Speed - How quickly a blob moves between grid spaces.
  • Sense - The radius of grid spaces around it that a blob can see.

However, each of these traits has an energy cost. A blob starts each day with an equal amount of energy and each step taken has an energy cost, where cost = size3speed2+sense. When a blob reproduces, there is mutation chance that one or more of the child's traits will be increased or decreased in value relative to the parent. As the population grows and competition increases, blobs with less preferable traits die out, and the population evolves towards optimal traits for that environment.

External Requirements To Run

  • Install gnuplot (last tested with version 5.2.7)
  • Install ffmpeg (last tested with version 4.2)
  • The boost C++ libraries (tested with version 1.70.0)

gnuplot installation

The latest version of gnuplot can be found here, last tested version here. Gnuplot just needs to be installed on the system such that "gnuplot" is a recognised command in cmd/terminal. In windows (my system) I used the "gp527-win64-mingw.exe" installer, ensuring to check the "Add application directory to your PATH environment variable" option. Otherwise gnuplot cannot be called from cmd.

ffmpeg installation

The latest version of ffmpeg can be found here.

boost libraries

The gnuplot-iosteam api is dependent on the boost libraries. The boost header (.h) files must be visible to the compiler, and the library (.lib and .dll) files visible to the linker. If they are not made available to the IDE the code will not compile. The following instructions are for Visual Studio (the IDE I used).

  • Download the latest boost version (last tested version here) and extract.
  • To add the header files to the include directory, in the toolbar, open Project -> Properties -> VC++ Directories -> Include Directories.
  • Click the dropdown arrow then <Edit...> and add the path to the overall boost folder, in my case C:\boost_1_70_0
  • To add the library files, in the toolbar open Project -> Properties -> Linker -> General -> Additional Library Directories
  • Click the dropdown arrow then <Edit...> and add the path to the lib and lib64-msvc-14.1 folders, e.g. C:\boost_1_70_0\lib and C:\boost_1_70_0\lib64-msvc-14.1

Note: I kept encountering linker errors with the \lib files when trying x86 builds within Visual Studio. Not sure whether this is a boost library problem, a problem with gnuplot-iostream on windows, or my own mistake. But rather than fix it, I just removed the lib path (kept the lib64 path) and always compiled x64 builds.

Art

The pixel art for the blob creatures and food were both adapted from free resources on itch.io, found here and here respectively.

nat-selection's People

Contributors

mpdcampbell avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nat-selection's Issues

Algorithm

What is the algorithm you use in your simulation are you use genetic algorithm and neural network?

Installation

You Said, you used Visual studio but i its not appear to me the properties optinal in cpp i cant add the Boost library to the project
And when i run it saw to me Please selected a valid startup item

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.