FMINCON requires the following inputs to be of data type double: 'X0'.

30 views (last 30 days)
muhammad ilyas khattak khattak
Edited: Walter Roberson on 11 Feb 2022
I am getting the subject mentioned error in matlab code... i spent consecutive 3 days to recitify and solve but not succesfful uptil now... It is requested to help... Many thanks inadvance.... Followis is the code that i use....
options = optimoptions('fmincon','Display','iter','Algorithm','sqp');
problem.options = options;
problem.solver = 'fmincon';
[NumRSUs,MuRSU,MaxNumtk,E,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3] = FCVECInitializeParameters08November2021V5();
% StoreInitialization = struct(NumRSUs,MuRSU,MaxNumtk,E,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3);
global StoreInitialization;
StoreInitialization = struct('NumRSUs_struct',NumRSUs,'MuRSU_struct',MuRSU,'MaxNumtk_struct',MaxNumtk,'E_struct',E,'DPrc_struct',DPrc,'pc_struct',pc,'Muc_struct',Muc,'Mui_struct',Mui,'Muk_struct',Muk,'B_struct',B,'pi_struct',pi,'pm_struct',pm,'W_struct',W,'Wfog_struct',Wfog,'t_struct',t,'T_struct',T,'cdk_struct',cdk,'j_struct',j,'i_struct',i,'TasksperVehicle_struct',TasksperVehicle,'NTotal_struct',NTotal,'Numv_struct',Numv,'Numtk_struct',Numtk,'TijPrime_struct',TijPrime,'loop1_struct',loop1,'Fprime_struct',Fprime,'PhikPrime_struct',PhikPrime,'D_struct',D,'F_struct',F,'Ravailk_struct',Ravailk,'Phik_struct',Phik,'theta1_struct',theta1,'matrixforAlgo3_struct',matrixforAlgo3);
% NumRSUs,MuRSU,MaxNumtk,E,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3;
D_Cloud = zeros(1,Numtk);
F_Cloud = zeros(1,Numtk);
theta1 = Algorithm1_24December2021V8(Numv,Numtk,Ravailk,i,j,F,theta1);
N_Total = Numv;
% non-orthogonal multiple access (NOMA) technique, the communication resource is divided into resource
L = 10*N_Total; % blocks (RBs), expressed as B1 = {1,2,.....L} for uplink and
M = 10*N_Total; % B2 = {1,2,.....,M} for downlink.
[Tctotal,Ttotal,tTijtoRij,ttransij,texeij,tkij] = FCVEC4AllOutputs05January2022V3(D_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3);
for iloopagn12 = 1:1:Numtk
if (theta1(1,iloopagn12) == 1) % Only those tasks that are to be offloaded to cloud-server are segregated....
D_Cloud(1,iloopagn12) = D(1,iloopagn12);
end
end
[ElapsedTime,Ttotal,matrixforAlgo3,MatTkAllocVehsLM,summation2] = Algorithm3_04January2022V6(NumRSUs,MuRSU,L,M,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3,Ttotal,tTijtoRij,ttransij,texeij,Tctotal);
tic
theta = optimvar('theta',Numtk','Type','integer','LowerBound',0,'UpperBound',1);
ttransij1 = optimvar('ttransij1',Numtk,'Type','continuous','LowerBound',min(ttransij),'UpperBound',max(ttransij));
texeij1 = optimvar('texeij1',Numtk,'Type','continuous','LowerBound',min(texeij),'UpperBound',max(texeij));
rng(0) % For reproducibility
% OptimizationConstraints
constr = optimconstr(Numtk,1);
X0.theta = theta1;
X0.ttransij1 = ttransij;
X0.texeij1 = texeij;
for (loop1 = 1:1:Numtk)
if(matrixforAlgo3(loop1,4) == 0)
D_Cloud(1,loop1) = D(1,loop1);
F_Cloud(1,loop1) = F(1,loop1);
end
end
fun = @(X0,StoreInitialization,ttransij1,texeij1,theta,D_Cloud,F_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3)FCVEC06January2022V2(X0,StoreInitialization,ttransij1,texeij1,theta,D_Cloud,F_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3);
nonlcon1 = @(Numtk,constr,ttransij1,texeij1,cdk)nlcons(Numtk,constr,ttransij1,texeij1,cdk);
problem.nonlcon = nonlcon1(Numtk,constr,ttransij1,texeij1,cdk); % Non-linear constraint....
problem.objective = fun(X0,StoreInitialization,ttransij1,texeij1,theta,D_Cloud,F_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3); % Objective function.....
problem.x0 = X0;
x = fmincon(problem);
% x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)

Answers (3)

Alan Weiss
Alan Weiss on 12 Jan 2022
Without testing anything, I see a problem in this line of code:
fun = @(X0,StoreInitialization,ttransij1,texeij1,theta,D_Cloud,F_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3)FCVEC06January2022V2(X0,StoreInitialization,ttransij1,texeij1,theta,D_Cloud,F_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3);
...
problem.objective = fun(X0,StoreInitialization,ttransij1,texeij1,theta,D_Cloud,F_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3);
For fmincon your objective function must be a function of one variable only. You can have
fun = @(x)otherfunction(x,stuff,otherstuff)
This is a function of x alone. Here, x can be an array or vector of any size, but it is just one argument, not multiple arguments.
If you want to use a bunch of named arguments in your code, then I suggest that you take a serious look at the Problem-Based Optimization Workflow.
Alan Weiss
MATLAB mathematical toolbox documentation
  9 Comments
