Unable to extract field 'build_problem' from 'mxArray'.
35 views (last 30 days)
Show older comments
coder.extrinsic("DeePC");
persistent deepc
if isempty(deepc)
deepc= DeePC(col_data,T_ini,horizon);
deepc = build_problem(deepc,...
LAMBDA_G_REGULARIZER, LAMBDA_Y_REGULARIZER, LAMBDA_U_REGULARIZER);
end
function result = build_problem(obj, ...
LAMBDA_G_REGULARIZER, LAMBDA_Y_REGULARIZER, LAMBDA_U_REGULARIZER)
result = obj.build_problem(LAMBDA_G_REGULARIZER, LAMBDA_Y_REGULARIZER, LAMBDA_U_REGULARIZER);
end
function obj = build_problem(obj,...
lambda_g,lambda_y,lambda_u,lambda_proj)
if nargin < 2, lambda_g = 0 ; end
if nargin < 3, lambda_y = 0 ; end
if nargin < 4, lambda_u = 0 ; end
if nargin < 5, lambda_proj = 0 ;end
% Assertions
if exist('build_loss', 'file') == 2
% nothing to do
else
disp('Loss function callback cannot be none');
end
assert(lambda_g >= 0 && lambda_y >= 0,'Regularizers must be non-negative');
assert(lambda_u >= 0,'Regularizer of u_ini must be non-negative');
assert(lambda_proj >= 0,'The projection regularizer must be non-negative');
obj.optimization_problem = []; % optimization problem is not build yet
%Build variables
%'full' indicates that the type of the variable is a full asymmetric matrix
u_ini = sdpvar(obj.M*obj.T_ini,1,'full'); % u_ini is the initial input
y_ini = sdpvar(obj.P*obj.T_ini,1,'full'); % y_ini is the initial output
u = sdpvar(obj.M*obj.horizon,1,'full');
y = sdpvar(obj.P*obj.horizon,1,'full');
g = sdpvar(obj.T-obj.T_ini-obj.horizon+1,1,'full');
slack_u = sdpvar(obj.T_ini*obj.P,1,'full');
slack_y = sdpvar(obj.T_ini*obj.M,1,'full');
U_p = obj.Up;
U_f = obj.Uf;
Y_p = obj.Yp;
Y_f = obj.Yf;
if lambda_proj > DeePC.SMALL_NUMBER
% Compute projection matrix (for the least square solution)
Zp = [U_p;Y_p;U_f];
ZpInv = pinv(Zp);
I = eye(obj.T-obj.T_ini-obj.horizon+1);
I_min_P = I-(ZpInv*Zp);
end
A = [U_p;Y_p;U_f;Y_f];
b = [u_ini+slack_u;y_ini+slack_y;u;y];
% Build constraints
constraints = [];
constraints = [constraints,A*g == b];
if isclose(lambda_y,0)
constraints = [constraints, norm(slack_y,2) <= DeePC.SMALL_NUMBER];
end
if isclose(lambda_u,0)
constraints = [constraints, norm(slack_u,2) <= DeePC.SMALL_NUMBER];
end
%Reshape u and y
%in order to calculate loss in all step k(1 to horizon)
u = reshape(u,[obj.horizon, obj.M]);
y = reshape(y,[obj.horizon, obj.P]);
if exist('build_constraints','file')==2
Constraints = build_constraints(u,y);
else
Constraints = [];
end
constraints = [constraints,Constraints];
% Build Loss
loss = build_loss(u,y);
if lambda_g > DeePC.SMALL_NUMBER
regulariers = lambda_g * norm(g,1); % add L1 norm of g
else
regulariers = 0;
end
if lambda_y > DeePC.SMALL_NUMBER
regulariers = regulariers + lambda_y*norm(slack_y,1);
end
if lambda_proj > DeePC.SMALL_NUMBER
% Add projection regularizer
regulariers = regulariers + lambda_proj*norm(I_min_P*g);
end
if lambda_u > DeePC.SMALL_NUMBER
% Add L1 norm of slack_u
regulariers = regulariers + lambda_u*norm(slack_u,1);
end
% Define the total loss
problem_loss = loss + regulariers;
% Define the optimization problem
obj.optimization_problem = OptimizationProblem(u_ini,y_ini,u,y,g,...
slack_u,slack_y,constraints,problem_loss);
end
0 Comments
Answers (1)
Walter Roberson
on 10 Oct 2024 at 18:41
Moved: Walter Roberson
on 10 Oct 2024 at 18:41
You are declaring the function build_problem twice in the same scope.
You cannot declare class-dependent methods inside a MATLAB Function Block.
deepc= DeePC(col_data,T_ini,horizon);
deepc = build_problem(deepc,...
LAMBDA_G_REGULARIZER, LAMBDA_Y_REGULARIZER, LAMBDA_U_REGULARIZER);
You appear to be changing the type of deepc, which is not something that can be done in a MATLAB Function Block.
4 Comments
Walter Roberson
on 11 Oct 2024 at 13:38
You still have the problem that you are declaring the function build_problem twice.
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!