调用[xval, fval, status, extra] = glpk (c, A, b, lb, ub, ctype, vartype, sense, param);
max/min f = c * x
A * x [>=,<= …] b
LB:x最小取值
UB:x最大取值
ctype:规定A*x与 b的关系,
“F”
A free (unbounded) constraint (the constraint is ignored).
“U”
An inequality constraint with an upper bound (A(i,:)*x <= b(i)).
“S”
An equality constraint (A(i,:)*x = b(i)).
“L”
An inequality with a lower bound (A(i,:)*x >= b(i)).
“D”
An inequality constraint with both upper and lower bounds (A(i,:)*x >= -b(i)) and (A(i,:)*x <= b(i)).
vartype:x变量是浮点数还是整数,’C‘为浮点数,‘I’为整型变化。
sense :为-1 最大化目标函数,为1最小化目标函数
param用法可参考自带文档
其中param.itlim 可以限制迭代次数,如果问题比较复杂,通过设置itlim可求近似解
c=[1.0445;1.3989;3.3211;1.0640;1.4662;3.4043;1.1255;2.8009;3.3211;1.1130;1.3590;2.8551;1.0312;1.1314;2.4428]';
b = 8000*[0.2;0.1;0.1;0.1;0.1;1;2.5;10]';
A = [ 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 1 1 1 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 1 1 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 1 1 1 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;
5 5 5 2 2 2 4 4 4 3 3 3 1 1 1;
2 9 20 3 12 25 4 15 20 4 9 18 2 5 18];
lb = zeros(15,1);%x最小是0
ub = lb+8000;%x不超过8000,默认不超过inf
ctype = "LLLLLULU";
vartype = "CCCCCCCCCCCCCCC";
sense = -1; %maximization
param.msglev = 3; % output all
%param.itlim = 1000;
[xval, fval, status, extra] = ...
glpk (c, A, b, lb, ub, ctype, vartype, sense, param);