Google OR-Tools 库:类型错误 GetOrMakeIndex
Google OR-Tools library: Type Error GetOrMakeIndex
我正在研究 google OR-Tools 库,我遇到了这个我无法理解的问题。
首先,如果我将 nqueens 示例中的代码复制并粘贴到 official guide.
中,我会得到同样的错误
我以经典的 n 皇后问题为例:
def main(board_size):
#1) Declare CP-SAT model
model = cp_model.CpModel()
#2) Create the variables
# queens[j]=i --> j = colonna, i (valore) = riga
queens = [model.NewIntVar(0, board_size - 1, "queens%i" %i) for i in range(board_size)]
#3) Constraints
# Row constraint
model.AddAllDifferent(queens)
# Diagonal constraint
model.AddAllDifferent([queens[j] - j for j in range(board_size)])
model.AddAllDifferent([queens[j] + j for j in range(board_size)])
#4) Call the solver and display the results
solver = cp_model.CpSolver()
solution_printer = NQueenSolutionPrinter(queens)
solver.parameters.enumerate_all_solutions = True
solver.Solve(model, solution_printer)
if __name__ == '__main__':
size = 4
if len(sys.argv) > 1:
size = int(sys.argv[1])
main(size)
控制台给我这个错误:
Traceback (most recent call last):
File "C:\Users\W\.spyder-py3\temp.py", line 90, in <module>
main(size)
File "C:\Users\W\.spyder-py3\temp.py", line 67, in main
model.AddAllDifferent([queens[j] - j for j in range(board_size)])
File "C:\ProgramData\Anaconda3\lib\site-packages\ortools\sat\python\cp_model.py", line 926, in AddAllDifferent
[self.GetOrMakeIndex(x) for x in variables])
File "C:\ProgramData\Anaconda3\lib\site-packages\ortools\sat\python\cp_model.py", line 926, in <listcomp>
[self.GetOrMakeIndex(x) for x in variables])
File "C:\ProgramData\Anaconda3\lib\site-packages\ortools\sat\python\cp_model.py", line 1583, in GetOrMakeIndex
raise TypeError('NotSupported: model.GetOrMakeIndex(' + str(arg) +
TypeError: NotSupported: model.GetOrMakeIndex((queens0))
因此,查看 GetOrMakeIndex 方法的定义:
def GetOrMakeIndex(self, arg):
"""Returns the index of a variable, its negation, or a number."""
if isinstance(arg, IntVar):
return arg.Index()
elif (isinstance(arg, _ProductCst) and
isinstance(arg.Expression(), IntVar) and arg.Coefficient() == -1):
return -arg.Expression().Index() - 1
elif isinstance(arg, numbers.Integral):
cp_model_helper.AssertIsInt64(arg)
return self.GetOrMakeIndexFromConstant(arg)
else:
raise TypeError('NotSupported: model.GetOrMakeIndex(' + str(arg) +
我想到变量无法识别它作为参数接收的变量类型。知道如何解决这个问题吗?
这需要最新版本的库。
你用的是哪一个?
我正在研究 google OR-Tools 库,我遇到了这个我无法理解的问题。 首先,如果我将 nqueens 示例中的代码复制并粘贴到 official guide.
中,我会得到同样的错误我以经典的 n 皇后问题为例:
def main(board_size):
#1) Declare CP-SAT model
model = cp_model.CpModel()
#2) Create the variables
# queens[j]=i --> j = colonna, i (valore) = riga
queens = [model.NewIntVar(0, board_size - 1, "queens%i" %i) for i in range(board_size)]
#3) Constraints
# Row constraint
model.AddAllDifferent(queens)
# Diagonal constraint
model.AddAllDifferent([queens[j] - j for j in range(board_size)])
model.AddAllDifferent([queens[j] + j for j in range(board_size)])
#4) Call the solver and display the results
solver = cp_model.CpSolver()
solution_printer = NQueenSolutionPrinter(queens)
solver.parameters.enumerate_all_solutions = True
solver.Solve(model, solution_printer)
if __name__ == '__main__':
size = 4
if len(sys.argv) > 1:
size = int(sys.argv[1])
main(size)
控制台给我这个错误:
Traceback (most recent call last):
File "C:\Users\W\.spyder-py3\temp.py", line 90, in <module>
main(size)
File "C:\Users\W\.spyder-py3\temp.py", line 67, in main
model.AddAllDifferent([queens[j] - j for j in range(board_size)])
File "C:\ProgramData\Anaconda3\lib\site-packages\ortools\sat\python\cp_model.py", line 926, in AddAllDifferent
[self.GetOrMakeIndex(x) for x in variables])
File "C:\ProgramData\Anaconda3\lib\site-packages\ortools\sat\python\cp_model.py", line 926, in <listcomp>
[self.GetOrMakeIndex(x) for x in variables])
File "C:\ProgramData\Anaconda3\lib\site-packages\ortools\sat\python\cp_model.py", line 1583, in GetOrMakeIndex
raise TypeError('NotSupported: model.GetOrMakeIndex(' + str(arg) +
TypeError: NotSupported: model.GetOrMakeIndex((queens0))
因此,查看 GetOrMakeIndex 方法的定义:
def GetOrMakeIndex(self, arg):
"""Returns the index of a variable, its negation, or a number."""
if isinstance(arg, IntVar):
return arg.Index()
elif (isinstance(arg, _ProductCst) and
isinstance(arg.Expression(), IntVar) and arg.Coefficient() == -1):
return -arg.Expression().Index() - 1
elif isinstance(arg, numbers.Integral):
cp_model_helper.AssertIsInt64(arg)
return self.GetOrMakeIndexFromConstant(arg)
else:
raise TypeError('NotSupported: model.GetOrMakeIndex(' + str(arg) +
我想到变量无法识别它作为参数接收的变量类型。知道如何解决这个问题吗?
这需要最新版本的库。 你用的是哪一个?