-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreach_avoid.py
executable file
·62 lines (50 loc) · 1.62 KB
/
reach_avoid.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
#!/usr/bin/env python
##
#
# Set up, solve, and plot the solution for a simple
# reach-avoid problem, where the robot must avoid
# a rectangular obstacle before reaching a rectangular
# goal.
#
##
import numpy as np
import matplotlib.pyplot as plt
from stlpy.benchmarks import ReachAvoid
from stlpy.solvers import *
# Specification Parameters
goal_bounds = (7,8,8,9) # (xmin, xmax, ymin, ymax)
obstacle_bounds = (3,5,4,6)
T = 10
# Define the system and specification
scenario = ReachAvoid(goal_bounds, obstacle_bounds, T)
spec = scenario.GetSpecification()
sys = scenario.GetSystem()
# Specify any additional running cost (this helps the numerics in
# a gradient-based method)
Q = 1e-1*np.diag([0,0,1,1]) # just penalize high velocities
R = 1e-1*np.eye(2)
# Initial state
x0 = np.array([1.0,2.0,0,0])
# Choose a solver
#solver = GurobiMICPSolver(spec, sys, x0, T, robustness_cost=True)
#solver = DrakeMICPSolver(spec, sys, x0, T, robustness_cost=True)
#solver = DrakeSos1Solver(spec, sys, x0, T, robustness_cost=True)
solver = DrakeSmoothSolver(spec, sys, x0, T, k=2.0)
#solver = ScipyGradientSolver(spec, sys, x0, T)
# Set bounds on state and control variables
u_min = np.array([-0.5,-0.5])
u_max = np.array([0.5, 0.5])
x_min = np.array([0.0, 0.0, -1.0, -1.0])
x_max = np.array([10.0, 10.0, 1.0, 1.0])
#solver.AddControlBounds(u_min, u_max)
#solver.AddStateBounds(x_min, x_max)
# Add quadratic running cost (optional)
solver.AddQuadraticCost(Q,R)
# Solve the optimization problem
x, u, _, _ = solver.Solve()
if x is not None:
# Plot the solution
ax = plt.gca()
scenario.add_to_plot(ax)
plt.scatter(*x[:2,:])
plt.show()