The way to define the cost function in Matlab script and consequentally an automatic change in the number of inputs in the S-function in Simulink

Hello everyone!

I’m doing a thesis regarding an Energy efficient adaptive cruise control that is controlled by an MPC, utilizing acados in Matlab.
I have an issue regarding the way to define the cost function in Matlab script and the consequences that I obtain in the number of input ports in the S-function of Simulink that represents MPC controller.
I explain the problem.

Initially I defined the states, control variable and parameters of MPC for a simple Adaptive cruise control without taking into account the terms of energy efficiency.
So, I defined states, control variable and parameters for MPC in this way:

nx = 4; % number of states: [x_h, v_h, x_rel, F_em]
nu = 1; % number of control input: F_req
np = 4; % number of parameters: [x_prec, v_prec, v_h_ref, teta]

Then, I rename the states, control variable and parameters with symbolic names as “casadi” want, and then I create a vectors for states and parameters:

sym_x = vertcat(x_h, v_h, x_rel, F_em); % States vector
sym_xdot = SX.sym('xdot',nx,1); %derivative of states
sym_u = F_req; % Control action vector
sym_p = vertcat(x_p, v_p, v_h_ref, teta); %parameters

After that, I define the cost function in this way:

%expr_ext_cost = 0.5*sym_x'*W_x*sym_x + 0.5*sym_u'*W_u*sym_u; %along the prediction horizon
%expr_ext_cost_e = 0.5*sym_x'*W_x*sym_x; %last step of the prediction horizon
And the cost function weights are: 
%weight_path = [W_v_h;0;0;0;W_F_em;0;0;0;W_F_req]; %along the prediction horizon
%weight_term = [W_v_h;0;0;W_F_em;0]; %last step of prediction horizon

If the problem is defined in this way, I obtain the correct number of inputs in Simulink for the S-function (17 input ports that I’ve defined in another part of the script).

The problem appears when I add the energy efficient terms to the cost function. Infact the way to write the cost function varies, considering each single term:

expr_ext_cost = 0.5*(x_h' * W_x(1,1)* x_h) + 0.5*(v_h_1' * W_x(2,2) * v_h_1) + 0.5*(x_rel' * W_x(3,3) * x_rel) + 0.5*(F_em' * W_x(4,4) * F_em) + 0.5*(P_EM_norm_eff' * W_eff(1,1) * P_EM_norm_eff) + 0.5*(eff_contribution' * W_eff(2,2) * eff_contribution) + 0.5*(P_loss_norm' * W_eff(3,3) * P_loss_norm) + 0.5*(E_unit_distance_norm' * W_eff(4,4) * E_unit_distance_norm) + 0.5*(F_req' * W_u * F_req); %along the prediction horizon

expr_ext_cost_e = 0.5*(x_h' * W_x(1,1)* x_h) + 0.5*(v_h_1' * W_x(2,2) * v_h_1) + 0.5*(x_rel' * W_x(3,3) * x_rel) + 0.5*(F_em' * W_x(4,4) * F_em) + 0.5*(P_EM_norm_eff' * W_eff(1,1) * P_EM_norm_eff) + 0.5*(eff_contribution' * W_eff(2,2) * eff_contribution) + 0.5*(P_loss_norm' * W_eff(3,3) * P_loss_norm) + 0.5*(E_unit_distance_norm' * W_eff(4,4) * E_unit_distance_norm); %last step of the prediction horizon

Writing the cost function in this way, I don’t know why the number of inputs port of S-function is modified (from 17 to 11 ports). The S-function loses 3 inputs that represent the reference ports (initial step, intermediate steps, final step) for the states and control variable. Furthermore the S-function loses 3 inputs regarding the weitghts of the terms of the cost function.

I don’t know if acados doesn’t accept this way to define the cost function in which is considered each single term multiplied for its weight.

If you have some ideas or you know the solution for this problem, you would give me a great help for the development of the work.

Thank you in advance!

Hi :wave:

could you check the .json file which is generated during solver creation for the value of the field cost_type? I suppose that in the first case the cost is detected to be linear least squares, which is not the case for the second solver.

If you do not have a linear least squares cost, you need to explicitly define the weighting matrices and references, etc. as parameters p. The corresponding S-function should then have a port for setting p.

Best, Katrin