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.
The URL
of the remote server required in all client-side calls should be a string of the form http://host:port
or https://host:port
.
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
import mosek
import sys
def streamprinter(msg):
sys.stdout.write(msg)
sys.stdout.flush()
if len(sys.argv) <= 2:
print("Missing argument, syntax is:")
print(" opt_server_sync inputfile addr [certpath]")
else:
inputfile = sys.argv[1]
serveraddr = sys.argv[2]
tlscert = None if len(sys.argv) < 4 else sys.argv[3]
with mosek.Task() as task:
task.set_Stream(mosek.streamtype.log, streamprinter)
# We assume that a problem file was given as the first command
# line argument (received in `argv')
task.readdata(inputfile)
# Set OptServer URL
task.putoptserverhost(serveraddr)
# Path to certificate, if any
if tlscert is not None:
task.putstrparam(mosek.sparam.remote_tls_cert_path, tlscert)
# Solve the problem remotely, no access token
trm = task.optimize()
# Print a summary of the solution
task.solutionsummary(mosek.streamtype.log)
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:
Task.asyncoptimize
: Offload the optimization task to a solver server.Task.asyncpoll
: Request information about the status of the remote job.Task.asyncgetresult
: Request the results from a completed remote job.Task.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.
import mosek
import sys
import time
def streamprinter(msg):
sys.stdout.write(msg)
sys.stdout.flush()
if len(sys.argv) < 4:
print("Missing argument, syntax is:")
print(" opt-server-async inputfile host:port numpolls [cert]")
else:
filename = sys.argv[1]
addr = sys.argv[2]
numpolls = int(sys.argv[3])
token = None
cert = None if len(sys.argv) < 5 else sys.argv[4]
with mosek.Task() as task:
print("reading task from file")
task.readdata(filename)
if cert is not None:
task.putstrparam(mosek.sparam.remote_tls_cert_path,cert)
print("Solve the problem remotely (async)")
token = task.asyncoptimize(addr,"")
print("Task token: %s" % token)
with mosek.Task() as task:
task.readdata(filename)
if cert is not None:
task.putstrparam(mosek.sparam.remote_tls_cert_path,cert)
task.set_Stream(mosek.streamtype.log, streamprinter)
i = 0
while i < numpolls:
time.sleep(0.1)
print("poll %d..." % i)
respavailable, res, trm = task.asyncpoll(addr,
"",
token)
print("done!")
if respavailable:
print("solution available!")
respavailable, res, trm = task.asyncgetresult(addr,
"",
token)
task.solutionsummary(mosek.streamtype.log)
break
i = i + 1
if i == numpolls:
print("max number of polls reached, stopping host.")
task.asyncstop(addr,"", token)