8 MOSEK API tutorial

In this section we show the simplest usage of OptServer directly from the MOSEK API. For the purpose of short demonstration we include the code in the MOSEK Optimizer API for Python. Please check the section Solver interaction tutorials/MOSEK OptServer in your API manual for an example in your preferred API and a further discussion of both the synchronous and asynchronous case.

To perform synchronous remote optimization the only modification to existing code is pointing the solver to the URL of the remote server; everything else happens transparently for the user. This, depnding on the API, can be done by

  • calling a method, like putoptserverhost in the example below,

  • passing an argument to solve() or similar optimization call,

  • setting the parameter MSK_SPAR_REMOTE_OPTSERVER_HOST,

  • other ways, see your API manual.

Below is a Python example that loads a problem from a file and optimizes it on a remote OptServer.

Listing 8.1 Optimizing remotely from Python Optimizer API. Click here to download.
    # Create task and read example data
    task = mosek.Task()
    task.readdata(infile)
    task.set_Stream(mosek.streamtype.log, sys.stdout.write)

    # Specify the OptServer coordinates
    task.putoptserverhost(URL)

    # Only relevant if using HTTPS, otherwise ignore
    task.putstrparam(mosek.sparam.remote_tls_cert_path, cert)

    # Solve (remotely)
    task.optimize()

    # Print some sample results (adjust to your task type)
    print(f"Solution status {task.getsolsta(mosek.soltype.itr)}")
    print(f"Objective value {task.getprimalobj(mosek.soltype.itr)}")