File: MIP_xpress_wrap.cpp

package info (click to toggle)
minizinc 2.9.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 17,548 kB
  • sloc: cpp: 74,695; ansic: 8,541; python: 3,301; sh: 79; makefile: 13
file content (677 lines) | stat: -rw-r--r-- 26,619 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
278
279
280
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
331
332
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
358
359
360
361
362
363
364
365
366
367
368
369
370
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
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
577
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
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
 *  main authors:
 *     Karsten Lehmann <karsten@satalia.com>
 */

/* this source code form is subject to the terms of the mozilla public
 * license, v. 2.0. if a copy of the mpl was not distributed with this
 * file, you can obtain one at http://mozilla.org/mpl/2.0/. */

#include <minizinc/config.hh>
#include <minizinc/exception.hh>
#include <minizinc/file_utils.hh>
#include <minizinc/solvers/MIP/MIP_xpress_wrap.hh>
#include <minizinc/utils.hh>

#include <cmath>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>

struct UserSolutionCallbackData {
  MIPWrapper::CBUserInfo* info;
  XPRBprob* problem;
  vector<XPRBvar>* variables;
  XpressPlugin* plugin;
};

class XpressException : public runtime_error {
public:
  XpressException(const string& msg) : runtime_error(" MIPxpressWrapper: " + msg) {}
};

XpressPlugin::XpressPlugin() : _inner(XpressPlugin::dlls()) { loadDll(); }

XpressPlugin::XpressPlugin(const std::string& dll_file) : _inner(dll_file) { loadDll(); }

void XpressPlugin::loadDll() {
  load_symbol_dynamic(_inner, XPRSinit);
  load_symbol_dynamic(_inner, XPRSfree);
  load_symbol_dynamic(_inner, XPRSgetversion);
  load_symbol_dynamic(_inner, XPRSgetlicerrmsg);
  load_symbol_dynamic(_inner, XPRBgetXPRSprob);
  load_symbol_dynamic(_inner, XPRBsetmsglevel);
  load_symbol_dynamic(_inner, XPRSsetlogfile);
  load_symbol_dynamic(_inner, XPRSsetintcontrol);
  load_symbol_dynamic(_inner, XPRSsetdblcontrol);
  load_symbol_dynamic(_inner, XPRBgetsol);
  load_symbol_dynamic(_inner, XPRSgetintattrib);
  load_symbol_dynamic(_inner, XPRSgetdblattrib);
  load_symbol_dynamic(_inner, XPRBbegincb);
  load_symbol_dynamic(_inner, XPRBsync);
  load_symbol_dynamic(_inner, XPRBendcb);
  load_symbol_dynamic(_inner, XPRBsetterm);
  load_symbol_dynamic(_inner, XPRBnewvar);
  load_symbol_dynamic(_inner, XPRBnewctr);
  load_symbol_dynamic(_inner, XPRBsetctrtype);
  load_symbol_dynamic(_inner, XPRBexportprob);
  load_symbol_dynamic(_inner, XPRBgetbounds);
  load_symbol_dynamic(_inner, XPRBsetobj);
  load_symbol_dynamic(_inner, XPRBmipoptimize);
  load_symbol_dynamic(_inner, XPRBsetsense);
  load_symbol_dynamic(_inner, XPRSsetcbintsol);
  load_symbol_dynamic(_inner, XPRBsetub);
  load_symbol_dynamic(_inner, XPRBsetlb);
  load_symbol_dynamic(_inner, XPRBsetindicator);
  load_symbol_dynamic(_inner, XPRBnewsol);
  load_symbol_dynamic(_inner, XPRBsetsolvar);
  load_symbol_dynamic(_inner, XPRBaddmipsol);
  load_symbol_dynamic(_inner, XPRBnewprob);
  load_symbol_dynamic(_inner, XPRBdelprob);
  load_symbol_dynamic(_inner, XPRSgetcontrolinfo);
  load_symbol_dynamic(_inner, XPRSgetintcontrol);
  load_symbol_dynamic(_inner, XPRSgetintcontrol64);
  load_symbol_dynamic(_inner, XPRSgetdblcontrol);
  load_symbol_dynamic(_inner, XPRSgetstrcontrol);
  load_symbol_dynamic(_inner, XPRSsetintcontrol64);
  load_symbol_dynamic(_inner, XPRSgetstringcontrol);
  load_symbol_dynamic(_inner, XPRSsetstrcontrol);
}

