1 #ifndef __GLPK_SOLVER_CPP_ 2 #define __GLPK_SOLVER_CPP_ 23 #include "glpk_solver.hpp" 30 lp = glp_create_prob();
31 glp_set_obj_dir(lp, GLP_MIN);
33 for (NVAR_TYPE i = 1; i <= n; ++i) {
34 glp_set_col_bnds(lp, i, GLP_LO, MIN_X, 0.0);
35 glp_set_obj_coef(lp, i, 1.0);
38 glp_term_out(GLP_OFF);
39 smcp.msg_lev = GLP_MSG_OFF;
40 smcp.presolve = GLP_ON;
41 row_data =
new double[1+n];
42 row_indx =
new int[1+n];
43 ray_data =
new RAYENT_TYPE[n];
48 m = other.m; n = other.n;
49 row_data =
new double[1+n];
50 row_indx =
new int[1+n];
51 ray_data =
new RAYENT_TYPE[n];
52 lp = glp_create_prob();
53 glp_copy_prob(lp, other.lp, GLP_OFF);
60 if (other !=
nullptr) {
63 delete [] row_data;
delete [] row_indx;
delete [] ray_data;
64 row_data =
new double[1+n];
65 row_indx =
new int[1+n];
66 ray_data =
new RAYENT_TYPE[n];
68 m = other->m; n = other->n;
70 lp = glp_create_prob();
71 glp_copy_prob(lp, other->lp, GLP_OFF);
75 return (other !=
nullptr);
78 GLPK_Solver::~GLPK_Solver() {
88 int new_m = newvecs.size();
90 glp_add_rows(lp, new_m);
91 for (
int i = 0; i < new_m; ++i) {
93 for (
int k = 0; k < n; ++k) {
94 if (newvecs[i][k] != 0) {
96 row_indx[num_valid] = 1 + k;
97 row_data[num_valid] = newvecs[i][k];
100 glp_set_mat_row(lp, 1 + m + i, num_valid, row_indx, row_data);
101 glp_set_row_bnds(lp, 1 + m + i, GLP_LO, MIN_X, 0.0);
104 smcp.presolve = GLP_OFF;
108 glp_result = glp_simplex(lp, &smcp);
110 glp_result = glp_exact(lp,
nullptr);
112 int status = glp_get_status(lp);
113 return (glp_result == 0 and (status == GLP_OPT or status == GLP_FEAS));
120 for (
int k = 0; k < n; ++k) {
121 if (newvec[k] != 0) {
123 row_indx[num_valid] = 1 + k;
124 row_data[num_valid] = newvec[k];
127 glp_set_mat_row(lp, 1 + m, num_valid, row_indx, row_data);
128 glp_set_row_bnds(lp, 1 + m, GLP_LO, MIN_X, 0.0);
130 smcp.presolve = GLP_OFF;
134 int glp_result = glp_simplex(lp, &smcp);
135 smcp.presolve = GLP_OFF;
137 glp_result = glp_exact(lp,
nullptr);
138 int status = glp_get_status(lp);
139 return (glp_result == 0 and (status == GLP_OPT or status == GLP_FEAS));
147 double curr_min = glp_get_obj_val(lp);
149 for (NVAR_TYPE i = 0; i < n; ++i) {
153 glp_set_mat_row(lp, 1 + m, n, row_indx, row_data);
154 glp_set_row_bnds(lp, 1 + m, GLP_DB, curr_min + 1, curr_min + 2);
156 for (
int i = 0; i < n; ++i)
157 glp_set_obj_coef(lp, 1 + i, 0.0);
158 for (
int i = 0; i < n; ++i) {
159 glp_set_obj_coef(lp, 1 + i, 1.0);
161 glp_set_obj_coef(lp, i, 0.0);
163 glp_simplex(lp, &smcp);
165 glp_exact(lp,
nullptr);
166 for (NVAR_TYPE j = 0; j < n; ++j)
167 ray_data[j] = static_cast<RAYENT_TYPE>(
168 round(1/MIN_X * glp_get_col_prim(lp, 1 + j))
170 rays.emplace(n, ray_data);
172 glp_set_obj_dir(lp, GLP_MAX);
173 glp_simplex(lp, &smcp);
175 glp_exact(lp,
nullptr);
176 for (NVAR_TYPE j = 0; j < n; ++j)
177 ray_data[j] = static_cast<RAYENT_TYPE>(
178 round(1/MIN_X * glp_get_col_prim(lp, 1 + j))
180 rays.emplace(n, ray_data);
181 glp_set_obj_dir(lp, GLP_MIN);
184 for (
unsigned i = 0; i < n; ++i)
185 glp_set_obj_coef(lp, 1 + i, 1.0);
188 glp_set_row_stat(lp, 1 + m, GLP_BS);
189 glp_del_rows(lp, 1, row_indx);
virtual bool copy(const LP_Solver *)
performs a deep copy, similar to a copy constructor
approximate skeleton of a polyhedral cone, using GLPK linear solver
exact or approximate polyhedral cone solution, with methods allowing definition and refinement ...
GLPK_Solver(NVAR_TYPE n)
initializes solver for variables
virtual const set< ray > & get_rays()
Returns the rays that define the skeleton.
virtual bool solve(constraint &)
Adds the indicated constraint (singular!) and re-computes the solution.