Hi
I’m currently trying to implement a constrained version of SINDy with Acados on Matlab.
Till now i’m feeding the matrix regarding the library function used in SINDy and the derivative vector of the state directly in the function that i want to minimize.
Now i want that these two are set as parameters that then i will feed with the appropriate matrix and vector.
Is there any method to do this?
The code now is this one:
clear all
clc
import casadi.*; % Import CasADi library
% Step 1: Generate simulated data
dt = 0.01; % Time step
t = 0:dt:1; % Time vector
A_true = randi([-9,-1],8,8); % True system matrix
x0 = ones(8,1); % Initial condition
x = zeros(length(x0), length(t));
x(:, 1) = x0;
for k = 1:length(t) - 1
x(:, k + 1) = x(:, k) + dt * A_true * x(:, k); % Simple Euler integration
end
x_dot = diff(x,1,2) / dt; % Numerical derivative
% Rescale data
Theta = [x(1, :)', x(2, :)', x(3, :)', x(4, :)', x(5, :)', x(6, :)', x(7, :)', x(8, :)']; % Linear terms only
Theta=Theta(1:end-1,:);
% Step 2: Define problem variables
eps=casadi.SX.sym('eps',8);
f = abs((Theta * eps - x_dot(1, :)')' * (Theta * eps - x_dot(1, :)'));
% Constraint bounds
g = [eps(1);eps(2);eps(3);eps(4);eps(5);eps(6);eps(7);eps(8)];
glb = -10*ones(8,1);
gub = 10*ones(8,1);
% Step 3: Model setup
model = AcadosModel();
model.name = 'prova_sindy';
model.x = eps;
model.f_expl_expr = casadi.SX.zeros(length(model.x),1);
% Cost and constraints
ocp = AcadosOcp();
ocp.name = 'nlp_solver';
ocp.model = model;
ocp.cost.cost_type_e = 'EXTERNAL';
ocp.model.cost_expr_ext_cost_e = f;
ocp.model.con_h_expr_e = g;
ocp.constraints.lh_e = glb;
ocp.constraints.uh_e = gub;
ocp.cost.cost_type_0 = 'EXTERNAL';
ocp.model.cost_expr_ext_cost_0 = 0;
ocp.cost.cost_type = 'EXTERNAL';
ocp.model.cost_expr_ext_cost = 0;
% Solver options
ocp.solver_options.tf = 1;
ocp.solver_options.N_horizon = 1;
ocp.solver_options.nlp_solver_type = 'SQP_RTI';
ocp.solver_options.nlp_solver_max_iter = 1000;
ocp.solver_options.nlp_solver_tol_stat = 1e-6;
ocp.solver_options.nlp_solver_tol_eq = 1e-6;
ocp.solver_options.nlp_solver_tol_comp = 1e-6;
% Solver creation
ocp_solver = AcadosOcpSolver(ocp);
% Initial guess
init_x = ones(8,1);
ocp_solver.set('init_x', repmat(init_x,1,2));
% Solve
tic
ocp_solver.solve();
total_time = toc;
% Check status
status = ocp_solver.get('status');
if status ~= 0
warning(['Solver failed with status ', num2str(status)]);
end
% Results
x_opt = ocp_solver.get('x',1);
disp('Optimal solution:')
disp(x_opt)
disp(['Total time: ', num2str(1e3 * total_time), ' ms'])
I have seen the example in the nlp_generic
but i do not have a simple value as parameters but instead a matrix and a vector.
Waiting for your reply