how to define if statement for fmincon function / optimization problem

8 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

Matt J
Matt J el 17 de Nov. de 2017
Editada: Matt J el 17 de Nov. de 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 comentario
Sara
Sara el 20 de Nov. de 2017
Thank you very much for your help Matt! The problem is now solved by using your method.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Surrogate Optimization en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by