-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
58 lines (46 loc) · 1.9 KB
/
main.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
import random
from mesa import Model
from mesa.space import MultiGrid
from mesa.time import RandomActivation
from agent import LightcycleAgent
def getStartingPosition(startingPositions, isRandom):
if isRandom:
coords = (random.randrange(0, 25), random.randrange(0, 25))
while coords in startingPositions:
coords = (random.randrange(0, 25), random.randrange(0, 25))
return coords
else:
options = [(2, 13), (23, 13), (13, 2), (13, 23), (2, 6), (23, 20), (2, 20), (23, 6), (6, 2), (20, 23), (20, 2),
(6, 23)]
return next(x for x in options if x not in startingPositions)
def getStartingDirection(position, isRandom):
if isRandom:
return random.choice(['N', 'S', 'W', 'E'])
if max(26 - position[0], position[0]) > max(26 - position[1], position[1]):
if 26 - position[0] > position[0]:
return 'E'
else:
return 'W'
else:
if 26 - position[1] > position[1]:
return 'N'
else:
return 'S'
class TronModel(Model):
def __init__(self, n_agents, max_path_length, fov, isStartingPositionRandom):
super().__init__()
self.schedule = RandomActivation(self)
self.grid = MultiGrid(26, 26, torus=False)
self.startingPositions = []
for i in range(n_agents):
self.startingPositions.append(getStartingPosition(self.startingPositions, isStartingPositionRandom))
a = LightcycleAgent(i, self.startingPositions[-1],
getStartingDirection(self.startingPositions[-1], isStartingPositionRandom), self, fov,
max_path_length)
self.schedule.add(a)
self.grid.place_agent(a, self.startingPositions[-1])
def step(self):
self.schedule.step()
if __name__ == '__main__':
model = TronModel(5, 3, 3, False)
model.step()