const std::vector<std::string>& XpressPlugin::dlls() {
  static std::vector<std::string> ret = {
#ifdef _WIN32
      "xprs", "C:\\xpressmp\\bin\\xprs.dll"
#elif __APPLE__
      "libxprs", "/Applications/FICO Xpress/xpressmp/lib/libxprs.dylib"
#else
      "libxprs", "/opt/xpressmp/lib/libxprs.so"
#endif
  };
  return ret;
}

void MIPxpressWrapper::openXpress() {
  checkDLL();
  _problem = _plugin->XPRBnewprob(nullptr);
  _xpressObj = _plugin->XPRBnewctr(_problem, nullptr, XB_N);
}

void MIPxpressWrapper::closeXpress() {
  _plugin->XPRBdelprob(_problem);
  _plugin->XPRSfree();
  delete _plugin;
}

void MIPxpressWrapper::checkDLL() {
  if (!_factoryOptions.xpressDll.empty()) {
    _plugin = new XpressPlugin(_factoryOptions.xpressDll);
  } else {
    _plugin = new XpressPlugin();
  }

  std::vector<std::string> paths;
  if (!_factoryOptions.xprsPassword.empty()) {
    paths.push_back(_factoryOptions.xprsPassword);
  } else {
    paths.emplace_back("");  // Try builtin xpress dirs
    auto dir = MiniZinc::FileUtils::dir_name(_plugin->path());
    auto file = dir + "/../bin/xpauth.xpr";
    if (!dir.empty() && MiniZinc::FileUtils::file_exists(file)) {
      paths.push_back(file);  // Try the bin dir license file if it exists
    }
  }

  for (const auto& path : paths) {
    int ret = _plugin->XPRSinit(path.empty() ? nullptr : path.c_str());
    if (ret == 0) {
      return;
    }
    // Return code of 32 means student licence, but otherwise it's an error
    if (ret == 32) {
      if (_options->verbose) {
        char message[512];
        _plugin->XPRSgetlicerrmsg(message, 512);
        std::cerr << message << std::endl;
      }
      return;
    }
  }

  char message[512];
  _plugin->XPRSgetlicerrmsg(message, 512);
  throw XpressException(message);
}

string MIPxpressWrapper::getDescription(FactoryOptions& factoryOpt,
                                        MiniZinc::SolverInstanceBase::Options* opt) {
  ostringstream oss;
  oss << "  MIP wrapper for FICO Xpress Optimiser version " << getVersion(factoryOpt, opt);
  return oss.str();
}

string MIPxpressWrapper::getVersion(FactoryOptions& factoryOpt,
                                    MiniZinc::SolverInstanceBase::Options* opt) {
  try {
    auto* p =
        factoryOpt.xpressDll.empty() ? new XpressPlugin : new XpressPlugin(factoryOpt.xpressDll);
    char v[16];
    p->XPRSgetversion(v);
    delete p;
    return v;
  } catch (MiniZinc::PluginError&) {
    return "<unknown version>";
  }
}

vector<string> MIPxpressWrapper::getRequiredFlags(FactoryOptions& factoryOpt) {
  Options opts;
  FactoryOptions triedFactoryOpts;
  vector<string> ret;
  // TODO: This is more complex than it should be
  // We only know if --xpress-password is required if we have the DLL available
  // So we have to try the user supplied --xpress-dll if given
  while (true) {
    try {
      // Try opening without considering factory options
      MIPxpressWrapper w(triedFactoryOpts, &opts);
      return ret;
    } catch (MiniZinc::PluginError&) {
      ret.emplace_back("--xpress-dll");  // The DLL needs to be given
      if (triedFactoryOpts.xpressDll == factoryOpt.xpressDll) {
        return ret;
      }
      triedFactoryOpts.xpressDll = factoryOpt.xpressDll;
    } catch (XpressException&) {
      ret.emplace_back("--xpress-password");  // The license needs to be given
      if (triedFactoryOpts.xprsPassword == factoryOpt.xprsPassword) {
        return ret;
      }
      triedFactoryOpts.xprsPassword = factoryOpt.xprsPassword;
    }
  }
}

