11.3 Concurrent optimizer

The idea of the concurrent optimizer is to run multiple optimizations of the same problem simultaneously, and pick the one that provides the fastest or best answer. This approach is especially useful for problems which require a very long time and it is hard to say in advance which optimizer or algorithm will perform best.

The major applications of concurrent optimization we describe in this section are:

  • Using the interior-point and simplex optimizers simultaneously on a linear problem. Note that any solution present in the task will also be used for hot-starting the simplex algorithms. One possible scenario would therefore be running a hot-start simplex in parallel with interior point, taking advantage of both the stability of the interior-point method and the ability of the simplex method to use an initial solution.
  • Using multiple instances of the mixed-integer optimizer to solve many copies of one mixed-integer problem. This is not in contradiction with the run-to-run determinism of MOSEK if a different value of the MIO seed parameter iparam.mio_seed is set in each instance. As a result each setting leads to a different optimizer run (each of them being deterministic in its own right).

The downloadable file contains usage examples of both kinds.

11.3.1 Common setup

We first define a method that runs a number of optimization tasks in parallel, using the standard multithreading setup available in the language. All tasks register for a callback function which will signal them to interrupt as soon as the first task completes successfully (with response code rescode.ok).

Listing 11.12 Simple callback function which signals the optimizer to stop. Click here to download.
    /**
       Defines a Mosek callback function whose only function
       is to indicate if the optimizer should be stopped.
     */
    private class CallbackProxy : mosek.Progress
    {
      private bool stop;
      public CallbackProxy()
      {
        stop = false;
      }

      public override int progressCB(mosek.callbackcode caller)
      {
        // Return non-zero implies terminate the optimizer
        return stop ? 1 : 0;
      }

      public bool Stop
      {
        get { return stop; }
        set { stop = value; }
      }
    }

When all remaining tasks respond to the stop signal, response codes and statuses are returned to the caller, together with the index of the task which won the race.

Listing 11.13 A routine for parallel task race. Click here to download.
    public static bool optimize(mosek.Task[]    tasks,
                                mosek.rescode[] res,
                                mosek.rescode[] trm,
                                out int         firstOK)
    {
      var n = tasks.Length;
      var jobs = new System.Threading.Tasks.Task[n];
      int firstStop = -1;

      // Set a callback function 
      var cb = new CallbackProxy();
      for (var i = 0; i < n; ++i)
        tasks[i].set_Progress(cb);
      
      // Initialize
      for (var i = 0; i < n; ++i) 
      {
        res[i] = mosek.rescode.err_unknown;
        trm[i] = mosek.rescode.err_unknown;
      }

      // Start parallel optimizations, one per task
      for (var i = 0; i < n; ++i)
      {
        int num = i;
        jobs[i] = System.Threading.Tasks.Task.Factory.StartNew( () => {
          try
          {
            trm[num] = tasks[num].optimize();
            res[num] = mosek.rescode.ok;
          }
          catch (mosek.Exception e)
          {
            trm[num] = mosek.rescode.err_unknown;
            res[num] = e.Code;
          }
          finally
          {
            // If this finished with success, inform other tasks to interrupt
            if (res[num] == mosek.rescode.ok)
            {
              if (!cb.Stop) firstStop = num;
              cb.Stop = true;
            }
          }
        } );
      }

      // Join all threads
      foreach (var j in jobs)
        j.Wait();

      // For debugging, print res and trm codes for all optimizers
      for (var i = 0; i < n; ++i)
        Console.WriteLine("Optimizer  {0}  res {1}   trm {2}", i, res[i], trm[i]);

      firstOK = firstStop;
      return cb.Stop;
    }

11.3.2 Linear optimization

We use the multithreaded setup to run the interior-point and simplex optimizers simultaneously on a linear problem. The next methods simply clones the given task and sets a different optimizer for each. The result is the clone which finished first.

