Пример #1
0
import tunempc
import tunempc.pmpc as pmpc
import tunempc.preprocessing as preprocessing
import tunempc.closed_loop_tools as clt
import numpy as np
import casadi as ca
import casadi.tools as ct
import pickle

# load user input
with open('user_input.pkl', 'rb') as f:
    user_input = pickle.load(f)

# set-up tuning problem
tuner = tunempc.Tuner(f=user_input['f'],
                      l=user_input['l'],
                      h=user_input['h'],
                      p=user_input['p'])

# solve OCP
wsol = tuner.solve_ocp(w0=user_input['w0'])

# convexify stage cost matrices
Hc = tuner.convexify(solver='mosek')

# extract OCP sensitivities at optimal solution
[Hlag, q, A, B, C_As] = tuner.pocp.get_sensitivities()

# set-up open-loop scenario
Nmpc = 20
alpha_steps = 20
Пример #2
0
    return ca.Function('cost', [x, u], [obj])


# discretization
T = 5  # [s]
N = 30

# set-up system
x, u = vars()
nx = x.shape[0]
nu = u.shape[0]
data = problemData()

tuner = tunempc.Tuner(f=dynamics(x, u, data, h=T / N)[0],
                      l=objective(x, u, data),
                      p=N)

# initialization
t = np.linspace(0, T, N + 1)
ez0 = list(map(np.cos, 2 * np.pi * t / T))
ey0 = list(map(np.sin, 2 * np.pi * t / T))
inv = list(map(lambda x, y: x**2 + y**2 - 1.0, ez0, ey0))
z0 = list(
    map(lambda x: data['v'] * T / (2 * np.pi) * np.sin(x), 2 * np.pi * t / T))
y0 = list(
    map(lambda x: -data['v'] * T / (2 * np.pi) * np.cos(x), 2 * np.pi * t / T))

# create initial guess
w0 = tuner.pocp.w(0.0)
for i in range(N):
Пример #3
0
        400.0 - u['P100'],
        400.0 - u['F200'],
    )

    return ca.Function('h', [x,u], [constr])


# set-up system
x, u = vars()
data = intermediate_vars(x,u, problem_data())
nx = x.shape[0]
nu = u.shape[0]

tuner = tunempc.Tuner(
    f = dynamics(x, u, data)[0],
    l = objective(x,u,data),
    h = constraints(x,u, data),
    p = 1
)

# solve
w0 = ca.vertcat(*[25.0, 49.743, 191.713, 215.888])
wsol = tuner.solve_ocp(w0)
Hc = tuner.convexify(rho = 1e-3, force = False, solver='mosek')

# nmpc horizon length
N = 20

# gradient
S  = tuner.S

# economic mpc controller
Пример #4
0
# simulation parameters
Ts = 20  # sampling time
Nsim = 70  # closed loop num of simulation steps
N = 60  # nmpc horizon length

# integrator options
data['rhoJ'] = 1e-1  # regularization
data['num_steps'] = 20  # number_of_finite_elements in integrator
data['ts'] = Ts  # sampling time
data['integrator'] = 'rk'  # numerical integrator

# cost function
cost, cost_comp = cstr.objective(x, u, data)

# solve steady state problem
tuner = tunempc.Tuner(f=cstr.dynamics(x, u, data)[0], l=cost, h=h, p=1)
wsol = tuner.solve_ocp(w0=cstr.initial_guess())
lOpt = cost(wsol['x', 0], wsol['u', 0])
Hc = tuner.convexify()
S = tuner.S

# prediction time grid
tgrid = [Ts * k for k in range(N)]
tgridx = [Ts * k for k in range(N + 1)]

# create controllers
ctrls = {}

# economic mpc controller
opts = {'ipopt_presolve': False, 'slack_flag': 'active'}
ctrls['economic'] = tuner.create_mpc('economic', N, opts=opts)