如何使用 OR-Tools 获得对偶问题,已使用 solver.Add() 添加约束
How to obtain the dual problem with OR-Tools, having used solver.Add() to add constraints
我已按照此教程使用 or-tools 解决 MIP:https://developers.google.com/optimization/mip/integer_opt
代码如下:
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 但它们没有以相同的方式实现约束,我宁愿不必重写整个代码(现在已经很长了)。
对偶变量没有暴露。我什至不确定它们是被创造出来的。
此外,对偶值仅适用于纯 LP,不适用于 MIP。
我已按照此教程使用 or-tools 解决 MIP:https://developers.google.com/optimization/mip/integer_opt
代码如下:
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 但它们没有以相同的方式实现约束,我宁愿不必重写整个代码(现在已经很长了)。
对偶变量没有暴露。我什至不确定它们是被创造出来的。 此外,对偶值仅适用于纯 LP,不适用于 MIP。