DOCloudSolver.cc 26.8 KB
Newer Older
1
2
3
4
5
6
//=============================================================================
//
//  CLASS DOCloudSolver - IMPLEMENTATION
//
//=============================================================================

7
// TODO: this uses Cbc for the MPS file export; consider implementing our own 
8
9
10
11
12
13
14
15
16
17
18
19
// MPS/LP export to remove the dependency.

//== INCLUDES =================================================================

//== COMPILE-TIME PACKAGE REQUIREMENTS ========================================
#include <CoMISo/Config/config.hh>
#if COMISO_DOCLOUD_AVAILABLE

//=============================================================================
#include "DOCloudSolver.hh"

#include <Base/Debug/DebTime.hh>
20
#include <Base/Debug/DebUtils.hh>
21
22
#include <Base/Utils/OutcomeUtils.hh>

23
24
25
26
#include <curl/curl.h>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
27
28

#include <stdexcept>
29
30
31
32
33
#include <iostream>
#include <algorithm>
#include <thread>         
#include <chrono>         

34
#include <stdio.h>
35
36
#include <stdlib.h>
#include <io.h>
37
38
39
40
41
42
43
44
45
46
47

DEB_module("DOCloudSolver")

//== NAMESPACES ===============================================================

namespace COMISO {

//== IMPLEMENTATION ==========================================================

namespace {

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
namespace cURLpp { // some classes to wrap around the libcurl C data

struct Session
{
  Session() { curl_global_init(CURL_GLOBAL_DEFAULT); }
  ~Session() { curl_global_cleanup(); }
};

// TODO: This inheritance could be restrictive in the future for now is OK
class Request
{
public:
  Request() : hnd_(curl_easy_init()), http_hdr_(nullptr) {}
  
  virtual ~Request() 
  { 
    curl_easy_cleanup(hnd_); 
    if (http_hdr_ != nullptr)
      curl_slist_free_all(http_hdr_);
  }

  //operator CURL*() { return hnd_; } 
  //operator const CURL*() const { return hnd_; } 

  bool valid() const { return hnd_ != nullptr; }

  void set_url(const char* _url)
  {
    curl_easy_setopt(hnd_, CURLOPT_URL, _url);
  }

  void add_http_header(const char* _hdr)
  {
    http_hdr_ = curl_slist_append(http_hdr_, _hdr);
  }

  // TODO: REMOVE this security hole!!
  void disable_ssl_verification()
  {
    /*
     * If you want to connect to a site who isn't using a certificate that is
     * signed by one of the certs in the CA bundle you have, you can skip the
     * verification of the server's certificate. This makes the connection
     * A LOT LESS SECURE.
     *
     * If you have a CA cert for the server stored someplace else than in the
     * default bundle, then the CURLOPT_CAPATH option might come handy for
     * you.
     */ 
    curl_easy_setopt(hnd_, CURLOPT_SSL_VERIFYPEER, 0L);
 
    /*
     * If the site you're connecting to uses a different host name that what
     * they have mentioned in their server certificate's commonName (or
     * subjectAltName) fields, libcurl will refuse to connect. You can skip
     * this check, but this will make the connection less secure.
     */ 
    curl_easy_setopt(hnd_, CURLOPT_SSL_VERIFYHOST, 0L);
  }

  void perform()
  {
    DEB_enter_func;

    prepare();

    // set the header we send to the server
    curl_easy_setopt(hnd_, CURLOPT_HTTPHEADER, http_hdr_);
    // set the write function to handle incoming data from the server
    curl_easy_setopt(hnd_, CURLOPT_WRITEFUNCTION, write_func);
    // set the string to store the incoming header data
    curl_easy_setopt(hnd_, CURLOPT_HEADERDATA, reinterpret_cast<void*>(&hdr_));
    // set the string to store the incoming main body (data)
    curl_easy_setopt(hnd_, CURLOPT_WRITEDATA, reinterpret_cast<void*>(&bdy_));
    // do the transmission
123
124
125
    CURLcode res;
    int try_nmbr = 0;
    do
126
    {
127
128
129
130
131
132
133
134
135
136
137
138
      // CURLE_SSL_CONNECT_ERROR is something that we are seeing a lot with 
      // the DOcloud service, a single retry is usually sufficient to recover.
      DEB_line_if(try_nmbr > 0, 2, "curl_easy_perform() retry #" << try_nmbr);
      res = curl_easy_perform(hnd_);
      if (res != CURLE_OK)
      {
        DEB_warning(1, "curl_easy_perform() failed with code: " << res << 
          ", message: " << curl_easy_strerror(res));
      }
    } while (res == CURLE_SSL_CONNECT_ERROR && try_nmbr++ < 10);

    THROW_OUTCOME_if(res != CURLE_OK, TODO);
139

140
141
    DEB_line(6, "Received Header: " << hdr_);
    DEB_line(6, "Received Body: " << bdy_);
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

    finalize();
  }

