Model (cpmpy.model)
The Model class is a lazy container for constraints and an objective function.
Constraints and objectives are CPMpy expressions.
It is lazy in that it only stores the constraints and objective that are added
to it. Processing only starts when solve() is called, and this does not modify
the constraints or objective stored in the model.
A model can be solved multiple times, and constraints can be added inbetween solve calls.
Note that constraints are added using the += operator (implemented by __add__()).
See the full list of functions below.
List of classes
CPMpy Model object, contains the constraint and objective expressions |
- class cpmpy.model.Model(*args, minimize=None, maximize=None)[source]
CPMpy Model object, contains the constraint and objective expressions
- __init__(*args, minimize=None, maximize=None)[source]
Arguments of constructor:
- Parameters:
*args – Expression object(s) or list(s) of Expression objects
minimize – Expression object representing the objective to minimize
maximize – Expression object representing the objective to maximize
At most one of minimize/maximize can be set, if none are set, it is assumed to be a satisfaction problem
- add(con)[source]
Add one or more constraints to the model.
- Parameters:
con (Expression or list) – Expression object(s) or list(s) of Expression objects representing constraints
- Returns:
Returns self to allow for method chaining
- Return type:
Example
m = Model() m += [x > 0]
- __add__(con)
Add one or more constraints to the model.
- Parameters:
con (Expression or list) – Expression object(s) or list(s) of Expression objects representing constraints
- Returns:
Returns self to allow for method chaining
- Return type:
Example
m = Model() m += [x > 0]
- minimize(expr)[source]
Minimize the given objective function
minimize() can be called multiple times, only the last one is stored
- maximize(expr)[source]
Maximize the given objective function
maximize() can be called multiple times, only the last one is stored
- objective(expr, minimize)[source]
Users will typically use
minimize()ormaximize()to set the objective function, this is the generic implementation for both.- Parameters:
expr (Expression) – the CPMpy expression that represents the objective function
minimize (bool) – whether it is a minimization problem (True) or maximization problem (False)
‘objective()’ can be called multiple times, only the last one is stored
- has_objective()[source]
Check if the model has an objective function
- Returns:
True if the model has an objective function, False otherwise
- Return type:
bool
- objective_value()[source]
Returns the value of the objective function of the last solver run on this model
- Returns:
an integer or ‘None’ if it is not run or is a satisfaction problem
- solve(solver: str | None = None, time_limit: int | float | None = None, **kwargs)[source]
Send the model to a solver and get the result.
Run
SolverLookup.solvernames()to find out the valid solver names on your system. (default: None = first available solver)- Parameters:
solver (string or a name in SolverLookup.solvernames() or a SolverInterface class (Class, not object!), optional) – name of a solver to use.
time_limit (int or float, optional) – time limit in seconds
- Returns:
the computed output:
True if a solution is found (not necessarily optimal, e.g. could be after timeout)
False if no solution is found
- Return type:
bool
- solveAll(solver: str | None = None, display: Expression | List[Expression] | Callable | None = None, time_limit: int | float | None = None, solution_limit: int | None = None, **kwargs)[source]
Compute all solutions and optionally display the solutions.
If no solution is found, the solver status will be ‘Unsatisfiable’. If at least one solution was found and the solver exhausted all possible solutions, the solver status will be ‘Optimal’, otherwise ‘Feasible’.
- 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)
- Returns:
number of solutions found (within the time and solution limit)
- Return type:
int
- status()[source]
Returns the status of the latest solver run on this model
Status information includes exit status (optimality) and runtime.
- Returns:
an object of
SolverStatus
- to_file(fname)[source]
Serializes this model to a
.pickleformat- Parameters:
fname (FileDescriptorOrPath) – Filename of the resulting serialized model