我是古罗比的新朋友。我写了下面的代码。我保存在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的值为二进制。
修改后,我得到了正确的解决方案。如果没有目标函数,我的兴趣是获得通用的二进制解。 如果有很多这样的解决方案,我想得到很少(例如1000)。 如何解决呢?为此,写
Maximize
1
我遇到错误:
Solution count 0
Model is infeasible or unbounded
Best objective -, best bound -, gap -
答案 0 :(得分:1)
您的lp文件左侧有一个常量,古罗比将这些常量解释为变量。
>>> 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)>]