寻找一个向量x,它最小化了c . x,满足约束条件m . x >= b,x是整数。
以下是一个样例输入集:
c : {1,2,3}
m : {{1,0,0},
{0,1,0},
{1,0,1}}
b : {1,1,1}
输出结果为:
x = {1,1,0}
什么工具适合解决这种问题,有哪些使用示例?
寻找一个向量x,它最小化了c . x,满足约束条件m . x >= b,x是整数。
以下是一个样例输入集:
c : {1,2,3}
m : {{1,0,0},
{0,1,0},
{1,0,1}}
b : {1,1,1}
输出结果为:
x = {1,1,0}
GLPK
我将使用GLPK的glpsol来回答,但我希望有更好的方法来解决这个问题(似乎GLPK对于这种简单的线性规划问题过于强大/通用):
为了生成下面给出的.mps文件,您需要将矩阵按行拆分成一组方程式,因此问题描述变为:
minimize
cost = 1 x_1 + 2 x_2 + 3 x_3
s.t. constraints:
S1 = 1 x_1 + 0 x_2 + 0 x_3
S2 = 0 x_1 + 1 x_2 + 0 x_3
S3 = 1 x_1 + 0 x_2 + 1 x_3
S1 >= 1
S2 >= 1
S3 >= 1
0 <= x1 <= 1
0 <= x2 <= 1
0 <= x3 <= 1
GLPK文档提供了关于.mps格式的详细信息,但你需要指定行、列、rhs和边界值。在ROWS部分,“N”和“G”分别表示约束类型(数字和大于)。在BOUNDS部分,“UI”表示边界是上限整数类型,强制解决方案为整数。
要在问题规范上运行求解器:
> glpsol --freemps example.mps -o example.out
示例.mps文件:
NAME VM
ROWS
N cost
G S1
G S2
G S3
COLUMNS
x_1 cost 1.0
x_1 S1 1.0
x_1 S3 1.0
x_2 cost 2.0
x_2 S2 1.0
x_3 cost 3.0
x_3 S3 1.0
RHS
RHS1 cost 0.0
RHS1 S1 1.0
RHS1 S2 1.0
RHS1 S3 1.0
BOUNDS
UI BND1 x_1 1.0
UI BND1 x_2 1.0
UI BND1 x_3 1.0
ENDATA
输出:
Problem: VM
Rows: 4
Columns: 3 (3 integer, 3 binary)
Non-zeros: 7
Status: INTEGER OPTIMAL
Objective: cost = 3 (MINimum)
No. Row name Activity Lower bound Upper bound
------ ------------ ------------- ------------- -------------
1 cost 3
2 S1 1 1
3 S2 1 1
4 S3 1 1
No. Column name Activity Lower bound Upper bound
------ ------------ ------------- ------------- -------------
1 x_1 * 1 0 1
2 x_2 * 1 0 1
3 x_3 * 0 0 1
Integer feasibility conditions:
INT.PE: max.abs.err. = 0.00e+00 on row 0
max.rel.err. = 0.00e+00 on row 0
High quality
INT.PB: max.abs.err. = 0.00e+00 on row 0
max.rel.err. = 0.00e+00 on row 0
High quality
End of output
No. Column name Activity
------ ------------ -------------
1 x_1 * 1
2 x_2 * 1
3 x_3 * 0
Mathematica 已经内置了此功能。(注:Mathematica 不是免费软件。)
LinearProgramming[c, m, b, Automatic, Integers]
输出:
{1, 1, 0}
Python: PULP
x = LpVariable("Some Variable", cat="Integer")
)来进行整数规划。默认的类型是连续型,还有一个二进制选项。 - undefined