docplex.mp.with_funcs module

docplex.mp.with_funcs.model_objective(mdl, temp_obj, temp_sense=None)[source]

This contextual function is used to temporarily override the objective of a model. As a contextual function, it is intended to be used with the with construct, for example:

>>> with model_objective(mdl, x+y) as mdl2:
>>>     mdl2.solve()

The new model returned from the with has a temporary objective overriding the initial objective.

when exiting the with block, the initial objective and sense are restored.

Parameters:
  • mdl – an instance of :class:Model.
  • temp_obj – an expression.
  • temp_sense – an optional objective sense to override the model’s. Default is None (keep same objective). Accepts either an instance of enumerated value :class:docplex.mp.constants.ObjectiveSense or a string ‘min’ or ‘max’.
Returns:

the same model, with overridden objective.

New in version 2.21

docplex.mp.with_funcs.model_parameters(mdl, temp_parameters)[source]

This contextual function is used to override a model’s parameters. As a contextual function, it is intended to be used with the with construct, for example:

>>> with model_parameters(mdl, {"timelimit": 30, "empahsis.mip": 4}) as mdl2:
>>>     mdl2.solve()

The new model returned from the with has temporary parameters overriding those of the initial model.

when exiting the with block, initial parameters are restored.

Parameters:
  • mdl – an instance of :class:Model.
  • temp_parameters – accepts either a dictionary of qualified names to values, for example {“mip.tolernaces.mipgap”: 0.03, “emphasis.mip”: 4}, or a dictionary from parameter objects to values.
Returns:

the same model, with overridden parameters.

See also

  • docplex.mp.params.Parameter.qualified_name()

New in version 2.21

docplex.mp.with_funcs.model_solvefixed(mdl)[source]

This contextual function is used to temporarily change the type of the model to “solveFixed”. As a contextual function, it is intended to be used with the with construct, for example:

>>> with model_solvefixed(mdl) as mdl2:
>>>     mdl2.solve()

The model returned from the with has a temporary problem type set to “solveFixex overriding the actual problem type. This function is useful for MIP models which have been successfully solved; the modified model can be solved as a LP, with all discrete values fixed to their solutions in the previous solve.

when exiting the with block, the actual problem type is restored.

Parameters:mdl – an instance of :class:Model.
Returns:the same model, with overridden problem type.

Note

  • an exception is raised if the model has not been solved
  • LP models are returned unchanged, as this mfunction has no use.

New in version 2.22