nonlcon : Too many input arguments.

2 views (last 30 days)
Ali T
Ali T on 24 Nov 2015
Edited: Ali T on 24 Nov 2015
Hi friends, I have a problem with optimization algorithm like GA, fmincon and patternsearch. When I try to add constrain to my problem error below appear:
Error using Constrain
Too many output arguments.
Error in poptimfcnchk (line 58)
[cineq,ceq] = feval(nonlcon,reshapeinput(Xin,X),conFcnArg{:});
Error in patternsearch (line 343)
[Iterate,OUTPUT.funccount] = poptimfcnchk(FUN,nonlcon,initialX,Iterate, ...
Error in Main (line 30)
[x,Fval,exitFlag,output]=patternsearch(@(x)
Objective(x),X0,[],[],[],[],LB,UB,@Constrain,options)
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
PATTERNSEARCH cannot continue.
However, when I omit the constrain from optimization it works very well. After adding adding constrain to my problem this error occur. Could anyone help???
Thanks.
  2 Comments
Torsten
Torsten on 24 Nov 2015
You will have to show us your function with name "Constrain".
Best wishes
Torsten.
Ali T
Ali T on 24 Nov 2015
The Constrain function is not like what other simple functions. Because of sizing component which runs with ADVISOR is implemented, it may sounds weird to you. Anyway, I bring me code down there:
function Con=Constrain(x)
input.accel.param = {'spds','max_speed_bool'} ;
input.accel.value = {[0 37.28]};%;37.28 55.9234]} ;
[error, resp] = adv_no_gui('accel_test',input) ;
accel_1 = resp.accel.times(1) ;
MaxSpeed = resp.accel.max_speed*1.60934 ;
if ~error
Con=[accel_1/10-1;1-MaxSpeed/100];
end

Sign in to comment.

Accepted Answer

Alan Weiss
Alan Weiss on 24 Nov 2015
To quote the documentation on nonlinear constraints, "Nonlinear constraint functions must return both c and ceq, the inequality and equality constraint functions, even if they do not both exist. Return empty [] for a nonexistent constraint."
In other words, you need to rewrite your constraint function as follows, assuming your existing constraints are inequality rather than equality constraints:
function [Con,coneq] = Constrain(x)
Con = [];
coneq = [];
input.accel.param = {'spds','max_speed_bool'} ;
input.accel.value = {[0 37.28]};%;37.28 55.9234]} ;
[error, resp] = adv_no_gui('accel_test',input) ;
accel_1 = resp.accel.times(1) ;
MaxSpeed = resp.accel.max_speed*1.60934 ;
if ~error
Con=[accel_1/10-1;1-MaxSpeed/100];
end
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Ali T
Ali T on 24 Nov 2015
Edited: Ali T on 24 Nov 2015
Marvelous, that worked... amazing, thank you. Unfortunately I am not convinced why do I have to use con=[] and coneq=[] ??? When I ran my code, something new error pops up... Could you please help me solving this error too???
Error in adv_no_gui case {accel_test}:
Index exceeds matrix dimensions.
Attempt to reference field of non-structure array.
Error in Constrain (line 11)
accel_1 = resp.accel.times(1) ;
Error in poptimfcnchk (line 58)
[cineq,ceq] = feval(nonlcon,reshapeinput(Xin,X),conFcnArg{:});
Error in patternsearch (line 343)
[Iterate,OUTPUT.funccount] = poptimfcnchk(FUN,nonlcon,initialX,Iterate, ...
Error in Main (line 30)
[x,Fval,exitFlag,output]=patternsearch(@Objective,X0,[],[],[],[],LB,UB,@Constrain,options)
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
PATTERNSEARCH cannot continue.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!