Listing 11.14 Concurrent optimization with different optimizers. Click here to download.
    public static int  optimizeconcurrent(mosek.Task            task, 
                                          int[]                 optimizers,
                                          out mosek.Task        winTask,
                                          out mosek.rescode     winTrm,
                                          out mosek.rescode     winRes)
    {
      var n = optimizers.Length;
      var tasks = new mosek.Task[n];
      var res   = new mosek.rescode[n];
      var trm   = new mosek.rescode[n];

      // Clone tasks and choose various optimizers
      for (var i = 0; i < n; ++i)
      {
        tasks[i] = new mosek.Task(task);
        tasks[i].putintparam(mosek.iparam.optimizer, optimizers[i]);
      }

      // Solve tasks in parallel
      bool success;
      int firstOK;
      success = optimize(tasks, res, trm, out firstOK);

      if (success) 
      {
        winTask  = tasks[firstOK]; 
        winTrm   = trm[firstOK]; 
        winRes   = res[firstOK];
        return firstOK;
      }
      else
      {
        winTask  = null; 
        winTrm   = 0; 
        winRes   = 0;        
        return -1;
      }
    }

It remains to call the method with a choice of optimizers, for example:

Listing 11.15 Calling concurrent linear optimization. Click here to download.
            int[] optimizers = { 
              mosek.optimizertype.conic,
              mosek.optimizertype.dual_simplex,
              mosek.optimizertype.primal_simplex
            };

            idx = optimizeconcurrent(task, optimizers, out t, out trm, out res);

11.3.3 Mixed-integer optimization

We use the multithreaded setup to run many, differently seeded copies of the mixed-integer optimizer. This approach is most useful for hard problems where we don’t expect an optimal solution in reasonable time. The input task would typically contain a time limit. It is possible that all the cloned tasks reach the time limit, in which case it doesn’t really mater which one terminated first. Instead we examine all the task clones for the best objective value.

Listing 11.16 Concurrent optimization of a mixed-integer problem. Click here to download.
    public static int  optimizeconcurrentMIO(mosek.Task            task, 
                                             int[]                 seeds,
                                             out mosek.Task        winTask,
                                             out mosek.rescode     winTrm,
                                             out mosek.rescode     winRes)
    {
      var n = seeds.Length;
      var tasks = new mosek.Task[n];
      var res   = new mosek.rescode[n];
      var trm   = new mosek.rescode[n];

      // Clone tasks and choose various seeds for the optimizer
      for (var i = 0; i < n; ++i)
      {
        tasks[i] = new mosek.Task(task);
        tasks[i].putintparam(mosek.iparam.mio_seed, seeds[i]);
      }

      // Solve tasks in parallel
      bool success;
      int firstOK;
      success = optimize(tasks, res, trm, out firstOK);

      if (success) 
      {
        // Pick the task that ended with res = ok
        // and contains an integer solution with best objective value
        mosek.objsense sense = task.getobjsense();
        double bestObj = (sense == mosek.objsense.minimize) ? 1.0e+10 : -1.0e+10;
        int bestPos = -1;

        for (var i = 0; i < n; ++i)
          Console.WriteLine("{0}   {1}   ", i, tasks[i].getprimalobj(mosek.soltype.itg));

        for (var i = 0; i < n; ++i)
          if ((res[i] == mosek.rescode.ok) &&
              (tasks[i].getsolsta(mosek.soltype.itg) == mosek.solsta.prim_feas ||
               tasks[i].getsolsta(mosek.soltype.itg) == mosek.solsta.integer_optimal) &&
              ((sense == mosek.objsense.minimize) ? 
                  (tasks[i].getprimalobj(mosek.soltype.itg) < bestObj) :
                  (tasks[i].getprimalobj(mosek.soltype.itg) > bestObj)   )   )
          {
            bestObj = tasks[i].getprimalobj(mosek.soltype.itg);
            bestPos = i;
          }

        if (bestPos != -1)
        {
          winTask  = tasks[bestPos]; 
          winTrm   = trm[bestPos]; 
          winRes   = res[bestPos];
          return bestPos;
        }
      }

      winTask  = null; 
      winTrm   = 0; 
      winRes   = 0;        
      return -1;
    }

It remains to call the method with a choice of seeds, for example:

Listing 11.17 Calling concurrent integer optimization. Click here to download.
            int[] seeds = { 42, 13, 71749373 };

            idx = optimizeconcurrentMIO(task, seeds, out t, out trm, out res);