fmincon nonlinear constrain problem set-up

One of my initial conditions is leaving me scratching my head. Since R & C are defined by summing functions (R will have w components and C will have d components), it may be simpliest to represent R & C as functions. Since there is a function f embedded in R, I run in to problems. I can see how to establish the A,b,Aeq and beq inputs, but I have no clue how to represent the nonlinear constraint of f and pass it in to R. Any help would be much appreciated.
P=R-C; % Objective Function
-P <= 0;
R= sum(U*f(sum(sqrt(((Xb+X)-Xw)^2)));
C= sum(Md*(X-Xb)+B);
f=@d h*exp(1/d);

 Respuesta aceptada

Matt J
Matt J el 18 de Ag. de 2016
Editada: Matt J el 18 de Ag. de 2016
Something like the following, perhaps?
Pfun=@(X) objectiveFull(X, Xb,Xw, U Md,B,h);
nonlcon=@(X) nonlconFull(X,Pfun);
Xopt = fmincon(Pfun,X0, A,b,Aeq,beq,[],[],nonlcon)
function P=objectiveFull(X,Xb,Xw, U Md,B,h)
f=@(d) h*exp(1/d);
R= sum(U*f( norm(X-(Xw-Sb) ) ) );
C= sum(Md*(X-Xb)+B);
P=R-C;
function [c,ceq]=nonlconFull(X,Pfun)
ceq=[];
c=-Pfun(X);

3 comentarios

Matt J
Matt J el 18 de Ag. de 2016
You could also make objectiveFull and nonlconFull into Nested Functions. This will make it so that they both have access to all the constant parameters simultaneously and can call each other.
Alec Jeffery
Alec Jeffery el 19 de Ag. de 2016
Editada: Matt J el 19 de Ag. de 2016
Great suggestions. I ended up using your nested functions with some modifications (code below). I changed the sign on the constraint function and the objective function. The results seem to be what I want.
% X=x
% Xb=tempBeePos
% Xw=tempWalkerPos
% U=UnitRewards;
% M=BeeAdj(:,1)
% B=BeeAdj(:,2)
% DistRewardFunc
function [Profit,Xopt]=newOptiCode(tempBeePos,tempWalkerPos,UnitRewards,BeeAdj,DistRewardFunc)
M=BeeAdj(:,1);
B=BeeAdj(:,2);
Pfun=@(x) -objectiveFull(x,tempBeePos,tempWalkerPos,UnitRewards,M,B,DistRewardFunc);
% Threw a '-' before function so that you are finding the minimum of -Max
nonlcon=@(x) nonlconFull(x,Pfun);
options = optimoptions('fmincon','Display','off');
Xopt = fmincon(Pfun,tempBeePos, [],[],[],[],[],[],nonlcon,options);
% Xopt = fmincon(Pfun,tempBeePos, A,b,Aeq,beq,[],[],nonlcon)
function P=objectiveFull(x,tempBeePos,tempWalkerPos,UnitRewards,M,B,DistRewardFunc)
R=[];
for w=1:size(tempWalkerPos,1)
f=DistRewardFunc{w};
BWdist=sum(sqrt(((tempBeePos+x)-tempWalkerPos(w,:)).^2));
R(w,1)=UnitRewards(w)*f(BWdist);
end
R=sum(R);
% R= sum(U*f( norm(X-(Xw-Xb) ) ) );
% C= sum(Md*(X-Xb)+B);
C=[];
for d=1:size(tempBeePos,2)
if x(d) > 0;
C(d,1)=M(d)*(x(d)-tempBeePos(d))+B(d);
else
C(d,1)=B(d);
end
end
C=sum(C);
P=R-C;
end
function [c,ceq]=nonlconFull(x,Pfun)
ceq=[];
c=Pfun(x);
% changed the sign on c=-Pfun(x)
end
Profit=objectiveFull(Xopt,tempBeePos,tempWalkerPos,UnitRewards,M,B,DistRewardFunc);
end
Matt J
Matt J el 19 de Ag. de 2016
Editada: Matt J el 19 de Ag. de 2016
Glad it worked out, but the purpose of my suggestion to use nested functions was to spare you the long argument lists. Below, the nested functions Pfun and nonlcon both share access to the workspace of newOptiCode, so there is no longer any need to pass extra arguments to them (like tempWalkerPos, etc...).
function [Profit,Xopt]=newOptiCode(tempBeePos,tempWalkerPos,...
UnitRewards,BeeAdj,DistRewardFunc)
M=BeeAdj(:,1);
B=BeeAdj(:,2);
Xopt = fmincon(@(x) -Pfun(x),tempBeePos, [],[],[],[],...
[],[],@nonlcon,options);
Profit=Pfun(Xopt);
function P=Pfun(x)
R=[];
for w=1:size(tempWalkerPos,1)
f=DistRewardFunc{w};
BWdist=sum(sqrt(((tempBeePos+x)-tempWalkerPos(w,:)).^2));
R(w,1)=UnitRewards(w)*f(BWdist);
end
R=sum(R);
% R= sum(U*f( norm(X-(Xw-Xb) ) ) );
% C= sum(Md*(X-Xb)+B);
C=[];
for d=1:size(tempBeePos,2)
if x(d) > 0;
C(d,1)=M(d)*(x(d)-tempBeePos(d))+B(d);
else
C(d,1)=B(d);
end
end
C=sum(C);
P=R-C;
end
function [c,ceq]=nonlcon(x)
ceq=[];
c=-Pfun(x);
% changed the sign on c=-Pfun(x)
end
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 18 de Ag. de 2016

Editada:

el 19 de Ag. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by