# Optimization: share additional output of cost function with constraint function

5 visualizzazioni (ultimi 30 giorni)
Martijn il 7 Mar 2012
Commentato: Conrad McGreal il 21 Dic 2020
I have a well defined optimization problem in which I have a separate cost function and a separate constraint function:
Nonlinconstr=@(C)nonlcon(C,lp,rw,xref,yref,sol);
[C,fval] = fmincon(Cost,C1,[],[],[],[],[],[],Nonlinconstr);
Inside the cost function (which is called first by fmincon), I evaluate nonlinear functions of the decision variables (C in this case). The outputs of the nonlinear function determine the cost.
However, in the constraint function I need to evaluate the same function, with the same decesion variables (thats the way fmincon works; first the cost, then the contraints). Hence, I would rather use the values already calculated in the cost functions.
I tried to do this by using a structure SOL as an output of the cost function, and provide it as an input to the constraint function hence (also see code above);
function [c,ceq]=nonlcon(C,lp,rw,xref,yref,sol)
However, Matlab will tell that "sol" is unknown. I'am guessing that "sol" is not an output when the function `costcenterroad` is called. I also used 'sol' as a global variable, but this is not very elegant and fast.
Does anyone have any suggestions how to do this? Your help will be greatly appreciated!
##### 2 CommentiMostra NessunoNascondi Nessuno
Martijn il 7 Mar 2012
I just did some extra testing and found out that indeed, saving in the cost function globally, and calling it in the constraint function globally, is slower than evaluating the functions just for a second time separately in the constraint function.
Hence, there must be a smart solution without the use of global variables that will save my day!
Martijn il 7 Mar 2012
Based on what Seth DeLand referred to, I tried the following:
[Cost,Nonlincontr]=ObjectiveAndConstraints
[C,fval] = fmincon(Cost,C1,[],[],[],[],[],[],Nonlincontr,lp,L,v,xref,yref,T,rw);
The set {lp,L,v,xref,yref,T,rw} are constrant parameters in the optimization, defined a-priori.
And this:
function [Cost,Nonlincontr]=ObjectiveAndConstraints
Nonlincontr=@nonlc;
%Will store items calculated in the objective function.
d=[];
dteta=[];
phi=[];
u1=[];
x=[];
y=[];
J=[];
c=[];
ceq=[];
function J=costroad(C) %some function which requires v,L,C
[x,y,dx,dy,ddx,ddy]=evalcubspline(lp,C); %evaluate spline
%Left out code that translates these {x,..,ddy} to cost
J=sum(cost)
end
function [c,ceq]=nonlc %requires intermediate solution from cost function
c=[d-rw; -(phi'+pi/3); phi'-pi/3; -(u1'+pi/3); u1'-pi/3];
ceq=[x(1)-xref(1);x(end)-xref(end);y(1)-yref(1);y(end)-yref(end)]; %equality constraints;
end
end
%%%%%%%%%%%%%
Unfortunatly I get the error:
Too many input arguments.
Error in fmincon (line 601)
initVals.f = feval(funfcn{3},X,varargin{:});
[C,fval] =
fmincon(Cost,C1,[],[],[],[],[],[],Nonlincontr,lp,L,v,xref,yref,T,rw);
Caused by:
Failure in initial user-supplied objective function evaluation.
FMINCON cannot continue.

Accedi per commentare.

### Risposta accettata

Seth DeLand il 7 Mar 2012
The technical term for this practice is "Memoization". There's some more good information on memoization in MATLAB in Loren's blog post:
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Martijn il 7 Mar 2012
This is great!! Thank you very much.

Accedi per commentare.

### Più risposte (1)

Martijn il 7 Mar 2012
I found the answer after some struggeling with passing on the parameters. Here is the solution:
[Cost,Nonlincontr]=ObjectiveAndConstraints(lp,L,v,xref,yref,T,rw);
[C,fval] = fmincon(Cost,C1,[],[],[],[],[],[],Nonlincontr)
Where the set {L,..,rw} are parameters (array,structures whatever).
The function ObjectiveAndConstraints looks like:
function [Cost,Nonlincontr]=ObjectiveAndConstraints(lp,L,v,xref,yref,T,rw)
Nonlincontr=@(C)nonlc(C,lp,L,v,xref,yref,T,rw);
%Will store items calculated in the objective function.
d=[];
dteta=[];
phi=[];
u1=[];
x=[];
y=[];
J=something based on inputs
end
function [c,ceq]=nonlc(C,lp,L,v,xref,yref,T,rw)
c=something based on inputs
ceq=something based on iputs
end
end
Hopefully this well help someone else :)
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Conrad McGreal il 21 Dic 2020
Thank you for taking the time to write this up. It was exactly what I needed today!

Accedi per commentare.

### Categorie

Scopri di più su Solver Outputs and Iterative Display in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by