我遵循本教程解决了使用或-tools:https://developers.google.com/optimization/mip/integer_opt的MIP。
以下是代码:
from ortools.linear_solver import pywraplp
def main():
# Create the mip solver with the SCIP backend.
solver = pywraplp.Solver.CreateSolver('SCIP')
infinity = solver.infinity()
# x and y are integer non-negative variables.
x = solver.IntVar(0.0, infinity, 'x')
y = solver.IntVar(0.0, infinity, 'y')
print('Number of variables =', solver.NumVariables())
# x + 7 * y <= 17.5.
solver.Add(x + 7 * y <= 17.5)
# x <= 3.5.
solver.Add(x <= 3.5)
print('Number of constraints =', solver.NumConstraints())
# Maximize x + 10 * y.
solver.Maximize(x + 10 * y)
status = solver.Solve()
if status == pywraplp.Solver.OPTIMAL:
print('Solution:')
print('Objective value =', solver.Objective().Value())
print('x =', x.solution_value())
print('y =', y.solution_value())
else:
print('The problem does not have an optimal solution.')
print('\nAdvanced usage:')
print('Problem solved in %f milliseconds' % solver.wall_time())
print('Problem solved in %d iterations' % solver.iterations())
print('Problem solved in %d branch-and-bound nodes' % solver.nodes())
if __name__ == '__main__':
main()现在我的问题是得到对偶问题,或者至少是对偶变量的值。我已经找到了这段代码https://github.com/google/or-tools/issues/419,但它们没有以相同的方式实现约束,而且我不希望重写整个代码(现在的代码相当长)。
发布于 2021-02-12 13:46:47
双变量不公开。我甚至不确定它们是被创造出来的。此外,双值仅适用于纯LP,而不适用于MIP。
https://stackoverflow.com/questions/66171915
复制相似问题