Effacer les filtres
Effacer les filtres

Matlab optimization - variables to satisfy L1 norm

47 vues (au cours des 30 derniers jours)
Simon Philipp Hehenberger
Simon Philipp Hehenberger le 10 Juil 2024 à 8:29
Commenté : Manikanta Aditya le 10 Juil 2024 à 9:58
Hi,
I am trying to set up an optimization problem, the variables of my problem are stored in vector x, which is of length 4.
How do I constrain my optimization such that the first three elements in my vector x satisfy the L1 norm.
In my current setup i am using fmincon with the following code:
x0=[A_init'; th_init];
opt=optimoptions('fmincon','MaxFunctionEvaluations',1e5,'Display','iter','MaxIterations',1e3,...
'Algorithm','sqp','FiniteDifferenceStepSize',1e-6,'DiffMinChange',0.00051,'DiffMaxChange',0.2);
[x,fval,exitflag,output]=fmincon(@(x)costFunction_es_sa(x,a,N,epRm,epR_target),x0,[],[],[],[],[0.1 0.1 0.1 0.01]',[1 1 1 0.61]',[],opt)

Réponses (2)

Manikanta Aditya
Manikanta Aditya le 10 Juil 2024 à 8:39
To constrain the first three elements of your vector ( x ) to satisfy the ( L1 ) norm in an optimization problem using fmincon in MATLAB, you can add a nonlinear constraint function.
% Initial guess
x0 = [A_init'; th_init];
% Optimization options
opt = optimoptions('fmincon', 'MaxFunctionEvaluations', 1e5, 'Display', 'iter', ...
'MaxIterations', 1e3, 'Algorithm', 'sqp', 'FiniteDifferenceStepSize', 1e-6, ...
'DiffMinChange', 0.00051, 'DiffMaxChange', 0.2);
% Nonlinear constraint function
nonlcon = @(x) l1NormConstraint(x);
% Call fmincon with the nonlinear constraint
[x, fval, exitflag, output] = fmincon(@(x) costFunction_es_sa(x, a, N, epRm, epR_target), ...
x0, [], [], [], [], [0.1 0.1 0.1 0.01]', [1 1 1 0.61]', nonlcon, opt);
% Nonlinear constraint function definition
function [c, ceq] = l1NormConstraint(x)
% L1 norm constraint for the first three elements of x
c = sum(abs(x(1:3))) - c_bound; % c_bound is your desired bound for the L1 norm
ceq = []; % No equality constraints
end
  5 commentaires
Simon Philipp Hehenberger
Simon Philipp Hehenberger le 10 Juil 2024 à 9:43
Initial condition not satisfying the nonlinear constraint was indeed the issue.
Thanks alot for your help @Aquatris and @Manikanta Aditya
Manikanta Aditya
Manikanta Aditya le 10 Juil 2024 à 9:58
Thanks for informing @Simon Philipp Hehenberger

Connectez-vous pour commenter.


Aquatris
Aquatris le 10 Juil 2024 à 8:45
Modifié(e) : Aquatris le 10 Juil 2024 à 8:45
You create a nonlinear constraint functio and give it as an argument to the fmincon.
[x,fval,exitflag,output]=fmincon(@(x)costFunction_es_sa(x,a,N,epRm,epR_target),x0,...
[],[],[],[],[0.1 0.1 0.1 0.01]',[1 1 1 0.61]',@normL1,opt)
function y = normL1(x); %fmincon will try to satisfy myFun(x)<=0
y = sum(abs(x(1:3))) - 5; % want it to be less than 5 for instance
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by