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
  • docloud_config.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.solver.docloud.url = 'https://your.application.url.ibm.com'
context.solver.docloud.key = 'This is an api_key'
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)

solver.docloud

The parent node for attributes controlling the solve on Decision Optimization on Cloud.

solver.docloud.url

The DOcplexcloud service URL.

solver.docloud.key

The DOcplexcloud service API key.

solver.docloud.run_deterministic

Specific engine parameters are uploaded to keep the run deterministic.

solver.docloud.verbose

Makes the connector verbose.

solver.docloud.timeout

The timeout for requests.

solver.docloud.waittime

The wait time to wait for jobs to finish.

solver.docloud.verify

If True, verifies SSL certificates.

solver.docloud.log_requests

If True, the REST requests are logged.

solver.docloud.log_poll_interval

The interval for log polling.

solver.docloud.progress_poll_interval

The interval for progress polling.

solver.docloud.exchange_format

The exchange format to use. When setting the format, you can use the following strings: “lp”. When getting the format, the property type is docplex.mp.format.ExchangeFormat.

solver.docloud.job_parameters

dict of job parameters passed to DOCplexcloud.

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
    • docloud_config.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
    • docloud_config.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:
    • docloud: forces the solve operation to use DOcplexcloud
    • local: forces the solve operation to use native CPLEX
  • url: Overwrites the URL of the DOcplexcloud service defined by context.solver.docloud.url.
  • key: Overwrites the authentication key of the DOcplexcloud service defined by context.solver.docloud.key.
  • 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: exceptions.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