how to define if statement for fmincon function / optimization problem

8 次查看(过去 30 天)
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!

采纳的回答

Matt J
Matt J 2017-11-17
编辑:Matt J 2017-11-17
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;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surrogate Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by