  const std::string& header() const { return hdr_; }
  const std::string& body() const { return bdy_; }

protected:
  static size_t write_func(const char* _ptr, const size_t _size, 
    const size_t _nmemb, void* _str)
  {
    // TODO: not sure how much exception-safe (e.g. out of memory this is!)
    size_t n_add = _size * _nmemb;
    if (n_add == 0)
      return 0;

    auto& str = *reinterpret_cast<std::string*>(_str);
    str.append(_ptr, n_add);
    return n_add;
  }

  // Apparently read callback function is required with Windows libcurl DLL!?
  //static size_t read_func(void* _ptr, const size_t _size, 
  //  const size_t _nmemb, FILE* _file)
  //{
  //  return fread(_ptr, _size, _nmemb, _file);
  //}


protected:
  CURL* hnd_;

protected:
  //virtual functions to control the perform() behavior
  virtual void prepare() {}
  virtual void finalize() {}

private:
  curl_slist* http_hdr_;
  std::string hdr_;
  std::string bdy_;
};

class Post : public Request
{
public: 
  Post(const char* _post) : post_(_post) {}
  Post(const std::string& _post) : post_(_post) {}

protected:
    virtual void prepare()
    {
      // set the post fields
      curl_easy_setopt(hnd_, CURLOPT_POSTFIELDS, post_.data());
      curl_easy_setopt(hnd_, CURLOPT_POSTFIELDSIZE, post_.size());
    }

private:
  std::string post_;
};

class Upload : public Request
{
public:
  Upload(const char* _filename) : filename_(_filename), file_(nullptr) {}
  Upload(const std::string& _filename) : filename_(_filename), file_(nullptr) {}
  virtual ~Upload()
  {
    if (file_ != nullptr)
      std::fclose(file_);
  }

protected:
  virtual void prepare();
  virtual void finalize();

private:
  std::string filename_;
  FILE* file_;
};

void Upload::prepare()
{
  /* tell it to "upload" to the URL */ 
  curl_easy_setopt(hnd_, CURLOPT_UPLOAD, 1L);

  /* set where to read from (on Windows you need to use READFUNCTION too) */ 
  file_ = std::fopen(filename_.data(), "rb");
  curl_easy_setopt(hnd_, CURLOPT_READDATA, file_);

  /* and give the size of the upload (optional) */ 
  const auto filelen = _filelength(fileno(file_));
  curl_easy_setopt(hnd_, CURLOPT_INFILESIZE_LARGE, (curl_off_t)filelen);

  /* we want to use our own read function */
  //curl_easy_setopt(hnd_, CURLOPT_READFUNCTION, read_func);

  /* enable verbose for easier tracing */ 
  //curl_easy_setopt(hnd_, CURLOPT_VERBOSE, 1L);
}

void Upload::finalize()
{
  DEB_enter_func;

  double rate, time;

  /* now extract transfer info */ 
  curl_easy_getinfo(hnd_, CURLINFO_SPEED_UPLOAD, &rate);
  curl_easy_getinfo(hnd_, CURLINFO_TOTAL_TIME, &time);

  DEB_double_format("%.2f");
  DEB_line(2, 
    "Upload speed: " << rate / 1024. << "Kbps; Time: " << time << "s.");
}

class Get : public Request
{
protected:
    virtual void prepare() { curl_easy_setopt(hnd_, CURLOPT_HTTPGET, 1L); }
};

class Delete : public Request
{
protected:
    virtual void prepare() 
    { 
      curl_easy_setopt(hnd_, CURLOPT_CUSTOMREQUEST, "DELETE"); 
    }
};

}//cURLpp 

namespace DOcloud {

static char* root_url__ = 
278
  "https://api-oaas.docloud.ibmcloud.com/job_manager/rest/v1/jobs";
279
static std::string api_key__ = 
280
  "X-IBM-Client-Id: api_0821c92f-0f2b-4ea5-be24-ecc9cd7695dd";
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
static char* app_type__ = "Content-Type: application/json";

class HeaderTokens
{
public:
  HeaderTokens(const std::string& _hdr)
  {
    // TODO: Performance can be improved by indexing, strtok_r(), etc ...
    //  ... but probably not worth the effort
    std::istringstream strm(_hdr);
    typedef std::istream_iterator<std::string> Iter;
    std::copy(Iter(strm), Iter(), std::back_inserter(tkns_));  
  }

