Repository navigation
Expand file tree
/
Copy pathSimulation.py
More file actions
138 lines (101 loc) · 4.01 KB
/
Copy pathSimulation.py
File metadata and controls
138 lines (101 loc) · 4.01 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from Robot import Robot, MoveStrategy
from RobotFileld import RobotField
from View import View
from Graph import GridLocation
import random
import itertools
class Simulation:
def __init__(self, field: RobotField,
robots: list[Robot],
colors: list[tuple[int, int, ]],
view: View,
infinity: bool = False):
self._field = field
self._robots = robots
# TODO: put colors in another place
self._colors = colors
self._view = view
self._infinity = infinity
def do_step(self):
robots = self._robots.copy()
adjacencyList = self._make_adjastment()
strategies = setting_active(adjacencyList, self._robots)
for (robot, strategy) in strategies.items():
if strategy == 1:
robot.move_strategy = MoveStrategy.ACTIVE
for robot in self._robots:
except_one = list(filter(lambda x: x != robot, robots))
robot.do_next_step(except_one)
self._update_strategy()
def _update_strategy(self):
for robot in self._robots:
if robot.location is robot.goal:
robot.move_strategy = MoveStrategy.ONGOAL
for robot in filter(lambda x: x.move_strategy is not MoveStrategy.ONGOAL,
self._robots):
robot.move_strategy = MoveStrategy.PASSIVE
def show(self):
self._view.draw_field(self._field)
#draw all robots
for (i, robot) in enumerate(self._robots):
self._view.draw_robot(self._field, robot.location,
robot.get_direction(), self._colors[i])
self._view.draw_goal(self._field, robot.goal, self._colors[i])
self._view.update_screen()
def is_all_robots_on_goals(self):
if self._infinity:
for robot in filter(lambda x: x.goal == x.location, self._robots):
robot.goal = self._update_goal(robot)
return False
for robot in self._robots:
if robot.location != robot.goal:
return False
return True
def _update_goal(self, robot: Robot) -> GridLocation:
goals = [robot.goal for robot in self._robots]
x_range = range(self._field.width)
y_range = range(self._field.height)
coords = list(itertools.product(x_range, y_range))
free_coords = list(filter(lambda x: x not in goals, coords))
new_goal = random.sample(free_coords,1)[0]
return new_goal
def _make_adjastment(self):
adjacencyList = {}
for vertex in self._robots:
adjacencyList[vertex] = []
robots = self._robots.copy()
for robot in self._robots:
except_one = list(filter(lambda x: x != robot, robots))
for other_robot in except_one:
if robot.distance(other_robot) < 2 and \
robot.move_strategy is not MoveStrategy.ONGOAL:
adjacencyList[robot].append(other_robot)
return adjacencyList
def setting_active(adjacency: dict[int, list[int]], vertices: set[int]):
queue = []
states = {}
for val in vertices:
states[val] = -1
#every isolate nodes is passive
for (key, adj) in adjacency.items():
if len(adj) == 0:
states[key] = 0
min = 10000
for (_, adj) in adjacency.items():
if len(adj) < min and len(adj) > 0:
min = len(adj)
for (key, adj) in adjacency.items():
if min == len(adj):
queue.append(key)
while len(queue) > 0:
node = queue.pop(0)
if states[node] == -1:
states[node] = 1
for adj in adjacency[node]:
states[adj] = 0
queue.append(adj)
elif states[node] == 0:
for adj in adjacency[node]:
if states[adj] == -1:
queue.append(adj)
return states