如何使用docplex python API获取不可行约束?

w6mmgewl  于 12个月前  发布在  Python
关注(0)|答案(2)|浏览(288)

我正在研究电动汽车路径问题,我的问题的解决方案是不可行的。现在,我如何获得解决方案不可行的约束?我想使用Python API在docplex中获得违反的约束。

solve_status = mdl.get_solve_status()
    if solve_status.name == 'INFEASIBLE_SOLUTION':  # or also 'INFEASIBLE_OR_UNBOUNDED_SOLUTION'
        cref = cr.ConflictRefiner()
        print('show some of the constraints that can be removed to arrive at a minimal conflict')
        cref.refine_conflict(mdl, display=True)

字符串
我试过了,但这给了我一些违反的约束。不打印约束。

11dmarpk

11dmarpk1#

在docplex文档中,有一些函数可以显示冲突并在冲突上进行重命名:https://ibmdecisionoptimization.github.io/docplex-doc/mp/docplex.mp.conflict_refiner.html这些函数是docplex.mp.conflict_refiner.dislay()-显示所有冲突。docplex.mp.conflict_refiner.iter_conflicts()-返回冲突的迭代器(命名为元组)。

0vvn1miw

0vvn1miw2#

https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoorelaxationandconflict.py上的小示例

from docplex.mp.model import Model
from docplex.mp.relaxer import Relaxer

from docplex.mp.conflict_refiner import ConflictRefiner

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.add_constraint(nbbus40 + nbbus30 <= 7, 'maxTotalBuses')

mdl.minimize(nbbus40*500 + nbbus30*400)

mdl.solve()

mdl.report()
print(f"* solve status is: '{mdl.solve_details.status}'") #infeasible model

print()
print("------- starting relaxation")
print()

rx = Relaxer()
rx.relax(mdl)

print ("number_of_relaxations= " + str(rx.number_of_relaxations))
rx.print_information()

mdl.report()
print(f"* status after relaxation is: {mdl.solve_details.status}")
#print(mdl.solution)

print()
print("------ starting conflict refiner")
print()

cr=ConflictRefiner()
conflicts=cr.refine_conflict(mdl)
conflicts.display()

# for conflict in conflicts:
#     st = conflict.status
#     ct = conflict.element
#     label = conflict.name
#     label_type = type(conflict.element)
#     if isinstance(conflict.element, VarLbConstraintWrapper) \
#             or isinstance(conflict.element, VarUbConstraintWrapper):
#         ct = conflict.element.get_constraint()
#
#     # Print conflict information in console
#     print("Conflict involving constraint: %s" % label)
#     print(" \tfor: %s" % ct)

"""

which gives

* solve status is: 'integer infeasible'

------- starting relaxation

number_of_relaxations= 1
* number of relaxations: 1
 - relaxed: maxTotalBuses, with relaxation: 1.0
* total absolute relaxation: 1.0
* model buses solved with objective = 3800
* status after relaxation is: optimal relaxed sum of infeasibilities

------ starting conflict refiner

conflict(s): 3
  - status: Member, Variable Lower Bound: nbBus30 >= 0
  - status: Member, LinearConstraint: kids: 40nbBus40+30nbBus30 >= 300
  - status: Member, LinearConstraint: maxTotalBuses: nbBus40+nbBus30 <= 7

  """

字符串

相关问题