  const std::string& operator[](const size_t _idx) const
  {
    return tkns_[_idx];
  }

  size_t number() const { return tkns_.size(); }

  // Find a token equal to the label and return its value (next token)
  bool find_value(const std::string& _lbl, std::string& _val) const
  {
    auto it = std::find(tkns_.begin(), tkns_.end(), _lbl);
    if (it == tkns_.end() || ++it == tkns_.end())
      return false;

    _val = *it;
    return true;
  }

  typedef std::vector<std::string>::const_iterator const_iterator;

  const_iterator begin() const { return tkns_.begin();}
  const_iterator end() const { return tkns_.end();}

private:
  std::vector<std::string> tkns_;
};

class JsonTokens
{
public:
  JsonTokens() {}
  JsonTokens(const std::string& _bdy) { set(_bdy); }

  void set(const std::string& _bdy)
  {
    ptree_.clear();
331
332
    if (_bdy.empty())
      return;
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
    std::istringstream strm(_bdy);
    boost::property_tree::json_parser::read_json(strm, ptree_);
  }

  //size_t number() const { return tkns_.size(); }

  // Find a token equal to the label and return its value
  bool find_value(const std::string& _lbl, std::string& _val) const
  {
    auto it = ptree_.find(_lbl);
    if (it == ptree_.not_found())
      return false;

    _val = it->second.get_value<std::string>();
    return true;
  }

  typedef boost::property_tree::ptree PTree;

  const PTree& ptree() const { return ptree_; }

private:
  PTree ptree_;
};

358
Debug::Stream& operator<<(Debug::Stream& _ds, const JsonTokens::PTree& _ptree)
359
360
{
  std::stringstream os;
361
  boost::property_tree::json_parser::write_json(os, _ptree);
362
363
364
365
  _ds << os.str();
  return _ds;
}

366
367
368
369
370
Debug::Stream& operator<<(Debug::Stream& _ds, const JsonTokens& _json_tkns)
{
  return _ds << _json_tkns.ptree();
}

371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
void throw_http_error(const int _err_code, const std::string& _bdy)
{
  DEB_enter_func;

  std::string err_msg;
  JsonTokens bdy_tkns(_bdy);
  bdy_tkns.find_value("message", err_msg);
  DEB_warning(1, "HTTP Status Code: " << _err_code << "; Message: " << err_msg);

  switch (_err_code)
  {
  case 400 : THROW_OUTCOME(TODO); // Invalid job creation data / status
  case 403 : THROW_OUTCOME(TODO); // Subscription limit exceeded
  case 404 : THROW_OUTCOME(TODO); // Requested job could not be found
  default : THROW_OUTCOME(TODO); // Unrecognized HTTP status code
  }
}

void check_http_error(
  const cURLpp::Request& _rqst, 
  const HeaderTokens& _hdr_tkns,
  const int code_ok = 201
  )
{
  const std::string http_lbl = "HTTP/1.1";
  const int code_cntn = 100; // continue code, ignore
  
  for (auto it = _hdr_tkns.begin(), it_end = _hdr_tkns.end(); it != it_end; 
    ++it)
  {
    if (*it != http_lbl) // search for the http label token
      continue; 
    THROW_OUTCOME_if(++it == it_end, TODO); // missing http code
    const auto code = atoi(it->data());

    if (code == code_ok) 
      return; // success code found, exit here
    else if (code == code_cntn)
      continue; // continue code found, continue
    else // another code found, throw an error
      throw_http_error(code, _rqst.body());
  }
  THROW_OUTCOME(TODO); // final http code not found
}


class Job : public cURLpp::Session
{
public:
  Job(const char* _filename) : filename_(_filename) {}
  Job(const std::string& _filename) : filename_(_filename) {}
  ~Job();