vector<string> MIPxpressWrapper::getFactoryFlags() {
  return {"--xpress-dll", "--xpress-password"};
};

string MIPxpressWrapper::getId() { return "xpress"; }

string MIPxpressWrapper::getName() { return "Xpress"; }

vector<string> MIPxpressWrapper::getTags() { return {"mip", "float", "api"}; }

vector<string> MIPxpressWrapper::getStdFlags() { return {"-i", "-s", "-p", "-r"}; }

vector<MiniZinc::SolverConfig::ExtraFlag> MIPxpressWrapper::getExtraFlags(
    FactoryOptions& factoryOpt) {
  try {
    Options opts;
    MIPxpressWrapper p(factoryOpt, &opts);

    auto* prb = p._plugin->XPRBgetXPRSprob(p._problem);
    // Using string parameter names because there doesn't seem to be a way to recover
    // the name from a parameter ID number
    static std::vector<std::string> all_params = {
        "algaftercrossover", "algafternetwork", "autoperturb", "backtrack", "backtracktie",
        "baralg", "barcrash", "bardualstop", "barfreescale", "bargapstop", "bargaptarget",
        "barindeflimit", "bariterlimit", "barkernel", "barobjscale", "barorder", "barorderthreads",
        "baroutput", "barpresolveops", "barprimalstop", "barregularize", "barrhsscale",
        "barsolution", "barstart", "barstartweight", "barstepstop", "barthreads", "barcores",
        "bigm", "bigmmethod", "branchchoice", "branchdisj", "branchstructural", "breadthfirst",
        "cachesize", "callbackfrommasterthread", "choleskyalg", "choleskytol", "conflictcuts",
        "concurrentthreads", "corespercpu", "covercuts", "cpuplatform", "cputime", "crash",
        "crossover", "crossoveraccuracytol", "crossoveriterlimit", "crossoverops",
        "crossoverthreads", "cstyle", "cutdepth", "cutfactor", "cutfreq", "cutstrategy",
        "cutselect", "defaultalg", "densecollimit", "deterministic", "dualgradient", "dualize",
        "dualizeops", "dualperturb", "dualstrategy", "dualthreads", "eigenvaluetol", "elimfillin",
        "elimtol", "etatol", "extracols", "extraelems", "extramipents", "extrapresolve",
        "extraqcelements", "extraqcrows", "extrarows", "extrasetelems", "extrasets",
        "feasibilitypump", "feastol", "feastoltarget", "forceoutput", "forceparalleldual",
        "globalfilebias", "globalfileloginterval", "gomcuts", "heurbeforelp", "heurdepth",
        "heurdiveiterlimit", "heurdiverandomize", "heurdivesoftrounding", "heurdivespeedup",
        "heurdivestrategy", "heurforcespecialobj", "heurfreq", "heurmaxsol", "heurnodes",
        "heursearcheffort", "heursearchfreq", "heursearchrootcutfreq", "heursearchrootselect",
        "heursearchtreeselect", "heurstrategy", "heurthreads", "historycosts", "ifcheckconvexity",
        "indlinbigm", "indprelinbigm", "invertfreq", "invertmin", "keepbasis", "keepnrows",
        "l1cache", "linelength", "lnpbest", "lnpiterlimit", "lpflags", "lpiterlimit",
        "lprefineiterlimit", "localchoice", "lpfolding", "lplog", "lplogdelay", "lplogstyle",
        "lpthreads", "markowitztol", "matrixtol", "maxchecksonmaxcuttime", "maxchecksonmaxtime",
        "maxmcoeffbufferelems", "maxcuttime", "maxglobalfilesize", "maxiis", "maximpliedbound",
        "maxlocalbacktrack", "maxmemoryhard", "maxmemorysoft", "maxmiptasks", "maxmipsol",
        "maxnode", "maxpagelines", "maxscalefactor", "maxtime", "mipabscutoff", "mipabsgapnotify",
        "mipabsgapnotifybound", "mipabsgapnotifyobj", "mipabsstop", "mipaddcutoff",
        "mipdualreductions", "mipfracreduce", "mipkappafreq", "miplog", "mippresolve", "miprampup",
        "miqcpalg", "miprefineiterlimit", "miprelcutoff", "miprelgapnotify", "miprelstop",
        "mipterminationmethod", "mipthreads", "miptol", "miptoltarget", "mps18compatible",
        "mpsboundname", "mpsecho", "mpsformat", "mpsobjname", "mpsrangename", "mpsrhsname",
        "mutexcallbacks", "netcuts", "nodeselection", "objscalefactor", "optimalitytol",
        "optimalitytoltarget", "outputlog", "outputmask", "outputtol", "penalty", "perturb",
        "pivottol", "ppfactor", "preanalyticcenter", "prebasisred", "prebndredcone",
        "prebndredquad", "precoefelim", "precomponents", "precomponentseffort", "preconedecomp",
        "preconvertseparable", "predomcol", "predomrow", "preduprow", "preelimquad",
        "preimplications", "prelindep", "preobjcutdetect", "prepermute", "prepermuteseed",
        "preprobing", "preprotectdual", "presolve", "presolvemaxgrow", "presolveops",
        "presolvepasses", "presort", "pricingalg", "primalops", "primalperturb", "primalunshift",
        "pseudocost", "qccuts", "qcrootalg", "qsimplexops", "quadraticunshift",
        //"randomseed",
        "refactor", "refineops", "relaxtreememorylimit", "relpivottol", "repairindefiniteq",
        "repairinfeasmaxtime", "resourcestrategy", "rootpresolve", "sbbest", "sbeffort",
        "sbestimate", "sbiterlimit", "sbselect", "scaling", "sifting", "sleeponthreadwait",
        "sosreftol", "symmetry", "symselect",
        //"threads",
        "trace", "treecompression", "treecovercuts", "treecutselect", "treediagnostics",
        "treegomcuts", "treememorylimit", "treememorysavingtarget", "treepresolve",
        "treepresolve_keepbasis", "treeqccuts", "tunerhistory", "tunermaxtime", "tunermethod",
        "tunermethodfile", "tunermode", "tuneroutput", "tuneroutputpath", "tunerpermute",
        "tunerrootalg", "tunersessionname", "tunertarget", "tunerthreads", "usersolheuristic",
        "varselection",
        //"version"
    };
    std::vector<MiniZinc::SolverConfig::ExtraFlag> res;
    for (auto param : all_params) {
      int n;
      int t;
      p._plugin->XPRSgetcontrolinfo(prb, param.c_str(), &n, &t);
      MiniZinc::SolverConfig::ExtraFlag::FlagType param_type;
      std::string param_default;
      switch (t) {
        case XPRS_TYPE_INT: {
          int d;
          p._plugin->XPRSgetintcontrol(prb, n, &d);
          param_type = MiniZinc::SolverConfig::ExtraFlag::FlagType::T_INT;
          param_default = to_string(d);
          break;
        }
        case XPRS_TYPE_INT64: {
          XPRSint64 d;
          p._plugin->XPRSgetintcontrol64(prb, n, &d);
          param_type = MiniZinc::SolverConfig::ExtraFlag::FlagType::T_INT;
          param_default = to_string(d);
          break;
        }
        case XPRS_TYPE_DOUBLE: {
          double d;
          p._plugin->XPRSgetdblcontrol(prb, n, &d);
          param_type = MiniZinc::SolverConfig::ExtraFlag::FlagType::T_FLOAT;
          param_default = to_string(d);
          break;
        }
        case XPRS_TYPE_STRING: {
          int l;
          p._plugin->XPRSgetstringcontrol(prb, n, nullptr, 0, &l);
          char* d = (char*)malloc(l);
          p._plugin->XPRSgetstringcontrol(prb, n, d, l, &l);
          param_type = MiniZinc::SolverConfig::ExtraFlag::FlagType::T_STRING;
          param_default = d;
          break;
        }
        default:
          continue;
      }
      // TODO: Some of these parameters have min/max or are categorical, but there's no way
      // to programatically get the possible values. We could manually maintain it, but it's
      // probably not worth doing right now.
      std::vector<std::string> param_range;  // unused for now
      res.emplace_back("--xpress-" + param, param, param_type, param_range, param_default);
    }
    return res;
  } catch (MiniZinc::PluginError&) {
    return {};
  } catch (XpressException&) {
    return {};
  }
  return {};
}

