docplex.mp.solution module¶
-
class
docplex.mp.solution.SolveSolution(model, var_value_map=None, obj=None, name=None, solved_by=None, keep_zeros=True, rounding=False)[source]¶ Bases:
objectThe
SolveSolutionclass holds the result of a solve.-
add_var_value(var_key, value)[source]¶ Adds a new (variable, value) pair to this solution.
Parameters: - var_key – A decision variable (
docplex.mp.linear.Var) or a variable name (string). - value (number) – The value of the variable in the solution.
- var_key – A decision variable (
-
check_as_mip_start()[source]¶ Checks that this solution is a valid MIP start.
To be valid, it must have:
- at least one discrete variable (integer or binary), and
- the values for decision variables should be consistent with the type.
Returns: True if this solution is a valid MIP start. Return type: Boolean
-
contains(dvar)[source]¶ Checks whether or not a decision variable is mentioned in the solution.
This predicate can also be used in the form var in solution, because the
__contains_()method has been redefined for this purpose.Parameters: dvar ( docplex.mp.linear.Var) – The variable to check.Returns: True if the variable is mentioned in the solution. Return type: Boolean
-
export(file_or_filename, format='json', **kwargs)[source]¶ Export this solution.
Parameters: - file_or_filename – If
file_or_filenameis a string, this argument contains the filename to write to. If this is a file object, this argument contains the file object to write to. - format – The format of the solution. The format can be: - json - xml
- kwargs – The kwargs passed to the actual exporter
- file_or_filename – If
-
export_as_mst(path=None, basename=None)[source]¶ Exports a solution to a file in CPLEX mst format.
Parameters: - basename – Controls the basename with which the solution is printed. Accepts None, a plain string, or a string format. If None, the model’s name is used. If passed a plain string, the string is used in place of the model’s name. If passed a string format (either with %s or {0}), this format is used to format the model name to produce the basename of the written file.
- path – A path to write the file, expects a string path or None.
Can be a directory, in which case the basename
that was computed with the basename argument is appended to the directory to produce
the file.
If given a full path, the path is directly used to write the file, and
the basename argument is not used.
If passed None, the output directory will be
tempfile.gettempdir().
Example
Assuming the solution has the name “prob”:
sol.export_as_mst()will write file prob.mst in a temporary directory.sol.export_as_mst(path="c:/temp/myprob1.mst")will write file “c:/temp/myprob1.mst”.sol.export_as_mst(basename="my_%s_mipstart", path ="z:/home/")will write “z:/home/my_prob_mipstart.mst”.
-
get_name()[source]¶ This property allows to get/set a name on the solution.
In some cases , it might be interesting to build different solutions for the same model, in this case, use the name property to disinguish them.
-
get_objective_value()[source]¶ Gets the objective value as defined in the solution. When the objective value has not been defined, a special value NO_SOLUTION is returned. To check whether the objective has been set, use
has_objective().Returns: The value of the objective as defined by the solution. Return type: float
-
get_value(dvar_arg)[source]¶ Gets the value of a solution variable in a solution. If the variable is not mentioned in the solution, the method returns 0 and does not raise an exception. Note that this method can also be used as
solution[dvar]()because the__getitem__()method has been overloaded.Parameters: dvar_arg – A decision variable ( docplex.mp.linear.Var) or a variable name (string).Returns: The value of the variable in the solution. Return type: float
-
get_values(dvars)[source]¶ Gets the value of a sequence of variables in a solution. If a variable is not mentioned in the solution, the method assumes 0 and does not raise an exception.
Returns: A sequence of float values.
-
has_objective()[source]¶ Checks whether or not the objective has been set.
Returns: True if the solution defines an objective value. Return type: Boolean
-
is_empty()[source]¶ Checks whether the solution is empty.
Returns: True if the solution is empty; in other words, the solution has no defined objective and no variable value. Return type: Boolean
-
iter_var_values()[source]¶ Iterates over the (variable, value) pairs in the solution.
Returns: A dict-style iterator which returns a two-component tuple (variable, value) for all variables mentioned in the solution. Return type: iterator
-
iter_variables()[source]¶ Iterates over all variables mentioned in the solution.
Returns: An iterator object over all variables mentioned in the solution. Return type: iterator
-
model¶ This property returns the model associated with the solution.
-
name¶ This property allows to get/set a name on the solution.
In some cases , it might be interesting to build different solutions for the same model, in this case, use the name property to disinguish them.
-
number_of_var_values¶ This property returns the number of variable values stored in this solution.
-
objective_value¶ Gets the objective value as defined in the solution. When the objective value has not been defined, a special value NO_SOLUTION is returned. To check whether the objective has been set, use
has_objective().Returns: The value of the objective as defined by the solution. Return type: float
-
print_mst()[source]¶ Writes the solution in an output stream “out” (assumed to satisfy the file interface) in CPLEX MST format.
-
set_objective_value(obj)[source]¶ Sets the objective value of the solution.
Parameters: obj (float) – The value of the objective in the solution.
-
solved_by¶ Returns a string indicating how the solution was produced.
- If the solution was created by a program, this field returns None.
- If the solution originated from a local CPLEX solve, this method returns the string ‘cplex_local’.
- If the solution originated from a DOcplexcloud solve, this method returns ‘cplex_cloud’.
Returns: A string, or None.
-