# How to set weights of costs according to the time interval in MPC prediction horizon

Hi,

I am a newbie on MPC and ACADOS seeking for help setting cost weight related to the time interval in MPC prediction horizon. I am also not a native English speaker. So I want to apologize in advance for my low-level expression and questions that might be too naive.

I am using the python interface of ACADOS. I have installed ACADOS in Ubuntu20.04 successfully.

Specifically, I am re-producing the MPC controller from High-MPC using ACADOS because it is said that ACADOS could run much faster than the C compiled version of Casadi. The paper of High-MPC can be viewed in https://rpg.ifi.uzh.ch/docs/TRO21_Yunlong.pdf

The released code is constructed in Casadi. I can totally understand the code. In this MPC, the cost function is constructed as like (1/2) * exp[-(t_{cur} - t_{ref})^2] * (x_{cur} - x_{ref})^2 where t_{cur} and x_{cur} represent the current time interval and the current state of the system, the t_{ref} and the x_{ref} represent the referenced time interval and the referenced state. By setting the weight exp[-(t_{cur} - t_{ref})^2], the MPC cost function would focus on the time interval near the referenced time. The MPC’s purpose is to control a quadrotor flying to the referenced place on the referenced time.

I have seen plenty of questions in this forum discussing how to change cost and its weight W over time, but I can’t see if they are suitable for my situation, because what I want is to adjust the cost value of MPC in its prediction time horizon. Maybe due to my weak English skills, the operation of setting W seems to be changing the weight after the MPC run a whole step with N time intervals where N is the MPC’s prediction time horizon, not after a single time step like N-1 to N.

So is it possible to do what I want in ACADOS using python interface? And if possible, how could I achieve my goal? I could only offer my appreciate.

I hope I got you right.

What you want to do is adjust the Weight W of your cost function for each shooting interval n = 0, 1, \ldots, N, right?

You can do this by setting a different value for W for each interval n with the ocp_solver.set(n, ‘p’, w_n) function. Since your cost function is not covered by the basic linear or nonlinear cost module from acados, you have to define your own cost function and assign the weight w_n as parameter p_n => This parameter can then be updated in every control cycle for the whole prediction trajectory.

1 Like

Thank you for your help! I believe you actually got my point. So what I have to do is to set the ocp.cost.cost_type and ocp.cost.cost_type to ‘EXTERNAL’ and then run \text{ocp_solver.set}(n, p', \omega_{n}) for every shooting interval from 0 to N since N is the number of shooting intervals, am I getting it right?

By the way, is the parameter p directly mutiplied on the cost function? I didn’t find much explanation of p in documents of ACADOS, but according to your advice it seems like what I inferred.

1 Like

You’re Welcome!

You’re right about the first part of your text.

However parameter are defined as loose symbols in acados. You can define everything as a parameter (e.g. your reference y_{ref}) with casadi/acados => those symbolic parameters can then used in your system dynamics, cost function, constraints, etc.

One example:
You want to change the weights of your external cost function in every control cycle. So you do something like this:

w = cs.SX.sym(‘w’)
ocp.model.p = w
ocp.model.parameter_values = 1

cost_func = w * (y - y_ref) * (y - y_ref)
ocp.model.cost_expr_ext_cost = cost_func

#run mpc

#change weight
for n in range(0,N+1)
ocp_solver.set(n, ‘p’, w_{new})

I hope you get what I mean. Maybe @FreyJo can point you towards a good example !
Best Luck with your implementation!
Thorben

1 Like

I can get what the example means

1 Like