Module docplex.cp.solution

This module contains the different elements representing the result to a solver request. The main classes are:

  • CpoRunResult: root class for all results returned by the solver. It contains general purpose information such as the model that has been solved, solver parameters, solver info and log.
  • CpoSolveResult: result of a solve request, including a solution to the model if any.
  • CpoRefineConflictResult: result of a refine conflict request.

These classes are using the following classes to store utility information:

  • CpoSolverInfos: miscellaneous information coming from the solver.
  • CpoProcessInfos: miscellaneous information about the processing and solving of the model by the Python API.

When a solution is found by the solver, it is represented by the following classes:

These solution objects can be used in multiple ways:

  • To represent a complete (fully instantiated) solution, where each model has a unique fixed value, as returned by a successful model solve.
  • To represent a partial model solution, that is proposed as a solve starting point (see docplex.cp.model.CpoModel.set_starting_point()) In this case, not all variables are present in the solution, and some of them may be partially instantiated.
  • To represent a partial model solution that is returned by the solver as result of calling method docplex.cp.solver.solver.CpoSolver.propagate().

Detailed description

class docplex.cp.solution.CpoFloatVarSolution(expr, value)[source]

Bases: docplex.cp.solution.CpoVarSolution

Constructor

Parameters:
  • expr – Variable expression, object of class CpoFloatVar. None if unknown.
  • value – Variable value, or domain if not completely instantiated
get_domain_max()[source]

Gets the domain upper bound.

Returns:Domain upper bound.
get_domain_min()[source]

Gets the domain lower bound.

Returns:Domain lower bound.
get_value()[source]

Gets the value of the variable.

Returns:Variable value (number), or range as a tuple (min, max)
class docplex.cp.solution.CpoIntVarSolution(expr, value)[source]

Bases: docplex.cp.solution.CpoVarSolution

This class represents a solution to an integer variable.

The solution can be:
  • complete when the value is a single integer,
  • partial when the value is a domain, set of multiple values.

A domain is a list of discrete integer values and/or intervals of values represented by a tuple containing interval min and max values (included).

For example, following are valid domains for an integer variable:
  • 7 (complete solution)
  • (1, 2, 4, 9)
  • (2, 3, (5, 7), 9, (11, 13))

Constructor

Parameters:
  • expr – Variable expression, object of class CpoIntVar. None if unknown.
  • value – Variable value, or domain if not completely instantiated
domain_contains(value)[source]

Check whether a given value is in the domain of the variable

Parameters:val – Value to check
Returns:True if the value is in the domain, False otherwise
domain_iterator()[source]

Iterator on the individual values of an integer variable domain.

Returns:Value iterator on the domain of this variable.
get_domain_max()[source]

Gets the domain upper bound.

Returns:Domain upper bound.
get_domain_min()[source]

Gets the domain lower bound.

Returns:Domain lower bound.
get_value()[source]

Gets the value of the variable.

Returns:Variable value (integer), or domain (list of integers or intervals)
class docplex.cp.solution.CpoIntervalVarSolution(expr, presence=None, start=None, end=None, size=None, length=None)[source]

Bases: docplex.cp.solution.CpoVarSolution

This class represents a solution to an interval variable.

The solution can be complete if all attribute values are integers, or partial if at least one of them is an interval expressed as a tuple.

Constructor

Parameters:
  • expr – Variable expression, object of class CpoIntervalVar. None if unknown.
  • presence – Presence indicator (True for present, False for absent, None for undetermined). Default is None.
  • start – Value of start, or tuple representing the start range. Default is None.
  • end – Value of end, or tuple representing the end range. Default is None.
  • size – Value of size, or tuple representing the size range. Default is None.
  • length – Value of the length, or tuple representing the length range. Default is None. Not to be used if other values are integers.
get_end()[source]

Gets the interval end.

To get the bounds of the returned domain, in particular if not fixed, use methods docplex.cp.expression.get_domain_min() and docplex.cp.expression.get_domain_max()

Returns:Interval end value, or domain (tuple (min, max)) if not fully instantiated. None if interval is absent.
get_length()[source]

Gets the length of the interval.

Length of the interval is the difference between end and start.

To get the bounds of the returned domain, in particular if not fixed, use methods docplex.cp.expression.get_domain_min() and docplex.cp.expression.get_domain_max()

Returns:Interval length value, or domain (tuple (min, max)) if not fully instantiated. None if interval is absent.
get_size()[source]

