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

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

namespace mosek.example
{
  class msgclass : mosek.Stream
  {
    public override void streamCB (string msg)
    {
      Console.Write ("{0}", msg);
    }
  }

  public class simple
  {
    public static void Main (string[] args)
    {
      if (args.Length == 0)
      {
        Console.WriteLine ("Missing arguments, syntax is:");
        Console.WriteLine ("  opt_server_sync inputfile addr [certpath]");
      }
      else
      {
        String inputfile = args[0];
        String addr      = args[1];
        String cert      = args.Length < 3 ? null : args[2];

        using (mosek.Task task = new mosek.Task())
        {
          task.set_Stream (mosek.streamtype.log, new msgclass ());

          // 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
          mosek.rescode 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:

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 System;
using System.Threading;

namespace mosek.example
{
  class msgclass : mosek.Stream
  {
    public override void streamCB (string msg)
    {
      Console.Write ("{0}", msg);
    }
  }

  public class opt_server_async
  {
    public static void Main (string[] args)
    {
      if (args.Length == 0) {
        Console.WriteLine ("Missing argument, syntax is:");
        Console.WriteLine ("  opt_server inputfile host:port numpolls [cert]");
      }
      else {

        string inputfile = args[0];
        string addr      = args[1];
        int    numpolls  = Convert.ToInt32(args[2]);
        String cert      = args.Length < 4 ? null : args[3];

        string token;

        using (mosek.Task task = new mosek.Task())
        {
          task.readdata (inputfile);
          if (cert != null)
            task.putstrparam(sparam.remote_tls_cert_path,cert);
          token = task.asyncoptimize (addr,"");
        }

        using (mosek.Task task = new mosek.Task())
        {
          task.readdata (inputfile);
          if (cert != null)
            task.putstrparam(sparam.remote_tls_cert_path,cert);
          task.set_Stream (mosek.streamtype.log, new msgclass ());
          Console.WriteLine("Starting polling loop...");

          int i = 0;

          while ( true )
          {
            Thread.Sleep(500);
            Console.WriteLine("poll {0}...\n", i);

            mosek.rescode resp, trm;
            bool respavailable;

            respavailable = task.asyncpoll(addr, "", token, out resp, out trm);

            Console.WriteLine("polling done");

            if (respavailable)
            {
              Console.WriteLine("solution available!");

              respavailable = task.asyncgetresult(addr, "", token, out resp, out trm);

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

            if (i == numpolls)
            {
              Console.WriteLine("max num polls reached, stopping host.");
              task.asyncstop (addr,"", token);
              break;
            }
            i++;
          }

        }
      }
    }
  }
}