docplex.mp.context module

Configuration of Mathematical Programming engine.

The Context is the base class to control the behaviour of solve engine.

It is not advised to instanciate a Context in your code.

Instead, you should obtain a Context by the following ways:

docplex.mp configuration files are stored in files named:

  • cplex_config.py
  • cplex_config_<hostname>.py

When obtaining a Context with make_default_context(), the PYTHONPATH is searched for the configuration files and read.

Configuration files are evaluated with a context object in their scope, and you set values from this context:

context.cplex_parameters.emphasis.memory = 1
context.cplex_parameters.emphasis.mip = 2
class docplex.mp.context.BaseContext(**kwargs)[source]

Bases: dict

class docplex.mp.context.Context(**kwargs)[source]

Bases: docplex.mp.context.BaseContext

The context used to control the behavior of solve engine.

cplex_parameters

A docplex.mp.params.parameters.RootParameterGroup to store CPLEX parameters.

solver.auto_publish

If True, a model being solved will automatically publish all publishable items (solve_details, result_output, kpis_output).

solver.auto_publish.solve_details

if True, solve details are published automatically.

solver.auto_publish.result_output

if not None, the filename where solution is saved. This can be a list of filenames if multiple solutions are to be published. If True, solution.json is used.

solver.auto_publish.kpis_output

if not None, the filename where KPIs are saved as a table with KPI name and values. Currently only csv files are supported. This can be a list of filenames if multiple KPIs files are to be published.

context.solver.auto_publish.kpis_output_field_name

Name of field for KPI names in KPI output table. Defaults to ‘Name’

context.solver.auto_publish.kpis_output_field_value

Name of field for KPI values for KPI output table. Defaults to ‘Value’

solver.log_output

This attribute can have the following values:

  • True: When True, logs are printed to sys.out.
  • False: When False, logs are not printed.
  • A file-type object: Logs are printed to that file-type object.
solver.kpi_reporting.filter_level

Specify the filtering level for kpi reporting. If None, no filtering is done. Can take values of docplex.mp.progress.KpiFilterLevel or a string representation of one of the values of this enum (Unfiltered, FilterObjectiveAndBound, FilterObjective)

copy() → a shallow copy of D[source]
static make_default_context(file_list=None, logger=None, **kwargs)[source]

Creates a default context.

If file_list is a string, then it is considered to be the name of a config file to be read.

If file_list is a list, it is considered to be a list of names of a config files to be read.

if file_list is None or not specified, the following files are read if they exist:

  • the PYTHONPATH is searched for the following files:

    • cplex_config.py
    • cplex_config_<hostname>.py
Parameters:
read_settings(file_list=None, logger=None)[source]

Reads settings for a list of files.

If file_list is a string, then it is considered to be the name of a config file to be read.

If file_list is a list, it is considered to be a list of names of config files to be read.

if file_list is None or not specified, the following files are read if they exist:

  • the PYTHONPATH is searched for the following files:

    • cplex_config.py
    • cplex_config_<hostname>.py
Parameters:file_list – The list of config files to read.
Raises:InvalidSettingsFileError – If an error occurs while reading a config file. (Since version 2.8)
update(kwargs, create_missing_nodes=False)[source]

Updates this context from child parameters specified in kwargs.

The following keys are recognized:

  • cplex_parameters: A set of CPLEX parameters to use instead of the parameters defined as context.cplex_parameters.
  • agent: Changes the context.solver.agent parameter.
    Supported agents include: - local: forces the solve operation to use native CPLEX
  • log_output: if True, solver logs are output to stdout.
    If this is a stream, solver logs are output to that stream object. Overwrites the context.solver.log_output parameter.
Parameters:
  • kwargs – A dict containing keyword args to use to update this context.
  • create_missing_nodes – When a keyword arg specify a parameter that is not already member of this context, creates the parameter if create_missing_nodes is True.
class docplex.mp.context.ContextOverride(initial_context)[source]

Bases: docplex.mp.context.Context

exception docplex.mp.context.InvalidSettingsFileError(mesg, filename=None, source=None, *args, **kwargs)[source]

Bases: Exception

The error raised when an error occured when reading a settings file.

New in version 2.8

class docplex.mp.context.SolverContext(**kwargs)[source]

Bases: docplex.mp.context.BaseContext