-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpursuit_game_3vs1_pp.py
240 lines (186 loc) · 10.5 KB
/
pursuit_game_3vs1_pp.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'''
Author: Qin Yang
05/08/2021
'''
#Import Robotarium Utilities
import rps.robotarium as robotarium
from rps.utilities.transformations import *
from rps.utilities.graph import *
from rps.utilities.barrier_certificates import *
from rps.utilities.misc import *
from rps.utilities.controllers import *
import numpy as np
from fractions import Fraction
import time
import random
import os
def evader_detector(numpursuer, x, id, sensing_distance):
detector = False
for i in range(numpursuer):
if np.linalg.norm(x[:2,[i]] - x[:2,[id]]) < sensing_distance:
detector = True
return detector
def gut_pursuit_game():
# Experiment Constants
iterations = 5000 #Run the simulation/experiment for 5000 steps (5000*0.033 ~= 2min 45sec)
N=4 #Number of robots to use, this must stay 4 unless the Laplacian is changed.
close_enough = 0.03; #How close the leader must get to the waypoint to move to the next one.
# sensing distance between pursuer and alien
sensing_distance = 0.3
# For computational/memory reasons, initialize the velocity vector
dx_si = np.zeros((2,N))
#Initialize agent state
numActiveAlien = 0
# initial agent's energy and hp
agent_energy_level = []
pursuer_evader_distance = []
for i in range(N):
# if i < N-1:
# agent_energy_level.append(100)
# agent_hp_level.append(100)
# else:
# agent_energy_level.append(100)
# agent_hp_level.append(100)
agent_energy_level.append(100)
pursuer_evader_distance.append(0)
#Max_simum linear speed of robot specified by motors
magnitude_limit = 0.1
# We're working in single-integrator dynamics, and we don't want the robots
# to collide or drive off the testbed. Thus, we're going to use barrier certificates
si_barrier_cert = create_single_integrator_barrier_certificate_with_boundary()
# Create SI to UNI dynamics tranformation
si_to_uni_dyn, uni_to_si_states = create_si_to_uni_mapping()
# Generated a connected graph Laplacian (for a cylce graph).
L = cycle_GL(N)
si_velocities = np.zeros((2, N))
# Initial Conditions to Avoid Barrier Use in the Beginning.
initial_conditions = np.array([[-0.5, 0, -1.4, 1.4],[0.121, 0.8, -0.8, -0.8],[0, 0, 0, 0]])
# Instantiate the Robotarium object with these parameters
r = robotarium.Robotarium(number_of_robots=N, show_figure=True, initial_conditions=initial_conditions, sim_in_real_time=True)
# define x initially
m = np.zeros((2,N))
CM1 = np.random.rand(N,3)
CM2 = np.random.rand(N,3)
CM3 = np.random.rand(N,3)
marker_size_goal = determine_marker_size(r,0.2)
robot_marker_size_m = 0.35
font_size_m = 0.1
font_size = determine_font_size(r,font_size_m)
font_size_m1 = 0.06
font_size1 = determine_font_size(r,font_size_m1)
font_size_m2 = 0.04
font_size2 = determine_font_size(r,font_size_m2)
marker_size_robot = determine_marker_size(r, robot_marker_size_m)
line_width = 5
# Plot Graph Connections
x = r.get_poses() # Need robot positions to do this.
old_x = []
for i in range(N):
old_x.append(initial_conditions[:2, [i]])
# Create labels
evader_label = r.axes.text(x[0,0],x[1,0]+0.25,"evader",fontsize=font_size1, color='r',fontweight='bold',horizontalalignment='center',verticalalignment='center',zorder=0)
evader_energy_label = r.axes.text(x[0,0],x[1,0]+0.2,"NRG: ",fontsize=font_size2, color='c',fontweight='bold',horizontalalignment='center',verticalalignment='center',zorder=0)
evader_hp_label = r.axes.text(x[0,0],x[1,0]+0.15,"Dist: ",fontsize=font_size2, color='m',fontweight='bold',horizontalalignment='center',verticalalignment='center',zorder=0)
pursuer1_label = r.axes.text(x[0,1],x[1,1]+0.25,"pursuer 1",fontsize=font_size1, color='b',fontweight='bold',horizontalalignment='center',verticalalignment='center',zorder=0)
pursuer1_energy_label = r.axes.text(x[0,1],x[1,1]+0.2,"NRG: ",fontsize=font_size2, color='c',fontweight='bold',horizontalalignment='center',verticalalignment='center',zorder=0)
pursuer1_hp_label = r.axes.text(x[0,1],x[1,1]+0.15,"Dist: ",fontsize=font_size2, color='m',fontweight='bold',horizontalalignment='center',verticalalignment='center',zorder=0)
pursuer2_label = r.axes.text(x[0,2],x[1,2]+0.2,"pursuer 2",fontsize=font_size1, color='b',fontweight='bold',horizontalalignment='center',verticalalignment='center',zorder=0)
pursuer2_energy_label = r.axes.text(x[0,2],x[1,2]+0.15,"NRG: ",fontsize=font_size2, color='c',fontweight='bold',horizontalalignment='center',verticalalignment='center',zorder=0)
pursuer2_hp_label = r.axes.text(x[0,2],x[1,2]+0.1,"Dist: ",fontsize=font_size2, color='m',fontweight='bold',horizontalalignment='center',verticalalignment='center',zorder=0)
pursuer3_label = r.axes.text(x[0,3],x[1,3]+0.25,"pursuer 3",fontsize=font_size1, color='b',fontweight='bold',horizontalalignment='center',verticalalignment='center',zorder=0)
pursuer3_energy_label = r.axes.text(x[0,3],x[1,3]+0.2,"NRG: ",fontsize=font_size2, color='c',fontweight='bold',horizontalalignment='center',verticalalignment='center',zorder=0)
pursuer3_hp_label = r.axes.text(x[0,3],x[1,3]+0.15,"Dist: ",fontsize=font_size2, color='m',fontweight='bold',horizontalalignment='center',verticalalignment='center',zorder=0)
r.step()
for k in range(iterations):
# Get the poses of the robots and convert to single-integrator poses
x = r.get_poses()
x_si = uni_to_si_states(x)
waypoints = np.array([[random.uniform(-1.4, 1.4)], [random.uniform(-1.4, 1.4)]])
evader_label.set_position([x_si[0,0],x_si[1,0]+0.25])
evader_label.set_fontsize(determine_font_size(r,font_size_m1))
evader_energy_label.set_position([x_si[0,0],x_si[1,0]+0.2])
evader_energy_label.set_fontsize(determine_font_size(r,font_size_m2))
evader_energy_label.set_text("NRG: " + str(round(agent_energy_level[0], 2)))
evader_hp_label.set_position([x_si[0,0],x_si[1,0]+0.15])
evader_hp_label.set_fontsize(determine_font_size(r,font_size_m2))
evader_hp_label.set_text("Dist: " + str(round(pursuer_evader_distance[0], 2)))
pursuer1_label.set_position([x_si[0,1],x_si[1,1]+0.25])
pursuer1_label.set_fontsize(determine_font_size(r,font_size_m1))
pursuer1_energy_label.set_position([x_si[0,1],x_si[1,1]+0.2])
pursuer1_energy_label.set_fontsize(determine_font_size(r,font_size_m2))
pursuer1_energy_label.set_text("NRG: " + str(round(agent_energy_level[1], 2)))
pursuer1_hp_label.set_position([x_si[0,1],x_si[1,1]+0.15])
pursuer1_hp_label.set_fontsize(determine_font_size(r,font_size_m2))
pursuer1_hp_label.set_text("Dist: " + str(round(pursuer_evader_distance[1], 2)))
pursuer2_label.set_position([x_si[0,2],x_si[1,2]+0.25])
pursuer2_label.set_fontsize(determine_font_size(r,font_size_m1))
pursuer2_energy_label.set_position([x_si[0,2],x_si[1,2]+0.2])
pursuer2_energy_label.set_fontsize(determine_font_size(r,font_size_m2))
pursuer2_energy_label.set_text("NRG: " + str(round(agent_energy_level[2], 2)))
pursuer2_hp_label.set_position([x_si[0,2],x_si[1,2]+0.15])
pursuer2_hp_label.set_fontsize(determine_font_size(r,font_size_m2))
pursuer2_hp_label.set_text("Dist: " + str(round(pursuer_evader_distance[2], 2)))
pursuer3_label.set_position([x_si[0,3],x_si[1,3]+0.25])
pursuer3_label.set_fontsize(determine_font_size(r,font_size_m1))
pursuer3_energy_label.set_position([x_si[0,3],x_si[1,3]+0.2])
pursuer3_energy_label.set_fontsize(determine_font_size(r,font_size_m2))
pursuer3_energy_label.set_text("NRG: " + str(round(agent_energy_level[3], 2)))
pursuer3_hp_label.set_position([x_si[0,3],x_si[1,3]+0.15])
pursuer3_hp_label.set_fontsize(determine_font_size(r,font_size_m2))
pursuer3_hp_label.set_text("Dist: " + str(round(pursuer_evader_distance[3], 2)))
# Initialize the single-integrator control inputs
#si_velocities = np.zeros((2, N))
# For each robot...
for i in range(N):
# Get the neighbors of robot 'i' (encoded in the graph Laplacian)
j = topological_neighbors(L, i)
# Compute the pp algorithm
if i == 0 and k%20 == 0:
si_velocities[:,i] = np.sum(waypoints[:, 0, None] - x_si[:, i, None], 1)
if i >= 1:
si_velocities[:, i] = np.sum(x_si[:, j] - x_si[:, i, None], 1)
# #Keep single integrator control vectors under specified magnitude
# # Threshold control inputs
norms = np.linalg.norm(si_velocities, 2, 0)
idxs_to_normalize = (norms > magnitude_limit)
si_velocities[:, idxs_to_normalize] *= magnitude_limit/norms[idxs_to_normalize]
# Use the barrier certificate to avoid collisions
si_velocities = si_barrier_cert(si_velocities, x_si)
# Transform single integrator to unicycle
dxu = si_to_uni_dyn(si_velocities, x)
for i in range(N):
# if i == 0: # evader
# dxu[:,i] = dxu[:,i] * 1.5
if i >= 1: # pursuer
dxu[:,i] = dxu[:,i] * 1.05
# if i==1 and k%100==0: # pursuer
# if i ==1 and k > 50:
# dxu[1,i] = random.random() * np.sign(random.uniform(-1, 1)) * 100
# dxu[1,i] = random.uniform(-100, 100)
# Set the velocities of agents 1,...,N
r.set_velocities(np.arange(N), dxu)
# Calculate agent energy cost
for i in range(N):
agent_energy_level[i] -= np.linalg.norm(old_x[i] - x[:2,[i]]) * 10
# Calculate the distance between pursuer and envader
if i == 0:
pursuer_evader_distance[i] = np.linalg.norm(old_x[i] - x[:2,[1]]) * 10
else:
pursuer_evader_distance[i] = np.linalg.norm(old_x[i] - x[:2,[0]]) * 10
# # detect the number of aliens
# if evader_detector(N-1, x, -1, sensing_distance):
# numActiveAlien +=1
# recode old position
old_x.clear()
for i in range(N):
old_x.append(x[:2, [i]])
# if (np.array(pursuer_evader_distance) <= 4).all():
if (pursuer_evader_distance[1] + pursuer_evader_distance[2] + pursuer_evader_distance[3]) / 3 <= 3.3:
print('cost time is ' + str(k))
os._exit(0)
# Iterate the simulation
r.step()
def main():
gut_pursuit_game()
if __name__ == '__main__':
main()