void MIPxpressWrapper::Options::printHelp(ostream& os) {
  os << "XPRESS MIP wrapper options:" << std::endl
     << "--msgLevel <n>       print solver output, default: 0" << std::endl
     << "--logFile <file>     log file" << std::endl
     << "--solver-time-limit <N>        stop search after N milliseconds, if negative, it "
        "will only stop if at least one solution was found"
     << std::endl
     << "-n <N>, --numSolutions <N>   stop search after N solutions" << std::endl
     << "--writeModel <file>  write model to <file>" << std::endl
     << "--writeModelFormat [lp|mps] the file format of the written model(lp "
        "or mps), default: lp"
     << std::endl
     << "--absGap <d>         absolute gap |primal-dual| to stop, default: " << 0 << std::endl
     << "--relGap <d>         relative gap |primal-dual|/<solver-dep> to stop, "
        "default: "
     << 0.0001 << std::endl
     << "-i                   print intermediate solution, default: false" << std::endl
     << "-r <N>, --seed <N>, --random-seed <N>" << std::endl
     << "    random seed, integer"
     << "-p <N>, --parallel <N>   use N threads" << std::endl
     << "--xpress-dll <file>      Xpress DLL file (xprs.dll/libxprs.so/libxprs.dylib)" << std::endl
     << "--xpress-password <dir>  directory where xpauth.xpr is located (optional)" << std::endl
     << std::endl;
}

