forked from kmmille/FACTEST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactest.py
214 lines (187 loc) · 5.66 KB
/
factest.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
#!/usr/bin/env python
# Run benchmarks
# Written by: Kristina Miller
import argparse
import sys
import time
import csv
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import pypoman as ppm
from util.ref2traj import *
from util.xref_yices import *
from util.partition_theta import *
from util.plot_polytope3d import *
parser = argparse.ArgumentParser()
parser.add_argument("model")
parser.add_argument("env")
parser.add_argument("--segs")
parser.add_argument("--plot")
args = parser.parse_args()
# Import the model
if args.model == 'car':
from models.kinematic_car import *
elif args.model == 'robot':
from models.bijective_robot import *
elif args.model == 'hovercraft':
from models.kinematic_model_3d import *
elif args.model == 'auv':
from models.auv_6d import *
else:
print('Not a valid model argument!')
sys.exit()
# Import the environment
if args.env == 'zigzag1':
from envs.zigzag import *
dim = 2
elif args.env == 'zigzag2':
from envs.zigzag2 import *
dim = 2
elif args.env == 'zigzag3':
from envs.zigzag3 import *
dim = 2
elif args.env == 'maze':
from envs.maze import *
dim = 2
elif args.env == 'SCOTS':
from envs.SCOTS import *
dim = 2
elif args.env == 'barrier':
from envs.partition2 import *
dim = 2
elif args.env == 'demo':
from envs.demo import *
dim = 2
elif args.env == 'ztunnel':
from envs.ztunnel import *
dim = 3
elif args.env == 'ltunnel':
from envs.Ltunnel import *
dim = 3
else:
print('Not a valid env argument!')
sys.exit()
# Make sure the model and the environment are compatible
if args.model in ['car', 'robot'] and args.env in ['ztunnel', 'ltunnel']:
print("Can't run 2d model in 3d workspace!")
sys.exit()
elif args.model not in ['car', 'robot'] and args.env not in ['ztunnel', 'ltunnel']:
print("Can't run 3d model in 2d workspace!")
sys.exit()
# Check the number of line segments
if args.segs == None:
max_segs = 100
else:
try:
max_segs = int(args.segs)
except:
print('segs must be an integer!')
sys.exit()
# Main controller algorithm
def main_algo():
obs, Theta, goal = problem() # Get the problem
theta_list = [Theta]
final_theta = []
ts = time.time()
while theta_list != []:
theta = theta_list.pop()
alpha = max_lyapunov(theta)
nodes = get_xref_yices(theta, goal, obs, max_segs, bloating, alpha) # Get the reference nodes
if nodes != None:
final_theta.append((theta, nodes))
else:
if alpha > 0.1: # This is the minimum size that Theta can be (Part_min)
if dim == 2:
theta_list.extend(shrink_Theta(theta))
else:
theta_list.extend(shrink_Theta3d(theta))
else:
final_theta.append((theta, 'FAIL'))
final_controller = []
for theta, nodes in final_theta:
if nodes != 'FAIL':
for node in nodes: # Convert to usable form
for i in range(len(node)):
node[i] = float(node[i])
final_controller.append((theta, ref2traj(nodes, 1)))
te = time.time()
with open('results/synthesis-results/'+args.model+'_'+args.env+'.csv', 'w') as file:
writer = csv.writer(file)
writer.writerows([['model', 'env', 'Theta size', 'num obs', 'time', 'num parts'],
[args.model, args.env, max_lyapunov(Theta), len(obs), te-ts, len(final_controller)]])
print('Saved results to results/synthesis-results/'+args.model+'_'+args.env+'.csv')
# final_controller contains (theta, (t, xref, uref)_seg)
return final_controller
# Plot the problem and the test runs
def plot_results(final_controller):
obs, Theta, goal = problem()
if dim == 2:
for Ao, bo in obs:
ppm.polygon.plot_polygon(ppm.duality.compute_polytope_vertices(Ao, bo), color = 'r')
ppm.polygon.plot_polygon(ppm.duality.compute_polytope_vertices(goal[0], goal[1]), color = 'g')
for theta, segs in final_controller:
if segs == 'FAIL':
ppm.polygon.plot_polygon(ppm.duality.compute_polytope_vertices(theta[0], theta[1]), color = 'r')
else:
ppm.polygon.plot_polygon(ppm.duality.compute_polytope_vertices(theta[0], theta[1]), color = 'b')
if args.model == 'car':
q0 = randomly_sample(theta)+[0]
else:
q0 = randomly_sample(theta)+[0,1]
for seg in segs:
xref = [seg[1][i][0] for i in range(len(seg[1]))]
yref = [seg[1][i][1] for i in range(len(seg[1]))]
plt.plot(xref, yref, color = 'k')
t = seg[0]
qref = seg[1]
uref = seg[2]
q = run_model(q0, t, qref, uref)
q0 = q[-1]
x = [q[i][0] for i in range(len(q))]
y = [q[i][1] for i in range(len(q))]
plt.plot(x, y, 'r--')
else:
ax = plot_problem()
for theta, segs in final_controller:
if segs != 'FAIL':
if args.model == 'hovercraft':
q0 = [segs[0][1][0][0], segs[0][1][0][1], segs[0][1][0][2], segs[0][1][0][4]]
else:
q0 = segs[0][1][0]
for seg in segs:
xref = [seg[1][i][0] for i in range(len(seg[1]))]
yref = [seg[1][i][1] for i in range(len(seg[1]))]
zref = [seg[1][i][2] for i in range(len(seg[1]))]
ax.plot(xref, yref, zref, color = 'k')
t = seg[0]
qref = seg[1]
uref = seg[2]
q = run_model(q0, t, qref, uref)
q0 = q[-1]
x = [q[i][0] for i in range(len(q))]
y = [q[i][1] for i in range(len(q))]
z = [q[i][2] for i in range(len(q))]
plt.plot(x, y, z, 'r--')
if args.env == 'SCOTS':
plt.xlim(0,10)
plt.ylim(0,10)
elif 'zigzag' in args.env:
plt.xlim(-1.5,5)
plt.ylim(0,3)
elif args.env == 'maze':
plt.xlim(0,7)
plt.ylim(0,5)
elif args.env == 'barrier':
plt.xlim(0,4)
plt.ylim(0,4)
plt.title(args.model + '-' + args.env)
plt.savefig('results/figures/'+args.model+'_'+args.env+'.png')
print('Saved figure to results/figures/'+args.model+'_'+args.env+'.png')
return None
if __name__ == '__main__':
final_controller = main_algo()
if args.plot == 'True':
plot_results(final_controller)
plt.show()
else:
sys.exit()