如果我使用google or-tools运行混合整数或线性编程问题,我想提取or-tools求解器使用的内部变量。
我正在使用python,并且已经查看了google优化示例,指南和参考(https://developers.google.com/optimization/)以及存储代码(https://github.com/google/or-tools)的GitHub,但是没有找到任何可以返回or-tools用于解决优化问题的变量。
为了更加清楚,我们可以从OR Tools中获取介绍示例:
from __future__ import print_function
from ortools.linear_solver import pywraplp
def main():
solver = pywraplp.Solver('SolveSimpleSystem',
pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
# Create the variables x and y.
x = solver.NumVar(0, 1, 'x')
y = solver.NumVar(0, 2, 'y')
# Create the objective function, x + y.
objective = solver.Objective()
objective.SetCoefficient(x, 1)
objective.SetCoefficient(y, 1)
objective.SetMaximization()
# Call the solver and display the results.
solver.Solve()
print('Solution:')
print('x = ', x.solution_value())
print('y = ', y.solution_value())
if __name__ == '__main__':
main()
如果我没记错的话,在执行solver.Solve()
之后,求解器将使用一些约束Ax = b
,其中我在代码中输入的约束都编码在变量A
中和b
。我有兴趣提取这些变量(或求解器用来解决问题的任何其他变量),以将它们用作
进一步的计算。
[作为参考:我也将问题发布在or-tools forum上。]