bool MIPxpressWrapper::FactoryOptions::processOption(int& i, std::vector<std::string>& argv,
                                                     const std::string& workingDir) {
  MiniZinc::CLOParser cop(i, argv);
  if (cop.get("--xpress-dll", &xpressDll)) {                 // NOLINT: Allow repeated empty if
  } else if (cop.get("--xpress-password", &xprsPassword)) {  // NOLINT: Allow repeated empty if
  } else {
    return false;
  }
  return true;
}

bool MIPxpressWrapper::Options::processOption(int& i, std::vector<std::string>& argv,
                                              const std::string& workingDir) {
  MiniZinc::CLOParser cop(i, argv);
  std::string buffer;
  if (cop.get("--msgLevel", &msgLevel)) {  // NOLINT: Allow repeated empty if
  } else if (cop.get("--logFile", &buffer)) {
    logFile = MiniZinc::FileUtils::file_path(buffer, workingDir);
  } else if (cop.get("--solver-time-limit", &timeout)) {     // NOLINT: Allow repeated empty if
  } else if (cop.get("-n --numSolutions", &numSolutions)) {  // NOLINT: Allow repeated empty if
  } else if (cop.get("--writeModel", &buffer)) {
    writeModelFile = MiniZinc::FileUtils::file_path(buffer, workingDir);
  } else if (cop.get("--writeModelFormat", &writeModelFormat)) {  // NOLINT: Allow repeated empty if
  } else if (cop.get("--relGap", &relGap)) {                      // NOLINT: Allow repeated empty if
  } else if (cop.get("--absGap", &absGap)) {                      // NOLINT: Allow repeated empty if
  } else if (cop.get("-i")) {
    intermediateSolutions = true;
  } else if (cop.get("-p --parallel", &numThreads)) {
  } else if (cop.get("-r --seed --random-seed", &randomSeed)) {
  } else {
    return false;
  }
  return true;
}

void MIPxpressWrapper::setOptions() {
  XPRSprob xprsProblem = _plugin->XPRBgetXPRSprob(_problem);

  _plugin->XPRBsetmsglevel(_problem, _options->msgLevel);

  _plugin->XPRSsetlogfile(xprsProblem, _options->logFile.c_str());
  if (_options->timeout > 1000 || _options->timeout < -1000) {
    _plugin->XPRSsetintcontrol(xprsProblem, XPRS_MAXTIME,
                               static_cast<int>(_options->timeout / 1000));
  }
  _plugin->XPRSsetintcontrol(xprsProblem, XPRS_MAXMIPSOL, _options->numSolutions);
  _plugin->XPRSsetdblcontrol(xprsProblem, XPRS_MIPABSSTOP, _options->absGap);
  _plugin->XPRSsetdblcontrol(xprsProblem, XPRS_MIPRELSTOP, _options->relGap);

  if (_options->numThreads > 0) {
    _plugin->XPRSsetintcontrol(xprsProblem, XPRS_THREADS, _options->numThreads);
  }

  if (_options->randomSeed != 0) {
    _plugin->XPRSsetintcontrol(xprsProblem, XPRS_RANDOMSEED, _options->randomSeed);
  }

  for (auto& it : _options->extraParams) {
    auto name = it.first.substr(9);
    int n;
    int t;
    _plugin->XPRSgetcontrolinfo(xprsProblem, name.c_str(), &n, &t);
    switch (t) {
      case XPRS_TYPE_INT:
        _plugin->XPRSsetintcontrol(xprsProblem, n, stoi(it.second));
        break;
      case XPRS_TYPE_INT64:
        _plugin->XPRSsetintcontrol64(xprsProblem, n, stoll(it.second));
        break;
      case XPRS_TYPE_DOUBLE:
        _plugin->XPRSsetdblcontrol(xprsProblem, n, stod(it.second));
        break;
      case XPRS_TYPE_STRING:
        _plugin->XPRSsetstrcontrol(xprsProblem, n, it.second.c_str());
        break;
      default:
        throw XpressException("Unknown type for parameter " + name);
    }
  }
}

static MIPWrapper::Status convert_status(int xpressStatus) {
  switch (xpressStatus) {
    case XPRB_MIP_OPTIMAL:
      return MIPWrapper::Status::OPT;
    case XPRB_MIP_INFEAS:
      return MIPWrapper::Status::UNSAT;
    case XPRB_MIP_UNBOUNDED:
      return MIPWrapper::Status::UNBND;
    case XPRB_MIP_NO_SOL_FOUND:
      return MIPWrapper::Status::UNKNOWN;
    case XPRB_MIP_NOT_LOADED:
      return MIPWrapper::Status::ERROR_STATUS;
    default:
      return MIPWrapper::Status::UNKNOWN;
  }
}

static string get_status_name(int xpressStatus) {
  string rt = "Xpress stopped with status: ";
  switch (xpressStatus) {
    case XPRB_MIP_OPTIMAL:
      return rt + "Optimal";
    case XPRB_MIP_INFEAS:
      return rt + "Infeasible";
    case XPRB_MIP_UNBOUNDED:
      return rt + "Unbounded";
    case XPRB_MIP_NO_SOL_FOUND:
      return rt + "No solution found";
    case XPRB_MIP_NOT_LOADED:
      return rt + "No problem loaded or error";
    default:
      return rt + "Unknown status";
  }
}

static void set_output_variables(XpressPlugin* plugin, MIPxpressWrapper::Output* output,
                                 vector<XPRBvar>* variables) {
  size_t nCols = variables->size();
  auto* x = (double*)malloc(nCols * sizeof(double));
  for (size_t ii = 0; ii < nCols; ii++) {
    x[ii] = plugin->XPRBgetsol((*variables)[ii]);
  }
  output->x = x;
}

static void set_output_attributes(XpressPlugin* plugin, MIPxpressWrapper::Output* output,
                                  XPRSprob xprsProblem) {
  int xpressStatus = 0;
  plugin->XPRSgetintattrib(xprsProblem, XPRS_MIPSTATUS, &xpressStatus);
  output->status = convert_status(xpressStatus);
  output->statusName = get_status_name(xpressStatus);

  plugin->XPRSgetdblattrib(xprsProblem, XPRS_MIPOBJVAL, &output->objVal);
  plugin->XPRSgetdblattrib(xprsProblem, XPRS_BESTBOUND, &output->bestBound);

  plugin->XPRSgetintattrib(xprsProblem, XPRS_NODES, &output->nNodes);
  plugin->XPRSgetintattrib(xprsProblem, XPRS_ACTIVENODES, &output->nOpenNodes);

  output->dWallTime =
      std::chrono::duration<double>(std::chrono::steady_clock::now() - output->dWallTime0).count();
  output->dCPUTime = double(std::clock() - output->cCPUTime0) / CLOCKS_PER_SEC;
}

static void XPRS_CC user_sol_notify_callback(XPRSprob xprsProblem, void* userData) {
  auto* data = (UserSolutionCallbackData*)userData;
  MIPWrapper::CBUserInfo* info = data->info;

  set_output_attributes(data->plugin, info->pOutput, xprsProblem);

  data->plugin->XPRBbegincb(*(data->problem), xprsProblem);
  data->plugin->XPRBsync(*(data->problem), XPRB_XPRS_SOL);
  set_output_variables(data->plugin, info->pOutput, data->variables);
  data->plugin->XPRBendcb(*(data->problem));

  if (info->solcbfn != nullptr) {
    (*info->solcbfn)(*info->pOutput, info->psi);
  }
}

void MIPxpressWrapper::doAddVars(size_t n, double* obj, double* lb, double* ub, VarType* vt,
                                 string* names) {
  if (obj == nullptr || lb == nullptr || ub == nullptr || vt == nullptr || names == nullptr) {
    throw XpressException("invalid input");
  }
  for (size_t i = 0; i < n; ++i) {
    char* var_name = (char*)names[i].c_str();
    int var_type = convertVariableType(vt[i]);
    XPRBvar var = _plugin->XPRBnewvar(_problem, var_type, var_name, lb[i], ub[i]);
    _variables.push_back(var);
    _plugin->XPRBsetterm(_xpressObj, var, obj[i]);
  }
}

void MIPxpressWrapper::addRow(int nnz, int* rmatind, double* rmatval, LinConType sense, double rhs,
                              int mask, const string& rowName) {
  addConstraint(nnz, rmatind, rmatval, sense, rhs, mask, rowName);
}

XPRBctr MIPxpressWrapper::addConstraint(int nnz, int* rmatind, double* rmatval, LinConType sense,
                                        double rhs, int mask, const string& rowName) {
  _nRows++;
  XPRBctr constraint = _plugin->XPRBnewctr(_problem, rowName.c_str(), convertConstraintType(sense));
  for (int i = 0; i < nnz; ++i) {
    _plugin->XPRBsetterm(constraint, _variables[rmatind[i]], rmatval[i]);
  }
  _plugin->XPRBsetterm(constraint, nullptr, rhs);

  return constraint;
}

void MIPxpressWrapper::writeModelIfRequested() {
  int format = XPRB_LP;
  if (_options->writeModelFormat == "lp") {
    format = XPRB_LP;
  } else if (_options->writeModelFormat == "mps") {
    format = XPRB_MPS;
  }
  if (!_options->writeModelFile.empty()) {
    _plugin->XPRBexportprob(_problem, format, _options->writeModelFile.c_str());
  }
}

void MIPxpressWrapper::addDummyConstraint() {
  if (getNCols() == 0) {
    return;
  }

  XPRBctr constraint = _plugin->XPRBnewctr(_problem, "dummy_constraint", XPRB_L);
  _plugin->XPRBsetterm(constraint, _variables[0], 1);
  double ub;
  _plugin->XPRBgetbounds(_variables[0], nullptr, &ub);
  _plugin->XPRBsetterm(constraint, nullptr, ub);
}

