Google OR-Tools使用 SCIP 求解器 - 如何访问求解器找到的中间解决方案?

问题描述

我是 Google OR-Tools 的新手。

我使用 Python 实现了一个以 SCIP 作为求解器的 MIP 模型。目标函数用于最小化 (solver.Minimize(C)),我通过 solver.Objective().Value() 访问最终解决方案。

但是,我还需要访问求解器找到的中间解决方,然后才能到达最终解决方案,以及它们的时间戳。 (最终目标是绘制出解决方案随时间演变的图表)。

我尝试使用 while 循环:

// route 
router.route("/:userId",protected,updateController)

const updateController = (req,res) => {
  const user = req.user; // this is the one generated by protected middleware
  const reqUserId = req.params.userId; // this is the one send by request
  
  if (user.id !== reqUserId) {
      // if two ids are not the same,it means someone is trying
      // to update the profile with the wrong token
      res.status(401);
  }

  // update profile in database
}

但它不起作用,因为 solver.Objective().Value() 只给出最终解决方案。

我已经挣扎了好几天了。谁能帮帮我吗?谢谢。

解决方法

在非 C++ 语言中,您无法访问求解器对象,并且在 Python 中无法访问现有回调。

您可以通过 SCIP 使用解决方案池。

只需运行 Solve(),然后在 NextSolution() 上循环。

如果您的问题纯粹是积分问题,您可以使用 CP-SAT 求解器(直接使用,而不是通过线性求解器包装器)。支持python中的解回调。