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

Cleanup and restructure basic example models #2365

Merged
merged 9 commits into from
Oct 16, 2024
Merged
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
113 changes: 0 additions & 113 deletions examples/basic/boid_flockers/Flocker Test.ipynb

This file was deleted.

8 changes: 2 additions & 6 deletions examples/basic/boid_flockers/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ Directly run the file ``run.py`` in the terminal. e.g.

## Files

* [boid_flockers/model.py](boid_flockers/model.py): Core model file; contains the Boid Model and Boid Agent class.
* [boid_flockers/SimpleContinuousModule.py](boid_flockers/SimpleContinuousModule.py): Defines ``SimpleCanvas``, the Python side of a custom visualization module for drawing agents with continuous positions.
* [boid_flockers/simple_continuous_canvas.js](boid_flockers/simple_continuous_canvas.js): JavaScript side of the ``SimpleCanvas`` visualization module; takes the output generated by the Python ``SimpleCanvas`` element and draws it in the browser window via HTML5 canvas.
* [boid_flockers/server.py](boid_flockers/server.py): Sets up the visualization; uses the SimpleCanvas element defined above
* [run.py](run.py) Launches the visualization.
* [Flocker_Test.ipynb](Flocker_Test.ipynb): Tests the model in a Jupyter notebook.
* [model.py](model.py): Core model file; contains the Boid Model and Boid Agent class.
* [app.py](app.py): Visualization code.

## Further Reading

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
"""
Flockers
=============================================================
A Mesa implementation of Craig Reynolds's Boids flocker model.
Uses numpy arrays to represent vectors.
"""

import mesa
from mesa import Agent
import numpy as np


class Boid(mesa.Agent):
class Boid(Agent):
"""
A Boid-style flocker agent.

Expand Down Expand Up @@ -80,67 +73,3 @@ def step(self):
self.direction /= np.linalg.norm(self.direction)
new_pos = self.pos + self.direction * self.speed
self.model.space.move_agent(self, new_pos)


class BoidFlockers(mesa.Model):
"""
Flocker model class. Handles agent creation, placement and scheduling.
"""

def __init__(
self,
seed=None,
population=100,
width=100,
height=100,
vision=10,
speed=1,
separation=1,
cohere=0.03,
separate=0.015,
match=0.05,
):
"""
Create a new Flockers model.

Args:
population: Number of Boids
width, height: Size of the space.
speed: How fast should the Boids move.
vision: How far around should each Boid look for its neighbors
separation: What's the minimum distance each Boid will attempt to
keep from any other
cohere, separate, match: factors for the relative importance of
the three drives.
"""
super().__init__(seed=seed)
self.population = population
self.vision = vision
self.speed = speed
self.separation = separation

self.space = mesa.space.ContinuousSpace(width, height, True)
self.factors = {"cohere": cohere, "separate": separate, "match": match}
self.make_agents()

def make_agents(self):
"""
Create self.population agents, with random positions and starting headings.
"""
for _ in range(self.population):
x = self.random.random() * self.space.x_max
y = self.random.random() * self.space.y_max
pos = np.array((x, y))
direction = np.random.random(2) * 2 - 1
boid = Boid(
model=self,
speed=self.speed,
direction=direction,
vision=self.vision,
separation=self.separation,
**self.factors,
)
self.space.place_agent(boid, pos)

def step(self):
self.agents.shuffle_do("step")
Loading
Loading