LINPROG requires the following inputs to be of data type double: 'f'.

8 次查看(过去 30 天)
I am trying to run an LP optimization.
My objective function is to estimate that minimize
where,
,
;
please note , in the code my reg1 , ...etc
I wrote the following code :
y= readtable('y.crra.csv'); % 249 x 1
y=table2array(y);
reg1=readtable('fitted.reg1.csv');
reg1=table2array(reg1);
trareg1=reg1.'; % 1 x 249
reg2=readtable('u.fitted.reg2.csv');
reg2=table2array(reg2);
trareg2=reg2.'; % 1 x 249
reg3=readtable('u.fitted.reg3.csv');
reg3=table2array(reg3);
trareg3=reg3.'; % 1 x 249
reg4=readtable('u.fitted.reg4.csv');
reg4=table2array(reg4);
trareg4=reg4.'; % 1 x 249
tau=.05;
yp= @(x) x*trareg1 + x*trareg2+x*trareg3 + x* trareg4;
e= @ (x) y-yp;
loss= @ (x) max(tau*(e), ( tau-1 )*(e));
obj = @(x) sum(loss);
Aeq = [1, 1, 1, 1];
lb = [0, 0, 0, 0];
beq = [1];
x = linprog(obj, [], [], Aeq, beq, lb, []);
I received the following error :LINPROG requires the following inputs to be of data type double: 'f'.
do you have any idea how to solve it?
I searched about the error and I've seen other people mentioning this error but I don't get how could work in my case
Thanks in advance.

采纳的回答

Torsten
Torsten 2023-2-9
编辑:Torsten 2023-2-9
y= readtable('y.crra.csv'); % 249 x 1
y=table2array(y);
reg1=readtable('fitted.reg1.csv');
reg1=table2array(reg1);
trareg1=reg1.'; % 1 x 249
reg2=readtable('u.fitted.reg2.csv');
reg2=table2array(reg2);
trareg2=reg2.'; % 1 x 249
reg3=readtable('u.fitted.reg3.csv');
reg3=table2array(reg3);
trareg3=reg3.'; % 1 x 249
reg4=readtable('u.fitted.reg4.csv');
reg4=table2array(reg4);
trareg4=reg4.'; % 1 x 249
tau=.05;
loss = @(x) (x(1)*trareg1 + x(2)*trareg2+x(3)*trareg3 + x(4)* trareg4).' - y;
obj = @(x) sum(max([tau*loss(x),(1-tau)*loss(x)],[],2));
Aeq = [1 1 1 1];
beq = 1;
lb = [0 0 0 0];
ub = [1 1 1 1];
sol = fmincon(obj,0.25*ones(4,1),[],[],Aeq,beq,lb,ub)
  7 个评论

请先登录,再进行评论。

更多回答(1 个)

John D'Errico
John D'Errico 2023-2-9
Apparently some or all of the arguments to LINPROG were not doubles. Which ones?
In your code, we see this:
yp= @(x) x*trareg1 + x*trareg2+x*trareg3 + x* trareg4;
e= @ (x) y-yp;
loss= @ (x) max(tau*(e), ( tau-1 )*(e));
obj = @(x) sum(loss);
So, is obj a double precision vector of numbers? (NO.) In fact, obj will return a SCALAR function of the argument vector.
obj is a function handle. While obj will return a number, if is NOT a vector of numbers. In fact, it is a NONLINEAR function of the argument, x. This is because of the max function inside the loss part of your objective.
Does LINPROG handle nonlinear objectives? (NO.)
Therefore, you cannot use LINPROG.
You may be able to use other tools like GA. But not FMINCON, because the max function inside the objective makes it not differentiable. And certainly not linprog.
  4 个评论
Az.Sa
Az.Sa 2023-2-9
编辑:Az.Sa 2023-2-9
can you please help with GA function code I have tried to edit my code based on that but it gives me error evry time
John D'Errico
John D'Errico 2023-2-10
Are you saying you want to solve the problem using GA? You would need to show the code you tried. But Torsten has already shown how to solve it using fmincon.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by