Gets the size of the interval.

The size of the interval is the amount of work done in the interval, that depends on the intensity function that has been associated to the interval.

To get the bounds of the returned domain, in particular if not fixed, use methods docplex.cp.expression.get_domain_min() and docplex.cp.expression.get_domain_max()

Returns:Interval size value, or domain (tuple (min, max)) if not fully instantiated. None if interval is absent.
get_start()[source]

Gets the interval start.

To get the bounds of the returned domain, in particular if not fixed, use methods docplex.cp.expression.get_domain_min() and docplex.cp.expression.get_domain_max()

Returns:Interval start value, or domain (tuple (min, max)) if not fully instantiated. None if interval is absent.
get_value()[source]

Gets the interval variable value as a tuple (start, end, size), or () if absent.

If the variable is absent, then the result is an empty tuple.

If the variable is fully instantiated, the result is a tuple of 3 integers (start, end, size). The variable length, easy to compute as end - start, can also be retrieved by calling get_length().

If the variable is partially instantiated, the result is a tuple (start, end, size, length) where each individual value can be an integer or an interval expressed as a tuple.

Returns:Interval variable value as a tuple.
is_absent()[source]

Check if the interval is absent.

Returns:True if interval is absent, False otherwise.
is_optional()[source]

Check if the interval is optional. Calling this function returns always False for a complete solution where Valid only for a partial solution, meaning that the status of the variable is not yet fixed.

Returns:True if interval is optional (undetermined), False otherwise.
is_present()[source]

Check if the interval is present.

Returns:True if interval is present, False otherwise.
class docplex.cp.solution.CpoModelSolution[source]

Bases: object

This class represents a solution to the problem represented by the model. It contains the solutions for the model variables plus the value of the objective(s), if any.

Each variable solution can be accessed using its name, or the variable object of the model. The solution is either CpoIntVarSolution, CpoIntervalVarSolution, CpoSequenceVarSolution or CpoStateFunctionSolution depending on the type of the variable.

A variable solution can be accessed in two ways:

  • using the method CpoModelSolution.get_var_solution(), that returns an object representing the solution to the variable, or None if the variable is not in the solution.
  • using the standard Python expression sol[<var>] that does the same but raises a KeyError exception if the variable is not in the solution.
Depending if the request to solver was a solve or a propagate, the solution can be:
  • complete, if each variable is assigned to a single value,
  • partial if not all variables are defined, or if some variables are defined with domains that are not restricted to a single value.

An instance of this class may also be created explicitly by the programmer of the model to express a starting point that can be passed to the model to optimize its solve (see docplex.cp.model.CpoModel.set_starting_point() for details).

add_integer_var_solution(var, value)[source]

Add a new integer variable solution.

The solution can be complete if the value is a single integer, or partial if the value is a domain, given as a list of integers or intervals expressed as tuples.

Parameters:
  • var – Variable expression, object of class CpoIntVar.
  • value – Variable value, or domain if not completely instantiated
add_interval_var_solution(var, presence=None, start=None, end=None, size=None, length=None)[source]

Add a new interval variable solution.

The solution can be complete if all attribute values are integers, or partial if at least one of them is an interval expressed as a tuple.

Parameters:
  • var – Variable expression, object of class CpoIntervalVar.
  • presence – Presence indicator (true for present, false for absent, None for undetermined). Default is None.
  • start – Value of start, or tuple representing the start range
  • end – Value of end, or tuple representing the end range
  • size – Value of size, or tuple representing the size range
  • length – Value of the length, or tuple representing the length range. Default is None. Not to be used if other values are integers.
add_kpi_value(name, value)[source]

Add a KPI value to this solution

Parameters:
  • name – Name of the KPI
  • value – Model variable representing this KPI
add_var(var, value=None, presence=None, start=None, end=None, size=None)[source]

Add a solution to a integer or interval variable.

Parameters:
  • var – CPO variable (object of a class extending CpoVariable)
  • value (Optional) – Value of the variable if the variable is a integer variable. Can be a domain if variable is not completely instantiated.
  • presence (Optional) – Presence indicator (true for present, false for absent, None for undetermined), if the variable is an interval variable.
  • start (Optional) – Value of start, or tuple representing the start range, if the variable is an interval variable.
  • end (Optional) – Value of end, or tuple representing the end range, if the variable is an interval variable.
  • size (Optional) – Value of size, or tuple representing the size range, if the variable is an interval variable.
