Python Example

This section describes how you can use the IBM DOcplexcloud Python Client to create a job, submit it, and get results.

Create the job

Initialize a new job and upload the associated model and data files.

Submit the job

Submit the job to execute and monitor its status until it completes.

Get results

Access results and download them to your application.

The IBM DOcplexcloud Python Client provides a simple API that contains the same low-level actions available in the REST API, but additionally provides an API with more efficient methods that are easy to use.

Then you submit the job for execution, passing the model file and data file of the problem, and you specify a file to which the solution will be written. The execute() method waits until the job has finished executing before writing the output to the results file:

To run the sample with more options

In a command prompt, navigate to the examples directory that contains the subdirectory trucking and type:

python trucking/truck_more_options.py

As well as the solution results file, you can also see the log from the solve engine in the solver.log file.

Going further: Encoding objects in JSON

You can encode objects in memory using a JSON encoder before sending them to the server. You can also get the result and stream it from the server to create in memory the objects related to the results.

The truck_json.py example demonstrates how you can represent model data by a Problem class, using namedtuples to store the problem data.

Then you encode your problem using a customized json.JSONEncoder defined in model.py. The default JSONEncoder does not encode namedtuple as a dict of the tuple values, so you must customize it to encode namedtuple as dicts instead of lists: