Skip to content

Commit

Permalink
[Py] add initial parameter to inverted pendulum example
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed Feb 21, 2024
1 parent 6a8f831 commit a9547af
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions examples/Python/mpc/inverted-pendulum-mpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@
# Box constraints on the actuator force:
C = -F_max * np.ones(N_horiz), +F_max * np.ones(N_horiz)

# Initial state of the system
state_0 = np.array([-np.pi, 0, 0.2, 0])

# Initial parameter value
Q = [10, 1e-2, 1, 1e-2]
Qf = np.array([10, 1e-1, 1000, 1e-1])
R = [1e-1]
param_0 = np.concatenate((state_0, Q, Qf, R))

# Compile into an alpaqa problem
import alpaqa

Expand All @@ -77,7 +86,7 @@
problem = (
alpaqa.minimize(mpc_cost, cs.vec(mpc_u)) # objective and variables
.subject_to_box(C) # box constraints on the variables
.with_param(mpc_param) # parameters to be changed later
.with_param(mpc_param, param_0) # parameters to be changed later
).compile(sym=cs.MX.sym)

from datetime import timedelta
Expand Down Expand Up @@ -145,14 +154,6 @@ def __call__(self, state_n):

# %% Simulate the system using the MPC controller

state_0 = np.array([-np.pi, 0, 0.2, 0]) # Initial state of the system

# Parameters
Q = [10, 1e-2, 1, 1e-2]
Qf = np.array([10, 1e-1, 1000, 1e-1])
R = [1e-1]
problem.param = np.concatenate((state_0, Q, Qf, R))

# Simulation
mpc_states = np.empty((nx, N_sim + 1))
mpc_inputs = np.empty((nu, N_sim))
Expand Down

0 comments on commit a9547af

Please sign in to comment.