Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leon #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# sheep-flock
# sheep-flock

### About

An agent-based model implementation for simulating a sheep flock and herd dogs.

In the .env file, you can set parameters for a simulation. We recommend focussing on these parameters:
- N_SHEEP
- N_DOG
- SPAWN_CONTROLLABLE_DOG (Set to True for a controllable dog that can be controlled using the arrow keys)
- SHEEP_SPAWN_DISTRIBUTION

### Setup

Use Python 3.12 or a similar version. Create a virtual environment using ``python -m venv venv``, activate it (Windows: ``venv\Scripts\activate ``, Linux/macOS: ``source venv/bin/activate``) and use ``pip install -r requirements.txt`` to install the necessary packages into the venv.

To start a simulation, run the main.py.

from the root directory of the project run:
```
python -m src.main
```



Binary file added Report.pdf
Binary file not shown.
Binary file modified requirements.txt
Binary file not shown.
11 changes: 5 additions & 6 deletions src/.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Environment basics
N_SHEEP=70
N_DOG=4
N_DOG=3
SHEEP_SPAWN_DISTRIBUTION=normal # Options: "uniform" or "normal"

# Controllable dog
SPAWN_CONTROLLABLE_DOG=False
CONTROLLABLE_DOG_MAX_SPEED = 0.02

# Sheep parameters
ANIMAL_OBSERVATION_RADIUS=0.55
SHEEP_MAX_SPEED=0.001
Expand Down Expand Up @@ -33,8 +37,3 @@ COLOR_SHEEP_DEFAULT=255,255,255
COLOR_SHEEP_EXCITED=0,0,0
COLOR_DOG=153,102,51
COLOR_CONTROLLABLE_DOG=0,0,255


# Controllable DOG
SPAWN_CONTROLLABLE_DOG=True
CONTROLLABLE_DOG_MAX_SPEED = 0.02
6 changes: 5 additions & 1 deletion src/animals/sheep.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from pygame import Color, Vector2
from src.animals.animals import Animal
from src.utils import timed
from src.animals.animals import Animal
import random as rand


Expand Down Expand Up @@ -164,6 +164,10 @@ def _calculate_separation_velocity(self, sheep):

@timed
def _calculate_dog_avoidance(self, dogs):

if not dogs:
return Vector2(0, 0) # No dogs at all

close_dogs = [
dog for dog in dogs
if (self.position - dog.position).magnitude() <= self.dog_avoidance_radius
Expand Down
13 changes: 6 additions & 7 deletions src/environment.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from distutils.util import strtobool

import numpy as np
import pygame
import os
from pygame import Vector2, DOUBLEBUF
from animals.sheep import Sheep
from animals.dog import Dog, ControllableDog
from utils import timed
from src.animals.sheep import Sheep
from src.animals.dog import Dog, ControllableDog
from src.utils import timed, strtobool
import random as rand


Expand Down Expand Up @@ -60,11 +58,12 @@ def _translate_to_canvas(self, vector: Vector2):
return translated

def _init_herd(self) -> list[Sheep]:

sheep = []

spawn_distribution = os.getenv("SHEEP_SPAWN_DISTRIBUTION")

print(spawn_distribution)
print(f"spawning sheep with distribution: {spawn_distribution}")

mean = [0, 0]
cov = np.array([[0.1, 0], # Variance for x and y (diagonal values)
Expand Down Expand Up @@ -119,7 +118,7 @@ def update_animals(self):
herd_copy = self.herd.copy()
dogs_copy = self.dogs.copy()

p_excited = 0.002
p_excited = 0.001
self._choose_sheep_to_excite(p_excited)

for sheep in self.herd:
Expand Down
7 changes: 6 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
from dotenv import load_dotenv
load_dotenv()

from environment import Environment
from src.environment import Environment
import os
import random as rand
import numpy as np

seed = 2
rand.seed(seed)
np.random.seed(seed)

if __name__ == "__main__":
n_sheep = int(os.getenv("N_SHEEP", 0))
Expand Down
14 changes: 13 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@

SHOULD_TIME = bool(os.getenv("TIME_EXECUTION", 0))


def timed(func):
def timer_wrapper(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
duration = time.time() - start
open(LOG_OUTPUTFILE, "a").write(f"{func.__name__},{duration}\n")
return res

def default(*args, **kwargs):
return func(*args, **kwargs)


return timer_wrapper if SHOULD_TIME else default


def strtobool(val: str) -> int:
val = val.lower()
if val in ("y", "yes", "t", "true", "on", "1"):
return 1
elif val in ("n", "no", "f", "false", "off", "0"):
return 0
else:
raise ValueError(f"Invalid truth value: {val}")