discrete_optimization.generic_tools.hub_solver.optal package
Submodules
discrete_optimization.generic_tools.hub_solver.optal.optalcp_tools module
- class discrete_optimization.generic_tools.hub_solver.optal.optalcp_tools.OptalCpSolver(problem: Problem, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs: Any)[source]
Bases:
CpSolver,BoundsProviderMixin,WarmstartFromPreviousRunMixin- add_allowed_assignments(variables: Sequence[Any], values: Iterable[Sequence[int]]) list[Any][source]
Enforce that variables evaluate to a sequence of values
- Parameters:
variables
values – possible sequences for variables values
- Returns:
created constraints
- add_at_least_one(exprs: Iterable[Any]) list[Any][source]
Enforce that at least one expression is true.
- Parameters:
exprs
Returns:
- add_at_most_one(exprs: Iterable[Any]) list[Any][source]
Enforce that at most one expression is true.
- Parameters:
exprs
Returns:
- add_bool_constraint(cstr: Any) Any[source]
Add boolean constraint on the underlying cp model.
- Parameters:
cstr – boolean constraint
- Returns:
the created constraint, linked to the model
- add_circuit(arcs: Sequence[tuple[int, int, Any]]) list[Any][source]
Adds Circuit(arcs).
Adds a circuit constraint from a sparse list of arcs that encode the graph.
A circuit is a unique Hamiltonian cycle in a subgraph of the total graph. In case a node ‘i’ is not in the cycle, then there must be a loop arc ‘i -> i’ associated with a true literal. Otherwise this constraint will fail.
- Parameters:
arcs – a list of arcs. An arc is a tuple (source_node, destination_node, literal). The arc is selected in the circuit if the literal is true. Both source_node and destination_node must be integers between 0 and the number of nodes - 1.
- Returns:
the generated constraints
- add_element(index: Any, expressions: Sequence[Any], target: Any) list[Any][source]
Adds the element constraint: expressions[index] == target.
- Parameters:
index – The index of the selected expression in the array.
expressions – A list of affine expressions.
target – The expression constrained to be equal to the selected expression.
Returns:
- add_exactly_one(exprs: Iterable[Any]) list[Any][source]
Enforce that exactly one expression is true.
- Parameters:
exprs
Returns:
- add_forbidden_assignments(variables: Sequence[Any], values: Iterable[Sequence[int]]) list[Any][source]
Forbid sequence variables to evaluate in a sequence of values.
A ForbiddenAssignments constraint is a constraint on an array of variables where the list of impossible combinations is provided in the tuples list.
- add_implication(left_bool_expr: Any, right_bool_expr: Any) list[Any][source]
Add implication constraint on the underlying cp model.
left_bool_expr => right_bool_expr
- or if left_bool_expr is an iterable
all(left_bool_expr) => right_bool_expr
- Parameters:
left_bool_expr – literal or iterable of literals
right_bool_expr – either a boolean expression or a constraint of the model or a list of such constraints
- Returns:
Implication constraints
- add_implication_at_least_one(left_bool_expr: Any, right_bool_exprs: Iterable[Any]) list[Any][source]
Add implication constraint left_bool_expr => any(right_bool_exprs)
- add_implication_bool_and(left_bool_expr: Any, right_bool_exprs: Sequence[Any]) list[Any][source]
Add implication constraint left_bool_expr => exactly_one(right_bool_exprs)
- add_implication_exactly_one(left_bool_expr: Any, right_bool_exprs: Iterable[Any]) list[Any][source]
Add implication constraint left_bool_expr => exactly_one(right_bool_exprs)
- add_implication_in_intervals(left_bool_expr: Any, right_expr: Any, intervals: Iterable[tuple[int, int]]) list[Any][source]
Add implication constraint left_bool_expr => in_intervals(target, intervals)
- add_max_equality(target: Any, variables: Iterable[Any]) list[Any][source]
Add constraint making target variable the maximum of given variables.
- Parameters:
target
variables
- Returns:
the constraints
- add_min_equality(target: Any, variables: Iterable[Any]) list[Any][source]
Add constraint making target variable the minimum of given variables.
- Parameters:
target
variables
- Returns:
the constraints
- add_modulo_equality(target: Any, expr: Any, mod: Any) list[Any][source]
Adds target = expr % mod.
It uses the C convention, that is the result is the remainder of the integral division rounded towards 0.
For example: * 10 % 3 = 1 * -10 % 3 = -1 * 10 % -3 = 1 * -10 % -3 = -1
- Parameters:
target – the target expression.
expr – the expression to compute the modulo of.
mod – the modulus expression.
Returns:
- add_multiple_circuit(arcs: Sequence[tuple[int, int, Any]]) list[Any][source]
Adds a multiple circuit constraint, aka the ‘VRP’ constraint.
The direct graph where arc #i (from tails[i] to head[i]) is present iff literals[i] is true must satisfy this set of properties: - #incoming arcs == 1 except for node 0. - #outgoing arcs == 1 except for node 0. - for node zero, #incoming arcs == #outgoing arcs. - There are no duplicate arcs. - Self-arcs are allowed except for node 0. - There is no cycle in this graph, except through node 0.
- Parameters:
arcs – a list of arcs. An arc is a tuple (source_node, destination_node, literal). The arc is selected in the circuit if the literal is true. Both source_node and destination_node must be integers between 0 and the number of nodes - 1.
- Returns:
the generated constraints
- cp_model: cp.Model | None = None
- create_bool_var(name: str) Any[source]
Create a boolean variable in the underlying cp model.
- Parameters:
min
max
name
- Returns:
created variable
- create_int_var(lb: int, ub: int, name: str) Any[source]
Create an integer variable in the underlying cp model.
- Parameters:
lb – lower bound
ub – upper bound
name
- Returns:
created variable
- create_int_var_from_flat_intervals(flat_intervals: list[int], name: str) Any[source]
Create an integer variable in the underlying cp model.
- Parameters:
flat_intervals – [lb0, ub0, lb1, ub1, …]
name
The created variable takes values in the union of intervals [lb0, ub0] U [lb1, ub1] U …
- Returns:
created variable
- create_int_var_from_possible_values(possible_values: Iterable[int], name: str) Any[source]
Create an integer variable in the underlying cp model.
- Parameters:
possible_values
name
- Returns:
created variable
- current_bound: int | float | None = None
- current_obj: int | float | None = None
- current_solution: cp.Solution | None = None
- early_stopping_exception: Exception | None = None
- hyperparameters = [CategoricalHyperparameter(name='searchType', default='Auto', depends_on=None, name_in_kwargs='searchType')]
Hyperparameters available for this solver.
- These hyperparameters are to be feed to **kwargs found in
__init__()
init_model() (when available)
solve()
- init_model(**args: Any) None[source]
Instantiate a CP model instance
Afterwards, self.instance should not be None anymore.
- abstractmethod retrieve_solution(result: cp.Solution) Solution[source]
Construct a d-o solution from OptalCP solver solution.
- Parameters:
solution – OptalCP solution
- Returns:
d-o solution
- solve(callbacks: list[Callback] | None = None, time_limit: int | None = 60, parameters_cp: ParametersCp | None = None, do_not_retrieve_solutions: bool = False, **args: Any) ResultStorage[source]
Solve the cp model
- Parameters:
callbacks
time_limit
parameters_cp
do_not_retrieve_solutions – if True, do no try to retrieve solutions. Useful when using preview version of optalcp.
**args
Returns:
- status_solver: StatusSolver = None
- sum(exprs: Iterable[Any]) Any[source]
Construct integer expression corresponding to sum of variables or expressions.
- use_warm_start: bool = False
- warm_start_solution: cp.Solution | None = None
- class discrete_optimization.generic_tools.hub_solver.optal.optalcp_tools.OptalSolutionCallback(do_solver: OptalCpSolver, optal_solver: cp.Solver, callback: Callback, do_not_retrieve_solutions: bool = False)[source]
Bases:
object- current_solve_result: cp.SolveResult | None
- discrete_optimization.generic_tools.hub_solver.optal.optalcp_tools.run_async_synchronously(coroutine: Awaitable[T]) T[source]
Runs a coroutine synchronously, blocking until it completes.
This helper handles two cases: 1. No event loop is running (standard script): Uses asyncio.run() 2. An event loop is running (e.g., Jupyter): Runs the coroutine in a
separate thread to avoid blocking the existing loop/raising runtime errors.