add_var_solution(vsol)[source]

Add a solution to a variable to this model solution.

Parameters:vsol – Variable solution (object of a class extending CpoVarSolution)
get_all_var_solutions()[source]

Gets the list of all variable solutions from this model solution.

Returns:List of all variable solutions (class extending CpoVarSolution).
get_as_constraints()[source]

Build a list of constraints corresponding to this solution.

This method builds a list of constraints that force the model variables to correspond to this solution.

It does not consider the KPIs and objective values, only the decision variables: integer, floating point, interval, sequence variables and state functions.

The result list of constraints can be added to the model using add(), for example to call refine_conflict() to identify which constraint(s) does not allow this solution to comply to the model constraints.

(From an initial version provided by P.Laborie)

Returns:List of constraints corresponding to this solution
get_kpi_value(name)[source]

Get the value of a KPI

Parameters:name – Name of the KPI
Returns:Value of the KPI
Raises:KeyError if KPI is not in the solution.
get_kpis()[source]

Get the solution KPIs.

Returns:Ordered dictionary containing value of the KPIs that have been defined in the model. Key is KPI publish name, value is expression value. Keys are sorted in the order the KPIs have been defined.
get_objective_bound()[source]

Gets the numeric values of the first objective bound.

Returns:First objective bound values, None if none.
get_objective_bounds()[source]

Gets the numeric values of all objectives bound.

Note that when minimize_static_lex() or maximize_static_lex() is used, the bound values must be taken as a whole, as are the values delivered by get_objective_values(). One cannot interpret bound values on each criterion independently. For example, suppose, we have a problem with two criteria specified to minimize_static_lex, a number of workers, and a number of days to complete a job. That is, we always prefer to use less workers, but for equal numbers of workers, we prefer to take less days. Then a solution with 3 workers and 10 days is perfectly compatible with a lower bound of 2 workers and 13 days, even though the lower bound on the number of days is higher than the value in the solution.

Returns:Array of all objective bound values, None if none.
get_objective_gap()[source]

Gets the numeric values of the gap between the first objective value and objective bound.

For a single objective, gap is calculated as gap = abs(value - bound) / max(1e-10, abs(value))

Returns:First objective gap value, None if none.
get_objective_gaps()[source]

Gets the numeric values of the gap between objective value and objective bound.

For a single objective, gap is calculated as gap = abs(value - bound) / max(1e-10, abs(value))

For multiple objectives, each gap is the gap between corresponding value and bound. However, after the first gap whose value is not within optimality tolerance specified by OptimalityTolerance and RelativeOptimalityTolerance, all returned gap values are positive infinity.

Returns:Array of all objective gap values, None if none.
get_objective_value()[source]

Gets the numeric values of the first objectives.

If the solution is partial, objective value may be an interval expressed as a tuple (min, max)

Returns:First objective value, None if none.
get_objective_values()[source]

Gets the numeric values of all objectives.

If the solution is partial, each objective value may be an interval expressed as a tuple (min, max)

Returns:Array of objective values, None if none.
get_value(expr)[source]

Gets the value of a variable or a KPI.

This method first find the variable with get_var_solution() and, if exists, returns the result of a call to the method get_value() on this variable.

The expression can also be the name of a KPI.

The result depends on the type of the variable. For details, please consult documentation of methods:

Note that the builtin method __getitem__() is overwritten to call this method. Writing sol.get_value(x) is then equivalent to write sol[x].

Parameters:expr – Variable expression, variable name or KPI name.
Returns:Variable value, None if variable is not found.
Raises:KeyError if expression is not in the solution.
get_var_solution(expr)[source]

Gets a variable solution from this model solution.

Parameters:expr – Variable expression or variable name if any
Returns:Variable solution (class extending CpoVarSolution), None if variable is not found
has_var_solutions()[source]

Check if this solution contains variable solutions.

Returns:True if there is at least one variable solution.
is_empty()[source]

Check whether this solution contains any information

Returns:True if this solution contains no data
map_solution(sobj)[source]

Map a python object on this solution.

This method builds a copy of the source object and replace in its attributes all occurrences of model expressions by their value in this solution. This method is called recursively on all child objects.

Parameters:sobj – Source object
Returns:Copy of the source object where model expressions are replaced by their values
print_solution(out=None)[source]

Prints the solution on a given output.

