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

40 visualizzazioni (ultimi 30 giorni)
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)

Risposte (3)

Alan Weiss
Alan Weiss il 12 Gen 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 Commenti
Alan Weiss
Alan Weiss il 20 Gen 2022
Your options specify that the objective function returns a gradient.
options = optimoptions(@fmincon,'Algorithm','interior-point',...
'SpecifyObjectiveGradient',true,'SpecifyConstraintGradient',true,...
'PlotFcn',{@optimplotx,@optimplotfval,@optimplotfirstorderopt});
However, your objective function does not seem to return a gradient:
function Ttotal = FCVEC06January2022V2(x)
% If this were to return a gradient the definition would be
% function [Ttotal,grad] = FCVEC06January2022V2(x)
end
I suggest that you remove these two name-value arguments and try again:
'SpecifyObjectiveGradient',true,'SpecifyConstraintGradient',true,...
Alan Weiss
MATLAB mathematical toolbox documentation
muhammad ilyas khattak khattak
Modificato: Walter Roberson il 21 Gen 2022
@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;

Accedi per commentare.


Walter Roberson
Walter Roberson il 21 Gen 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 Commenti
muhammad ilyas khattak khattak
@Walter Roberson@Alan Weiss .. Thanks once again for being with me for my several queries all the time 🙏🙏🙏…..I defined lower and upper bounds (lb and ub) exactly of the same length as that of “x”….And now the error / response is changed.. I am now getting the error/response “Exiting due to infeasibility: 166 lower bounds exceed the corresponding upper bounds”…. I am getting the response, while I am using the following configuration/settings of “fmincon”…..
If I get through this phase, I am pretty confidant, that an error-free run will be very soon….. Many thanks once again for answering my querries … 🙏🙏🙏.
Try 1:
x = fmincon(fun, X0, A, b, Aeq, beq, lb, ub, ncon, options);

Accedi per commentare.


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 Commenti
Walter Roberson
Walter Roberson il 31 Gen 2022
Numtk starts at 262, and you have various variables that are Numtk long in some dimension.
But you failed to lock in Numtk using ub and lb to ensure that it can never change. So it does change. It becomes slightly less -- something whose floor is 261. Then because it is used as the length of other variables, those other variables become shorter.
So eventually instead of Numv being pulled out of element 1070 of x, it gets pulled out of element 1066 -- because 4 variables in x before it each got 1 element shorter because floor(Numtk) reduced to 261 instead of 262. And what is at x(1070) ? Well, it happens to be a value that is almost 100000000 .
Now: line 3672
Ravailk = x(1,floor(Numtk+size(ttransij,2) + size(texeij,2)+ size(D_Cloud,2)+size(F_Cloud,2)+size(MuRSU,2)+... % "Ravail" keeps number of OBCU-mounted
size(MaxNumtk,2)+ size(E,2)+ size(DPrc,2)+size(pc,2)+size(Muc,2)+size(Mui,2)+size(Muk,2)+... % fog-vehicles with redundant/available
size(B,2)+size(pi)+size(pm,2)+ size(W,2)+size(Wfog,2)+size(t,2)+... % computation-resources.....
size(T,2)+size(cdk,2)+size(j,2)+ size(i,2)+ size(TasksperVehicle,2)+size(NTotal,2)+...
size(Numv,2)+ size(Numtk,2)+size(TijPrime,2)+size(loop1,2)+...
size(Fprime,2)+size(PhikPrime,2)+size(D,2)+size(F,2)+1):...
floor(Numtk+size(ttransij,2)+ size(texeij,2)+ size(D_Cloud,2)+size(F_Cloud,2)+...
size(MuRSU,2)+size(MaxNumtk,2)+size(E,2)+size(DPrc,2)+size(pc,2)+size(Muc,2)+...
size(Mui,2)+size(Muk,2)+size(B,2)+size(pi)+size(pm,2)+size(W,2)+size(Wfog,2)+...
size(t,2)+size(T,2)+size(cdk,2)+size(j,2)+size(i,2)+size(TasksperVehicle,2)+...
size(NTotal,2)+size(Numv,2)+size(Numtk,2)+size(TijPrime,2)+size(loop1,2)+...
size(Fprime,2)+size(PhikPrime,2)+size(D,2)+size(F,2)+Numv))
Notice the Numv at the end: it is being used to construct a range of indices into x. But it is now nearly 10^8 so you are constructing an index range up to close to 10^8 . For an array that has less than 5000 elements.
Walter Roberson
Walter Roberson il 31 Gen 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.

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by