nonlcon in fmincon not satisfying my constraints :( getting crazy.

I'm currently trying to satisfy non linear constraints through adding a nonlcon in my fmincon function, however when optimizing, it is not satisfying my constraints :( at all.
% vector of initial weights
x0=zeros(8,1)
this is my nonlcon function. The idea behind this constraint is that at any point, the weights in asset 3 can only be 10% of the weight in asset 2.
----------weights x0----------
function [c, ceq] = mycon(x0)
ceq=[];
c=[];
% (x0(3)) - (0.1*x0(2)) < 0 ;
% c(1) = (x0(3)) - (0.1*x0(2)) < 0 ;
% c < 0;
c(1) = x0(3)<(0.1*x0(2)) ;
This is my fmincon:
[weights] = fmincon(@(x0) (a typical function),x0,[],[],Aeq,beq,zeros(size(Data,2),1),C,'mycon',optimopt);
Where i've tried
'mycon'
and
@(x0)mycon(x0)
for the nonlcon place in fmincon. I'm running this through a for loop where i've created a return constraint in my Aeq. What i'm looking for while optimizing through fmincon is that it satisfies the constraints in my nonlcon at every 'required return' step. However, at any(!) point, it does not satisfy my nonlcon, but it does run perfectly smooth. With no error messages.

Respuestas (1)

Hi,
fmincon will try to satisfy the relation c<0. To this end you need to compute c, but you give the relation. So: you need to write
c = x0(3)-0.1*x0(2);
Now: when the optimizer uses the computed c and satisfies "c<0", your relation x0(3)<0.1*x0(2) will be satisfied ....
Titus

4 comentarios

Hi Titus, thank you for your reply.
However, after trying several variations after your reply, I still have not found the optimal solution where, when I use fmincon, it also satisfies my constraint.
I now have my mycon written like
function [c, ceq] = mycon(x0)
ceq=[];
c=[];
c < 0;
c = x0(3)<(0.1*x0(2)) ;
But still, when I look at my optimal solution, I will still have more(!) than 10% of the weight of my second asset in my third asset.
I really have no idea what im doing wrong. Maybe i should use the output of the fmincon as restriction? Which I name 'weights' rather than the x0 vector?
that's still not right - the body of your function should be
function [c, ceq] = mycon(x0)
ceq = [];
c = x0(3) - 0.1*x0(2);
end
and that's it
The use of the logical operation "<" makes this nonlinear constraint non-smooth: either 0 or 1. This can be unmanageable for fmincon, which works best with smooth, continuous functions. Try Richard's coding.
actually your constraint is linear, so you should insert it as such (it is likely to converge easier)

Iniciar sesión para comentar.

Preguntada:

el 16 de Mayo de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by