如何访问google或tools内部变量?

2024-03-28 21:50:53 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我用google或tools运行一个混合整数或线性规划问题,我想提取工具或解算器使用的内部变量。你知道吗

我正在使用python,并且已经查看了google优化示例、指南和引用(https://developers.google.com/optimization/)以及存储代码的GitHub(https://github.com/google/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,其中我在代码中输入的约束被编码在变量Ab中。我感兴趣的是提取这些变量(或者解算器用来解决问题的任何其他变量),将它们作为初始输入 进一步计算。你知道吗

[作为参考:我还在or-tools forum中发布了这个问题。]


Tags: or工具the代码httpscom示例main