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

Risposte (2)

Torsten
Torsten il 5 Dic 2018

0 voti

Put the constraints on y and yy in "nonlcon".

12 Commenti

i don't know how.
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 = [];
Thank you . But if i want to add one more constraints for example :
global par2
global par2
y= par2(1)*x1 + par2(2)*x2 + par2(3)*x3+ par2(4)*x1*x2 +par2(5)*x1*x3 +par2(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
this is for example for just one of the two equation. How i can compute with nonlocn these additional constraints?
Torsten
Torsten il 5 Dic 2018
Modificato: Torsten il 5 Dic 2018
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
Marcellino D'Avino il 5 Dic 2018
Modificato: Marcellino D'Avino il 5 Dic 2018
yes i'm intrestesed to make that d assumes differnet values dipending by Y .I 've tried doing a if construct but i have some difficuties in building the function file, as imput of fmincon. Can you help me with the code?
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
But in this way it doesn't seems to work
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 ? ...
It don't give me an error message, but i don't get the solution i expected. That's why i've asked to have some tips to build up in a correct way the imput function in fmnincon.
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
Marcellino D'Avino il 5 Dic 2018
Modificato: Marcellino D'Avino il 5 Dic 2018
you're right maybe i have to give gou more deails. My aim was to study the beaviour of a desirability funcion in order to optimize the composition of a mixture of three components and so to maximize and minimize respectively two responses. So frist i've simulated the same problem with the software MINITAB and than i switched to MATLAB.
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.

Accedi per commentare.

Matt J
Matt J il 5 Dic 2018
Modificato: Matt J il 5 Dic 2018
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 Commento

Matt J
Matt J il 5 Dic 2018
Modificato: Matt J il 5 Dic 2018
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]);

Accedi per commentare.

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Modificato:

il 5 Dic 2018

Community Treasure Hunt

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

Start Hunting!

Translated by