Failure in initial user-supplied nonlinear constraint function evaluation. FMINCON cannot continue.

15 ビュー (過去 30 日間)
Hi,
I have been having trouble with this problem for quite some time. I want to minimize a function depending on a two matrices of variables (S and V as initial values). The code I use is:
global Foodprice Nutrients S V ZSum WSum SOnes VOnes X0 x
Foodprice=xlsread('MaxEntropySansCarbs.xlsx','Sheet1','C2:C22');
Nutrients=xlsread('MaxEntropySansCarbs.xlsx','Sheet1','D2:U22');
S=xlsread('MaxEntropySansCarbs.xlsx','Z','Q2:Z19');
V=xlsread('MaxEntropySansCarbs.xlsx','W','R2:AA22');
ZSum=xlsread('MaxEntropySansCarbs.xlsx','Z','O2:O19');
WSum=xlsread('MaxEntropySansCarbs.xlsx','W','P2:P22');
SOnes=ones(18,1);
VOnes=ones(21,1);
X0=vertcat(S,V);
for j=1:18,1:10;
for k=19:39,1:10;
objfun{j}{k}=@(x)sum(sum(x(j)))*log(x(j))'+sum(sum(x(k)))*log(x(k))';
ceq1=@(x)Foodprice-(sum(Nutrients')'*sum(ZSum)*sum(sum(x(j))))-WSum*sum(x(k));
ceq2=@(x)sum(x(j))'-SOnes';
ceq3=@(x)sum(x(k))'-VOnes';
ceq=@(x)[ceq1;ceq2;ceq3];
options=optimset('Algorithm','interior-point');
fmincon('objfun',X0,[],[],[],[],[],[],'ceq');
end
end
I get these warnings and errors:
Warning: The default trust-region-reflective algorithm does not solve problems with the
constraints you have specified. FMINCON will use the active-set algorithm instead. For
information on applicable algorithms, see Choosing the Algorithm in the documentation.
> In fmincon at 486
Error using feval
Undefined function 'ceq' for input arguments of type 'double'.
Error in fmincon (line 681)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation. FMINCON
cannot continue.
I'm not sure if I'm coding the choice variables from the matrix wrong or what. Any help would be extremely appreciated.

回答 (3 件)

Alan Weiss
Alan Weiss 2012 年 11 月 14 日
You have a few problems. A nonlinear constraint function must return both the inequality constraints and the equality constraints. See the documentation. Write a function file, say cfunction.m, that outputs both c and ceq.
Also, you do not pass your options, even though you created them. You have to pass options as the last argument:
[x,fval] = fmincon('objfun',X0,[],[],[],[],[],[],@cfunction,options);
I am not 100% sure I understand your loop, why do you give a different objfun at every step in the loop? There might be an error there, too.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

Walter Roberson
Walter Roberson 2012 年 11 月 14 日
Change
fmincon('objfun',X0,[],[],[],[],[],[],'ceq');
to
fmincon('objfun',X0,[],[],[],[],[],[],ceq);
You can only pass a function by name if the function has its own .m file on the MATLAB path.

Nazam Ali
Nazam Ali 2020 年 10 月 16 日
Hello, I am trying to optimize operator_profit with x as a variable in the following code. I am new to coding and trying to solve for quite a sometime, but I am getting following error:
Error in
Main_code3>@(x)confun(x,demand,theta,passenger_flow,alpha,car_probability,beta,a0,distance_travelled,bus_fare,bus_fuel_cost,car_travelling_cost_const)
Error in fmincon (line 633)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Error in Main_code3 (line 50)
[x,fval,exitflag] = fmincon(operator_profit,x0,A,b,Aeq,beq,lb,ub,confunxval);
Caused by:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
While, my main code is:
clear
clc
% -----------------------------------------------------------------------%
% Parameters Setting
theta = 0.1;
demand = 5000;
alpha = 500;
beta = 400;
bus_fare = 100;
bus_fuel_cost = 200;
car_travelling_cost_const = 1000;
distance_travelled = 10;
a0 = 4000;
x = [];
% Calculation of Mathematical Expressions
% -----------------------------------------------------------------------%
bus_probability = exp(-theta * ((1./x) * alpha + beta + bus_fare))...
/(exp(-theta * ((1./x) * alpha + beta + bus_fare)) + exp(-theta * car_travelling_cost_const));
car_probability = exp(-theta * car_travelling_cost_const)./(exp(-theta * car_travelling_cost_const)...
+ exp(-theta * ((1./x) * alpha + beta + bus_fare)));
passenger_flow = demand * bus_probability;
bus_operation_cost = a0 + distance_travelled * bus_fuel_cost;
% Objective Function to Maximize the Bus Operator Profit
% -----------------------------------------------------------------------%
operator_profit = @(x)...
(demand .*...
(exp(-theta * ((1./x) * alpha + beta + bus_fare))...
/(exp(-theta * ((1./x) * alpha + beta + bus_fare)) + exp(-theta * car_travelling_cost_const)))...
* (distance_travelled * bus_fare)...
- ((a0 + distance_travelled * bus_fuel_cost) *x));
real(operator_profit(20))
A = [];
b = [];
Aeq = [];
beq = [];
lb = 1;
ub = 200;
x0 = 4;
confunxval = @(x) confun(x,demand,theta,passenger_flow,alpha,car_probability,beta,a0,distance_travelled, bus_fare,bus_fuel_cost, car_travelling_cost_const);
[x,fval,exitflag] = fmincon(operator_profit,x0,A,b,Aeq,beq,lb,ub,confunxval);
Kind help will be much appreciated. Thank you!
  2 件のコメント
Alan Weiss
Alan Weiss 2020 年 10 月 16 日
I suggest that you start a new question instead oof asking a new question inside an old answer.
Alan Weiss
MATLAB mathematical toolbox documentation
Walter Roberson
Walter Roberson 2020 年 10 月 16 日
The problem is in confun but you do not show us the code for that.

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

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by