CPMpy Hexaly interface (cpmpy.solvers.hexaly)
Interface to Hexaly’s API
Hexaly is a global optimization solver that supports nonlinear and a few global constraints.
Always use cp.SolverLookup.get("hexaly") to instantiate the solver object.
Installation
Requires that the ‘hexaly’ python package is installed:
$ pip install hexaly -i https://pip.hexaly.com
It also requires to install the Hexaly Optimizer with a Hexaly license (for example a free academic license) You can read more about available licences at https://www.hexaly.com/
See detailed installation instructions at: https://www.hexaly.com/docs/last/installation/pythonsetup.html
The rest of this documentation is for advanced users.
List of classes
Interface to Hexaly's API |
- class cpmpy.solvers.hexaly.CPM_hexaly(cpm_model=None, subsolver=None)[source]
Interface to Hexaly’s API
Creates the following attributes (see parent constructor for more):
hex_model: object, Hexaly’s model object
hex_solver: object, Hexaly’s solver object (to solve hex_model)
Documentation of the solver’s own Python API: https://www.hexaly.com/docs/last/pythonapi/index.html
- add(cpm_expr_orig)[source]
Eagerly add a constraint to the underlying solver.
Any CPMpy expression given is immediately transformed (through transform()) and then posted to the solver in this function.
This can raise ‘NotImplementedError’ for any constraint not supported after transformation
The variables used in expressions given to add are stored as ‘user variables’. Those are the only ones the user knows and cares about (and will be populated with a value after solve). All other variables are auxiliary variables created by transformations.
- Parameters:
cpm_expr (Expression or list of Expression) – CPMpy expression, or list thereof
- Returns:
self
- get_core()
For use with
s.solve(assumptions=[...]). Only meaningful if the solver returned UNSAT.Typically implemented in SAT-based solvers
Returns a small subset of assumption literals that are unsat together. (a literal is either a
_BoolVarImplor aNegBoolViewin case of its negation, e.g. x or ~x) Setting these literals to True makes the model UNSAT, setting any to False makes it SAT
- maximize(expr)
Post the given expression to the solver as objective to maximize
maximize() can be called multiple times, only the last one is stored
- minimize(expr)
Post the given expression to the solver as objective to minimize
minimize() can be called multiple times, only the last one is stored
- property native_model
Returns the solver’s underlying native model (for direct solver access).
- objective(expr, minimize=True)[source]
Post the given expression to the solver as objective to minimize/maximize
‘objective()’ can be called multiple times, only the last one is stored
(technical side note: any constraints created during conversion of the objective
are permanently posted to the solver)
- objective_value()
Returns the value of the objective function of the latest solver run on this model
- Returns:
an integer or ‘None’ if it is not run, or a satisfaction problem
- solution_hint(cpm_vars: List[_NumVarImpl], vals: List[int | bool])[source]
For warmstarting the solver with a variable assignment
Typically implemented in SAT-based solvers
- Parameters:
cpm_vars – list of CPMpy variables
vals – list of (corresponding) values for the variables
- solve(time_limit: float | None = None, solution_callback=None, **kwargs)[source]
Call the Hexaly solver
- Parameters:
time_limit – maximum solve time in seconds (float, optional)
kwargs – any keyword argument, sets parameters of solver object
Arguments that correspond to solver parameters:
nb_threads: number of threads used to parallelize the search.
iteration_limit: max number of iterations
verbosity: verbosity level
full list of parameters availble at: https://www.hexaly.com/docs/last/pythonapi/optimizer/hxparam.html
- solveAll(display: Expression | List[Expression] | Callable | None = None, time_limit: float | None = None, solution_limit: int | None = None, call_from_model=False, **kwargs)[source]
A shorthand to (efficiently) compute all solutions, map them to CPMpy and optionally display the solutions.
- Parameters:
display – either a list of CPMpy expressions, OR a callback function, called with the variables after value-mapping default/None: nothing displayed
solution_limit – stop after this many solutions (default: None)
time_limit (float) – maximum solve time in seconds
- Returns:
number of solutions found
Note
Hexaly does not support exhaustive search to find all solutions. Set time_limit to do a limited search.
- solver_var(cpm_var)[source]
Creates solver variable for cpmpy variable or returns from cache if previously created
- solver_vars(cpm_vars)
Like solver_var() but for arbitrary shaped lists/tensors
- status()
- static supported()[source]
Check for support in current system setup. Return True if the system has package installed or supports solver, else returns False.
- Returns:
Solver support by current system setup.
- Return type:
[bool]
- supported_global_constraints: frozenset[str] = frozenset({'abs', 'div', 'element', 'max', 'min', 'mod', 'pow'})
- supported_reified_global_constraints: frozenset[str] = frozenset({})
- transform(cpm_expr)[source]
Transform arbitrary CPMpy expressions to constraints the solver supports
Implemented through chaining multiple solver-independent transformation functions from the cpmpy/transformations/ directory.
See the Adding a new solver docs on readthedocs for more information.
- Parameters:
cpm_expr (Expression or list of Expression) – CPMpy expression, or list thereof
- Returns:
list of Expression
- class cpmpy.solvers.hexaly.HexSolutionPrinter(solver, display=None, solution_limit=None, verbose=False)[source]
Native Hexaly callback for solution printing.
Use with
CPM_hexalyas follows:cb = HexSolutionPrinter(s, display=vars) s.solve(solution_callback=cb)
For multiple variables (single or NDVarArray), use:
For a custom print function, use for example:
def myprint(): print(f"x0={x[0].value()}, x1={x[1].value()}") cb = HexSolutionPrinter(s, display=myprint)
Optionally retrieve the solution count with
cb.solution_count().- Parameters:
verbose (bool, default = False) – whether to print info on every solution found
display – either a list of CPMpy expressions, OR a callback function, called with the variables after value-mapping default/None: nothing displayed
solution_limit (default = None) – stop after this many solutions