7.7 MOSEK OptServer

MOSEK provides an easy way to offload optimization problem to a remote server in both synchronous or asynchronous mode. This section describes 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 and requires very few modifications to existing code: instead of Task.optimize the user must invoke Task.optimizermt with the host and port where the server is running and listening as additional arguments. 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.6 Using the OptServer in synchronous mode. Click here to download.
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 host port numpolls");
    } else {

      String inputfile = args[0];
      String host      = args[1];
      String port      = args[2];

      rescode trm[]  = new rescode[1];

      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); }
        });

        task.readdata (inputfile);

        task.optimizermt (host, port, trm);

        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:

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.7 Using the OptServer in asynchronous mode. Click here to download.
package com.mosek.example;
import mosek.*;

public class opt_server_async {
  public static void main (String[] args) {
    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 host      = args[1];
      String port      = args[2];
      int numpolls     = Integer.parseInt(args[3]);

      try (Env env = new Env()) {
        String token;

        try(Task task = new Task(env, 0, 0)) {
          task.readdata (inputfile);
          token = task.asyncoptimize (host, port);
        }

        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);

          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( host,
                                                    port,
                                                    token,
                                                    resp,
                                                    trm);


            System.out.println("polling done");

            if (respavailable) {
              System.out.println("solution available!");
              task.asyncgetresult(host,
                                  port,
                                  token,
                                  resp,
                                  trm);

              task.solutionsummary (mosek.streamtype.log);
              break;
            }

            i++;

            if (i == numpolls) {
              System.out.println("max num polls reached, stopping host.");
              task.asyncstop (host, port, token);
              break;
            }

          }
        } catch (java.lang.Exception e) {
          System.out.println("Something unexpected happend...");
        }
      }
    }
  }
}