-
Notifications
You must be signed in to change notification settings - Fork 0
/
coopa_model.py
103 lines (80 loc) · 3.37 KB
/
coopa_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import logging
import time
from mesa import Model
from trash import Trash
from drop_point import DropPoint
from mesa.time import RandomActivation
from mesa.space import SingleGrid
from mesa.datacollection import DataCollector
from message_dispatcher import MessageDispatcher
from layout import Layout
from ui_styling import AGENT_TYPES
from recharge_point import RechargePoint
from context import Context
import utils
def compute_gini(model):
agent_resources = [agent.trash_count for agent in model.schedule.agents]
x = sorted(agent_resources)
N = model.num_agents
if sum(x) > 0:
B = sum(xi * (N-i) for i, xi in enumerate(x)) / (N*sum(x))
return 1 + (1/N) - 2*B
else:
return 0
def compute_dropped_trashes(model):
drop_point_trashes = [dp.trash_count for dp in model.drop_points]
return sum(drop_point_trashes)
def compute_average_battery_power(model):
bps = [agent.battery_power for agent in model.schedule.agents if hasattr(agent, 'battery_power')]
return sum(bps) / len(bps)
def compute_max_battery_power(model):
bps = [agent.battery_power for agent in model.schedule.agents if hasattr(agent, 'battery_power')]
return max(bps)
def compute_min_battery_power(model):
bps = [agent.battery_power for agent in model.schedule.agents if hasattr(agent, 'battery_power')]
return min(bps)
class CoopaModel(Model):
"""A model with some number of agents."""
def __init__(self, N, width, height, agent_type, log_path=None):
self.running = True
self.num_agents = N
self.grid = SingleGrid(width, height, torus=False)
self.schedule = RandomActivation(self)
self.message_dispatcher = MessageDispatcher()
self.layout = Layout()
self._context = Context()
self.agent_type = AGENT_TYPES[agent_type]
self.layout.draw(self.grid)
# Add drop point(s)
self.drop_points = [DropPoint(1, self)]
self.grid.place_agent(self.drop_points[0], (5,5))
# Add recharging station(s)
self.recharge_points = [RechargePoint(1, self)]
self.grid.place_agent(self.recharge_points[0], (55,5))
# Place resources tactically
self._context.place_few_trash_in_all_rooms(self)
# the mighty agents arrive
for i in range(self.num_agents):
a = self.agent_type(i, self, log_path=log_path)
self.schedule.add(a)
self.grid.position_agent(a)
self.datacollector = DataCollector(
model_reporters={"Trash collected": compute_dropped_trashes,
"Average battery power": compute_average_battery_power,
"Max battery power": compute_max_battery_power,
"Min battery power": compute_min_battery_power
},
# agent_reporters={"Trash": "trash_count"}
) # An agent attribute
self.name = "CoopaModel"
self._logger = utils.create_logger(self.name, log_path=log_path)
@property
def time(self):
return self.schedule.time
def step(self):
t = time.monotonic()
self.datacollector.collect(self)
self.schedule.step()
self._log("Finished in {:.5f} seconds.".format(time.monotonic() - t), logging.INFO)
def _log(self, msg, lvl=logging.DEBUG):
self._logger.log(lvl, msg, extra={'time': self.time})