From ca7a2d21fc9e65d5c8ff35faa011193c8121163e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksander=20Szama=C5=82ek?= Date: Sun, 6 Mar 2022 22:11:30 +0100 Subject: [PATCH] initial --- .gitignore | 12 +++++++++ ForkliftAgent.py | 0 ForkliftModel.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ README.MD | 0 main.py | 0 requirements.txt | 0 6 files changed, 82 insertions(+) create mode 100644 .gitignore create mode 100644 ForkliftAgent.py create mode 100644 ForkliftModel.py create mode 100644 README.MD create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cfcc546 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +# Virtualenv +# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ +.Python +[Bb]in +[Ii]nclude +[Ll]ib +[Ll]ib64 +[Ll]ocal +[Ss]cripts +pyvenv.cfg +.venv +pip-selfcheck.json \ No newline at end of file diff --git a/ForkliftAgent.py b/ForkliftAgent.py new file mode 100644 index 0000000..e69de29 diff --git a/ForkliftModel.py b/ForkliftModel.py new file mode 100644 index 0000000..08c2219 --- /dev/null +++ b/ForkliftModel.py @@ -0,0 +1,70 @@ +import random + +from mesa import Agent, Model +from mesa.time import RandomActivation +from mesa.space import MultiGrid +from mesa.datacollection import DataCollector + + +def compute_gini(model): + agent_wealths = [agent.wealth for agent in model.schedule.agents] + x = sorted(agent_wealths) + N = model.num_agents + B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x)) + return (1 + (1 / N) - 2 * B) + + +class MoneyModel(Model): + """A model with some number of agents.""" + + def __init__(self, N, width, height): + self.num_agents = N + self.running = True + self.grid = MultiGrid(height, width, True) + self.schedule = RandomActivation(self) + self.datacollector = DataCollector( + model_reporters={"Gini": compute_gini}, + agent_reporters={"Wealth": lambda a: a.wealth} + ) + # Create agents + for i in range(self.num_agents): + a = MoneyAgent(i, self) + self.schedule.add(a) + # Add the agent to a random grid cell + x = random.randrange(self.grid.width) + y = random.randrange(self.grid.height) + self.grid.place_agent(a, (x, y)) + + def step(self): + self.datacollector.collect(self) + self.schedule.step() + + def run_model(self, n): + for i in range(n): + self.step() + + +class MoneyAgent(Agent): + """ An agent with fixed initial wealth.""" + def __init__(self, unique_id, model): + super().__init__(unique_id, model) + self.wealth = 1 + + def move(self): + possible_steps = self.model.grid.get_neighborhood( + self.pos, moore=True, include_center=False + ) + new_position = random.choice(possible_steps) + self.model.grid.move_agent(self, new_position) + + def give_money(self): + cellmates = self.model.grid.get_cell_list_contents([self.pos]) + if len(cellmates) > 1: + other = random.choice(cellmates) + other.wealth += 1 + self.wealth -= 1 + + def step(self): + self.move() + if self.wealth > 0: + self.give_money() \ No newline at end of file diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29