-
Notifications
You must be signed in to change notification settings - Fork 8
/
gurobi_model.cpp
1283 lines (1121 loc) · 34.8 KB
/
gurobi_model.cpp
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
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
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
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "pyoptinterface/gurobi_model.hpp"
#include "fmt/core.h"
extern "C"
{
int __stdcall GRBloadenvinternal(GRBenv **envP, const char *logfilename, int major, int minor,
int tech);
int __stdcall GRBemptyenvinternal(GRBenv **envP, int major, int minor, int tech);
int __stdcall GRBloadenv_1200(GRBenv **envP, const char *logfile);
int __stdcall GRBemptyenv_1200(GRBenv **envP);
}
namespace gurobi
{
#define B DYLIB_DECLARE
APILIST
#undef B
// Gurobi 12.0.0 workarounds
DYLIB_DECLARE(GRBloadenvinternal);
DYLIB_DECLARE(GRBemptyenvinternal);
static DynamicLibrary lib;
static bool is_loaded = false;
bool is_library_loaded()
{
return is_loaded;
}
bool load_library(const std::string &path)
{
bool success = lib.try_load(path.c_str());
if (!success)
{
return false;
}
DYLIB_LOAD_INIT;
#define B DYLIB_LOAD_FUNCTION
APILIST
#undef B
// We have to deal with Gurobi 12.0.0 where some functions are absent from the dynamic library
{
int major, minor, techinical;
auto GRBversion_p =
reinterpret_cast<decltype(GRBversion)>(_function_pointers["GRBversion"]);
if (GRBversion_p != nullptr)
{
GRBversion_p(&major, &minor, &techinical);
if (major == 12 && minor == 0 && techinical == 0)
{
DYLIB_LOAD_FUNCTION(GRBloadenvinternal);
DYLIB_LOAD_FUNCTION(GRBemptyenvinternal);
_function_pointers["GRBloadenv"] = reinterpret_cast<void *>(&GRBloadenv_1200);
_function_pointers["GRBemptyenv"] = reinterpret_cast<void *>(&GRBemptyenv_1200);
// Now check there is no nullptr in _function_pointers
_load_success = true;
for (auto &pair : _function_pointers)
{
if (pair.second == nullptr)
{
fmt::print("function {} is not loaded correctly\n", pair.first);
_load_success = false;
}
}
if (_load_success)
{
DYLIB_SAVE_FUNCTION(GRBloadenvinternal);
DYLIB_SAVE_FUNCTION(GRBemptyenvinternal);
}
}
}
}
if (IS_DYLIB_LOAD_SUCCESS)
{
#define B DYLIB_SAVE_FUNCTION
APILIST
#undef B
is_loaded = true;
return true;
}
else
{
return false;
}
}
} // namespace gurobi
int GRBloadenv_1200(GRBenv **envP, const char *logfile)
{
return gurobi::GRBloadenvinternal(envP, logfile, 12, 0, 0);
}
int GRBemptyenv_1200(GRBenv **envP)
{
return gurobi::GRBemptyenvinternal(envP, 12, 0, 0);
}
static char gurobi_con_sense(ConstraintSense sense)
{
switch (sense)
{
case ConstraintSense::LessEqual:
return GRB_LESS_EQUAL;
case ConstraintSense::Equal:
return GRB_EQUAL;
case ConstraintSense::GreaterEqual:
return GRB_GREATER_EQUAL;
default:
throw std::runtime_error("Unknown constraint sense");
}
}
static int gurobi_obj_sense(ObjectiveSense sense)
{
switch (sense)
{
case ObjectiveSense::Minimize:
return GRB_MINIMIZE;
case ObjectiveSense::Maximize:
return GRB_MAXIMIZE;
default:
throw std::runtime_error("Unknown objective sense");
}
}
static char gurobi_vtype(VariableDomain domain)
{
switch (domain)
{
case VariableDomain::Continuous:
return GRB_CONTINUOUS;
case VariableDomain::Integer:
return GRB_INTEGER;
case VariableDomain::Binary:
return GRB_BINARY;
case VariableDomain::SemiContinuous:
return GRB_SEMICONT;
default:
throw std::runtime_error("Unknown variable domain");
}
}
static int gurobi_sostype(SOSType type)
{
switch (type)
{
case SOSType::SOS1:
return GRB_SOS_TYPE1;
case SOSType::SOS2:
return GRB_SOS_TYPE2;
default:
throw std::runtime_error("Unknown SOS type");
}
}
GurobiModel::GurobiModel(const GurobiEnv &env)
{
init(env);
}
void GurobiModel::init(const GurobiEnv &env)
{
if (!gurobi::is_library_loaded())
{
throw std::runtime_error("Gurobi library is not loaded");
}
GRBmodel *model;
int error = gurobi::GRBnewmodel(env.m_env, &model, NULL, 0, NULL, NULL, NULL, NULL, NULL);
check_error(error);
m_env = gurobi::GRBgetenv(model);
m_model = std::unique_ptr<GRBmodel, GRBfreemodelT>(model);
}
void GurobiModel::write(const std::string &filename)
{
int error = gurobi::GRBwrite(m_model.get(), filename.c_str());
check_error(error);
}
VariableIndex GurobiModel::add_variable(VariableDomain domain, double lb, double ub,
const char *name)
{
if (name != nullptr && name[0] == '\0')
{
name = nullptr;
}
IndexT index = m_variable_index.add_index();
VariableIndex variable(index);
// Create a new Gurobi variable
char vtype = gurobi_vtype(domain);
int error = gurobi::GRBaddvar(m_model.get(), 0, NULL, NULL, 0.0, lb, ub, vtype, name);
check_error(error);
m_update_flag |= m_variable_creation;
return variable;
}
void GurobiModel::delete_variable(const VariableIndex &variable)
{
if (!is_variable_active(variable))
{
throw std::runtime_error("Variable does not exist");
}
// Delete the corresponding Gurobi variable
int variable_column = _variable_index(variable);
int error = gurobi::GRBdelvars(m_model.get(), 1, &variable_column);
check_error(error);
m_variable_index.delete_index(variable.index);
m_update_flag |= m_variable_deletion;
}
void GurobiModel::delete_variables(const Vector<VariableIndex> &variables)
{
int n_variables = variables.size();
if (n_variables == 0)
return;
std::vector<int> columns;
columns.reserve(n_variables);
for (int i = 0; i < n_variables; i++)
{
if (!is_variable_active(variables[i]))
{
continue;
}
auto column = _variable_index(variables[i]);
columns.push_back(column);
}
int error = gurobi::GRBdelvars(m_model.get(), columns.size(), columns.data());
check_error(error);
for (int i = 0; i < n_variables; i++)
{
m_variable_index.delete_index(variables[i].index);
}
m_update_flag |= m_variable_deletion;
}
bool GurobiModel::is_variable_active(const VariableIndex &variable)
{
return m_variable_index.has_index(variable.index);
}
double GurobiModel::get_variable_value(const VariableIndex &variable)
{
return get_variable_raw_attribute_double(variable, GRB_DBL_ATTR_X);
}
std::string GurobiModel::pprint_variable(const VariableIndex &variable)
{
return get_variable_raw_attribute_string(variable, GRB_STR_ATTR_VARNAME);
}
void GurobiModel::set_variable_bounds(const VariableIndex &variable, double lb, double ub)
{
auto column = _checked_variable_index(variable);
int error;
error = gurobi::GRBsetdblattrelement(m_model.get(), GRB_DBL_ATTR_LB, column, lb);
check_error(error);
error = gurobi::GRBsetdblattrelement(m_model.get(), GRB_DBL_ATTR_UB, column, ub);
check_error(error);
m_update_flag |= m_attribute_update;
}
void GurobiModel::set_variable_name(const VariableIndex &variable, const char *name)
{
set_variable_raw_attribute_string(variable, GRB_STR_ATTR_VARNAME, name);
}
void GurobiModel::set_constraint_name(const ConstraintIndex &constraint, const char *name)
{
const char *attr_name;
switch (constraint.type)
{
case ConstraintType::Linear:
attr_name = GRB_STR_ATTR_CONSTRNAME;
break;
case ConstraintType::Quadratic:
attr_name = GRB_STR_ATTR_QCNAME;
break;
default:
throw std::runtime_error("Unknown constraint type to set name!");
}
set_constraint_raw_attribute_string(constraint, attr_name, name);
}
ConstraintIndex GurobiModel::add_linear_constraint(const ScalarAffineFunction &function,
ConstraintSense sense, CoeffT rhs,
const char *name)
{
IndexT index = m_linear_constraint_index.add_index();
ConstraintIndex constraint_index(ConstraintType::Linear, index);
// Create a new Gurobi linear constraint
AffineFunctionPtrForm<int, int, double> ptr_form;
ptr_form.make(this, function);
int numnz = ptr_form.numnz;
int *cind = ptr_form.index;
double *cval = ptr_form.value;
char g_sense = gurobi_con_sense(sense);
double g_rhs = rhs - function.constant.value_or(0.0);
if (name != nullptr && name[0] == '\0')
{
name = nullptr;
}
int error = gurobi::GRBaddconstr(m_model.get(), numnz, cind, cval, g_sense, g_rhs, name);
check_error(error);
// _require_update();
m_update_flag |= m_linear_constraint_creation;
return constraint_index;
}
ConstraintIndex GurobiModel::add_quadratic_constraint(const ScalarQuadraticFunction &function,
ConstraintSense sense, CoeffT rhs,
const char *name)
{
IndexT index = m_quadratic_constraint_index.add_index();
ConstraintIndex constraint_index(ConstraintType::Quadratic, index);
// Create a new Gurobi quadratic constraint
const auto &affine_part = function.affine_part;
int numlnz = 0;
int *lind = NULL;
double *lval = NULL;
AffineFunctionPtrForm<int, int, double> affine_ptr_form;
if (affine_part.has_value())
{
const auto &affine_function = affine_part.value();
affine_ptr_form.make(this, affine_function);
numlnz = affine_ptr_form.numnz;
lind = affine_ptr_form.index;
lval = affine_ptr_form.value;
}
QuadraticFunctionPtrForm<int, int, double> ptr_form;
ptr_form.make(this, function);
int numqnz = ptr_form.numnz;
int *qrow = ptr_form.row;
int *qcol = ptr_form.col;
double *qval = ptr_form.value;
char g_sense = gurobi_con_sense(sense);
double g_rhs = rhs;
if (affine_part)
g_rhs -= affine_part->constant.value_or(0.0);
if (name != nullptr && name[0] == '\0')
{
name = nullptr;
}
int error = gurobi::GRBaddqconstr(m_model.get(), numlnz, lind, lval, numqnz, qrow, qcol, qval,
g_sense, g_rhs, name);
check_error(error);
m_update_flag |= m_quadratic_constraint_creation;
return constraint_index;
}
ConstraintIndex GurobiModel::add_sos_constraint(const Vector<VariableIndex> &variables,
SOSType sos_type)
{
Vector<CoeffT> weights(variables.size(), 1.0);
return add_sos_constraint(variables, sos_type, weights);
}
ConstraintIndex GurobiModel::add_sos_constraint(const Vector<VariableIndex> &variables,
SOSType sos_type, const Vector<CoeffT> &weights)
{
IndexT index = m_sos_constraint_index.add_index();
ConstraintIndex constraint_index(ConstraintType::SOS, index);
// Create a new Gurobi SOS constraint
int numsos = 1;
int nummembers = variables.size();
int types = gurobi_sostype(sos_type);
int beg[] = {0, nummembers};
std::vector<int> ind_v(nummembers);
for (int i = 0; i < nummembers; i++)
{
ind_v[i] = _variable_index(variables[i].index);
}
int *ind = ind_v.data();
double *weight = (double *)weights.data();
int error = gurobi::GRBaddsos(m_model.get(), numsos, nummembers, &types, beg, ind, weight);
check_error(error);
m_update_flag |= m_sos_constraint_creation;
return constraint_index;
}
void GurobiModel::delete_constraint(const ConstraintIndex &constraint)
{
// Delete the corresponding Gurobi constraint
int error = 0;
int constraint_row = _constraint_index(constraint);
if (constraint_row >= 0)
{
switch (constraint.type)
{
case ConstraintType::Linear:
m_linear_constraint_index.delete_index(constraint.index);
error = gurobi::GRBdelconstrs(m_model.get(), 1, &constraint_row);
m_update_flag |= m_linear_constraint_deletion;
break;
case ConstraintType::Quadratic:
m_quadratic_constraint_index.delete_index(constraint.index);
error = gurobi::GRBdelqconstrs(m_model.get(), 1, &constraint_row);
m_update_flag |= m_quadratic_constraint_deletion;
break;
case ConstraintType::SOS:
m_sos_constraint_index.delete_index(constraint.index);
error = gurobi::GRBdelsos(m_model.get(), 1, &constraint_row);
m_update_flag |= m_sos_constraint_deletion;
break;
default:
throw std::runtime_error("Unknown constraint type");
}
}
check_error(error);
}
bool GurobiModel::is_constraint_active(const ConstraintIndex &constraint)
{
switch (constraint.type)
{
case ConstraintType::Linear:
return m_linear_constraint_index.has_index(constraint.index);
case ConstraintType::Quadratic:
return m_quadratic_constraint_index.has_index(constraint.index);
case ConstraintType::SOS:
return m_sos_constraint_index.has_index(constraint.index);
default:
throw std::runtime_error("Unknown constraint type");
}
}
void GurobiModel::_set_affine_objective(const ScalarAffineFunction &function, ObjectiveSense sense,
bool clear_quadratic)
{
int error = 0;
if (clear_quadratic)
{
// First delete all quadratic terms
error = gurobi::GRBdelq(m_model.get());
check_error(error);
}
// Set Obj attribute of each variable
int n_variables = get_model_raw_attribute_int(GRB_INT_ATTR_NUMVARS);
std::vector<double> obj_v(n_variables, 0.0);
int numnz = function.size();
for (int i = 0; i < numnz; i++)
{
auto column = _variable_index(function.variables[i]);
if (column < 0)
{
throw std::runtime_error("Variable does not exist");
}
obj_v[column] = function.coefficients[i];
}
error = gurobi::GRBsetdblattrarray(m_model.get(), "Obj", 0, n_variables, obj_v.data());
check_error(error);
error = gurobi::GRBsetdblattr(m_model.get(), "ObjCon", function.constant.value_or(0.0));
check_error(error);
int obj_sense = gurobi_obj_sense(sense);
set_model_raw_attribute_int("ModelSense", obj_sense);
m_update_flag |= m_objective_update;
}
void GurobiModel::set_objective(const ScalarAffineFunction &function, ObjectiveSense sense)
{
_set_affine_objective(function, sense, true);
}
void GurobiModel::set_objective(const ScalarQuadraticFunction &function, ObjectiveSense sense)
{
int error = 0;
// First delete all quadratic terms
error = gurobi::GRBdelq(m_model.get());
check_error(error);
// Add quadratic term
int numqnz = function.size();
if (numqnz > 0)
{
QuadraticFunctionPtrForm<int, int, double> ptr_form;
ptr_form.make(this, function);
int numqnz = ptr_form.numnz;
int *qrow = ptr_form.row;
int *qcol = ptr_form.col;
double *qval = ptr_form.value;
error = gurobi::GRBaddqpterms(m_model.get(), numqnz, qrow, qcol, qval);
check_error(error);
}
// Affine part
const auto &affine_part = function.affine_part;
if (affine_part)
{
const auto &affine_function = affine_part.value();
_set_affine_objective(affine_function, sense, false);
}
else
{
ScalarAffineFunction zero;
_set_affine_objective(zero, sense, false);
}
}
void GurobiModel::set_objective(const ExprBuilder &function, ObjectiveSense sense)
{
auto deg = function.degree();
if (deg <= 1)
{
ScalarAffineFunction f(function);
set_objective(f, sense);
}
else if (deg == 2)
{
ScalarQuadraticFunction f(function);
set_objective(f, sense);
}
else
{
throw std::runtime_error("Objective must be linear or quadratic");
}
}
void GurobiModel::optimize()
{
if (has_callback)
{
// Store the number of variables for the callback
m_callback_userdata.n_variables = get_model_raw_attribute_int(GRB_INT_ATTR_NUMVARS);
}
m_update_flag = 0;
int error = gurobi::GRBoptimize(m_model.get());
check_error(error);
}
void GurobiModel::update()
{
int error = gurobi::GRBupdatemodel(m_model.get());
check_error(error);
m_update_flag = 0;
}
int GurobiModel::raw_parameter_type(const char *param_name)
{
return gurobi::GRBgetparamtype(m_env, param_name);
}
void GurobiModel::set_raw_parameter_int(const char *param_name, int value)
{
int error = gurobi::GRBsetintparam(m_env, param_name, value);
check_error(error);
}
void GurobiModel::set_raw_parameter_double(const char *param_name, double value)
{
int error = gurobi::GRBsetdblparam(m_env, param_name, value);
check_error(error);
}
void GurobiModel::set_raw_parameter_string(const char *param_name, const char *value)
{
int error = gurobi::GRBsetstrparam(m_env, param_name, value);
check_error(error);
}
int GurobiModel::get_raw_parameter_int(const char *param_name)
{
int retval;
int error = gurobi::GRBgetintparam(m_env, param_name, &retval);
check_error(error);
return retval;
}
double GurobiModel::get_raw_parameter_double(const char *param_name)
{
double retval;
int error = gurobi::GRBgetdblparam(m_env, param_name, &retval);
check_error(error);
return retval;
}
std::string GurobiModel::get_raw_parameter_string(const char *param_name)
{
char retval[GRB_MAX_STRLEN];
int error = gurobi::GRBgetstrparam(m_env, param_name, retval);
check_error(error);
return std::string(retval);
}
int GurobiModel::raw_attribute_type(const char *attr_name)
{
int datatypeP;
int error = gurobi::GRBgetattrinfo(m_model.get(), attr_name, &datatypeP, NULL, NULL);
check_error(error);
return datatypeP;
}
void GurobiModel::set_model_raw_attribute_int(const char *attr_name, int value)
{
int error = gurobi::GRBsetintattr(m_model.get(), attr_name, value);
check_error(error);
m_update_flag |= m_attribute_update;
}
void GurobiModel::set_model_raw_attribute_double(const char *attr_name, double value)
{
int error = gurobi::GRBsetdblattr(m_model.get(), attr_name, value);
check_error(error);
m_update_flag |= m_attribute_update;
}
void GurobiModel::set_model_raw_attribute_string(const char *attr_name, const char *value)
{
int error = gurobi::GRBsetstrattr(m_model.get(), attr_name, value);
check_error(error);
m_update_flag |= m_attribute_update;
}
int GurobiModel::get_model_raw_attribute_int(const char *attr_name)
{
_update_for_information();
int retval;
int error = gurobi::GRBgetintattr(m_model.get(), attr_name, &retval);
check_error(error);
return retval;
}
double GurobiModel::get_model_raw_attribute_double(const char *attr_name)
{
_update_for_information();
double retval;
int error = gurobi::GRBgetdblattr(m_model.get(), attr_name, &retval);
check_error(error);
return retval;
}
std::string GurobiModel::get_model_raw_attribute_string(const char *attr_name)
{
_update_for_information();
char *retval;
int error = gurobi::GRBgetstrattr(m_model.get(), attr_name, &retval);
check_error(error);
return std::string(retval);
}
std::vector<double> GurobiModel::get_model_raw_attribute_vector_double(const char *attr_name,
int start, int len)
{
_update_for_information();
std::vector<double> retval(len);
int error = gurobi::GRBgetdblattrarray(m_model.get(), attr_name, start, len, retval.data());
check_error(error);
return retval;
}
std::vector<double> GurobiModel::get_model_raw_attribute_list_double(const char *attr_name,
const std::vector<int> &ind)
{
_update_for_information();
std::vector<double> retval(ind.size());
int error = gurobi::GRBgetdblattrlist(m_model.get(), attr_name, ind.size(), (int *)ind.data(),
retval.data());
check_error(error);
return retval;
}
void GurobiModel::set_variable_raw_attribute_int(const VariableIndex &variable,
const char *attr_name, int value)
{
auto column = _checked_variable_index(variable);
int error = gurobi::GRBsetintattrelement(m_model.get(), attr_name, column, value);
check_error(error);
m_update_flag |= m_attribute_update;
}
void GurobiModel::set_variable_raw_attribute_char(const VariableIndex &variable,
const char *attr_name, char value)
{
auto column = _checked_variable_index(variable);
int error = gurobi::GRBsetcharattrelement(m_model.get(), attr_name, column, value);
check_error(error);
m_update_flag |= m_attribute_update;
}
void GurobiModel::set_variable_raw_attribute_double(const VariableIndex &variable,
const char *attr_name, double value)
{
auto column = _checked_variable_index(variable);
int error = gurobi::GRBsetdblattrelement(m_model.get(), attr_name, column, value);
check_error(error);
m_update_flag |= m_attribute_update;
}
void GurobiModel::set_variable_raw_attribute_string(const VariableIndex &variable,
const char *attr_name, const char *value)
{
auto column = _checked_variable_index(variable);
int error = gurobi::GRBsetstrattrelement(m_model.get(), attr_name, column, value);
check_error(error);
m_update_flag |= m_attribute_update;
}
int GurobiModel::get_variable_raw_attribute_int(const VariableIndex &variable,
const char *attr_name)
{
_update_for_information();
auto column = _checked_variable_index(variable);
int retval;
int error = gurobi::GRBgetintattrelement(m_model.get(), attr_name, column, &retval);
check_error(error);
return retval;
}
char GurobiModel::get_variable_raw_attribute_char(const VariableIndex &variable,
const char *attr_name)
{
_update_for_information();
auto column = _checked_variable_index(variable);
char retval;
int error = gurobi::GRBgetcharattrelement(m_model.get(), attr_name, column, &retval);
check_error(error);
return retval;
}
double GurobiModel::get_variable_raw_attribute_double(const VariableIndex &variable,
const char *attr_name)
{
_update_for_information();
auto column = _checked_variable_index(variable);
double retval;
int error = gurobi::GRBgetdblattrelement(m_model.get(), attr_name, column, &retval);
check_error(error);
return retval;
}
std::string GurobiModel::get_variable_raw_attribute_string(const VariableIndex &variable,
const char *attr_name)
{
_update_for_information();
auto column = _checked_variable_index(variable);
char *retval;
int error = gurobi::GRBgetstrattrelement(m_model.get(), attr_name, column, &retval);
check_error(error);
return std::string(retval);
}
int GurobiModel::_variable_index(const VariableIndex &variable)
{
_update_for_variable_index();
return m_variable_index.get_index(variable.index);
}
int GurobiModel::_checked_variable_index(const VariableIndex &variable)
{
int column = _variable_index(variable);
if (column < 0)
{
throw std::runtime_error("Variable does not exist");
}
return column;
}
void GurobiModel::set_constraint_raw_attribute_int(const ConstraintIndex &constraint,
const char *attr_name, int value)
{
int row = _checked_constraint_index(constraint);
int error = gurobi::GRBsetintattrelement(m_model.get(), attr_name, row, value);
check_error(error);
m_update_flag |= m_attribute_update;
}
void GurobiModel::set_constraint_raw_attribute_char(const ConstraintIndex &constraint,
const char *attr_name, char value)
{
int row = _checked_constraint_index(constraint);
int error = gurobi::GRBsetcharattrelement(m_model.get(), attr_name, row, value);
check_error(error);
m_update_flag |= m_attribute_update;
}
void GurobiModel::set_constraint_raw_attribute_double(const ConstraintIndex &constraint,
const char *attr_name, double value)
{
int row = _checked_constraint_index(constraint);
int error = gurobi::GRBsetdblattrelement(m_model.get(), attr_name, row, value);
check_error(error);
m_update_flag |= m_attribute_update;
}
void GurobiModel::set_constraint_raw_attribute_string(const ConstraintIndex &constraint,
const char *attr_name, const char *value)
{
int row = _checked_constraint_index(constraint);
int error = gurobi::GRBsetstrattrelement(m_model.get(), attr_name, row, value);
check_error(error);
m_update_flag |= m_attribute_update;
}
int GurobiModel::get_constraint_raw_attribute_int(const ConstraintIndex &constraint,
const char *attr_name)
{
_update_for_information();
int row = _checked_constraint_index(constraint);
int retval;
int error = gurobi::GRBgetintattrelement(m_model.get(), attr_name, row, &retval);
check_error(error);
return retval;
}
char GurobiModel::get_constraint_raw_attribute_char(const ConstraintIndex &constraint,
const char *attr_name)
{
_update_for_information();
int row = _checked_constraint_index(constraint);
char retval;
int error = gurobi::GRBgetcharattrelement(m_model.get(), attr_name, row, &retval);
check_error(error);
return retval;
}
double GurobiModel::get_constraint_raw_attribute_double(const ConstraintIndex &constraint,
const char *attr_name)
{
_update_for_information();
int row = _checked_constraint_index(constraint);
double retval;
int error = gurobi::GRBgetdblattrelement(m_model.get(), attr_name, row, &retval);
check_error(error);
return retval;
}
std::string GurobiModel::get_constraint_raw_attribute_string(const ConstraintIndex &constraint,
const char *attr_name)
{
_update_for_information();
int row = _checked_constraint_index(constraint);
char *retval;
int error = gurobi::GRBgetstrattrelement(m_model.get(), attr_name, row, &retval);
check_error(error);
return std::string(retval);
}
double GurobiModel::get_normalized_rhs(const ConstraintIndex &constraint)
{
const char *name;
switch (constraint.type)
{
case ConstraintType::Linear:
name = GRB_DBL_ATTR_RHS;
break;
case ConstraintType::Quadratic:
name = GRB_DBL_ATTR_QCRHS;
break;
default:
throw std::runtime_error("Unknown constraint type to get_normalized_rhs");
}
return get_constraint_raw_attribute_double(constraint, name);
}
void GurobiModel::set_normalized_rhs(const ConstraintIndex &constraint, double value)
{
const char *name;
switch (constraint.type)
{
case ConstraintType::Linear:
name = GRB_DBL_ATTR_RHS;
break;
case ConstraintType::Quadratic:
name = GRB_DBL_ATTR_QCRHS;
break;
default:
throw std::runtime_error("Unknown constraint type to set_normalized_rhs");
}
set_constraint_raw_attribute_double(constraint, name, value);
}
double GurobiModel::get_normalized_coefficient(const ConstraintIndex &constraint,
const VariableIndex &variable)
{
if (constraint.type != ConstraintType::Linear)
{
throw std::runtime_error("Only linear constraint supports get_normalized_coefficient");
}
_update_for_information();
int row = _checked_constraint_index(constraint);
int col = _checked_variable_index(variable);
double retval;
int error = gurobi::GRBgetcoeff(m_model.get(), row, col, &retval);
check_error(error);
return retval;
}
void GurobiModel::set_normalized_coefficient(const ConstraintIndex &constraint,
const VariableIndex &variable, double value)
{
if (constraint.type != ConstraintType::Linear)
{
throw std::runtime_error("Only linear constraint supports set_normalized_coefficient");
}
int row = _checked_constraint_index(constraint);
int col = _checked_variable_index(variable);
int error = gurobi::GRBchgcoeffs(m_model.get(), 1, &row, &col, &value);
check_error(error);
m_update_flag |= m_constraint_coefficient_update;
}
double GurobiModel::get_objective_coefficient(const VariableIndex &variable)
{
return get_variable_raw_attribute_double(variable, GRB_DBL_ATTR_OBJ);
}
void GurobiModel::set_objective_coefficient(const VariableIndex &variable, double value)
{
set_variable_raw_attribute_double(variable, GRB_DBL_ATTR_OBJ, value);
}
void GurobiModel::_converttofixed()
{
int error = gurobi::GRBconverttofixed(m_model.get());
check_error(error);
}
void GurobiModel::computeIIS()
{
int error = gurobi::GRBcomputeIIS(m_model.get());
check_error(error);
}
int GurobiModel::_constraint_index(const ConstraintIndex &constraint)
{
_update_for_constraint_index(constraint.type);
switch (constraint.type)
{
case ConstraintType::Linear:
return m_linear_constraint_index.get_index(constraint.index);
case ConstraintType::Quadratic:
return m_quadratic_constraint_index.get_index(constraint.index);
case ConstraintType::SOS:
return m_sos_constraint_index.get_index(constraint.index);
default:
throw std::runtime_error("Unknown constraint type");
}
}
int GurobiModel::_checked_constraint_index(const ConstraintIndex &constraint)
{
int row = _constraint_index(constraint);
if (row < 0)
{
throw std::runtime_error("Variable does not exist");
}
return row;
}
void GurobiModel::check_error(int error)
{
if (error)
{
throw std::runtime_error(gurobi::GRBgeterrormsg(m_env));
}
}
void GurobiModel::_update_for_information()
{