  void setup()
  {
    make();
    upload();
    start();
  }

  void wait();
  void sync_status();
433
  void sync_log();
434
  bool active() const; // requires synchronized status
435
436
437
438
439
440
441
442
  bool stalled() const 
  { 
    // exit quick if we have a solution, or wait 5 min if we don't have one
    return (sol_nmbr_ > 0 && stld_sec_nmbr_ >= 15) || 
      (sol_nmbr_ == 0 && stld_sec_nmbr_ >= 300);
  }

  void abort();
443
  double solution(std::vector<double>& _x) const;
444
445
446
447
448

private:
  const std::string filename_;
  std::string url_;
  JsonTokens stts_;
449
450
451
452
453
  // these variables are initialized in start() 
  int log_seq_idx_; // the log sequence number, used to get DOcloud log entires
  int sol_nmbr_; // number of solutions found so far, according to the log
  int sol_sec_nmbr_; // number of seconds at the last new solution 
  int stld_sec_nmbr_; // number of seconds since the last new solution 
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475

private:
  void make();
  void upload();
  void start();
};

Job::~Job()
{
  DEB_enter_func;

  if (url_.empty()) // not setup
    return;

  cURLpp::Delete del;
  if (!del.valid()) 
  {
    DEB_error("Failed to construct a delete request");
    return; // no point in throwing an exception here
  }

  del.set_url(url_.data());
476
  del.add_http_header(api_key__.c_str());
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
  del.perform();

  // no point in checking the return value either, we can't do much if the
  // delete request has failed
}

void Job::make()
{
  DEB_enter_func;

  cURLpp::Post post(std::string(
    "{\"attachments\" : [{\"name\" :\"" + filename_ + "\"}]}"));
  THROW_OUTCOME_if(!post.valid(), TODO); //Failed to initialize the request

  post.set_url(root_url__);
492
  post.add_http_header(api_key__.c_str());
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
  post.add_http_header(app_type__);
  post.perform();

  HeaderTokens hdr_tkns(post.header());
  check_http_error(post, hdr_tkns);
  // TODO: DOcloud header is successful but no location value
  THROW_OUTCOME_if(!hdr_tkns.find_value("Location:", url_), TODO); 
}

void Job::upload()
{
  cURLpp::Upload upload(filename_);
  THROW_OUTCOME_if(!upload.valid(), TODO); //Failed to initialize the request

  auto url = url_ + "/attachments/" + filename_ + "/blob";
  upload.set_url(url.data());
509
  upload.add_http_header(api_key__.c_str());
510
511
512
513
514
515
516
517
518
519
520
521
  upload.perform();
  HeaderTokens hdr_tkns(upload.header());
  check_http_error(upload, hdr_tkns, 204);
}

void Job::start()
{
  cURLpp::Post post("");
  THROW_OUTCOME_if(!post.valid(), TODO); //Failed to initialize the request

  auto url = url_ + "/execute";
  post.set_url(url.data());
522
  post.add_http_header(api_key__.c_str());
523
524
525
526
  post.add_http_header(app_type__);
  post.perform();
  HeaderTokens hdr_tkns(post.header());
  check_http_error(post, hdr_tkns, 204);
527
528

  log_seq_idx_ = sol_nmbr_ = sol_sec_nmbr_ = stld_sec_nmbr_ = 0;
529
530
531
532
}

void Job::sync_status()
{
533
534
  DEB_enter_func;

535
536
537
  cURLpp::Get get;
  THROW_OUTCOME_if(!get.valid(), TODO); //Failed to initialize the request
  get.set_url(url_.data());
538
  get.add_http_header(api_key__.c_str());
539
540
541
542
543
  get.perform();
  HeaderTokens hdr_tkns(get.header());
  check_http_error(get, hdr_tkns, 200);

  stts_.set(get.body());
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
  // The code below attempted to analyse the status data to find out the 
  // progress of the solver. This is an undocumented use and does not seem to 
  // work so far. Achieved here for potential use in the future.
  
  DEB_line(2, stts_);

  const auto& details = stts_.ptree().get_child_optional("details");
  if (!details)
    return;

  DEB_line(2, details.get());

  const auto& prg_gap = 
    details.get().get_child("PROGRESS_GAP").get_value<std::string>();
  std::string mip_gap;
  const auto mip_gap_it = details.get().find("cplex.mipabsgap");
  if (mip_gap_it != details.get().not_found())
    mip_gap = mip_gap_it->second.get_value<std::string>();

  DEB_line(2, "Status, MIP gap: " << mip_gap << "; Progress gap: " << prg_gap);
*/
}

void Job::sync_log()
{
  DEB_enter_func;

  cURLpp::Get get;
  THROW_OUTCOME_if(!get.valid(), TODO); //Failed to initialize the request
  const std::string url = url_ + "/log/items?start=" + 
    std::to_string(log_seq_idx_) + "&continuous=true";
  get.set_url(url.data());
577
  get.add_http_header(api_key__.c_str());
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
  get.perform();
  HeaderTokens hdr_tkns(get.header());
  check_http_error(get, hdr_tkns, 200);

  JsonTokens log(get.body());

  // iterate the log items, deb_out messages and analyze for solutions #
  for (const auto& log_item : log.ptree())
  {
    DEB_line_if(log_seq_idx_ == 0, 2, "**** DOcloud log ****");
    const auto& records = log_item.second.get_child("records");
    for (const auto& record : records)
    {// the message ends with \n
      const std::string msg = record.second.get_child("message").
        get_value<std::string>();
      DEB_out(2, record.second.get_child("level").get_value<std::string>() << 
        ": " << msg);

      const int time_str_len = 15;
      const char time_str[time_str_len + 1] = "Elapsed time = ";
      const auto time_str_idx = msg.find(time_str);
      if (time_str_idx == std::string::npos)
        continue;

      const int sec_nmbr = atoi(msg.data() + time_str_idx + time_str_len);
      //DEB_line(1, "# seconds elapsed : " << sec_nmbr);

      const int sol_str_len = 12;
      const char sol_str[sol_str_len + 1] = "solutions = ";
      const auto sol_str_idx = msg.find(sol_str);
      if (sol_str_idx == std::string::npos)
        continue;

      const int sol_nmbr = atoi(msg.data() + sol_str_idx + sol_str_len);
      //DEB_line(1, "# solutions found so far: " << sol_nmbr);
      if (sol_nmbr > sol_nmbr_) // new solution(s) found
      {// update the number of solutions and the time of the last solution found
        sol_nmbr_ = sol_nmbr;
        sol_sec_nmbr_ = sec_nmbr;
      }
      stld_sec_nmbr_ = sec_nmbr - sol_sec_nmbr_;
    }
    log_seq_idx_ = log_item.second.get_child("seqid").get_value<int>() + 1; 
  }
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
}

bool Job::active() const
{
  std::string exct_stts;
  stts_.find_value("executionStatus", exct_stts);
  
  // assume the job is not active if the status is not recognized
  return exct_stts == "CREATED" || exct_stts == "NOT_STARTED" || 
    exct_stts == "RUNNING" || exct_stts == "INTERRUPTING";

  /*
  Backup of old code converting execution status strings to enum value
  enum StatusType { ST_CREATED, ST_NOT_STARTED, ST_RUNNING, ST_INTERRUPTING, 
    ST_INTERRUPTED, ST_FAILED, ST_PROCESSED, ST_UNKNOWN };

  const int n_stts = (int)ST_UNKNOWN;
  const char stts_tbl[n_stts][16] = { "CREATED", "NOT_STARTED", "RUNNING", 
    "INTERRUPTING", "INTERRUPTED", "FAILED", "PROCESSED" };

  for (int i = 0; i < n_stts; ++i)
  {
    if (stts == stts_tbl[i])
      return (StatusType)i;
  }
  return ST_UNKNOWN;
  while (stts == ST_CREATED || stts == ST_NOT_STARTED || stts == ST_RUNNING ||
      stts == ST_INTERRUPTING);
  */
}

653
654
655
656
657
658
659
660
661
662
663
void Job::abort()
{
  std::string exct_stts;
  stts_.find_value("executionStatus", exct_stts);
  if (exct_stts != "RUNNING")
    return; // already aborted or aborting

  cURLpp::Delete del;
  THROW_OUTCOME_if(!del.valid(), TODO); //Failed to initialize the request
  const std::string url = url_ + "/execute";
  del.set_url(url.data());
664
  del.add_http_header(api_key__.c_str());
665
666
667
668
669
670
  del.perform();

  HeaderTokens hdr_tkns(del.header());
  check_http_error(del, hdr_tkns, 204);
}

671
672
673
674
675
676
void Job::wait()
{
  do 
  {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    sync_status();
677
678
679
    sync_log();
    if (stalled())
      abort();
680
681
682
  } while (active());
}

683
double Job::solution(std::vector<double>& _x) const
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
{
  DEB_enter_func;

  // check the solution status (assume it's synchronized already)

  // What are the possible values for this??
  std::string slv_stts;
  stts_.find_value("solveStatus", slv_stts);
  DEB_line(2, "solveStatus=" << slv_stts);
  
  cURLpp::Get get;
  THROW_OUTCOME_if(!get.valid(), TODO); //Failed to initialize the request

  auto url = url_ + "/attachments/solution.json/blob";
  get.set_url(url.data());
699
  get.add_http_header(api_key__.c_str());
700
701
702
703
704
705
  get.perform();

  HeaderTokens hdr_tkns(get.header());
  check_http_error(get, hdr_tkns, 200);

  JsonTokens bdy_tkns(get.body());
706
  DEB_line(7, bdy_tkns);
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721

  const auto& vrbls = bdy_tkns.ptree().get_child("CPLEXSolution.variables");
  const auto n_vrbls = vrbls.size();
  THROW_OUTCOME_if(n_vrbls != _x.size(), TODO); // Solution variables number does not match
  
  size_t i = 0;
  for (const auto& v : vrbls)
  {
    // TODO: this way of conversion is rather hacky
    const std::string name = 
      v.second.get_child("name").get_value<std::string>(); // this is x#IDX
    const int idx = atoi(name.data() + 1);
    THROW_OUTCOME_if(idx < 0 || idx > n_vrbls, TODO);  // Invalid index
    _x[idx] = v.second.get_child("value").get_value<double>();

722
    DEB_out(7, "#" << idx << "=" << 
723
724
      v.second.get_child("value").get_value<std::string>() << "; ");
  }
725
726
  const auto obj_val = 
    bdy_tkns.ptree().get_child("CPLEXSolution.header.objectiveValue").get_value<double>();
727

728
  DEB_line(3, "X=" << _x);
729
  return obj_val;
730
731
732
733
734
}


} // namespace DOcloud

735
std::string lp_file_name()
736
{
737
738
739
740
741
742
743
  // TODO: This is not MT-safe, it's not even process-safe!
  static int n_mps = 0;
  std::string filename("DOcloud_problem_");
  filename += std::to_string(n_mps++);
  return filename + ".lp";
}

744
#define P(X) ((X).data())
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#define XVAR(IDX) "x" << IDX

class WriteExpression
{
  // lp format allows a max line length = 560.
  // For simplicity we put end line when the number of written characters on the
  // same line exceeds LINE_TRESHOLD_LEN.
  enum { LINE_TRESHOLD_LEN = 100 };

public:
  WriteExpression(std::ofstream& _out_str) : out_str_(_out_str)
  {
    start();
  }

