call function in fmicon optimization failing

Hi guys, I have 4 functions where each one is derived from previous one. When I call the last one (revenue.m) I got an error using fmincon. Any clue? I want to optimize in respect to x0 that is present in each function. Many Thanks! Here is my code:
%% Total out flow equation T = zeros([N,M]); S=zeros([N,M]); for i = 1:M
R(:,i)=revenue(price(:,1),HH(:,i),ro,g,eff,x0(:,i),N,k1);
options = optimset('MaxFunEvals',Inf,'MaxIter',50,...
'Algorithm','interior-point','Display','iter');
tic
[x(:,i),fval(:,i)] = fmincon(@revenue,x0(1:2*N,i),A,b(:,i),Aeq,beq(:,i),LB(:,i),UB(:,i),[],options); %good
toc
end
with revenue fun being:
function R=revenue(price,HH,ro,g,eff,x0,N,k1)
R= -sum(price.*((HH*ro*g*eff).*x0(1:N))/k1);
end
% saved as revenue.m
Error msg:
Not enough input arguments.
Error in revenue (line 5)
R= -sum(price.*((HH*ro*g*eff).*x0(1:N))/k1);
Error in fmincon (line 535)
initVals.f = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
end

4 件のコメント

Torsten
Torsten 2018 年 5 月 25 日
fmincon expects "revenue" to be of the form
function R=revenue(x)
where x is the vector of unknowns.
Your function has the form
function R=revenue(price,HH,ro,g,eff,x0,N,k1)
sensation
sensation 2018 年 5 月 25 日
Thanks Torsten. How I am supposed to write then revenue fun if it is calculated using (price,HH,ro,g,eff,x0,N,k1)? Any tip? Many thanks really!
Torsten
Torsten 2018 年 5 月 25 日
What is the "x" in your list that fmincon expects ? Is it x0 ?
sensation
sensation 2018 年 5 月 25 日
編集済み: sensation 2018 年 5 月 25 日
yes. with initial guess :
inFlow = rand(10,3);
x0 = [inFlow; zeros(size(inFlow))];
Thanks!

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2018 年 5 月 25 日
編集済み: Stephen23 2018 年 5 月 25 日

0 投票

The fmincon documentation clearly states that the first input " fun is a function that accepts a vector or array x and returns a real scalar f, the objective function evaluated at x". So it only accepts one input, which can be a vector or matrix. How many inputs does revenue have?
You need to parameterize the function, e.g. using an anonymous function and assuming that all of HH, ro, etc. are defined in that workspace, and the first input price is that one that you want to optimize:
fun = @(x) revenue(x,HH,ro,g,eff,x0,N,k1);
fmincon(fun,...)

5 件のコメント

sensation
sensation 2018 年 5 月 25 日
Hi Stephen and thanks for your msg. I used eventually as you suggested the wrapper funtion:
tic
[x(:,i),fval(:,i)] = fmincon(@(x0)revenue(price(:,i),HH(:,i),ro,g,eff,x0(:,i),N,k1),x0(1:2*N,i),A,b(:,i),Aeq,beq(:,i),LB(:,i),UB(:,i),[],options);
toc
and it runs for the first time step when M=1, then it crashes because: Index exceeds matrix dimensions. Thanks for any tip!
Torsten
Torsten 2018 年 5 月 25 日
... = fmincon(@(x0)revenue(price(:,i),HH(:,i),ro,g,eff,x0,N,k1),...
sensation
sensation 2018 年 5 月 25 日
Thanks. Can you just explain me why we did not have to use every column of x0(:,i) in wrapped fun and only x0? Because we expect x0? Cheers!
Torsten
Torsten 2018 年 5 月 25 日
編集済み: Torsten 2018 年 5 月 25 日
The solution inside fmincon is passed as a one-dimensional vector.
Writing
... = fmincon(@(x0)revenue(price(:,i),HH(:,i),ro,g,eff,x0(:,i),N,k1),...
assumes that x0 inside fmincon is a matrix from which you want to pass the i'th column to "revenue". But this i'th column of a matrix does not exist unless i=1.
Best wishes
Torsten.
sensation
sensation 2018 年 5 月 25 日
Thanks!!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOptimization Toolbox についてさらに検索

質問済み:

2018 年 5 月 25 日

編集済み:

2018 年 6 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by