
Hi,
I think perhaps you cannot multiply two IntVars directly.
First, looking at the python API, I see
def __mul__(self, arg):
arg = cmh.assert_is_a_number(arg)
if cmh.is_one(arg):
return self
elif cmh.is_zero(arg):
return 0
return _ProductCst(self, arg)which maybe is the assert you are hitting?
Second, reading the docs on CPSAT here: https://github.com/google/or-tools/blob/stable/ortools/sat/docs/boolean_logic.md#product-of-two-boolean-variables
There is a long section on a trick for multiplying two boolean variables.
So probably you cannot just multiply the variables, but instead need to create new booleans for each multiplication and set it up properly using the channeling constraints.
In your case this is a bit of a pain, but sometimes programming is all about tedium to avoid more tedium.
Maybe this will work:
for n in range(roomsorcells_idunno):
for k in range(self.num_cells):
pnk = self.position_matrix[n, k]
for j in range(k, self.num_cells):
pnj = self.position_matrix[n, j]
ajk = self.adjacency_matrix[j, k]
# copy paste coding
ppjk = model.NewBoolVar(f"j={j} and k={k} in same room")
# x and y implies p, rewrite as not(x and y) or p
model.AddBoolOr(pnk.Not(), pnj.Not(), ppjk)
# p implies x and y, expanded into two implication
model.AddImplication(ppjk, pnk)
model.AddImplication(ppjk, pnj)
# then do it all again? Is there an easier way with AddBoolOr? Probably should create the full truth table and use that
# copy paste again
appjk = model.NewBoolVar(f"j={j} and k={k} in same room and adjacent")
# x and y implies p, rewrite as not(x and y) or p
model.AddBoolOr(ajk.Not(), ppjk.Not(), appjk)
# p implies x and y, expanded into two implication
model.AddImplication(appjk, ajk)
model.AddImplication(appjk, ppjk)Probably a smarter idea is to write out the full truth table for the triple multiplication (similar to what is done in the docs page linked above) and then be more clever about the implications, rather than doing the cargo cult double multiply I do above.
Hope that helps,
James
Ah much cleaner. I'm still climbing the API learning curve on the CP-SAT side of the solver. I was hoping if I piped up something dumb someone else would improve it.
James
Ah much cleaner. I'm still climbing the API learning curve on the CP-SAT side of the solver. I was hoping if I piped up something dumb someone else would improve it.
James
--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/YnvObGN9pf65VbWn%40b3493d06ae14.

To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/991dcf81-b562-41b6-90a9-42c7311c8d81n%40googlegroups.com.