线性规划在 Matlab 中使用 linprog 可行,使用 Gurobi 不可行
Linear Programming feasible using linprog and unfeasible using Gurobi in Matlab
我要在 Matlab 中解决以下非常简单的线性规划问题
clear
%The unknown
%x=[x1,...,x10];
%The constraints
%x2+x8=Phi12
%x3+x7=Phi21
%x5=infvalue;
%x10=infvalue;
%The known parameters
Phi12=-3.3386;
Phi21=3.0722;
infvalue=50;
sizex=10; %size of the unknown
问题求解答。
当我使用 linprogr
实现此 LP 时,它找到了解决方案。
当我使用 Gurobi 求解器实现此 LP 时,它告诉我该问题不可行。
我做错了什么?这是我的代码。
beq=[Phi12; Phi21; infvalue; infvalue];
rAeq=[ 1 1 ...
2 2 ...
3 ...
4];
cAeq=[ 2 8 ...
3 7 ...
5 10];
fillAeq=[1 1 ...
1 1 ...
ones(1,2)];
Aeq=sparse(rAeq, cAeq,fillAeq, size(beq,1),sizex);
Aeqfull=full(Aeq);
%linprogr
f=zeros(sizex,1);
xlinprog = linprog(f,[],[],Aeqfull,beq);
%Gurobi
clear model;
model.A=Aeq;
model.rhs=beq;
model.sense=repmat('=', size(Aeq,1),1);
model.obj=f;
resultgurobi=gurobi(model);
在我试图理解发生了什么的过程中:如果我用任何 positive 值代替 -3.3386
,那么 Gurobi 就可以完美运行。怀特
在 Matlab 的 linprog
中,您的变量 x 默认为 -inf <= x <= inf,而对于 Gurobi,它是 x >= 0。因此,您需要设置下限您的变量为 -Inf:
model.lb = -inf * ones(sizex, 1);
我要在 Matlab 中解决以下非常简单的线性规划问题
clear
%The unknown
%x=[x1,...,x10];
%The constraints
%x2+x8=Phi12
%x3+x7=Phi21
%x5=infvalue;
%x10=infvalue;
%The known parameters
Phi12=-3.3386;
Phi21=3.0722;
infvalue=50;
sizex=10; %size of the unknown
问题求解答。
当我使用 linprogr
实现此 LP 时,它找到了解决方案。
当我使用 Gurobi 求解器实现此 LP 时,它告诉我该问题不可行。
我做错了什么?这是我的代码。
beq=[Phi12; Phi21; infvalue; infvalue];
rAeq=[ 1 1 ...
2 2 ...
3 ...
4];
cAeq=[ 2 8 ...
3 7 ...
5 10];
fillAeq=[1 1 ...
1 1 ...
ones(1,2)];
Aeq=sparse(rAeq, cAeq,fillAeq, size(beq,1),sizex);
Aeqfull=full(Aeq);
%linprogr
f=zeros(sizex,1);
xlinprog = linprog(f,[],[],Aeqfull,beq);
%Gurobi
clear model;
model.A=Aeq;
model.rhs=beq;
model.sense=repmat('=', size(Aeq,1),1);
model.obj=f;
resultgurobi=gurobi(model);
在我试图理解发生了什么的过程中:如果我用任何 positive 值代替 -3.3386
,那么 Gurobi 就可以完美运行。怀特
在 Matlab 的 linprog
中,您的变量 x 默认为 -inf <= x <= inf,而对于 Gurobi,它是 x >= 0。因此,您需要设置下限您的变量为 -Inf:
model.lb = -inf * ones(sizex, 1);