how to define if statement for fmincon function / optimization problem

8 vues (au cours des 30 derniers jours)
Sara
Sara le 17 Nov 2017
Commenté : Sara le 20 Nov 2017
Hello, Can anyone help me with below problem please?
I am using fmincon to do the optimization by Matlab. I have a 'price' array p (1x24) that has some zero elements. I want to define a condition that if an element of the price array p is zero, then the same element of solution array x be zero too. I define it in the objective function file as:
function f = objfun(x,p)
x1=x(1:24);
for i=1:24
if p(i)==0
x(i)=0;
end
end
f=x1*p';
end
However, when I run my main file, this condition is not observed in the answer. In other words, p(2) is zero, but x(2) has a non-zero value. I also defined this condition in the main file, but the answers are the same (the condition is not observed)
How can I define this condition for fmincon?
Thanks a lot!

Réponse acceptée

Matt J
Matt J le 17 Nov 2017
Modifié(e) : Matt J le 17 Nov 2017
If you know certain x(i) are zero in advance, then they are not unknowns, and you should remove them from the problem. So, it should look like
idx=(p~=0);
z0=x0(idx); %discard irrelevant initial guess parameters
z=fmincon(@(z) z*p(idx).' , z0, A, b, Aeq,beq,...);
x=zeros(size(p));
x(idx)=z;
  1 commentaire
Sara
Sara le 20 Nov 2017
Thank you very much for your help Matt! The problem is now solved by using your method.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Surrogate Optimization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by