-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrigidTubeMpcLti.py
More file actions
52 lines (42 loc) · 1.38 KB
/
rigidTubeMpcLti.py
File metadata and controls
52 lines (42 loc) · 1.38 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
# Code implementing the rigid tube MPC algorithm.
# This code does not do closed-loop MPC. It generates an inner approx. of the ROA.
import numpy as np
import problemDef as pdef
import polytope as pc
from solveMPC import solveMpcRigidTube
import matplotlib.pyplot as plt
# Load the parameters of the problem here.
params = pdef.ProblemParams()
# Assign the horizon of the MPC.
params.setHorizon(5)
# Assign the number of initial condition samples to use.
params.setx0SampleCount(576)
# Compute the error invariant E.
params.computeMinRobustPositiveInvariantLTI()
# Compute terminal set Xn.
params.computeMaxPosInvariantLTI()
# Solve MPC from a bunch of sampled initial conditions.
# Store feasible initial conditions as ROA samples.
xs = params.getInitialStateMesh()
xFeas = np.zeros([1, params.nx])
for i in range(params.Nx):
solverSuccessFlag = solveMpcRigidTube(xs[:,i], params)
# If feasible, add to the ROA sample collection set.
if (solverSuccessFlag == True):
xFeas = np.vstack((xFeas, xs[:,i].T))
if (xFeas.shape[0] == 1):
print("Nothing Feasible!")
else:
# Finally form the approximate ROA.
approxRoa = pc.qhull(xFeas)
# Plot a non-empty returned approx. ROA.
if (approxRoa.b.shape[0] !=0):
fig = plt.figure()
ax = fig.gca()
approxRoa.plot(ax)
ax.relim()
ax.autoscale_view()
plt.grid(True)
plt.show()
else:
print("CVX hull not full dimensional. Increase Nx.")