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
package com.mosek.example;
import mosek.*;
public class opt_server_sync {
public static void main (String[] args) {
if (args.length == 0) {
System.out.println ("Missing argument, syntax is:");
System.out.println (" opt_server_sync inputfile addr [certpath]");
} else {
String inputfile = args[0];
String addr = args[1];
String cert = args.length < 3 ? null : args[2];
rescode trm;
try (Env env = new Env();
Task task = new Task(env, 0, 0)) {
task.set_Stream (mosek.streamtype.log,
new mosek.Stream() {
public void stream(String msg) { System.out.print(msg); }
});
// Load some data into the task
task.readdata (inputfile);
// Set OptServer URL
task.putoptserverhost(addr);
// Path to certificate, if any
if (cert != null)
task.putstrparam(sparam.remote_tls_cert_path, cert);
// Optimize remotely, no access token
trm = task.optimize ();
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.
package com.mosek.example;
import mosek.*;
public class opt_server_async {
public static void main (String[] args) throws java.lang.Exception {
if (args.length == 0) {
System.out.println ("Missing argument, syntax is:");
System.out.println (" opt_server_async inputfile host:port numpolls");
} else {
String inputfile = args[0];
String addr = args[1];
int numpolls = Integer.parseInt(args[2]);
String cert = args.length < 4 ? null : args[3];
try (Env env = new Env()) {
String token;
try(Task task = new Task(env, 0, 0)) {
task.readdata (inputfile);
if (cert != null)
task.putstrparam(sparam.remote_tls_cert_path,cert);
token = task.asyncoptimize (addr,"");
}
System.out.printf("Task token = %s\n", token);
try(Task task = new Task(env, 0, 0)) {
System.out.println("Reading input file...");
task.readdata (inputfile);
if (cert != null)
task.putstrparam(sparam.remote_tls_cert_path,cert);
System.out.println("Setting log stream...");
task.set_Stream (mosek.streamtype.log,
new mosek.Stream() {
public void stream(String msg) { System.out.print(msg); }
});
long start = System.currentTimeMillis();
System.out.println("Starting polling loop...");
int i = 0;
while ( true ) {
Thread.sleep(100);
System.out.printf("poll %d...\n", i);
rescode trm[] = new rescode[1];
rescode resp[] = new rescode[1];
boolean respavailable = task.asyncpoll( addr,
"",
token,
resp,
trm);
System.out.println("polling done");
if (respavailable) {
System.out.println("solution available!");
task.asyncgetresult(addr,
"",
token,
resp,
trm);
task.solutionsummary (mosek.streamtype.log);
break;
}
i++;
if (i == numpolls) {
System.out.println("max num polls reached, stopping host.");
task.asyncstop (addr, "", token);
break;
}
}
} catch (java.lang.Exception e) {
System.out.println("Something unexpected happend...");
throw e;
}
}
}
}
}