If the given output is a string, it is considered as a file name that is opened by this method using ‘utf-8’ encoding.

DEPRECATED. Use write() instead.

Parameters:out – Target output stream or output file, standard output if not given.
set_value(var, value)[source]

Set the value of a variable.

This method allows to set an integer variable or an interval variable with the short representation used to represent it, as returned by CpoIntVarSolution.get_value() or CpoIntervalVarSolution.get_value().

For an integer variable, value can be:

  • If the variable is fully instantiated, a single integer.
  • If the variable is partially instantiated, a domain expressed as a list of integers or intervals.

For an interval variable, value can be:

  • If the variable is absent, an empty tuple.
  • If the variable is fully instantiated, a tuple of 3 integers (start, end, size).
  • If the variable is partially instantiated, a tuple (start, end, size, length) where each individual value can be an integer or an interval expressed as a tuple.

Note that the builtin method __setitem__() is overwritten to call this method. Writing sol.set_value(x, y) is then equivalent to write sol[x] = y.

New in version 2.9.

Parameters:
  • var – Model variable
  • value – short representation of the variable value
write(out=None)[source]

Write the solution.

If the given output is a string, it is considered as a file name that is opened by this method using ‘utf-8’ encoding.

Parameters:out (Optional) – Target output stream or file name. If not given, default value is sys.stdout.
class docplex.cp.solution.CpoProcessInfos[source]

Bases: docplex.cp.utils.InfoDict

Dictionary of various process information.

This class groups various information related to the processing of the model by the Python API. It is implemented as an extension of the class InfoDict and takes profit of the methods such as write() that allows to easily print the full content of the information structure.

Note that the content is purely informative. Information names and values depends on the implementation of the solver agent that has been used to solve the model.

This class provides few methods to access the most important information stored in it. All information is available using regular dictionary access expression.

get_model_build_time()[source]

Get the time spent to build the model.

Modeling time is computed as the time spent between model creation and last addition of a model expression.

Returns:Total modeling time in seconds.
get_solver_version()[source]

Returns the version of the solver.

Returns:Solver version string, None if unknown
get_total_solve_time()[source]

Get the total solve time, including time to send model and retrieve result.

Returns:Total solve time in seconds.
class docplex.cp.solution.CpoRefineConflictResult(model)[source]

Bases: docplex.cp.solution.CpoRunResult

This class represents the result of a call to the conflict refiner.

A conflict is a subset of the constraints and/or variables of the model which are mutually contradictory.

The conflict refiner first examines the full infeasible model to identify portions of the conflict that it can remove. By this process of refinement, the conflict refiner arrives at a minimal conflict. A minimal conflict is usually smaller than the full infeasible model and thus makes infeasibility analysis easier. Since the conflict is minimal, removal of any one of these constraints will remove that particular cause for infeasibility. There may be other conflicts in the model; consequently, repair of a given conflict does not guarantee feasibility of the remaining model. If a model happens to include multiple independent causes of infeasibility, then it may be necessary for the user to repair one such cause and then repeat the diagnosis with further conflict analysis.

get_all_member_constraints()[source]

Returns the list of all constraints that are certainly member of the conflict.

Same as get_member_constraints().

Returns:List of model constraints (class CpoExpr) certainly member of the conflict.
get_all_member_variables()[source]

Returns the list of all variables that are certainly member of the conflict.

Same as get_member_variables().

Returns:List of model variables (class CpoIntVar or CpoIntervalVar) certainly member of the conflict.
get_all_possible_constraints()[source]

Returns the list of all constraints that are possibly member of the conflict.

Same as get_possible_constraints().

Returns:List of model constraints (class CpoExpr) possibly member of the conflict.
get_all_possible_variables()[source]

Returns the list of all variables that are possibly member of the conflict.

Same as get_possible_variables().

Returns:List of model variables (class CpoIntVar or CpoIntervalVar) possibly member of the conflict.
get_conflict_status()[source]

Returns the status of the conflict refiner.

Returns:Conflict refiner status, with value in CONFLICT_STATUS_*
get_cpo()[source]

Returns the conflict represented in CPO format.

Returns:String containing the conflict in CPO format, None if not given.
get_member_constraints()[source]

Returns the list of all constraints that are certainly member of the conflict.

Returns:List of model constraints (class CpoExpr) certainly member of the conflict.
get_member_variables()[source]

Returns the list of all variables that are certainly member of the conflict.

