FMINCON requires the following inputs to be of data type double: 'X0'.
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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)
0 commentaires
Réponses (3)
Alan Weiss
le 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 commentaires
Alan Weiss
le 20 Jan 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
le 21 Jan 2022
Modifié(e) : Walter Roberson
le 21 Jan 2022
Walter Roberson
le 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 commentaires
muhammad ilyas khattak khattak
le 23 Jan 2022
11 commentaires
Walter Roberson
le 31 Jan 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
le 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.
Voir également
Catégories
En savoir plus sur Problem-Based Nonlinear Optimization dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!