  void start()
  {
    f_size_ = out_str_.tellp();
    at_start_ = true;
  }
  
  // Writes a monomial.
  void add_monomial(const double _coeff, const size_t _i_var)
  {
    if (_coeff == 0)
      return;
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
    add_monomial_internal(_coeff, _i_var);
    wrap_long_line();
  }

  // Writes a binomial.
  void add_binomial(const double _coeff, const size_t _i_var, const size_t _j_var)
  {
    if (_coeff == 0)
      return;
    add_monomial_internal(_coeff, _i_var);
    if (_j_var == _i_var)
      out_str_ << "^2";
    else
      out_str_ << " * " << XVAR(_j_var);
    wrap_long_line();
  }

private:

  void wrap_long_line()
  {
    const auto new_f_size = out_str_.tellp();
    if (new_f_size - f_size_ > LINE_TRESHOLD_LEN)
    {
      out_str_ << std::endl;
      f_size_ = new_f_size;
    }
  }

  void add_monomial_internal(const double _coeff, const size_t _i_var)
  {
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
    if (_coeff == 1)
    {
      if (!at_start_)
        out_str_ << " + ";
    }
    else if (_coeff == -1)
      out_str_ << " - ";
    else
    {
      if (!at_start_)
      {
        if (_coeff > 0)
          out_str_ << " + ";
        else
          out_str_ << ' ';
      }
      out_str_ << _coeff << ' ';
    }
    out_str_ << XVAR(_i_var);
    at_start_ = false;
  }

private:
  std::ofstream& out_str_;
  std::fstream::pos_type f_size_;
  bool at_start_;
};
829

830
831
832
833
834
835
836
837
838
839
// Create a lp file for the given constraints and object function.
// Here is the lp format specifications:
// http://www-01.ibm.com/support/knowledgecenter/SSSA5P_12.6.1/ilog.odms.cplex.help/CPLEX/FileFormats/topics/LP.html
std::string create_lp_file(
  NProblemInterface* _problem,
  const std::vector<NConstraintInterface*>& _constraints,
  const std::vector<PairIndexVtype>& _discrete_constraints,
  const std::vector<double>& _x
  )
{
840
841
  const int n_cols = _problem->n_unknowns(); // Unknowns #

842
843
844
  const std::string f_name(lp_file_name());
  std::ofstream lp_file(f_name);
  THROW_OUTCOME_if(!lp_file.is_open(), TODO);
845

846
847
  // Set the ofstream options.
  lp_file << std::setprecision(std::numeric_limits<double>::digits10 + 2);
848

849
850
  lp_file << "\\Problem name: " << std::endl << std::endl;
  lp_file << "Minimize" << std::endl;
851

852
853
  // Writes objective function.
  lp_file << "obj: ";
854

855
856
  WriteExpression wrte_expr(lp_file);
  // 1. Linear part.
857
858
859
860
861
  std::vector<double> objective(n_cols);
  _problem->eval_gradient(P(_x), P(objective));
  for (size_t i = 0; i < objective.size(); ++i)
    wrte_expr.add_monomial(objective[i], i);

862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
  // 2. Quadratic part (if requested).
  if (!_problem->constant_gradient())
  {
    NProblemInterface::SMatrixNP H;
    _problem->eval_hessian(P(_x), H);
    lp_file << " + [ ";
    for (int i = 0; i < H.outerSize(); ++i)
    {
      for (NProblemInterface::SMatrixNP::InnerIterator it(H, i); it; ++it)
        wrte_expr.add_binomial(it.value(), it.row(), it.col());
    }
    lp_file << " ] / 2";
  }


877
878
879
  // Writes constraints.
  lp_file << std::endl << "Subject To" << std::endl;
  for (const auto& cstr : _constraints)
880
881
  {
    NConstraintInterface::SVectorNC gc;
882
    cstr->eval_gradient(P(_x), gc);
883

884
    wrte_expr.start();
885
886
    for (NConstraintInterface::SVectorNC::InnerIterator v_it(gc); v_it; ++v_it)
    {
887
888
889
890
891
892
893
      auto coeff = v_it.value();
      wrte_expr.add_monomial(coeff, v_it.index());
    }
    switch (cstr->constraint_type())
    {
    case NConstraintInterface::NC_EQUAL:
      lp_file << " = ";
894
      break;
895
896
    case NConstraintInterface::NC_GREATER_EQUAL:
      lp_file << " >= ";
897
      break;
898
899
    case NConstraintInterface::NC_LESS_EQUAL:
      lp_file << " <= ";
900
      break;
901
902
    default:
      THROW_OUTCOME(TODO); // Can not express current constraint.
903
    }
904
    lp_file << -cstr->eval_constraint(P(_x)) << std::endl;
905
906
  }

907
908
909
910
  // Writes the variables.
  lp_file << "Bounds" << std::endl;
  for (size_t i = 0; i < n_cols; ++i)
    lp_file << XVAR(i) << " Free" << std::endl;
911

912
913
914
  // Integer and binary variables.
  std::vector<unsigned int> int_var, bin_var;
  for (const auto& dc : _discrete_constraints)
915
  {
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
    if (dc.second == Integer)
      int_var.push_back(dc.first);
    else if (dc.second == Binary)
      bin_var.push_back(dc.first);
  }
  auto write_var_set = [&lp_file](const std::vector<unsigned int>& _vars,
    const char* _type)
  {
    if (_vars.empty())
      return;
    // Writes integer variables.
    lp_file << _type << std::endl;
    auto var_it = _vars.begin();
    lp_file << XVAR(*var_it);
    size_t n_wrt_var = 1;
    while (++var_it != _vars.end())
932
    {
933
934
935
936
937
      if (n_wrt_var++ % 16) // 16 variables per line. Lines length must be < 560.
        lp_file << ' ';
      else
        lp_file << std::endl;
      lp_file << XVAR(*var_it);
938
    }
939
940
941
942
943
944
945
946
947
948
949
950
951
952
    lp_file << std::endl;

  };
  // Writes integer variables.
  write_var_set(int_var, "Integers");

  // Writes Binary variables.
  write_var_set(bin_var, "Binary");

  lp_file << "End";

  return f_name;
}

953
#undef XVAR 
954

955
}// namespace 
956
957


958
959
960
961
void DOCloudSolver::set_api_key(const char* _api_key)
{
  DOcloud::api_key__ = std::string("X-IBM-Client-Id: ") + _api_key;
}
962

963
void DOCloudSolver::solve(
964
965
966
  NProblemInterface*                        _problem,
  const std::vector<NConstraintInterface*>& _constraints,
  const std::vector<PairIndexVtype>&        _discrete_constraints,
967
  const double                              _time_limit
968
969
970
971
972
973
974
975
976
977
978
)
{
  DEB_enter_func;
  DEB_warning_if(!_problem->constant_hessian(), 1,
    "DOCloudSolver received a problem with non-constant hessian!");
  DEB_warning_if(!_problem->constant_gradient(), 1,
    "DOCloudSolver received a problem with non-constant gradient!");

  std::vector<double> x(_problem->n_unknowns(), 0.0); // solution
  std::string filename = create_lp_file(_problem, _constraints, 
    _discrete_constraints, x);
979

980
981
982
  DOcloud::Job job(filename);
  job.setup();
  job.wait();
983
984
985
986
987
988
989
990
991
992
  DEB_only(const auto dc_obj_val = ) job.solution(x);

  DEB_only(
  // The lp problem ignores the constant term in the objective function.
  const std::vector<double> x_zero(_problem->n_unknowns(), 0.0);
  const auto zero_val = _problem->eval_f(P(x_zero));
  const auto obj_val = _problem->eval_f(P(x));
  DEB_error_if(fabs(dc_obj_val + zero_val - obj_val) > (1e-10 + zero_val * 0.01),
               "DOCloudSolver solved a wrong problem.");
  )
993
994
  
  _problem->store_result(P(x));
995
996
997
998
999
1000
1001
1002
1003
1004
1005
}

#undef P

//=============================================================================
} // namespace COMISO
//=============================================================================

#endif // COMISO_DOCLOUD_AVAILABLE
//=============================================================================