7.1 Synchronous Problem Submission

This tutorial shows how to

  • submit a job to the OptServer,
  • remotely run MOSEK and wait for the optimization to terminate and
  • retrieve the solution and log.

The optimization problem is assumed to be stored in a file using one of the available file format.

The connection is managed using the http Python module, and it is assumed to be established successfully: as a result, an object con is available to manage the connection.

First of all, the problem is submitted to the OptServer by a submit command

Listing 7.1 How to submit a job to the OptServer.
            con.request("POST",'/submit', dataf)
            resp = con.getresponse()

If no errors have occourred, a request for running the optimizer can be sent

Listing 7.2 How to run an optimization job with OptServer.
            con.request("GET","/solve?token="+token)
            resp = con.getresponse()

At this point the request wil return when the optimization terminates. If no errors have happened, the results are already available

Listing 7.3 How to get the results.
            res = resp.getheader('X-Mosek-Res-Code',None)
            trm = resp.getheader('X-Mosek-Trm-Code',None)

            print("\tMOSEK response: %s" % res)
            print("\t      trm resp: %s" % trm)
            if resp.status == http.client.OK:
                print("Solution:")
                print(resp.read().decode('ascii',errors='ignore'))

The log is readly available as well

Listing 7.4 How to retrieve the log for a job on OptServer.
            con.request("GET","/log?token="+token)
            resp = con.getresponse()

The whole example is in Listing 7.5.

Listing 7.5 How to submit a job and solve the problem synchronously. Click here to download.
import http.client
import sys


def check_status(resp):

    print("\tHTTPResponse: %s / %s" % (resp.status,resp.reason))
    for k,v in  resp.getheaders():
        print("\t%s: %s" % (k,v))
            
    if resp.status not in [http.client.OK, http.client.NO_CONTENT]:
        print("An error occourred!")
        sys.exit(1)


if __name__ == '__main__':
    host = sys.argv[1]
    port = int(sys.argv[2])
    probfile = sys.argv[3]


    con = http.client.HTTPConnection(host,port)
    try:
        with open(probfile,'rb') as dataf:
            ## Submit job
            print("POST /submit")

            con.request("POST",'/submit', dataf)
            resp = con.getresponse()
            check_status(resp)
            
            token = resp.read().decode('ascii')

            ## Solve and wait for solution
            print("GET /solve")
            con.request("GET","/solve?token="+token)
            resp = con.getresponse()
            check_status(resp)
            res = resp.getheader('X-Mosek-Res-Code',None)
            trm = resp.getheader('X-Mosek-Trm-Code',None)

            print("\tMOSEK response: %s" % res)
            print("\t      trm resp: %s" % trm)
            if resp.status == http.client.OK:
                print("Solution:")
                print(resp.read().decode('ascii',errors='ignore'))
            print("GET /log")

            con.request("GET","/log?token="+token)
            resp = con.getresponse()

            check_status(resp)

            if resp.status == http.client.OK:
                print(resp.read().decode('utf-8',errors='ignore'))
    finally:
        con.close()