alpaqa 0.0.1
Nonconvex constrained optimization
Classes | Variables
hanging-chain-mpc Namespace Reference

Classes

class  Animation
 
class  MPCController
 

Variables

float Ts = 0.05
 
int N = 6
 
int dim = 2
 
 model = HangingChain(N, dim)
 
 f_d = model.dynamics(Ts)
 
 y_null
 
 u_null
 
list param = [0.03, 1.6, 0.033 / N]
 
int N_dist = 3
 
list u_dist = 3 else [-0.5, 0.5]
 
 y_dist = model.simulate(N_dist, y_null, u_dist, param)
 
int N_sim = 180
 
 y_sim = model.simulate(N_sim, y_dist[:, -1], u_null, param)
 
int N_horiz = 12
 
 L_cost = model.generate_cost_fun()
 
 y_init = cs.SX.sym("y_init", *y_null.shape)
 
 U = cs.SX.sym("U", dim * N_horiz)
 
 constr_param = cs.SX.sym("c", 3)
 
 mpc_param = cs.vertcat(y_init, model.params, constr_param)
 
 U_mat = model.input_to_matrix(U)
 
 mpc_sim = model.simulate(N_horiz, y_init, U_mat, model.params)
 
int mpc_cost = 0
 
 y_n = mpc_sim[:, n]
 
 u_n = U_mat[:, n]
 
 mpc_cost_fun = cs.Function('f_mpc', [U, mpc_param], [mpc_cost])
 
int g_constr = lambda c, x: c[0] * x**3 + c[1] * x**2 + c[2] * x
 
list constr = []
 
 yx_n = y_n[dim * i]
 
 yy_n = y_n[dim * i + dim - 1]
 
 mpc_constr_fun = cs.Function("g", [U, mpc_param], [cs.vertcat(*constr)])
 
 a
 
 b
 
 c
 
 d
 
list constr_coeff = [c, -3 * a * c, 3 * a * a * c + d]
 
int constr_lb = b - c * a**3 - d * a
 
 prob = pa.generate_and_compile_casadi_problem(mpc_cost_fun, mpc_constr_fun)
 
 lowerbound
 
 upperbound
 
 solver
 
 n_state = y_n.shape[0]
 
 y_mpc = np.empty((n_state, N_sim))
 
 controller = MPCController(model, prob)
 
 fig
 
 ax
 
 x = np.linspace(-0.25, 1.25, 256)
 
 y = np.linspace(-2.5, 1, 256)
 
 z
 
 label
 
 X
 
 Y
 
int Z = g_constr(constr_coeff, X) + constr_lb - Y
 
list fx = [patheffects.withTickedStroke(spacing=7, linewidth=0.8)]
 
 cgc = plt.contour(X, Y, Z, [0], colors='tab:green', linewidths=0.8)
 
 collections
 
 path_effects
 
 ani
 
 out
 
 exist_ok
 

Variable Documentation

◆ Ts

float Ts = 0.05

Definition at line 18 of file hanging-chain-mpc.py.

◆ N

int N = 6

Definition at line 19 of file hanging-chain-mpc.py.

◆ dim

int dim = 2

Definition at line 20 of file hanging-chain-mpc.py.

◆ model

model = HangingChain(N, dim)

Definition at line 22 of file hanging-chain-mpc.py.

◆ f_d

f_d = model.dynamics(Ts)

Definition at line 23 of file hanging-chain-mpc.py.

◆ y_null

y_null

Definition at line 24 of file hanging-chain-mpc.py.

◆ u_null

u_null

Definition at line 24 of file hanging-chain-mpc.py.

◆ param

param = [0.03, 1.6, 0.033 / N]

Definition at line 26 of file hanging-chain-mpc.py.

◆ N_dist

int N_dist = 3

Definition at line 30 of file hanging-chain-mpc.py.

◆ u_dist

list u_dist = 3 else [-0.5, 0.5]

Definition at line 31 of file hanging-chain-mpc.py.

◆ y_dist

y_dist = model.simulate(N_dist, y_null, u_dist, param)

Definition at line 32 of file hanging-chain-mpc.py.

◆ N_sim

int N_sim = 180

Definition at line 37 of file hanging-chain-mpc.py.

◆ y_sim

y_sim = model.simulate(N_sim, y_dist[:, -1], u_null, param)

Definition at line 38 of file hanging-chain-mpc.py.

◆ N_horiz

int N_horiz = 12

Definition at line 43 of file hanging-chain-mpc.py.

◆ L_cost

L_cost = model.generate_cost_fun()

Definition at line 45 of file hanging-chain-mpc.py.

◆ y_init

y_init = cs.SX.sym("y_init", *y_null.shape)

Definition at line 46 of file hanging-chain-mpc.py.

◆ U

U = cs.SX.sym("U", dim * N_horiz)

Definition at line 47 of file hanging-chain-mpc.py.

◆ constr_param

constr_param = cs.SX.sym("c", 3)

Definition at line 48 of file hanging-chain-mpc.py.

◆ mpc_param

mpc_param = cs.vertcat(y_init, model.params, constr_param)

Definition at line 49 of file hanging-chain-mpc.py.

◆ U_mat

U_mat = model.input_to_matrix(U)

Definition at line 50 of file hanging-chain-mpc.py.

◆ mpc_sim

