Skip to content

Commit

Permalink
Run ruff on basic examples
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutH committed Oct 16, 2024
1 parent d7a3834 commit 7e118d3
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 65 deletions.
13 changes: 5 additions & 8 deletions examples/basic/boid_flockers/agents.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from mesa import Agent
import numpy as np

from mesa import Agent


class Boid(Agent):
"""
A Boid-style flocker agent.
"""A Boid-style flocker agent.
The agent follows three behaviors to flock:
- Cohesion: steering towards neighboring agents.
Expand All @@ -28,8 +28,7 @@ def __init__(
separate=0.015,
match=0.05,
):
"""
Create a new Boid flocker agent.
"""Create a new Boid flocker agent.
Args:
speed: Distance to move per step.
Expand All @@ -51,10 +50,8 @@ def __init__(
self.neighbors = None

def step(self):
"""Get the Boid's neighbors, compute the new vector, and move accordingly.
"""
Get the Boid's neighbors, compute the new vector, and move accordingly.
"""

self.neighbors = self.model.space.get_neighbors(self.pos, self.vision, False)
n = 0
match_vector, separation_vector, cohere = np.zeros((3, 2))
Expand Down
5 changes: 3 additions & 2 deletions examples/basic/boid_flockers/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from model import BoidFlockers
from mesa.visualization import SolaraViz, make_space_matplotlib
from mesa.visualization import Slider

from mesa.visualization import Slider, SolaraViz, make_space_matplotlib


def boid_draw(agent):
if not agent.neighbors: # Only for the first Frame
Expand Down
15 changes: 6 additions & 9 deletions examples/basic/boid_flockers/model.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
"""
Flockers
"""Flockers
=============================================================
A Mesa implementation of Craig Reynolds's Boids flocker model.
Uses numpy arrays to represent vectors.
"""

import mesa
import numpy as np
from agents import Boid

import mesa


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

def __init__(
Expand All @@ -28,8 +27,7 @@ def __init__(
separate=0.015,
match=0.05,
):
"""
Create a new Flockers model.
"""Create a new Flockers model.
Args:
population: Number of Boids
Expand All @@ -52,8 +50,7 @@ def __init__(
self.make_agents()

def make_agents(self):
"""
Create self.population agents, with random positions and starting headings.
"""Create self.population agents, with random positions and starting headings.
"""
for _ in range(self.population):
x = self.random.random() * self.space.x_max
Expand Down
3 changes: 2 additions & 1 deletion examples/basic/boltzmann_wealth_model/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from model import BoltzmannWealthModel

from mesa.visualization import (
SolaraViz,
make_plot_measure,
make_space_matplotlib,
)
from model import BoltzmannWealthModel


def agent_portrayal(agent):
Expand Down
4 changes: 3 additions & 1 deletion examples/basic/boltzmann_wealth_model/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import mesa
from agents import MoneyAgent

import mesa


class BoltzmannWealthModel(mesa.Model):
"""A simple model of an economy where agents exchange currency at random.
Expand Down
10 changes: 3 additions & 7 deletions examples/basic/conways_game_of_life/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class Cell(Agent):
ALIVE = 1

def __init__(self, pos, model, init_state=DEAD):
"""
Create a cell, in the given state, at the given x, y position.
"""Create a cell, in the given state, at the given x, y position.
"""
super().__init__(model)
self.x, self.y = pos
Expand All @@ -25,14 +24,12 @@ def neighbors(self):
return self.model.grid.iter_neighbors((self.x, self.y), True)

def determine_state(self):
"""
Compute if the cell will be dead or alive at the next tick. This is
"""Compute if the cell will be dead or alive at the next tick. This is
based on the number of alive or dead neighbors. The state is not
changed here, but is just computed and stored in self._nextState,
because our current state may still be necessary for our neighbors
to calculate their next state.
"""

# Get the neighbors and apply the rules on whether to be alive or dead
# at the next tick.
live_neighbors = sum(neighbor.isAlive for neighbor in self.neighbors)
Expand All @@ -47,7 +44,6 @@ def determine_state(self):
self._nextState = self.ALIVE

def assume_state(self):
"""
Set the state to the new computed state -- computed in step().
"""Set the state to the new computed state -- computed in step().
"""
self.state = self._nextState
13 changes: 5 additions & 8 deletions examples/basic/conways_game_of_life/model.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
from agents import Cell

from mesa import Model
from mesa.space import SingleGrid

from agents import Cell


class ConwaysGameOfLife(Model):
"""
Represents the 2-dimensional array of cells in Conway's
"""Represents the 2-dimensional array of cells in Conway's
Game of Life.
"""

def __init__(self, width=50, height=50):
"""
Create a new playing area of (width, height) cells.
"""Create a new playing area of (width, height) cells.
"""
super().__init__()
# Use a simple grid, where edges wrap around.
Expand All @@ -29,8 +27,7 @@ def __init__(self, width=50, height=50):
self.running = True

def step(self):
"""
Perform the model step in two stages:
"""Perform the model step in two stages:
- First, all cells assume their next state (whether they will be dead or alive)
- Then, all cells change state to their next state
"""
Expand Down
3 changes: 1 addition & 2 deletions examples/basic/conways_game_of_life/portrayal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
def portrayCell(cell):
"""
This function is registered with the visualization server to be called
"""This function is registered with the visualization server to be called
each tick to indicate how to draw the cell in its current state.
:param cell: the cell in the simulation
:return: the portrayal dictionary.
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/conways_game_of_life/server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import mesa

from model import ConwaysGameOfLife
from portrayal import portrayCell

import mesa

# Make a world that is 50x50, on a 250x250 display.
canvas_element = mesa.visualization.CanvasGrid(portrayCell, 50, 50, 250, 250)

Expand Down
6 changes: 2 additions & 4 deletions examples/basic/schelling/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@


class SchellingAgent(Agent):
"""
Schelling segregation agent
"""Schelling segregation agent
"""

def __init__(self, model: Model, agent_type: int) -> None:
"""
Create a new Schelling agent.
"""Create a new Schelling agent.
Args:
agent_type: Indicator for the agent's type (minority=1, majority=0)
Expand Down
6 changes: 3 additions & 3 deletions examples/basic/schelling/app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import solara
from model import Schelling

from mesa.visualization import (
Slider,
SolaraViz,
make_plot_measure,
make_space_matplotlib,
)
from model import Schelling


def get_happy_agents(model):
"""
Display a text count of how many happy agents there are.
"""Display a text count of how many happy agents there are.
"""
return solara.Markdown(f"**Happy agents: {model.happy}**")

Expand Down
14 changes: 5 additions & 9 deletions examples/basic/schelling/model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from agents import SchellingAgent

import mesa
from mesa import Model

from agents import SchellingAgent


class Schelling(Model):
"""
Model class for the Schelling segregation model.
"""Model class for the Schelling segregation model.
"""

def __init__(
Expand All @@ -19,8 +18,7 @@ def __init__(
minority_pc=0.2,
seed=None,
):
"""
Create a new Schelling model.
"""Create a new Schelling model.
Args:
width, height: Size of the space.
Expand All @@ -30,7 +28,6 @@ def __init__(
radius: Search radius for checking similarity
seed: Seed for Reproducibility
"""

super().__init__(seed=seed)
self.homophily = homophily
self.radius = radius
Expand All @@ -55,8 +52,7 @@ def __init__(
self.datacollector.collect(self)

def step(self):
"""
Run one step of the model.
"""Run one step of the model.
"""
self.happy = 0 # Reset counter of happy agents
self.agents.shuffle_do("step")
Expand Down
6 changes: 3 additions & 3 deletions examples/basic/virus_on_network/agents.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from mesa import Agent
from enum import Enum

from mesa import Agent


class State(Enum):
SUSCEPTIBLE = 0
Expand All @@ -9,8 +10,7 @@ class State(Enum):


class VirusAgent(Agent):
"""
Individual Agent definition and its properties/interaction methods
"""Individual Agent definition and its properties/interaction methods
"""

def __init__(
Expand Down
3 changes: 2 additions & 1 deletion examples/basic/virus_on_network/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import solara
from matplotlib.figure import Figure
from matplotlib.ticker import MaxNLocator
from mesa.visualization import SolaraViz, Slider, make_space_matplotlib
from model import State, VirusOnNetwork, number_infected

from mesa.visualization import Slider, SolaraViz, make_space_matplotlib


def agent_portrayal(graph):
def get_agent(node):
Expand Down
9 changes: 4 additions & 5 deletions examples/basic/virus_on_network/model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import math

import mesa
from mesa import Model
import networkx as nx
from agents import State, VirusAgent

from agents import VirusAgent, State
import mesa
from mesa import Model


def number_state(model, state):
Expand All @@ -24,8 +24,7 @@ def number_resistant(model):


class VirusOnNetwork(Model):
"""
A virus model with some number of agents
"""A virus model with some number of agents
"""

def __init__(
Expand Down

0 comments on commit 7e118d3

Please sign in to comment.