Returns:List of model variables (class CpoIntVar or CpoIntervalVar) certainly member of the conflict.
get_possible_constraints()[source]

Returns the list of all constraints that are possibly member of the conflict.

Returns:List of model constraints (class CpoExpr) possibly member of the conflict.
get_possible_variables()[source]

Returns the list of all variables that are possibly member of the conflict.

Returns:List of model variables (class CpoIntVar or CpoIntervalVar) possibly member of the conflict.
is_conflict()[source]

Checks if this descriptor contains a valid conflict.

Returns:True if there is a conflict, False otherwise.
print_conflict(out=None)[source]

Prints this conflict on a given output.

If the given output is a string, it is considered as a file name that is opened by this method using ‘utf-8’ encoding.

DEPRECATED. Use write() instead.

Parameters:out – Target output stream or output file, standard output if not given.
write(out=None, add_cpo=True)[source]

Write the conflict

If the given output is a string, it is considered as a file name that is opened by this method using ‘utf-8’ encoding.

Parameters:
  • out (Optional) – Target output stream or file name. If not given, default value is sys.stdout.
  • add_cpo (Optional) – Add the conflict in CPO file format after the detailed list of conflict elements. By default, this parameter is set to true.
class docplex.cp.solution.CpoRunResult(model)[source]

Bases: object

This class is an abstract class extended by classes representing the result of a call to the solver.

It contains the following elements:
  • model that has been solved,
  • solver parameters,
  • solver information,
  • solver output log, if configuration has been set to store it (default),
  • internal processing information.
get_info(name, default=None)[source]

Gets a particular solver information attribute.

Deprecated. use get_solver_info() instead.

Parameters:
  • name – Name of the information to get
  • default – (optional) Default value if not found. None by default.
Returns:

Information attribute value, None if not found.

get_infos()[source]

Gets the complete dictionary of solver information attributes.

Deprecated. use get_solver_infos() instead.

Returns:Solver information, object of class CpoSolverInfos.
get_model()[source]

Gets the source model

Returns:Source model, object of class CpoModel
get_parameter(name, default=None)[source]

Get a particular solving parameter.

Parameters:
  • name – Name of the parameter to get
  • default – (optional) Default value if not found. None by default.
Returns:

Parameter value, default value if not found.

get_parameters()[source]

Gets the complete dictionary of solving parameters.

Returns:Solving parameters, object of class CpoParameters.
get_process_info(name, default=None)[source]

Get a particular process information.

Parameters:
  • name – Name of the process info to get
  • default – (optional) Default value if not found. None by default.
Returns:

Value of the process info, default value if not found.

get_process_infos()[source]

Gets the set of informations provided by the Python API concerning the solving of the model.

Returns:Object of class CpoProcessInfos that contains general information on model processing.
get_solver_info(name, default=None)[source]

Gets a particular solver information attribute.

Parameters:
  • name – Name of the information to get
  • default – (optional) Default value if not found. None by default.
Returns:

Information attribute value, None if not found.

get_solver_infos()[source]

Gets the set of information provided by the solver concerning to the solving of the model.

Returns:Solver information, object of class CpoSolverInfos.
get_solver_log()[source]

Gets the log of the solver.

Returns:Solver log as a string, None if unknown.
class docplex.cp.solution.CpoSequenceVarSolution(expr, lvars)[source]

Bases: docplex.cp.solution.CpoVarSolution

This class represents a solution to a sequence variable.

Constructor

Parameters:
  • expr – Variable expression, object of class CpoSequenceVar. None if unknown.
  • lvars – Ordered list of interval variable solutions that are in this sequence (objects of class CpoIntervalVarSolution), or list of interval variables (object of class CpoIntervalVar).
get_interval_variables()[source]

Gets the list of CpoIntervalVarSolution in this sequence.

Returns:List of CpoIntervalVarSolution in this sequence.
get_value()[source]

Gets the list of CpoIntervalVarSolution in this sequence.

Returns:List of CpoIntervalVarSolution in this sequence.
class docplex.cp.solution.CpoSolveResult(model)[source]

Bases: docplex.cp.solution.CpoRunResult

This class represents the result of a returned by a call to a model solve request.

On top of those already stored in CpoRunResult, it contains the following elements:

If this result contains a solution, the methods implemented in the class CpoModelSolution to access solution elements are available directly from this class.

Constructor

Parameters:model – Related model
get_all_var_solutions()[source]

