MINIMIZE A FUNCTION WITH CONSTRAINTS
显示 更早的评论
HI,
i have a function that has to be minimize with some costraints using fmincon. Is it possible to use conditional istruction in the file function,(if else elseif..etc etc) in order to evaluate the minimum if a statement is true ? This is the function file, called 'overall' used as fminicon input.
function [ DD ] = overall( x )
global par par2
x1=x(1);
x2=x(2);
x3=x(3);
y= par(1)*x1 + par(2)*x2 + par(3)*x3+ par(4)*x1*x2 +par(5)*x1*x3 +par(6)*x2*x3;
d=((y-25)/(31-25));
yy= par2(1)*x1 + par2(2)*x2 + par2(3)*x3+ par2(4)*x1*x2 +par2(5)*x1*x3 +par2(6)*x2*x3;
dd=((30-yy)/(30-24));
DD=(((d)^2)*dd)^0.5;
DD=-DD;
end
par and par 2 are the vector of the coefficients of the 2 functions. So i need that y and yy belongs to a specifc range in order to evaluate d and dd.
25<y<31
24<yy<30.
How can I do that?
Thank you
回答(2 个)
Torsten
2018-12-5
0 个投票
Put the constraints on y and yy in "nonlcon".
12 个评论
Marcellino D'Avino
2018-12-5
Torsten
2018-12-5
function [c,ceq] = nonlcon(x)
global par par2
x1=x(1);
x2=x(2);
x3=x(3);
y= par(1)*x1 + par(2)*x2 + par(3)*x3+ par(4)*x1*x2 +par(5)*x1*x3 +par(6)*x2*x3;
yy= par2(1)*x1 + par2(2)*x2 + par2(3)*x3+ par2(4)*x1*x2 +par2(5)*x1*x3 +par2(6)*x2*x3;
c(1) = y-31;
c(2) = -y+25;
c(3) = yy-30;
c(4) = -yy+24;
ceq = [];
Marcellino D'Avino
2018-12-5
I don't understand.
You wrote that y has to be constrained to lie in between 24 and 30. This has been accompished by the setting in "nonlcon". Now you can compute "d" in "overall" to be d=(30-y)/(30-24) (because "nonlcon" ensures that 24<=y<=30).
If it is not necessary that y lies in between 24 and 30 and you only have different values for d depending on the value of y, don't use "nonlcon" and only use the if-construct from above in "overall".
Marcellino D'Avino
2018-12-5
编辑:Marcellino D'Avino
2018-12-5
Torsten
2018-12-5
But you already did it. Just insert the if-construct from above in "overall".
function [ DD ] = overall( x )
global par par2
x1=x(1);
x2=x(2);
x3=x(3);
y= par(1)*x1 + par(2)*x2 + par(3)*x3+ par(4)*x1*x2 +par(5)*x1*x3 +par(6)*x2*x3;
if y<=24
d=1;
end
if 24<y & y<=30;
d=((30-y)/(30-24));
end
if y>30
d=0;
end
yy= par2(1)*x1 + par2(2)*x2 + par2(3)*x3+ par2(4)*x1*x2 +par2(5)*x1*x3 +par2(6)*x2*x3;
if yy<24
dd = 1;
end
if yy>=24 & yy<=30
dd=((30-yy)/(30-24));
end
if yy>30
dd=0;
end
DD=(d^2*dd)^0.5;
DD=-DD;
end
Marcellino D'Avino
2018-12-5
Torsten
2018-12-5
What do you mean by "It doesn't seem to work" ?
Do you get an error message ? Don't you get the result you expect ? Does the optimizer give up iterating ? ...
Marcellino D'Avino
2018-12-5
Torsten
2018-12-5
Your input file is correct, you don't get an error message. So without knowing about the background of your problem, how should anybody be able to help you ?
Marcellino D'Avino
2018-12-5
编辑:Marcellino D'Avino
2018-12-5
Matt J
2018-12-5
The if construct,
if y<=24
d=1;
end
if 24<y & y<=30;
d=((30-y)/(30-24));
end
if y>30
d=0;
end
is problematic for the optimization for several reasons. Firstly, it is non-differentiable. A differentiable approximation to this operation would be something like
d=erf(27-z);
It is also a problem, however, because the objective function is flat in the regions y<=24 and y>=30. Since there is no gradient there, the algorithm cannot find a search direction if it lands there.
You might consider this modification,
function [ DD ] = overall( x )
global par par2
x1=x(1);
x2=x(2);
x3=x(3);
y= par(1)*x1 + par(2)*x2 + par(3)*x3+ par(4)*x1*x2 +par(5)*x1*x3 +par(6)*x2*x3;
yy= par2(1)*x1 + par2(2)*x2 + par2(3)*x3+ par2(4)*x1*x2 +par2(5)*x1*x3 +par2(6)*x2*x3;
s1=mean([25,31]);
s2=mean([30,24]);
DD=+softplus(y-s1)+0.5*softplus(yy-s2);
end
function y=softplus(x)
%Accurate implementation of log(1+exp(x))
idx=x<=33;
y=x;
y(idx)=log1p( exp(x(idx)) );
1 个评论
Note that
>> fplot(@(y) exp(-softplus(y-s1)),[20,40]);

is a smoothened approximation of the thresholding you were trying to do with if...else. If you take the -log of this, however, you get something nice and convex. This motivates the choice of cost function above.
fplot(@(y) softplus(y-s1),[20,40]);

类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!