7.7 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.7.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 serveraddr") else: inputfile = sys.argv addr = sys.argv # Create the mosek environment. with mosek.Env() as env: # Create a task object linked with the environment env. # We create it with 0 variables and 0 constraints initially, # since we do not know the size of the problem. with env.Task(0, 0) 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(addr) # Solve the problem remotely trm = task.optimize() # Print a summary of the solution task.solutionsummary(mosek.streamtype.log)
7.7.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) != 5: print("Missing argument, syntax is:") print(" opt-server-async inputfile host port numpolls") else: filename = sys.argv host = sys.argv port = sys.argv numpolls = int(sys.argv) token = None with mosek.Env() as env: with env.Task(0, 0) as task: print("reading task from file") task.readdata(filename) print("Solve the problem remotely (async)") token = task.asyncoptimize(host, port) print("Task token: %s" % token) with env.Task(0, 0) as task: task.readdata(filename) 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(host, port, token) print("done!") if respavailable: print("solution available!") respavailable, res, trm = task.asyncgetresult(host, port, token) task.solutionsummary(mosek.streamtype.log) break i = i + 1 if i == numpolls: print("max number of polls reached, stopping host.") task.asyncstop(host, port, token)