如何使用 Gurobi 解决优化问题
How to solve optimization problem using Gurobi
我是古罗比新人。我写了下面的代码。我保存在 Sample.lp。
运行 作为
gurobi_cl sample.lp
Maximize
x1 + x2 + x3 + 1 x4 + 1 x5
Subject To
c1: 3 x1 + 5 x2 + 2 x3 + 5 x4 + 7 x5 - 28 <= 0
c2: 2 x1 + 0 x2 + 0 x3 + 8 x4 - 14 <= 0
c3: 4 x4 + 5 x5 - 22 <= 0
c4: 3 x2 - 2 <= 0
D1: 3 x4 -1 >= 0
Bounds
x1 <= 1
x2 <= 1
x3 <= 1
x4 <= 1
x5 <= 1
Integers
x1 x2 x3 x4 x5
End
看来我没有得到正确的解决方案。你能帮我么。
x 的值是二进制的。
修改后,我得到了正确的解决方案。如果没有 objective 函数,我的兴趣是得到一个通用的二元解。
如果有很多这样的解决方案,我想得到其中的几个(比如 1000)。
如何解决这个问题?为此,如果写
Maximize
1
我遇到错误:
Solution count 0
Model is infeasible or unbounded
Best objective -, best bound -, gap -
您的 lp 文件在左侧有常量,gurobi 将其解释为变量。
>>> m.read("sample.lp")
Read LP format model from file sample.lp
Reading time = 0.00 seconds
: 5 rows, 10 columns, 16 nonzeros
>>> m.getVars()
[<gurobi.Var x1>,
<gurobi.Var x2>,
<gurobi.Var x3>,
<gurobi.Var x4>,
<gurobi.Var x5>,
<gurobi.Var 28>,
<gurobi.Var 14>,
<gurobi.Var 22>,
<gurobi.Var 2>,
<gurobi.Var 1>]
要修复,请将常量向右移动。
Maximize
x1 + x2 + x3 + 1 x4 + 1 x5
Subject To
c1: 3 x1 + 5 x2 + 2 x3 + 5 x4 + 7 x5 <= 28
c2: 2 x1 + 0 x2 + 0 x3 + 8 x4 <= 14
c3: 4 x4 + 5 x5 <= 22
c4: 3 x2 <= 2
D1: 3 x4 >= 1
Bounds
x1 <= 1
x2 <= 1
x3 <= 1
x4 <= 1
x5 <= 1
Integers
x1 x2 x3 x4 x5
End
这将给出预期的答案。
>>> m.read("sample.lp")
Read LP format model from file sample.lp
Reading time = 0.00 seconds
: 5 rows, 5 columns, 11 nonzeros
>>> m.optimize()
Optimal solution found (tolerance 1.00e-04)
Best objective 4.000000000000e+00, best bound 4.000000000000e+00, gap 0.0000%
>>> m.getVars()
[<gurobi.Var x1 (value 1.0)>,
<gurobi.Var x2 (value 0.0)>,
<gurobi.Var x3 (value 1.0)>,
<gurobi.Var x4 (value 1.0)>,
<gurobi.Var x5 (value 1.0)>]
我是古罗比新人。我写了下面的代码。我保存在 Sample.lp。 运行 作为
gurobi_cl sample.lp
Maximize
x1 + x2 + x3 + 1 x4 + 1 x5
Subject To
c1: 3 x1 + 5 x2 + 2 x3 + 5 x4 + 7 x5 - 28 <= 0
c2: 2 x1 + 0 x2 + 0 x3 + 8 x4 - 14 <= 0
c3: 4 x4 + 5 x5 - 22 <= 0
c4: 3 x2 - 2 <= 0
D1: 3 x4 -1 >= 0
Bounds
x1 <= 1
x2 <= 1
x3 <= 1
x4 <= 1
x5 <= 1
Integers
x1 x2 x3 x4 x5
End
看来我没有得到正确的解决方案。你能帮我么。 x 的值是二进制的。
修改后,我得到了正确的解决方案。如果没有 objective 函数,我的兴趣是得到一个通用的二元解。 如果有很多这样的解决方案,我想得到其中的几个(比如 1000)。 如何解决这个问题?为此,如果写
Maximize
1
我遇到错误:
Solution count 0
Model is infeasible or unbounded
Best objective -, best bound -, gap -
您的 lp 文件在左侧有常量,gurobi 将其解释为变量。
>>> m.read("sample.lp")
Read LP format model from file sample.lp
Reading time = 0.00 seconds
: 5 rows, 10 columns, 16 nonzeros
>>> m.getVars()
[<gurobi.Var x1>,
<gurobi.Var x2>,
<gurobi.Var x3>,
<gurobi.Var x4>,
<gurobi.Var x5>,
<gurobi.Var 28>,
<gurobi.Var 14>,
<gurobi.Var 22>,
<gurobi.Var 2>,
<gurobi.Var 1>]
要修复,请将常量向右移动。
Maximize
x1 + x2 + x3 + 1 x4 + 1 x5
Subject To
c1: 3 x1 + 5 x2 + 2 x3 + 5 x4 + 7 x5 <= 28
c2: 2 x1 + 0 x2 + 0 x3 + 8 x4 <= 14
c3: 4 x4 + 5 x5 <= 22
c4: 3 x2 <= 2
D1: 3 x4 >= 1
Bounds
x1 <= 1
x2 <= 1
x3 <= 1
x4 <= 1
x5 <= 1
Integers
x1 x2 x3 x4 x5
End
这将给出预期的答案。
>>> m.read("sample.lp")
Read LP format model from file sample.lp
Reading time = 0.00 seconds
: 5 rows, 5 columns, 11 nonzeros
>>> m.optimize()
Optimal solution found (tolerance 1.00e-04)
Best objective 4.000000000000e+00, best bound 4.000000000000e+00, gap 0.0000%
>>> m.getVars()
[<gurobi.Var x1 (value 1.0)>,
<gurobi.Var x2 (value 0.0)>,
<gurobi.Var x3 (value 1.0)>,
<gurobi.Var x4 (value 1.0)>,
<gurobi.Var x5 (value 1.0)>]