我正在用 cbc 和 gurobi(通过 pyomo)解决 MILP。Gurobi 指出该模型是不可行的。至于 cbc,求解器状态表明找到了可行且最优的解决方案,但随后它无法访问目标的值(这反过来又使我的程序崩溃)。
这是我访问目标值的方式
solver_parameters = "ResultFile=model.lp"
opt = SolverFactory(solver)
results = opt.solve(self.model, tee=False, options_string=solver_parameters,logfile="model.log")
# checking status and terminiation condition
print ("The solver returned a status of: "+str(results.solver.status))
print (str(results.solver))
if (results.solver.status == SolverStatus.ok) and (results.solver.termination_condition== TerminationCondition.optimal):
print ("Problem is feasible and solution is optimal")
objective_value = value(self.model.objective)
else:
print("Problem is infeasible")
objective_value = None
随着 cbc 我得到
The solver returned a status of: ok
- Status: ok
Message: CBC 2.10.5
Termination condition: optimal
Id: 0
Error rc: 0
Time: 0.011972904205322266
Problem is feasible and solution is optimal
ERROR: evaluating object as numeric value: x[0]
(object: <class 'pyomo.core.base.var._GeneralVarData'>)
No value for uninitialized NumericValue object x[0]
ERROR: evaluating object as numeric value: objective
(object: <class 'pyomo.core.base.objective.SimpleObjective'>)
No value for uninitialized NumericValue object x[0]
Traceback (most recent call last):
...
File "c:...", line 531, in solve
objective_value = value(self.model.objective)
File "pyomo\core\expr\numvalue.pyx", line 246, in pyomo.core.expr.numvalue.value
File "pyomo\core\expr\numvalue.pyx", line 231, in pyomo.core.expr.numvalue.value
File "C:...\lib\site-packages\pyomo\core\base\expression.py", line 55, in __call__
return self.expr(exception=exception)
File "pyomo\core\expr\numeric_expr.pyx", line 218, in pyomo.core.expr.numeric_expr.ExpressionBase.__call__
File "C:...\lib\site-packages\pyomo\core\expr\visitor.py", line 1054, in evaluate_expression
return visitor.dfs_postorder_stack(exp)
File "C:...\venv\lib\site-packages\pyomo\core\expr\visitor.py", line 584, in dfs_postorder_stack
flag, value = self.visiting_potential_leaf(_sub)
File "C:...\lib\site-packages\pyomo\core\expr\visitor.py", line 962, in visiting_potential_leaf
return True, value(node)
File "pyomo\core\expr\numvalue.pyx", line 246, in pyomo.core.expr.numvalue.value
File "pyomo\core\expr\numvalue.pyx", line 233, in pyomo.core.expr.numvalue.value
ValueError: No value for uninitialized NumericValue object x[0]
另一方面,我得到 gurobi
WARNING: Loading a SolverResults object with a warning status into
model.name="unknown";
- termination condition: infeasible
- message from solver: Model was proven to be infeasible.
The solver returned a status of: warning
- Status: warning
Return code: 0
Message: Model was proven to be infeasible.
Termination condition: infeasible
Termination message: Model was proven to be infeasible.
Wall time: 0.0019626617431640625
Error rc: 0
Time: 0.08476400375366211
Problem is infeasible
我假设问题确实不可行,并且错误来自 cbc。这种错误是否常见,有什么方法可以避免这种情况?
最后,这是gurobi返回的lp模型
\ Model x79
\ LP format - for model browsing. Use MPS format to capture full model detail.
Maximize
- 100 x12 - 200 x13 - 300 x14 - 400 x15 - 70 x16 - 140 x17 - 90 x18
- 90 x19
Subject To
c_u_x80_: x12 - 100 x1 <= 0
c_u_x81_: x13 - 100 x2 <= 0
c_u_x82_: x14 - 100 x3 <= 0
c_u_x83_: x15 - 100 x4 <= 0
c_u_x84_: x16 - 50 x5 <= 0
c_u_x85_: x17 - 50 x6 <= 0
c_u_x86_: x18 - 25 x7 <= 0
c_u_x87_: x19 - 25 x8 <= 0
c_l_x88_: x12 >= 0
c_l_x89_: x13 >= 0
c_l_x90_: x14 >= 0
c_l_x91_: x15 >= 0
c_l_x92_: x16 >= 0
c_l_x93_: x17 >= 0
c_l_x94_: x18 >= 0
c_l_x95_: x19 >= 0
c_u_x96_: x1 <= 1
c_u_x97_: x2 <= 1
c_u_x98_: x3 <= 1
c_u_x99_: x4 <= 1
c_u_x100_: x5 <= 1
c_u_x101_: x6 <= 1
c_u_x102_: x7 <= 1
c_u_x103_: x8 <= 1
c_e_x104_: x12 + x16 = 51
c_e_x105_: x13 + x17 = 51
c_e_x106_: x14 + x18 = 51
c_e_x107_: x15 + x19 = 51
c_u_x108_: - x7 + x9 <= 0
c_u_x109_: - x6 + x10 <= 0
c_u_x110_: - x2 - x3 + x11 <= 0
c_u_x111_: x7 - 2 x9 <= 0
c_u_x112_: x6 - 2 x10 <= 0
c_u_x113_: x2 + x3 - 2 x11 <= 0
c_u_x114_: 100 x13 + 20 x14 + 100 x17 + 20 x18 <= 6072
c_u_x115_: - 20 x18 + 1260 x9 <= 0
c_u_x116_: - 100 x17 + 1260 x10 <= 0
c_u_x117_: - 100 x13 - 20 x14 + 1260 x11 <= 0
c_u_x118_: 2 x1 - x24 <= 0
c_u_x119_: 2 x2 - x25 <= 0
c_u_x120_: 15 x3 - x26 <= 0
c_u_x121_: 10 x4 - x27 <= 0
c_u_x122_: 1.4 x5 - x24 <= 0
c_u_x123_: 1.4 x6 - x25 <= 0
c_u_x124_: 4.5 x7 - x26 <= 0
c_u_x125_: 2.25 x8 - x27 <= 0
c_u_x126_: 100 x12 + 200 x13 + 300 x14 + 400 x15 + 70 x16 + 140 x17
+ 90 x18 + 90 x19 - 2550 x24 - 5100 x25 - 1020 x26 - 2040 x27 + 6072 x48
+ 100 x60 + 100 x61 + 100 x62 + 100 x63 + 50 x64 + 50 x65 + 25 x66
+ 25 x67 - 1260 x76 - 1260 x77 - 1260 x78 <= 0
c_u_x127_: - 1000 x1 + x60 <= 0
c_u_x128_: - 1000 x2 + x61 <= 0
c_u_x129_: - 1000 x3 + x62 <= 0
c_u_x130_: - 1000 x4 + x63 <= 0
c_u_x131_: - 1000 x5 + x64 <= 0
c_u_x132_: - 1000 x6 + x65 <= 0
c_u_x133_: - 1000 x7 + x66 <= 0
c_u_x134_: - 1000 x8 + x67 <= 0
c_u_x135_: x60 - x32 <= 0
c_u_x136_: x61 - x33 <= 0
c_u_x137_: x62 - x34 <= 0
c_u_x138_: x63 - x35 <= 0
c_u_x139_: x64 - x36 <= 0
c_u_x140_: x65 - x37 <= 0
c_u_x141_: x66 - x38 <= 0
c_u_x142_: x67 - x39 <= 0
c_u_x143_: 1000 x1 - x60 + x32 <= 1000
c_u_x144_: 1000 x2 - x61 + x33 <= 1000
c_u_x145_: 1000 x3 - x62 + x34 <= 1000
c_u_x146_: 1000 x4 - x63 + x35 <= 1000
c_u_x147_: 1000 x5 - x64 + x36 <= 1000
c_u_x148_: 1000 x6 - x65 + x37 <= 1000
c_u_x149_: 1000 x7 - x66 + x38 <= 1000
c_u_x150_: 1000 x8 - x67 + x39 <= 1000
c_u_x151_: - 1000 x1 + x68 <= 0
c_u_x152_: - 1000 x2 + x69 <= 0
c_u_x153_: - 1000 x3 + x70 <= 0
c_u_x154_: - 1000 x4 + x71 <= 0
c_u_x155_: - 1000 x5 + x72 <= 0
c_u_x156_: - 1000 x6 + x73 <= 0
c_u_x157_: - 1000 x7 + x74 <= 0
c_u_x158_: - 1000 x8 + x75 <= 0
c_u_x159_: x68 - x40 <= 0
c_u_x160_: x69 - x41 <= 0
c_u_x161_: x70 - x42 <= 0
c_u_x162_: x71 - x43 <= 0
c_u_x163_: x72 - x44 <= 0
c_u_x164_: x73 - x45 <= 0
c_u_x165_: x74 - x46 <= 0
c_u_x166_: x75 - x47 <= 0
c_u_x167_: 1000 x1 - x68 + x40 <= 1000
c_u_x168_: 1000 x2 - x69 + x41 <= 1000
c_u_x169_: 1000 x3 - x70 + x42 <= 1000
c_u_x170_: 1000 x4 - x71 + x43 <= 1000
c_u_x171_: 1000 x5 - x72 + x44 <= 1000
c_u_x172_: 1000 x6 - x73 + x45 <= 1000
c_u_x173_: 1000 x7 - x74 + x46 <= 1000
c_u_x174_: 1000 x8 - x75 + x47 <= 1000
c_u_x175_: - 1000 x9 + x76 <= 0
c_u_x176_: - 1000 x10 + x77 <= 0
c_u_x177_: - 1000 x11 + x78 <= 0
c_u_x178_: x76 - x49 <= 0
c_u_x179_: x77 - x50 <= 0
c_u_x180_: x78 - x51 <= 0
c_u_x181_: 1000 x9 - x76 + x49 <= 1000
c_u_x182_: 1000 x10 - x77 + x50 <= 1000
c_u_x183_: 1000 x11 - x78 + x51 <= 1000
c_l_x184_: - 50 x24 + x32 - x40 >= -100
c_l_x185_: - 100 x25 + 100 x48 + x33 - x41 - 100 x51 >= -200
c_l_x186_: - 20 x26 + 20 x48 + x34 - x42 - 20 x51 >= -300
c_l_x187_: - 40 x27 + x35 - x43 >= -400
c_l_x188_: - 50 x24 + x36 - x44 >= -70
c_l_x189_: - 100 x25 + 100 x48 + x37 - x45 - 100 x50 >= -140
c_l_x190_: - 20 x26 + 20 x48 + x38 - x46 - 20 x49 >= -90
c_l_x191_: - 40 x27 + x39 - x47 >= -90
c_u_x192_: x24 <= 2
c_u_x193_: x25 <= 2
c_u_x194_: x26 <= 15
c_u_x195_: x27 <= 10
c_e_ONE_VAR_CONSTANT: ONE_VAR_CONSTANT = 1
Bounds
x24 free
x25 free
x26 free
x27 free
Binaries
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11
End