如何打印空白(每次迭代)像OPL的引擎在Python?我想保持空格像这样:
Nodes Cuts/
Node Left Objective IInf Best Integer Best Bound ItCnt Gap
0 0 5627,2325 167 5627,2325 11
* 0+ 0 8610,2984 5627,2325 34,65%
0 0 5627,2325 186 8610,2984 Cuts: 157 158 34,65%
0 0 5627,2325 186 8610,2984 Cuts: 38 188 34,65%
* 0+ 0 8100,9408 5627,2325 30,54%
* 0+ 0 7058,3430 5627,2325 20,28%
0 2 5627,2325 186 7058,3430 5678,4051 188 19,55%
Elapsed time = 1,00 sec. (349,63 ticks, tree = 0,02 MB, solutions = 3)
* 86+ 13 7029,5430 5678,5253 19,22%
* 128+ 12 7026,1830 5678,5253 19,18%
* 170+ 12 7025,9430 5724,8207 18,52%
* 227+ 78 6972,1152 5757,6981 17,42%
* 415+ 157 6907,3358 5757,6981 16,64%
* 562+ 241 6787,0188 5757,6981 15,17%
我使用的是solve_details。,但它返回解决方案的差距。
>>> model.solve_details.gap
在Easy optimization with python中我分享了get solution 1 by 1
from docplex.mp.model import Model
from docplex.mp.progress import *
mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)
mdl.parameters.mip.limits.solutions=1
while (1==1):
sol=mdl.solve(log_output=False)
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
print("objective = ",sol.get_objective_value())
print("best bound = ",mdl.solve_details.best_bound)
print("mip gap = ",mdl.solve_details.mip_relative_gap)
print("status : ",mdl.solve_details.status)
if ("optimal solution" in str(mdl.solve_details.status)):
break
可以帮助你