-
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()
.
-
write_level
– an enumerated value which controls which variables are printed.
The default is WriteLevel.Auto, which prints the values of all discrete variables.
This parameter also accepts the number values of the corresponding CPLEX parameters
(1 for AllVars, 2 for DiscreteVars, 3 for NonZeroVars, 4 for NonZeroDiscreteVars)
Returns:
The full path of the file, when successful, else None
Examples
Assuming the solution has the name “prob”:
sol.export_as_mst()
will write file prob.mst in a temporary directory.
-
sol.export_as_mst(write_level=WriteLevel.ALlvars)
will write file prob.mst in a temporary directory,
-
and will print all variables in the problem.
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”.
The complete description of MST format is found here:
https://www.ibm.com/support/knowledgecenter/SSSA5P_20.1.0/ilog.odms.cplex.help/CPLEX/FileFormats/topics/MST.html
See also
docplex.mp.constants.WriteLevel
export_as_sol
(
path=None
,
basename=None
,
**kwargs
)
[source]
-
Exports a solution to a file in CPLEX SOL format.
SOL format is valid for all types of solutions, LP or MIP, but cannot be used for warm starts.
Arguments are identical to the method
export_as_mst()
Note:
The complete description of SOL format is found here:
https://www.ibm.com/support/knowledgecenter/SSSA5P_20.1.0/ilog.odms.cplex.help/CPLEX/FileFormats/topics/SOL.html
See also
docplex.mp.model.SolveSolution.export_as_mst()
get_blended_objective_value_by_priority
(
)
[source]
-
Gets the blended objective value (or list of blended objectives value) by priority level 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 (or list of values for multi-objective) as defined by
the solution.
Return type:float or list(float)
get_dual_values
(
lcts
)
[source]
-
Returns the dual values of a sequence of linear constraints.
Note: the model must a pure LP: no integer or binary variable, no piecewise, no SOS.
The model must also be solved successfully before calling this method.
get_objective_value
(
)
[source]
-
Gets the objective value (or list of objectives 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 (or list of values for multi-objective) as defined by
the solution.
Return type:float or list(float)
get_reduced_costs
(
dvars
)
[source]
-
Returns the reduced costs for a variable iterable.
Note: the model must a pure LP: no integer or binary variable, no piecewise, no SOS.
The model must also be solved successfully before calling this method.
get_sensitivity
(
dvars
)
[source]
-
Returns the sensitivity values for a variable iterable.
Note: The model must be solved successfully before calling this method.
get_slacks
(
cts
)
[source]
-
Return the slack values for a sequence of constraints.
Slack values are available for linear, quadratic and indicator constraints.
The model must be solved successfully before calling this method.
get_status
(
ct
)
[source]
-
Returns the status of a linear constraint in the solution.
Returns 1 if the constraint is satisfied, else returns 0. This is particularly useful when using
the status variable of constraints.
get_value
(
arg
)
[source]
-
Gets the value of a variable or an expression 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[arg]()
because the
__getitem__()
method has been overloaded.
Parameters:
arg
– A decision variable (
docplex.mp.dvar.Var
),
a variable name (a string), or an expression.
Returns:The value of the variable in the solution.
Return type:float
get_value_df
(
var_dict
,
value_column_name=None
,
key_column_names=None
)
[source]
-
Returns values of a dicitonary of variables, as a pandas dataframe.
If pandas is not present, returns a dicitonary of columns.
Parameters:
-
var_dict
– the dicitonary of variables, as created by Model.xx_var_dict
-
value_column_name
– an optional string to name the value column. Default is ‘value’
-
key_column_names
– an optional list of strings to name th ekeys of the dicitonary.
If not present, keys are named ‘k1’, ‘k2’, …
Returns:
a pandas DataFrame, if pandas is present.
get_value_dict
(
var_dict
,
keep_zeros=True
,
precision=1e-06
)
[source]
-
Converts a dictionary of variables to a dictionary of solutions
Assuming
var_dict
is a dictionary of variables
(for example, as returned by
Model.integer_var_dict()
,
returns a dictionary with the same keys and as values the solution values of the
variables.
Parameters:
-
var_dict
– a dictionary of decision variables.
-
keep_zeros
– an optional flag to keep zero values (default is True)
-
precision
– an optional precision, used to filter small non-zero values.
The default is 1e-6.
Returns:
A dictionary from variable keys to solution values (floats).
get_value_list
(
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 a 0 value.
Parameters:
dvars
– an ordered sequence of decision variables.
Returns:A list of float values, in the same order as the variable sequence.
Return type:list
Returns:True if the solution is empty; in other words, the solution has no defined objective and no variable value.
Return type:Boolean
is_feasible_solution
(
tolerance=1e-06
,
silent=True
)
-
Returns True if the solution is feasible.
This method checks that solution values for variables are compatible for their types
and bounds. It also checks that all constraints are satisfied, within the tolerance.
Parameters:
-
tolerance
– a float number used to check satisfaction; default is 1e-6.
-
silent
– optional flag. If False, prints which variable (or constraint)
causes the solution to be invalid. default is True.
Returns:
True if the solution is valid, within the tolerance value.
is_valid_solution
(
tolerance=1e-06
,
silent=True
)
[source]
-
Returns True if the solution is feasible.
This method checks that solution values for variables are compatible for their types
and bounds. It also checks that all constraints are satisfied, within the tolerance.
Parameters:
-
tolerance
– a float number used to check satisfaction; default is 1e-6.
-
silent
– optional flag. If False, prints which variable (or constraint)
causes the solution to be invalid. default is True.
Returns:
True if the solution is valid, within the tolerance value.
Returns:A dict-style iterator which returns a two-component tuple (variable, value)
for all variables mentioned in the solution.
Return type:iterator
Parameters:
-
name
(
string
) – The string to be matched.
-
match_case
(
boolean
) – If True, looks for a case-exact match, else
ignores case. Default is False.
Returns:
The value of the KPI, evaluated in the solution.
multi_objective_values
-
This property is used to get the list of objective values of the solution.
In case of single objective this property returns the value for the objective as a singleton list
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()
.
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 distinguish them.
objective_value
-
This property is used to get the objective value of the solution.
In case of multi-objective this property returns the value for the first objective
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()
.
Parameters:
-
obj
(
float
or
list
(
float
)
) – The value of the objective (or list of values for multi-objective) in
-
solution.
(
the
) –
slack_value
(
ct
,
error='raise'
)
[source]
-
Return the slack value for a constraint.
Slack values are available for linear, quadratic and indicator constraints.
The model must be solved successfully before calling this method.
solve_details
-
This property returns the solve_details associated with the solution,if any.
This property returns an instance of solve details if the solution is the result
of a solve operation. If the solution has been created by API, this property returns None
See also
docplex.mp.sdetails.SolveDetails
-
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’.
update
(
var_values_iterable
)
[source]
-
Updates the solution from a dictionary. Keys can be either strings, interpreted as variable names,
or variables; values are the new values for the variable.
This method returns nothing, only performs a side effect on the solution object.