muhammad ilyas khattak khattak
@Alan Weiss Right Alan.... Thanks I ammended options as you guided....and executed the "fmincon" with "objective" and "nlcon" functions....As a result, I recieved the error that I used to recive when "options" with two (02) "gradient" sub-options as true..... The value of "x" is passed to the "fun" that is the objective function but when starts using the value of "x" inside the objetive function , it gives the error "Not enough input arguments."..... I tried with some altered configurations of "fun" objective function and the error remains the same in all the four (04) attemtps....
Try 1:
I received suggestions on mathworks online from Alan Wies to try change the ‘options’ by removing the two gradient options elements….
So I changed the options to ::
options = optimoptions(@fmincon,'Algorithm','interior-point',...
'PlotFcn',{@optimplotx,@optimplotfval,@optimplotfirstorderopt});
Try 2:
options = optimoptions('fmincon');
Try 3:
options = optimoptions(fmincon,'Algorithm','interior-point');
While keeping objective, non linear constraints and fmincon same in all the above as follows….
x = fmincon(fun, X0, A, b, Aeq, beq, lb, ub, ncon, options);
ncon = @nlcons;
fun = @(x)FCVEC06January2022V2;
So, now the new error am getting is:
Not enough input arguments.
Error in AllinOne14January2022V1>FCVEC06January2022V2 (line 1515)
Numtk = x(1,size(x,2)-1);
Error in AllinOne14January2022V1>@(x)FCVEC06January2022V2 (line 950)
fun = @(x)FCVEC06January2022V2;
Error in fmincon (line 562)
initVals.f = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
While keeping objective, non linear constraints and fmincon as follows….
x = fmincon(fun, X0, A, b, Aeq, beq, lb, ub, ncon, options);
ncon = @nlcons;
fun = @(x)FCVEC06January2022V2;
So, now the above mentioned same error (as in try-1) am getting is:
Not enough input arguments.
Error in AllinOne14January2022V1>FCVEC06January2022V2 (line 1515)
Numtk = x(1,size(x,2)-1);
Error in AllinOne14January2022V1>@(x)FCVEC06January2022V2 (line 950)
fun = @(x)FCVEC06January2022V2;
Error in fmincon (line 562)
initVals.f = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
While keeping objective, non linear constraints and fmincon as follows….
x = fmincon(fun, X0, A, b, Aeq, beq, lb, ub, ncon, options);
ncon = @nlcons;
fun = @(x)FCVEC06January2022V2;
% Try 4:
% Configuration:
options = optimoptions('fmincon','Algorithm','interior-point');
x = fmincon(fun(x), X0, A, b, Aeq, beq, lb, ub, ncon, options);
% Error received:
Not enough input arguments.
Error in AllinOne14January2022V1>FCVEC06January2022V2 (line 1516)
Numtk = x(1,size(x,2)-1);
Error in AllinOne14January2022V1>@(x)FCVEC06January2022V2 (line 950)
fun = @(x)FCVEC06January2022V2;

Sign in to comment.


Walter Roberson
Walter Roberson on 21 Jan 2022
fun = @(x)FCVEC06January2022V2;
That says that fun is to become an anonymous function that accepts zero or one input arguments, and when invoked, ignores the input argument and calls FCVC06January2022V2 with no parameters.
Refer back to where Alan said
fun = @(x)FCVEC06January2022V2(x,PUT ALL OTHER FUNCTION ARGUMENTS HERE);
ncon = @(x)nlcons(x,PUT ALL OTHER FUNCTION ARGUMENTS HERE);
If you do not happen to have any other function arguments, then you would still need to (x) part,
fun = @(x)FCEVEC06January2022V2(x)
ncon = @(x)nlcons(x)
but in your earlier versions you did need additional arguments so we suspect that you still need some.
  4 Comments

Sign in to comment.


muhammad ilyas khattak khattak
@Walter Roberson 🤝✌️😊☺️😊👍 Attached MATLAB code contains the assigned lb and ub….Tha is the matlab code when opened in MATLAB editor then from line-52 till line-2406 contains the assigned values to lb and ub…..Only, if you scroll down from line-52 till line-2406, you will find how and what values are assigned to lb and ub….. Here I think I should share with you that the length of both lb and ub is eactly equal to that of “x”….
I doubt this online editor is not allowing the length portion of my code to be appended here that contain the assigned values to lb and ub…..So, I had attach the entire updated code in matlab file "AllinOne14January2022V1."
Also, please ask freely that you need to know about this code, I will answer soon and precisely as possible..
I am getting very hopeful this time as I think I am getting nearer with help of response from you and other staff of the mathworks…. 🙏🙏🙏
Many Thanks once again….
  11 Comments
Walter Roberson
Walter Roberson on 31 Jan 2022
Note: I refuse to debug any more of this code. You need to completely redesign it.
Any value that is fixed (unchanging) during optimization should be passed in by parameterization http://www.mathworks.com/help/matlab/math/parameterizing-functions.html . This should include the sizes of all of the elements.
If you need to pack a lot of variable data (that truly changes) into a single vector, then write a function to unpack it -- a function that uses the static size information that was passed in by way of parameterization.
If you want to have entities that randomly interact (so part of your vector is random indices of which ones are interacting) then make sure you declare those index parameters as integer parameters and make sure you use lb and ub for them that lock them in to valid ranges according to the size of the data.
I do not recommend randomly changing the number of items you are using as you progress. However, it can be productive to use a fixed number of items together with binary "indicator" variables that switch elements on or off. Binary indicator variables should be declared as integer with range 0 to 1... and your unpacking function can logical() them as it unpacks them from x.
I repeat: I refuse to debug your current structure any further. No, you are not "just one or two errors away from being finished": your code is jam-packed with similar errors that you just haven't gotten to yet. The design you are using is too fragile to continue with.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by