-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use ortool's global constraints which are not integrated by CPMpy? #74
Comments
Hi Hakan, I would like to support
which should then automatically be translated in CPM_ortools into
This is TODO. On a side-note, it is possible to do it more directly interleaved with a slight change of style:
with
|
Thanks for the suggestion about the rewriting of the model. |
OK, so we reworked the solver interfaces a few months back, and the above approach (s.ort_model.Add...) is now documented, with 's.solver_var(x)' the generic way for all solvers to translate CPMpy variables to solver-specific variables. But our discussion here actually went quite further. I've implemented a prototype of it in #121 I'll copy paste the example here from cpmpy import *
from cpmpy.expressions.globalconstraints import NativeConstraint
x = intvar(0,10, shape=3)
# CPMpy standard
s = SolverLookup.get("ortools")
s += Table(x, [[0,1,2],[2,1,0]])
print(s.solveAll())
# CPMpy with current 'direct solver access' use
s = SolverLookup.get("ortools")
s.ort_model.AddAllowedAssignments(s.solver_vars(x), [[0,1,2],[2,1,0]])
print(s.solveAll())
# Proposal in branch
s = SolverLookup.get("ortools")
s += NativeConstraint("AddAllowedAssignments", (x, [[0,1,2],[2,1,0]]))
print(s.solveAll())
# Proposal also allows to indicate some args are const/novar/should be passed as is
# the top two variants (now) also do not scan the data part
s = SolverLookup.get("ortools")
s += NativeConstraint("AddAllowedAssignments", (x, [[0,1,2],[2,1,0]]), arg_novar=[1])
print(s.solveAll()) (the reason for a new NativeConstraint class instead of reusing GlobalConstraint is that the latter requires a decompose function to be generic, while the former clearly does not; also allows us to add the arg_novar property.) What do you think? Feedback welcome... |
Hi @hakank, I'm happy to report that we recently merged and released a new CPMpy version with 'DirectConstraint', which started in this discussion! (it is just a rename of NativeConstraint, but then fully implemented, documented and tested for all solvers). See the documentation and links to ortools examples (automaton, circuit, multicircuit) here: and the nonogram example implemented in a notebook here: We'd be happy for you to experiment with it and give feedback! (we are also working on a DirectVariable for a similar generic access to custom solver variables like OrTools' IntervalVars, that is very much WIP, discussion and code in #121 ) |
The ortools CP-SAT solver has quite a few constraints that's not (yet) integrated in CPMpy, for example; AddAutomaton, AddCumulative, AddForbiddenAssignments, AddInverse, AddNoOverlap, AddNoOverlap2D, AddReservoirConstraint.
It would be great to be able to use these constraints even if they are now defined in globalconstraints.py and/or ortools.py.
There are two methods in principles. Here I use
AddAutomaton
as an example (since I want to use it in my Nonogram solver :-)).First, the approach that was suggested by Tias in a mail exchange a while ago. For a simple model it works by adding the constraint to the solver object, e.g.
This works and is fast. Note that is use the
CPM_ortools
object and one have to translate the CPMpy variables to ORtools variables (thess.varmap
step).However, for larger models this can be quite messy. It would be really nice if one could use the constraint as any other constraint, i.e. using via
model += [AddAutomaton(...)]
and without the step to translate the CPMpy variables.For a "real" - or at least more complex example - see my Nonogram solver (http://hakank.org/cpmpy/nonogram_regular.py ) where I call
regular
several times in a loop over the hints:One way would be that a user could defined this via some interface, e.g.
Or, perhaps simply use the ortools constraint directly, which would be neater:
I don't expect that CPMpy should have to do any check specific checks in these cases. Instead it's the user's responsibility to ensure that all variables are properly used (and be prepared to get strange error messages if not).
The text was updated successfully, but these errors were encountered: