-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhighs_model.hpp
240 lines (209 loc) · 8.61 KB
/
highs_model.hpp
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
#pragma once
#include <memory>
#include "interfaces/highs_c_api.h"
#include "lp_data/HConst.h"
#include "pyoptinterface/core.hpp"
#include "pyoptinterface/container.hpp"
#include "pyoptinterface/solver_common.hpp"
#include "pyoptinterface/dylib.hpp"
#define APILIST \
B(Highs_create); \
B(Highs_destroy); \
B(Highs_writeModel); \
B(Highs_writeSolution); \
B(Highs_writeSolutionPretty); \
B(Highs_addCol); \
B(Highs_passColName); \
B(Highs_getColName); \
B(Highs_getNumCol); \
B(Highs_changeColIntegrality); \
B(Highs_deleteColsBySet); \
B(Highs_addRow); \
B(Highs_passRowName); \
B(Highs_getRowName); \
B(Highs_getNumRow); \
B(Highs_deleteRowsBySet); \
B(Highs_passHessian); \
B(Highs_changeColsCostByRange); \
B(Highs_changeObjectiveOffset); \
B(Highs_changeObjectiveSense); \
B(Highs_run); \
B(Highs_getNumCols); \
B(Highs_getNumRows); \
B(Highs_getModelStatus); \
B(Highs_getDualRay); \
B(Highs_getPrimalRay); \
B(Highs_getIntInfoValue); \
B(Highs_getSolution); \
B(Highs_getHessianNumNz); \
B(Highs_getBasis); \
B(Highs_version); \
B(Highs_getRunTime); \
B(Highs_getOptionType); \
B(Highs_setBoolOptionValue); \
B(Highs_setIntOptionValue); \
B(Highs_setDoubleOptionValue); \
B(Highs_setStringOptionValue); \
B(Highs_getBoolOptionValue); \
B(Highs_getIntOptionValue); \
B(Highs_getDoubleOptionValue); \
B(Highs_getStringOptionValue); \
B(Highs_getInfoType); \
B(Highs_getInt64InfoValue); \
B(Highs_getDoubleInfoValue); \
B(Highs_getColIntegrality); \
B(Highs_changeColsBoundsBySet); \
B(Highs_getColsBySet); \
B(Highs_getObjectiveSense); \
B(Highs_getObjectiveValue); \
B(Highs_getColsByRange); \
B(Highs_setSolution); \
B(Highs_getRowsBySet); \
B(Highs_changeRowsBoundsBySet); \
B(Highs_changeCoeff); \
B(Highs_changeColCost);
namespace highs
{
#define B DYLIB_EXTERN_DECLARE
APILIST
#undef B
bool is_library_loaded();
bool load_library(const std::string &path);
} // namespace highs
struct HighsfreemodelT
{
void operator()(void *model) const
{
highs::Highs_destroy(model);
};
};
enum class HighsSolutionStatus
{
OPTIMIZE_NOT_CALLED,
OPTIMIZE_OK,
OPTIMIZE_ERROR,
};
struct POIHighsSolution
{
HighsSolutionStatus status = HighsSolutionStatus::OPTIMIZE_NOT_CALLED;
HighsInt model_status;
std::vector<double> colvalue;
std::vector<double> coldual;
std::vector<HighsInt> colstatus;
std::vector<double> rowvalue;
std::vector<double> rowdual;
std::vector<HighsInt> rowstatus;
HighsInt primal_solution_status;
HighsInt dual_solution_status;
bool has_primal_ray;
bool has_dual_ray;
std::vector<double> primal_ray;
std::vector<double> dual_ray;
};
class POIHighsModel : public OnesideLinearConstraintMixin<POIHighsModel>,
public TwosideLinearConstraintMixin<POIHighsModel>,
public LinearObjectiveMixin<POIHighsModel>,
public PPrintMixin<POIHighsModel>,
public GetValueMixin<POIHighsModel>
{
public:
POIHighsModel();
void init();
void close();
void write(const std::string &filename, bool pretty);
VariableIndex add_variable(VariableDomain domain = VariableDomain::Continuous,
double lb = -kHighsInf, double ub = kHighsInf,
const char *name = nullptr);
void delete_variable(const VariableIndex &variable);
void delete_variables(const Vector<VariableIndex> &variables);
bool is_variable_active(const VariableIndex &variable);
double get_variable_value(const VariableIndex &variable);
std::string pprint_variable(const VariableIndex &variable);
void set_variable_bounds(const VariableIndex &variable, double lb, double ub);
ConstraintIndex add_linear_constraint(const ScalarAffineFunction &function,
ConstraintSense sense, CoeffT rhs,
const char *name = nullptr);
ConstraintIndex add_linear_constraint(const ScalarAffineFunction &function,
const std::tuple<double, double> &interval,
const char *name = nullptr);
ConstraintIndex add_quadratic_constraint(const ScalarQuadraticFunction &function,
ConstraintSense sense, CoeffT rhs,
const char *name = nullptr);
void delete_constraint(const ConstraintIndex &constraint);
bool is_constraint_active(const ConstraintIndex &constraint);
void _set_affine_objective(const ScalarAffineFunction &function, ObjectiveSense sense,
bool clear_quadratic);
void set_objective(const ScalarAffineFunction &function, ObjectiveSense sense);
void set_objective(const ScalarQuadraticFunction &function, ObjectiveSense sense);
void set_objective(const ExprBuilder &function, ObjectiveSense sense);
void optimize();
void *get_raw_model();
std::string version_string();
double getruntime();
int getnumrow();
int getnumcol();
// option
int raw_option_type(const char *param_name);
void set_raw_option_bool(const char *param_name, bool value);
void set_raw_option_int(const char *param_name, int value);
void set_raw_option_double(const char *param_name, double value);
void set_raw_option_string(const char *param_name, const char *value);
bool get_raw_option_bool(const char *param_name);
int get_raw_option_int(const char *param_name);
double get_raw_option_double(const char *param_name);
std::string get_raw_option_string(const char *param_name);
// information
int raw_info_type(const char *info_name);
int get_raw_info_int(const char *info_name);
std::int64_t get_raw_info_int64(const char *info_name);
double get_raw_info_double(const char *info_name);
// Accessing information of m_problem
std::string get_variable_name(const VariableIndex &variable);
void set_variable_name(const VariableIndex &variable, const char *name);
VariableDomain get_variable_type(const VariableIndex &variable);
void set_variable_type(const VariableIndex &variable, VariableDomain domain);
double get_variable_lower_bound(const VariableIndex &variable);
double get_variable_upper_bound(const VariableIndex &variable);
void set_variable_lower_bound(const VariableIndex &variable, double lb);
void set_variable_upper_bound(const VariableIndex &variable, double ub);
std::string get_constraint_name(const ConstraintIndex &constraint);
void set_constraint_name(const ConstraintIndex &constraint, const char *name);
double get_constraint_primal(const ConstraintIndex &constraint);
double get_constraint_dual(const ConstraintIndex &constraint);
ObjectiveSense get_obj_sense();
void set_obj_sense(ObjectiveSense sense);
double get_obj_value();
HighsInt _variable_index(const VariableIndex &variable);
HighsInt _checked_variable_index(const VariableIndex &variable);
HighsInt _constraint_index(const ConstraintIndex &constraint);
HighsInt _checked_constraint_index(const ConstraintIndex &constraint);
// Primal start
void set_primal_start(const Vector<VariableIndex> &variables, const Vector<double> &values);
// Modifications of model
// 1. set/get RHS of a constraint
double get_normalized_rhs(const ConstraintIndex &constraint);
void set_normalized_rhs(const ConstraintIndex &constraint, double value);
// 2. set/get coefficient of variable in constraint
double get_normalized_coefficient(const ConstraintIndex &constraint,
const VariableIndex &variable);
void set_normalized_coefficient(const ConstraintIndex &constraint,
const VariableIndex &variable, double value);
// 3. set/get linear coefficient of variable in objective
double get_objective_coefficient(const VariableIndex &variable);
void set_objective_coefficient(const VariableIndex &variable, double value);
private:
MonotoneIndexer<HighsInt> m_variable_index;
MonotoneIndexer<HighsInt> m_linear_constraint_index;
// Highs does not discriminate between integer variable and binary variable
// So we need to keep track of binary variables
Hashset<IndexT> binary_variables;
/* Highs part */
std::unique_ptr<void, HighsfreemodelT> m_model;
public:
// cache the solution
POIHighsSolution m_solution;
// cache the number of variable and constraints because querying them via HiGHS API is very
// expensive
HighsInt m_n_variables = 0;
HighsInt m_n_constraints = 0;
};