Model (cpmpy.Model)

The Model class is a lazy container for constraints and an objective function.

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 to it inbetween solve calls.

See the examples for basic usage, which involves:

  • creation, e.g. m = Model(cons, minimize=obj)

  • solving, e.g. m.solve()

  • optionally, checking status/runtime, e.g. m.status()

List of classes

Model

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

copy()[source]

Makes a shallow copy of the model. Constraints and variables are shared among the original and copied model.

deepcopy(memodict={})[source]

Deep copies a the model to a new instance. :return: an object of :class: ‘Model’ with equivalent constraints as the current model. There are no shared variables/constraints between the original model and its copied version.

static from_file(fname)[source]

Reads a Model instance from a binary pickled file

Returns

an object of :class: Model

maximize(expr)[source]

Maximize the given objective function

maximize() can be called multiple times, only the last one is stored

minimize(expr)[source]

Minimize the given objective function

minimize() can be called multiple times, only the last one is stored

objective(expr, minimize)[source]

Post the given expression to the solver as objective to minimize/maximize

  • 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

objective_value()[source]

Returns the value of the objective function of the latste solver run on this model

Returns

an integer or ‘None’ if it is not run, or a satisfaction problem

solve(solver=None, time_limit=None)[source]

Send the model to a solver and get the result

Parameters
  • solver – name of a solver to use. Run SolverLookup.solvernames() to find out the valid solver names on your system. (default: None = first available solver)

  • time_limit (int or float) – optional, time limit in seconds

Returns

Bool: the computed output: - True if a solution is found (not necessarily optimal, e.g. could be after timeout) - False if no solution is found

solveAll(solver=None, display=None, time_limit=None, solution_limit=None)[source]

Compute all solutions and optionally display the solutions.

Delegated to the solver, who might implement this efficiently

Arguments:
  • 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

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 .pickle format

Param

fname: Filename of the resulting serialized model