mpc_sim = model.simulate(N_horiz, y_init, U_mat, model.params)

Definition at line 53 of file hanging-chain-mpc.py.

◆ mpc_cost

int mpc_cost = 0

Definition at line 54 of file hanging-chain-mpc.py.

◆ y_n

y_n = mpc_sim[:, n]

Definition at line 56 of file hanging-chain-mpc.py.

◆ u_n

u_n = U_mat[:, n]

Definition at line 57 of file hanging-chain-mpc.py.

◆ mpc_cost_fun

mpc_cost_fun = cs.Function('f_mpc', [U, mpc_param], [mpc_cost])

Definition at line 59 of file hanging-chain-mpc.py.

◆ g_constr

int g_constr = lambda c, x: c[0] * x**3 + c[1] * x**2 + c[2] * x

Definition at line 62 of file hanging-chain-mpc.py.

◆ constr

list constr = []

Definition at line 63 of file hanging-chain-mpc.py.

◆ yx_n

yx_n = y_n[dim * i]

Definition at line 67 of file hanging-chain-mpc.py.

◆ yy_n

yy_n = y_n[dim * i + dim - 1]

Definition at line 68 of file hanging-chain-mpc.py.

◆ mpc_constr_fun

mpc_constr_fun = cs.Function("g", [U, mpc_param], [cs.vertcat(*constr)])

Definition at line 71 of file hanging-chain-mpc.py.

◆ a

a

Definition at line 74 of file hanging-chain-mpc.py.

◆ b

b

Definition at line 74 of file hanging-chain-mpc.py.

◆ c

c

Definition at line 74 of file hanging-chain-mpc.py.

◆ d

d

Definition at line 74 of file hanging-chain-mpc.py.

◆ constr_coeff

list constr_coeff = [c, -3 * a * c, 3 * a * a * c + d]

Definition at line 75 of file hanging-chain-mpc.py.

◆ constr_lb

int constr_lb = b - c * a**3 - d * a

Definition at line 76 of file hanging-chain-mpc.py.

◆ prob

prob = pa.generate_and_compile_casadi_problem(mpc_cost_fun, mpc_constr_fun)

Definition at line 81 of file hanging-chain-mpc.py.

◆ lowerbound

lowerbound

Definition at line 82 of file hanging-chain-mpc.py.

◆ upperbound

upperbound

Definition at line 83 of file hanging-chain-mpc.py.

◆ solver

solver
Initial value:
1= pa.ALMSolver(
2 alm_params={
3 'ε': 1e-4,
4 'δ': 1e-4,
5 'Σ_0': 1e5,
6 'max_time': timedelta(seconds=0.5),
7 },
8 inner_solver=pa.StructuredPANOCLBFGSSolver(
9 panoc_params={
10 'stop_crit': pa.ProjGradNorm2,
11 'max_time': timedelta(seconds=0.2),
12 'hessian_step_size_heuristic': 15,
13 },
14 lbfgs_params={'memory': N_horiz},
15 ),
16)

Definition at line 89 of file hanging-chain-mpc.py.

◆ n_state

n_state = y_n.shape[0]

Definition at line 141 of file hanging-chain-mpc.py.

◆ y_mpc

y_mpc = np.empty((n_state, N_sim))

Definition at line 144 of file hanging-chain-mpc.py.

◆ controller

controller = MPCController(model, prob)

Definition at line 145 of file hanging-chain-mpc.py.

◆ fig

fig

Definition at line 165 of file hanging-chain-mpc.py.

◆ ax

ax

Definition at line 165 of file hanging-chain-mpc.py.

◆ x

x = np.linspace(-0.25, 1.25, 256)

Definition at line 166 of file hanging-chain-mpc.py.

◆ y

y = np.linspace(-2.5, 1, 256)

Definition at line 166 of file hanging-chain-mpc.py.

◆ z

z

Definition at line 166 of file hanging-chain-mpc.py.

◆ label

label

Definition at line 167 of file hanging-chain-mpc.py.

◆ X

X

Definition at line 175 of file hanging-chain-mpc.py.

◆ Y

Y

Definition at line 175 of file hanging-chain-mpc.py.

◆ Z

Definition at line 176 of file hanging-chain-mpc.py.

◆ fx

list fx = [patheffects.withTickedStroke(spacing=7, linewidth=0.8)]

Definition at line 178 of file hanging-chain-mpc.py.

◆ cgc

cgc = plt.contour(X, Y, Z, [0], colors='tab:green', linewidths=0.8)

Definition at line 179 of file hanging-chain-mpc.py.

◆ collections

collections

Definition at line 180 of file hanging-chain-mpc.py.

◆ path_effects

path_effects

Definition at line 180 of file hanging-chain-mpc.py.

◆ ani

ani
Initial value:
1= mpl.animation.FuncAnimation(fig,
2 Animation(),
3 interval=1000 * Ts,
4 blit=True,
5 repeat=True,
6 frames=1 + N_dist + N_sim)

Definition at line 205 of file hanging-chain-mpc.py.

◆ out

out
Initial value:
1= join(dirname(__file__), '..', '..', '..', '..', 'sphinx', 'source',
2 'sphinxstatic', 'hanging-chain.html')

Definition at line 213 of file hanging-chain-mpc.py.

◆ exist_ok

exist_ok

Definition at line 215 of file hanging-chain-mpc.py.