-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModeloReto.py
110 lines (93 loc) · 3.56 KB
/
ModeloReto.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
104
105
106
107
108
109
110
from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.space import MultiGrid
from mesa.datacollection import DataCollector
import random
class Semaforo(Agent):
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
self.estado = "verde"
self.ticks = 0
def step(self):
self.ticks += 1
if self.ticks == 6:
self.estado = "rojo"
elif self.ticks == 9:
self.estado = "verde"
self.ticks = 0
class Peaton(Agent):
def __init__(self, unique_id, model, lado):
super().__init__(unique_id, model)
self.lado = lado
def step(self):
cell = self.model.grid.get_cell_list_contents([self.pos])
if cell:
cell = cell[0]
if isinstance(cell, Semaforo) and cell.estado == "verde" and cell.ticks < 3:
self.model.grid.move_agent(self, (self.pos[0], 1 - self.pos[1]))
class Guardia(Agent):
def step(self):
cell = self.model.grid.get_cell_list_contents([self.pos])
if cell:
cell = cell[0]
if isinstance(cell, Semaforo) and cell.ticks >= 9:
neighbors = self.model.grid.get_neighbors(self.pos, moore=False)
for neighbor in neighbors:
if isinstance(neighbor, Peaton):
self.model.grid.move_agent(neighbor, (neighbor.pos[0], 1 - neighbor.pos[1]))
cell.ticks = 0
class Vehiculo(Agent):
def step(self):
cell = self.model.grid.get_cell_list_contents([self.pos])
if cell:
cell = cell[0]
if isinstance(cell, Guardia):
neighbors = self.model.grid.get_neighbors(self.pos, moore=False)
for neighbor in neighbors:
if isinstance(neighbor, Peaton):
self.model.grid.move_agent(neighbor, (neighbor.pos[0], 1 - neighbor.pos[1]))
class CruceModel(Model):
def __init__(self, width, height, num_peatones):
self.num_peatones = num_peatones
self.grid = MultiGrid(width, height, True)
self.schedule = RandomActivation(self)
# Crear agentes
semaforo = Semaforo(0, self)
self.schedule.add(semaforo)
self.grid.place_agent(semaforo, (1, 1))
guardia = Guardia(1, self)
self.schedule.add(guardia)
self.grid.place_agent(guardia, (0, 0))
for i in range(self.num_peatones):
lado = "tec" if random.random() < 0.5 else "e1"
peaton = Peaton(i + 2, self, lado)
self.schedule.add(peaton)
if lado == "tec":
self.grid.place_agent(peaton, (0, 1))
else:
self.grid.place_agent(peaton, (1, 0))
for i in range(2):
vehiculo = Vehiculo(i + self.num_peatones + 2, self)
self.schedule.add(vehiculo)
if i == 0:
self.grid.place_agent(vehiculo, (0, 1))
else:
self.grid.place_agent(vehiculo, (1, 0))
self.datacollector = DataCollector(
agent_reporters={"Estado Semaforo": "estado"}
)
def step(self):
self.datacollector.collect(self)
self.schedule.step()
# Parámetros del modelo
width = 2
height = 2
num_peatones = 6
# Crear el modelo
model = CruceModel(width, height, num_peatones)
# Simular
for i in range(20):
print(f"Tick {i}")
model.step()
# Mostrar los datos recopilados del semáforo
print(model.datacollector.get_agent_vars_dataframe().head())