Gets the list of all variable solutions from this model solution.

Returns:List of all variable solutions (class CpoVarSolution).
get_fail_status()[source]

Gets the solving fail status.

This method is deprecated since release 12.8. Use get_search_status() and get_stop_cause() instead.

Returns:Fail status, element of the global list ALL_FAIL_STATUSES.
get_kpis()[source]

Get the solution kpis

Returns:Dictionary containing value of the KPIs that have been defined in the model, None if none.
get_objective_bound()[source]

Gets the numeric values of the first objective bound.

Returns:First objective bound values, None if none.
get_objective_bounds()[source]

Gets the numeric values of all objectives bound.

Note that when minimize_static_lex() or maximize_static_lex() is used, the bound values must be taken as a whole, as are the values delivered by get_objective_values(). One cannot interpret bound values on each criterion independently. For example, suppose, we have a problem with two criteria specified to minimize_static_lex, a number of workers, and a number of days to complete a job. That is, we always prefer to use less workers, but for equal numbers of workers, we prefer to take less days. Then a solution with 3 workers and 10 days is perfectly compatible with a lower bound of 2 workers and 13 days, even though the lower bound on the number of days is higher than the value in the solution.

Returns:Array of all objective bound values, None if none.
get_objective_gap()[source]

Gets the numeric values of the gap between the first objective value and objective bound.

For a single objective, gap is calculated as gap = |value - bound| / max(1e-10, |value|)

Returns:First objective gap value, None if not defined.
get_objective_gaps()[source]

Gets the numeric values of the gap between objective value and objective bound.

For a single objective, gap is calculated as gap = |value - bound| / max(1e-10, |value|)

For multiple objectives, each gap is the gap between corresponding value and bound. However, after the first gap whose value is not within optimality tolerance specified by OptimalityTolerance and RelativeOptimalityTolerance, all returned gap values are positive infinity.

Returns:Array of all objective gap values, None if not defined.
get_objective_value()[source]

Gets the numeric values of the first objective.

Returns:First objective value, None if none.
get_objective_values()[source]

Gets the numeric values of all objectives.

Returns:Array of all objective values, None if none.
get_search_status()[source]

Gets the search status.

This solver information is provided by the COS 12.8 CP solver in addition/replacement to solve_status. Value is None if the solver is earlier than this version.

Returns:Search status, element of the global list ALL_SEARCH_STATUSES. None if not defined.
get_solution()[source]

Get the model solution

Returns:Model solution, object of class CpoModelSolution, None if no solution.
get_solve_status()[source]

Gets the solve status.

Returns:Solve status, element of the global list ALL_SOLVE_STATUSES.
get_solve_time()[source]

Gets the solve time required for this solution.

Returns:(float) Solve time in seconds.
get_stop_cause()[source]

Gets the stop cause.

This solver information is provided by the COS 12.8 CP solver in addition/replacement to fail_status. Value is None if the solver is earlier than this version.

Returns:Stop cause, element of the global list ALL_STOP_CAUSES. None if not defined.
get_value(name)[source]

Gets the value of a variable.

For IntVar, value is an integer. For IntervalVar, value is a tuple (start, end, size), () if absent. For SequenceVar, value is list of interval variable solutions. For StateFunction, value is list of steps.

Parameters:name – Variable name, or model variable descriptor.
Returns:Variable value, None if variable is not found.
get_var_solution(name)[source]

Gets a variable solution from this model solution.

Parameters:name – Variable name or variable expression.
Returns:Variable solution, object of class CpoVarSolution, None if not found.
is_new_solution()[source]

Checks if this result contains a new valid solution to the problem.

A new solution is present if the solve status is ‘Feasible’ or ‘Optimal’. Optimality of the solution should be tested using method is_solution_optimal().

Note that, if the solve is done by calling multiple times docplex.cp.solver.solver.CpoSolver.search_next(), this method may return False if the solve status is ‘Optimal’. This is because the same solution has already been returned with status ‘Feasible’.

Returns:True if this result contains a new solution to the problem.
is_solution()[source]

Checks if this result contains a solution to the problem.

A solution is present if the solve status is ‘Feasible’ or ‘Optimal’. Optimality of the solution should be tested using method is_solution_optimal().

This method returns True if a valid solution is present, even if it has already been provided in a previous result. Use is_new_solution() to be sure the solution is a new one.

Returns:True if this result contains a solution to the problem.
is_solution_optimal()[source]

