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

조회 수: 8 (최근 30일)
Az.Sa
Az.Sa 2023년 2월 9일
댓글: Az.Sa 2023년 2월 10일
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
Torsten
Torsten 2023년 2월 10일
Did you correct the error before you ran the code ?
Az.Sa
Az.Sa 2023년 2월 10일
Great !! Thank you very much for your help !!

댓글을 달려면 로그인하십시오.

추가 답변 (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