How to implement online Adaptive linear MPC

Hi :wave:
Thank you for providing the powerful ACADOS!

Using acados python to implement the MPC algorithm for online linear model adaptive update. For discrete time linear models x_{t+1}=A x_t+B u_t, A and B will be continuously updated during operation, but when solving the iterative prediction within the prediction time domain N_p at each time step, it can be regarded as if A B within the prediction time domain is unchanged. How to modify A and B in MPC online?

I have test the code as follows:

model.x = MX.sym('x', model.nx)  
model.u = MX.sym('u', model.nu)  
model.y = MX.sym('y', model.ny)  
model.A = MX.sym('A', model.nx, model.nx)  
model.B = MX.sym('B', model.nx, model.nu) 
model.name = 'linear_discrete_system_varying_AB'

model.p = vertcat(model.A.reshape((-1, 1)), model.B.reshape((-1, 1)))

model.disc_dyn_expr = model.A @ model.x + model.B @ model.u

And I get an error message,

File ~/demo/Adaptive-koopman/control_files/acados_mpc.py:165, in MPCControllerAcados.construct_controller(self)
    163 # Compile acados OCP solver if necessary
    164 json_file = f'acados_ocp_{self.model.name}.json'
--> 165 self.acados_solver = AcadosOcpSolver(ocp, json_file=json_file)
    166 print("ACADOS MPC controller is constructed.")

File ~/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py:234, in AcadosOcpSolver.__init__(self, acados_ocp, json_file, simulink_opts, build, generate, cmake_builder, verbose, save_p_global)
    232     if json_file is not None:
    233         acados_ocp.json_file = json_file
--> 234     self.generate(acados_ocp, json_file=acados_ocp.json_file, simulink_opts=simulink_opts, cmake_builder=cmake_builder, verbose=verbose)
    235     json_file = acados_ocp.json_file
    236 else:

File ~/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py:120, in AcadosOcpSolver.generate(cls, acados_ocp, json_file, simulink_opts, cmake_builder, verbose)
    117         acados_ocp.simulink_opts = simulink_opts
    119 # make consistent
--> 120 acados_ocp.make_consistent(verbose=verbose)
...
   1020 if self.p_global_values.shape[0] != dims.np_global:

ValueError: inconsistent dimension np, regarding model.p and parameter_values.
Got np = 340, self.parameter_values.shape = 0

Thanks for any suggestion!

I have solved this problem by adding the following codes:

def construct_controller(self):
        nx = self.model.nx
        nu = self.model.nu
        ny = self.model.ny
        n_param = self.model.p.size()[0] if isinstance(self.model.p, cs.MX) else 0


        # Create OCP object to formulate the optimization
        ocp = AcadosOcp()
        ocp.model = self.model
        ocp.dims.N = self.N
        ocp.solver_options.tf = self.N * self.dt  
        # Initialize parameters
        ocp.dims.np = n_param
        ocp.parameter_values = np.zeros(n_param)

When using the parameter p, you need to set dims.np and initialize the value of p.