Skip to content

TheMariuolo/flock_simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

161 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🐦 🐦 🐦 What the Flock! 🐦 🐦 🐦

Flock simulation

Agent based imulation of Flock dynamic with Python. The scripts contained in this repository allow to simulate the flock behaviour given some basic rules, and visualizes it with an animation. This project also includes unit tests to ensure the correct functionality of the simulation.

Table of Contents

  1. Installation
  2. Requirements
  3. Usage and examples
  4. Repository structure
  5. Documentation
  6. Scripts overview
  7. Theory background

Installation

To start using the repository, first clone it:

git clone https://github.com/MassimoMario/flock_simulation.git

Requirements

This project requires Python ≥ 3.8 and the following libraries:

  • numpy
  • matplotlib
  • ipython
  • tqdm
  • pytest
  • pytest-cov

To install them you can run on the Bash:

pip install -r requirements.txt

Usage and examples

The main script simulation.py can be runned from the command line providing different argument ending in different configuration.

ℹ️ Help

For a complete list of parameters and their descriptions, run:

python simulation.py --help

1️⃣ Simulate and animate a flock

Example of a simulation with a parameters that produce a good behaviour flock dynamic:

python simulation.py --N 300 --separation 11 --alignment 2.35 --coherence 1.1 --visual_range 30 --avoid_range 23 

Example GIF of a good behaviour flock dynamic

2️⃣ Provide intial positions and velocities

You can provide initial positions and velocities of N birds in two ways:

1️⃣ Providing individual values:

python simulation.py --N 3 --positions_i 1 1 --positions_i 50 50 --positions_i 30 55 --velocities_i 1 1 --velocities_i -5 5 --velocities_i -1.1 -2.3

⚠️ The previous setup produce an initial position array equal to [[1,1],[50,50],[30,55]] and an initial velocity array equal to [[1,1],[-5,5],[-1.1,-2.3]]

2️⃣ Providing a .npy file:

python simulation.py --N 3 --positions_file init_pos.npy --velocities_file init_vel.npy

3️⃣ Provide a config file

You can provide simulation parameter from a .ini config file:

python simulation.py --config config/config_good_behaviour.ini

4️⃣ Savings

You can save the animation as a GIF and the resulting simulation arrays of bird positions and velocities as .npy files:

python simulation.py --save_anim True --save_ps_vs True

The saving key words are True/Yes, False/No.

Repository structure

The repository contains the following folders and files:

  • simulation.py is the main script for simulating and animating the flock.
  • scripts folder containing the flock class, the test script and the utils scripts
    • flock_class.py contains the class Flock definition, where all the simulation computation are described
    • test.py is the unit test scripts made using pytest
    • utils.py script contains the animation function and some useful functions for the configparser arguments interpretation
  • config folder contains 3 configuration files .ini to be used from command line as inputs in simulation.py script
  • requirements.txt file contains the list of dependencies required for the project
  • docs folder contains the source files used to generate the documentation.

Documentation

The documentation can be seen at this link.

It's a Github page containing the html files generated using Sphinx starting from docstrings in the code.

docs folder contains the source files used to generate this documentation.

Scripts overview

Script containing the Flock class definition. Each Flock object is an ensamble of N birds and can simulate their interaction and dynamic. The only methods accessible to the user are init_given_position, init_given_velocities which initialize the birds positions/velocities with given arrays, and simulate which simulate the flock dynamic given physic parameters such as separation, alignment, coherence and avoidance (see Theory background section).


Script containing the animation function, which animates and displays the flock dynamic with matplotlib.animation and IPython.display taking as input the resulting arrays of a Flock object simulation method. The script contains also some useful functions for the configparser arguments interpretation, which cast a string in the corresponding correct type in order to give it as input to the simulation.


Script containing unit tests for all methods in flock_class.py. The tests ensure correctness and stability of the code following the unit testing approach and are implemented using pytest.

Usage : To run all the tests, navigate in the scripts folder and run the test script:

cd scripts
pytest test.py

Since the Flock class needs a random seed to initialize an object, to provide it run:

SEED=42 pytest test.py

The default seed is 1999.

Coverage : To check the test coverage, use pytest-cov:

cd scripts
pytest --cov=flock_class test.py

It's the main script where all the required parameters are taken from command line using argparse or from a config file .ini using configparser library. In this script a flock dynamic is simulated and animated using a Flock object and the animation function from utils.py. The user can choose to save the animation if GIF format using the --save_anim argument in the command line.

The simulation is agent-based, it computes the interaction between birds and update their positions and velocities following some simple rules. The main paramaters of the simulation are separation, alignment, coherence and avoidance. Each of those parameters controls the strength of a force acting on each bird. See Theory background section for a mathematical description.

⚠️ If you are providing a config file, take in mind that explicit command line arguments overwrite the config file ones.

Theory background

To simulate the collective emerging behaviour of a flock four simple rules have been taken into account that each bird must follow:

  • Separation: move away from neighbour that is too near to avoid collision
  • Alignment: adapt its velocity to the flock average velocity to move in the same direction as other birds
  • Coherence: adapt its position to the flock average position moving toward the center of the flock to keep group toghether
  • Avoidance: move away from obstacles/simulation barriers
Graphical representation of flock rules
Graphical representation of flock rules. From DataSctientest

Mathematically, each rule is associated with a force and a scaling factor, and the simulation update is done using a pseudo accerelerated system formula.

First the flock mean position and velocity is computed, Formula and Formula. Then for each bird $i$ with position Formula is computed the position of the nearest bird to Formula, named nearest(Formula).

For each bird $i$ with position Formula and velocity Formula four forces are computed:

  • Formula
  • Formula
  • Formula
  • Formula

Where Formula is the vector connecting each bird to the center of the simulation space.

The total force Formula is used for updating birds position abd velocity:

  • Formula
  • Formula

About

Python implementation of a flock dynamic simulation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages