8.9 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.
8.9.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
#include <fusion.h>
#include <stdlib.h>
#include <iostream>
using namespace mosek::fusion;
using namespace monty;
int main(int argc, char ** argv) {
if (argc<2) {
std::cout << "Missing argument, syntax is:" << std::endl;
std::cout << " opt_server_sync addr [certpath]" << std::endl;
exit(0);
}
std::string serveraddr(argv[1]);
std::string tlscert(argc==3 ? argv[2] : "");
// Setup a simple test problem
Model::t M = new Model("testOptServer"); auto _M = finally([&]() { M->dispose(); } );
Variable::t x = M->variable("x", 3, Domain::greaterThan(0.0));
M->constraint("lc", Expr::dot(new_array_ptr<double, 1>({1.0, 1.0, 2.0}), x), Domain::equalsTo(1.0));
M->objective("obj", ObjectiveSense::Minimize, Expr::sum(x));
// Attach log handler
M->setLogHandler([](const std::string & msg) { std::cout << msg << std::flush; } );
// Set OptServer URL
M->optserverHost(serveraddr);
// Path to certificate, if any
M->setSolverParam("remoteTlsCertPath", tlscert);
// Solve the problem on the OptServer
M->solve();
// Get the solution
std::cout << "x1,x2,x2 = " << *(x->level()) << std::endl;
return 0;
}