或工具Constraint_SetCoefficient返回NULL而不设置错误

问题描述

我一般是python的新手,特别是在OR工具中,用于优化和我尝试运行的任何模型都出现此错误

errorr

从完成的研究来看,它可能与ortools本身有关,并且id必须更改其代码中的某些内容,但我不确定。有什么想法吗?

解决方法

我也遇到了这个问题,结果证明,根据 the documentation,我试图为“不属于求解器”的变量设置系数。

enter image description here

例如下面的代码(借用自this example)重现了这个问题:

from ortools.linear_solver import pywraplp
solver = pywraplp.Solver.CreateSolver('GLOP')
ct = solver.Constraint(0,2,'ct')
x = None # This is a contrived bug
ct.SetCoefficient(x,1)

x 的值是错误的,它需要类似于 x = solver.NumVar(0,1,'x') 以便 solver 在我们尝试对其设置系数之前知道 x

您不太可能犯我在我的代码示例中设计的错误,但可能在代码中的某个地方您混淆了变量,并且您发送了错误的 SetCoefficient 值参数,导致它用 SystemError: <built-in function Constraint_SetCoefficient> returned NULL without setting an error 出错。