I dont know how to fix MILP error
3 次查看(过去 30 天)
显示 更早的评论
function f = fit(q)
J = 1;
T = 2;
K = 2;
%item,period
R = reshape(q(1:J*T),J,T);
IP = reshape(q(J*T+1:J*T*2+1),J,T);
IM = reshape(q(J*T*2+2:J*T*3+2),J,T);
XS = reshape(q(J*T*3+3:J*T*4+3),J,T);
X = reshape(q(J*T*4+4:J*T*4+4+J*K*T),J,K,T);
MAB = reshape(q(J*T*4+5+J*K*T:J*T*5+5+J*K*T),K,T); %binary
MIB = reshape(q(J*T*5+6+J*K*T:J*T*5+6+J*K*T*2),J,K,T);%binary
i=0.1;
a = [3; 2];
cr = [3.5; 1.5];
c = [3.5 1.5];
h = 3.5;
b = 1;
A = [2 3];
for j = 1:J
for k = 1:K
for t = 1:T
H(j,t) = (1/(1+i)^t)*((h(j)*(R(j,t)+IP(j,t))/2)+b(j)*IM(j,t)+c(j,t)*XS(j,t));
MAO(k,t) = (1/(1+i)^t)*(A(k)*MAB(k,t));
MIO(j,k,t) = (1/(1+i)^t)*(a(j,k)*MIB(j,k,t)+cr(j,k)*X(j,k,t));
end
end
end
f = H + MaO + MiO;
intcon = 1:J*T*5+6+J*K*T*2;
lb = zeros(J*T*5+6+J*K*T*2,1);
ub = ones(1:J*T*5+6+J*K*T*2,1);
ub(1:J*T*4+4+J*K*T)=inf;
x = intlinprog(@fit,intcon,lb,ub);
I made this code. I try to solve this use intlinprog. but error message tells me "Not enough input arguments."
How can I fix it? Am i using intlinprog as a wrong way?
0 个评论
回答(1 个)
Ced
2016-3-16
编辑:Ced
2016-3-16
I'm afraid so, yes. Two main points:
1. The function to minimize has to be linear one (hence the name linear program) and has to be passed as a numerical vector. Meaning: You need to reformulate your problem as fit(q) = f'*q, and then pass f to intlinprog.
2. The correct order of arguments is
X = intlinprog(f,intcon,A,b,Aeq,beq,LB,UB)
If your problem does not have certain parts, e.g. the equality constraints, then you need to pass empty matrices [] instead. You can't just leave them away.
I put together a little dummy example to show you how this works:
function x_sol = my_int_minimizer()
% simple example:
% min( -x1 -x2 + x3)
% s.t.
% (1.) x1 + x2 + x3 <= 5.5
% (2.) x2 <= 2
% (3.) x3 >= 1
% (4.) x1 integer
% vector of unknowns: x, dimension 3x1
% function to minimize: J = f'*x
f = [ -1 -1 1 ]';
% integer constraint (4.)
intcon = 1;
% inequality constraint A*x <= b (1.)
Aineq = [ 1 1 1 ];
bineq = 5.5;
% equality constraints (none)
Aeq = [];
beq = [];
% lower / upper bounds
lb = [ -inf -inf 1 ]'; % (3.)
ub = [ inf 2 inf ]'; % (2.)
% solve problem
x_sol = intlinprog(f,intcon,Aineq,bineq,Aeq,beq,lb,ub);
% output solution
disp(x_sol)
end
Cheers
2 个评论
Alan Weiss
2016-3-18
Did you read what Ced told you? Your objective function cannot be nonlinear, it must be a simple vector of constants. See the intlinprog function reference page for the f argument (the objective).
Alan Weiss
MATLAB mathematical toolbox documentation
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Problem-Based Optimization Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!