7.8 MOSEK OptServer

MOSEK provides an easy way to offload optimization problem to a remote server. This section demonstrates related functionalities from the client side, i.e. sending optimization tasks to the remote server and retrieving solutions.

Setting up and configuring the remote server is described in a separate manual for the OptServer.

7.8.1 Synchronous Remote Optimization

In synchronous mode the client sends an optimization problem to the server and blocks, waiting for the optimization to end. Once the result has been received, the program can continue. This is the simplest mode all it takes is to provide the address of the server before starting optimization. The rest of the code remains untouched.

Note that it is impossible to recover the job in case of a broken connection.

Source code example

Listing 7.7 Using the OptServer in synchronous mode. Click here to download.
using Mosek

if length(ARGS) < 2
    println("Missing argument, syntax is:")
    println("  opt_server_sync inputfile addr [certpath]")
else
    maketask() do task
        putstreamfunc(task,MSK_STREAM_LOG,msg -> print(msg))

        inputfile  = ARGS[1]
        serveraddr = ARGS[2]
        tlscert    = if length(ARGS) < 3 Nothing else ARGS[3] end

        # We assume that a problem file was given as the first command
        # line argument (received in `argv')
        readdata(task,inputfile)

        # Set OptServer URL
        putoptserverhost(task,serveraddr)

        # Path to certificate, if any
        if tlscert !== Nothing
            putstrparam(task,MSK_SPAR_REMOTE_TLS_CERT_PATH, tlscert)

            # Solve the problem remotely, no access token
            trm = optimize(task)

            # Print a summary of the solution
            solutionsummary(task,MSK_STREAM_LOG)
        end
    end
end

7.8.2 Asynchronous Remote Optimization

In asynchronous mode the client sends a job to the remote server and the execution of the client code continues. In particular, it is the client’s responsibility to periodically check the optimization status and, when ready, fetch the results. The client can also interrupt optimization. The most relevant methods are:

  • asyncoptimize : Offload the optimization task to a solver server.

  • asyncpoll : Request information about the status of the remote job.

  • asyncgetresult : Request the results from a completed remote job.

  • asyncstop : Terminate a remote job.

Source code example

In the example below the program enters in a polling loop that regularly checks whether the result of the optimization is available.

Listing 7.8 Using the OptServer in asynchronous mode. Click here to download.
using Mosek

if length(ARGS) < 2
    println("Missing argument, syntax is:")
    println("  opt_server_async inputfile host:port numpolls [cert]")
else
    filename   = ARGS[1]
    serveraddr = ARGS[2]
    numpolls   = parse(Int,ARGS[3])
    cert       = (if length(ARGS) > 3
                      ARGS[4]
                  else
                      Nothing
                  end)

    token = maketask() do task
        putstreamfunc(task,MSK_STREAM_LOG,msg -> print(msg))

        token = Nothing

        print("reading task from file")
        readdata(task,filename)

        if cert !== Nothing
            putstrparam(task,MSK_SPAR_REMOTE_TLS_CERT_PATH,cert)
        end

        println("Solve the problem remotely (async)")
        asyncoptimize(task,serveraddr,"")
    end

    println("Task token: '$token'")

    maketask() do task
        putstreamfunc(task,MSK_STREAM_LOG,msg -> print(msg))
        readdata(task,filename)

        if cert !== Nothing
            putstrparam(task,MSK_SPAR_REMOTE_TLS_CERT_PATH,cert)
        end


        i = 0
        while i < numpolls
            sleep(0.1)

            println("poll $i...")
            respavailable, res, trm = asyncpoll(task, serveraddr, "", token)

            println("done!")

            if respavailable
                println("solution available!")

                respavailable, res, trm = asyncgetresult(task, serveraddr, "", token)

                solutionsummary(task,MSK_STREAM_LOG)
                break
            end
            i = i + 1

            if i == numpolls
                println("max number of polls reached, stopping host.")
                asyncstop(task,serveraddr,"", token)
            end
        end
    end
end