Reputation: 155
I'm studying the google OR-Tools library and I came across this problem that I can't understand. First of all, I get the same error if I copy and paste the code from the nqueens example in the official guide.
I propose as an example the classic n-queens problem:
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)
The console gives me this error:
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))
So, looking at the definition of the GetOrMakeIndex method:
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) +
it comes to my mind that the variable does not recognise the type of variable it receives as an argument. Any idea how to solve the problem?
Upvotes: 1
Views: 524
Reputation: 11034
This requires a recent version of the library. Which one are you using ?
Upvotes: 1