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.
댓글 수: 0
채택된 답변
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
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
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!