Checks if this descriptor contains an optimal solution to the problem.

Returns:True if there is a solution that is optimal.
map_solution(sobj)[source]

Map a python object on the solution of this result.

This method builds a copy of the source object and replace in its attributes all occurrences of model expressions by their value in this solution. This method is called recursively on all child objects.

Parameters:sobj – Source object
Returns:Copy of the source object where model expressions are replaced by their values
print_solution(out=None)[source]

Prints the solution on a given output.

If the given output is a string, it is considered as a file name that is opened by this method using ‘utf-8’ encoding.

DEPRECATED. Use write() instead.

Parameters:out – Target output stream or output file, standard output if not given.
write(out=None)[source]

Write the solve result

If the given output is a string, it is considered as a file name that is opened by this method using ‘utf-8’ encoding.

Parameters:out (Optional) – Target output stream or file name. If not given, default value is sys.stdout.
write_in_string()[source]

Build a string representation of this object.

The string that is returned is the same than what is printed by calling write().

Returns:String representation of this object.
class docplex.cp.solution.CpoSolverInfos[source]

Bases: docplex.cp.utils.InfoDict

Dictionary of various solver informations.

This class groups various information returned by the solver at the end of the solve. It is implemented as an extension of the class docplex.cp.utils.InfoDict and takes profit of the methods such as write() that allows to easily print the full content of the information structure.

The keys of this disctioanr are those defined by the CP solver itsefs. This class implements accessors for the most important of them.

get_memory_usage()[source]

Gets the memory usage.

Returns:Memory usage in bytes.
get_number_of_branches()[source]

Gets the number of branches.

Returns:Number of branches.
get_number_of_constraints()[source]

Gets the number of constraints in the model.

Returns:Number of constraints.
get_number_of_fails()[source]

Gets the number of fails.

Returns:Number of fails.
get_number_of_integer_vars()[source]

Gets the number of integer variables in the model.

Returns:Number of integer variables.
get_number_of_interval_vars()[source]

Gets the number of interval variables in the model.

Returns:Number of interval variables.
get_number_of_sequence_vars()[source]

Gets the number of sequence variables in the model.

Returns:Number of sequence variables.
get_number_of_solutions()[source]

Gets the number of solutions found in the solve.

Returns:Number of solutions.
get_solve_time()[source]

Gets the solve time.

Returns:Solve time in seconds, -1 if unknown
get_total_time()[source]

Gets the total solve time.

Returns:Total solve time in seconds, -1 if unknown
class docplex.cp.solution.CpoStateFunctionSolution(expr, steps)[source]

Bases: docplex.cp.solution.CpoVarSolution

This class represents a solution to a step function.

A solution to a step function is represented by a list of steps. A step is a triplet (start, end, value) that gives the value of the function on the interval [start, end).

Constructor

Parameters:
  • expr – Variable expression, object of class CpoStateFunction. None in unknown.
  • steps – List of function steps represented as tuples (start, end, value).
get_function_steps()[source]

Gets the list of function steps.

Returns:List of function steps. Each step is a tuple (start, end, value).
get_value()[source]

Gets the list of function steps. Identical to get_function_steps().

Returns:List of function steps.
class docplex.cp.solution.CpoVarSolution(expr)[source]

Bases: object

This class is the super class of all classes representing a solution to a variable.

Constructor

Parameters:expr – Variable expression, object of class CpoVariable or extending class.
get_expr()[source]

Get the variable expression for which this object is a solution.

Returns:Model variable expression.
get_name()[source]

Gets the name of the variable.

Returns:Name of the variable, None if anonymous.
get_value()[source]

Gets the variable value. This method is overloaded by each class extending this class.

Returns:Value of the variable, represented according to its semantic (see specific variable documentation).
get_var()[source]

Get the variable expression for which this object is a solution.

This method is equivalent to get_expr()

Returns:Model variable expression.
class docplex.cp.solution.IntervalVarPartialValue(start, end, size, length)

Bases: tuple

Create new instance of IntervalVarPartialValue(start, end, size, length)

end

Alias for field number 1

length

Alias for field number 3

size

Alias for field number 2

start

Alias for field number 0

class docplex.cp.solution.IntervalVarValue(start, end, size)

Bases: tuple

Create new instance of IntervalVarValue(start, end, size)

end

Alias for field number 1

size

Alias for field number 2

start

Alias for field number 0