void MIPxpressWrapper::solve() {
  if (getNRows() == 0) {
    addDummyConstraint();
  }

  setOptions();
  writeModelIfRequested();
  setUserSolutionCallback();

  _plugin->XPRBsetobj(_problem, _xpressObj);

  cbui.pOutput->dWallTime0 = output.dWallTime0 = std::chrono::steady_clock::now();
  cbui.pOutput->cCPUTime0 =
      static_cast<clock_t>(output.dCPUTime = static_cast<double>(std::clock()));

  if (_plugin->XPRBmipoptimize(_problem, "c") == 1) {
    throw XpressException("error while solving");
  }

  set_output_variables(_plugin, &output, &_variables);
  set_output_attributes(_plugin, &output, _plugin->XPRBgetXPRSprob(_problem));

  if (!_options->intermediateSolutions && cbui.solcbfn != nullptr) {
    cbui.solcbfn(output, cbui.psi);
  }
}

void MIPxpressWrapper::setUserSolutionCallback() {
  if (!_options->intermediateSolutions) {
    return;
  }

  auto* data = new UserSolutionCallbackData{&cbui, &_problem, &_variables, _plugin};

  _plugin->XPRSsetcbintsol(_plugin->XPRBgetXPRSprob(_problem), user_sol_notify_callback, data);
}

void MIPxpressWrapper::setObjSense(int s) {
  _plugin->XPRBsetsense(_problem, convertObjectiveSense(s));
}

void MIPxpressWrapper::setVarLB(int iVar, double lb) { _plugin->XPRBsetlb(_variables[iVar], lb); }

void MIPxpressWrapper::setVarUB(int iVar, double ub) { _plugin->XPRBsetub(_variables[iVar], ub); }

void MIPxpressWrapper::setVarBounds(int iVar, double lb, double ub) {
  setVarLB(iVar, lb);
  setVarUB(iVar, ub);
}

void MIPxpressWrapper::addIndicatorConstraint(int iBVar, int bVal, int nnz, int* rmatind,
                                              double* rmatval, LinConType sense, double rhs,
                                              const string& rowName) {
  if (bVal != 0 && bVal != 1) {
    throw XpressException("indicator bval not in 0/1");
  }
  XPRBctr constraint = addConstraint(nnz, rmatind, rmatval, sense, rhs, 0, rowName);
  _plugin->XPRBsetindicator(constraint, 2 * bVal - 1, _variables[iBVar]);
}

bool MIPxpressWrapper::addWarmStart(const std::vector<VarId>& vars,
                                    const std::vector<double>& vals) {
  XPRBsol warmstart = _plugin->XPRBnewsol(_problem);
  for (size_t ii = 0; ii < vars.size(); ii++) {
    _plugin->XPRBsetsolvar(warmstart, _variables[vars[ii]], vals[ii]);
  }
  return (_plugin->XPRBaddmipsol(_problem, warmstart, nullptr) == 0);
}

int MIPxpressWrapper::convertConstraintType(LinConType sense) {
  switch (sense) {
    case MIPWrapper::LQ:
      return XPRB_L;
    case MIPWrapper::EQ:
      return XPRB_E;
    case MIPWrapper::GQ:
      return XPRB_G;
    default:
      throw XpressException("unkown constraint sense");
  }
}

int MIPxpressWrapper::convertVariableType(VarType varType) {
  switch (varType) {
    case REAL:
      return XPRB_PL;
    case INT:
      return XPRB_UI;
    case BINARY:
      return XPRB_BV;
    default:
      throw XpressException("unknown variable type");
  }
}

int MIPxpressWrapper::convertObjectiveSense(int s) {
  switch (s) {
    case 1:
      return XPRB_MAXIM;
    case -1:
      return XPRB_MINIM;
    default:
      throw XpressException("unknown objective sense");
  }
}