[Matlab] Create multiple OCP models in one script

I was trying to compare performance of several MHEs (with different time horizon, for example), but I’m not sure how to correctly code it - it looks like acados generates code with predefined filenames, so during compilation of the second model matlab crashes:

%% acados ocp
ocp1 = acados_ocp(ocp_model, ocp_opts);
% modify second problem with shorter horizon
ocp_model.set('T',t_step*ocp_Nsmall);
ocp_opts.set('param_scheme_N', ocp_Nsmall);
ocp2 = acados_ocp(ocp_model, ocp_opts);

Is there a way around it?

Hi Anton,

Indeed it is not really supported to have multiple acados ocp solvers in Matlab.
However, to compare different OCP/MHE solvers, you can always delete the old one with clear.

Here is a sketch of for a performance comparison script:

for ii = 1:length(n_formulations)
    clear ocp
    % formulate your problem in ocp_model, ocp_opts.
    ocp = acados_ocp(ocp_model, ocp_opts);
    % call the solver, store data to be compared
end
%% evaluation

Cheers,
Jonathan

Hi, apologies for bumping an old topic, but I also had a similar question regarding the use of multiple ocp solvers.

In my case, I’m working on a multi-agent problem and would like to execute multiple instances of my controller at the same time. Using the MATLAB interface, I ran into the same issue as Anton where the compilation fails because of read/write permission conflicts. I wanted to ask if this is a limitation with the MATLAB interface only, or would I run into the same problem using e.g. the Python interface?

Besides switching languages, one workaround I thought of is to re-use the same ocp solver for each agent (since the controller itself is identical). For 1 timestep, we solve the ocp for each agent and save the solution data. Then on the next timestep, we use the kth agent’s data to warm-start the solver for the kth agent (see sketch below).

% form the ocp solver beforehand
ocp = acados_ocp(ocp_model, ocp_opts);
%
for i=1:(sim_time/dt)
    for k=1:num_agents
        % set references, warm-start initializations, etc. for kth agent
        ocp.set('cost_y_ref', agents{k}.cost,  ... );
        ...
        ocp.set('init_x', agents{k}.init_x);
        ocp.set('init_u', agents{k}.init_u);

        % solve ocp
        ocp.solve();

        % save the warm-start data that we need for next timestep
        agents{k}.init_x = ocp.get('x');
        agents{k}.init_u = ocp.get('u');
        ...
    end
end

Do you think this approach is advisable? The main part that I’m unsure of is which solution fields to save at each timestep - e.g. we definitely need to get/set init_x and init_u, but what about init_pi, the sensitivities, etc. …

Thanks in advance for your thoughts!