41 #if defined (HAVE_GLPK)
45 #if defined (HAVE_GLPK_GLPK_H)
46 #include <glpk/glpk.h>
77 glpk (
int sense,
int n,
int m,
double *c,
int nz,
int *rn,
int *cn,
78 double *a,
double *b,
char *ctype,
int *freeLB,
double *lb,
79 int *freeUB,
double *ub,
int *vartype,
int isMIP,
int lpsolver,
81 double *
xmin,
double *fmin,
int *status,
82 double *lambda,
double *redcosts,
double *time)
87 clock_t t_start = clock ();
89 glp_prob *lp = glp_create_prob ();
93 glp_set_obj_dir (lp, GLP_MIN);
95 glp_set_obj_dir (lp, GLP_MAX);
98 for (
int i = 0; i < n; i++)
101 if (! freeLB[i] && ! freeUB[i])
104 glp_set_col_bnds (lp, i+1, GLP_DB, lb[i], ub[i]);
106 glp_set_col_bnds (lp, i+1, GLP_FX, lb[i], ub[i]);
110 if (! freeLB[i] && freeUB[i])
111 glp_set_col_bnds (lp, i+1, GLP_LO, lb[i], ub[i]);
114 if (freeLB[i] && ! freeUB[i])
115 glp_set_col_bnds (lp, i+1, GLP_UP, lb[i], ub[i]);
117 glp_set_col_bnds (lp, i+1, GLP_FR, lb[i], ub[i]);
123 glp_set_obj_coef(lp,i+1,c[i]);
126 glp_set_col_kind (lp, i+1, vartype[i]);
129 glp_add_rows (lp, m);
131 for (
int i = 0; i < m; i++)
164 glp_set_row_bnds (lp, i+1, typx, b[i], b[i]);
168 glp_load_matrix (lp, nz, rn, cn, a);
172 static char tmp[] =
"outpb.lp";
173 if (glp_write_lp (lp, NULL, tmp) != 0)
175 error (
"__glpk__: unable to write problem");
181 if (!par->
presol || lpsolver != 1)
182 glp_scale_prob (lp, scale);
185 if (lpsolver == 1 && !par->
presol)
186 glp_adv_basis (lp, 0);
190 if ((!isMIP && lpsolver == 1)
191 || (isMIP && !par->
presol))
194 glp_init_smcp (&smcp);
195 smcp.msg_lev = par->
msglev;
196 smcp.meth = par->
dual;
197 smcp.pricing = par->
price;
198 smcp.r_test = par->
rtest;
199 smcp.tol_bnd = par->
tolbnd;
200 smcp.tol_dj = par->
toldj;
201 smcp.tol_piv = par->
tolpiv;
202 smcp.obj_ll = par->
objll;
203 smcp.obj_ul = par->
objul;
204 smcp.it_lim = par->
itlim;
205 smcp.tm_lim = par->
tmlim;
206 smcp.out_frq = par->
outfrq;
207 smcp.out_dly = par->
outdly;
208 smcp.presolve = par->
presol;
209 errnum = glp_simplex (lp, &smcp);
215 glp_init_iocp (&iocp);
216 iocp.msg_lev = par->
msglev;
217 iocp.br_tech = par->
branch;
218 iocp.bt_tech = par->
btrack;
219 iocp.tol_int = par->
tolint;
220 iocp.tol_obj = par->
tolobj;
221 iocp.tm_lim = par->
tmlim;
222 iocp.out_frq = par->
outfrq;
223 iocp.out_dly = par->
outdly;
224 iocp.presolve = par->
presol;
225 errnum = glp_intopt (lp, &iocp);
228 if (!isMIP && lpsolver == 2)
231 glp_init_iptcp (&iptcp);
232 iptcp.msg_lev = par->
msglev;
233 errnum = glp_interior (lp, &iptcp);
240 *status = glp_mip_status (lp);
241 *fmin = glp_mip_obj_val (lp);
247 *status = glp_get_status (lp);
248 *fmin = glp_get_obj_val (lp);
252 *status = glp_ipt_status (lp);
253 *fmin = glp_ipt_obj_val (lp);
259 for (
int i = 0; i < n; i++)
260 xmin[i] = glp_mip_col_val (lp, i+1);
265 for (
int i = 0; i < n; i++)
268 xmin[i] = glp_get_col_prim (lp, i+1);
270 xmin[i] = glp_ipt_col_prim (lp, i+1);
274 for (
int i = 0; i < m; i++)
277 lambda[i] = glp_get_row_dual (lp, i+1);
279 lambda[i] = glp_ipt_row_dual (lp, i+1);
283 for (
int i = 0; i < glp_get_num_cols (lp); i++)
286 redcosts[i] = glp_get_col_dual (lp, i+1);
288 redcosts[i] = glp_ipt_col_dual (lp, i+1);
292 *time = (clock () - t_start) / CLOCKS_PER_SEC;
295 glp_delete_prob (lp);
302 #define OCTAVE_GLPK_GET_REAL_PARAM(NAME, VAL) \
305 octave_value tmp = PARAM.getfield (NAME); \
307 if (tmp.is_defined ()) \
309 if (! tmp.is_empty ()) \
311 VAL = tmp.scalar_value (); \
315 error ("glpk: invalid value in PARAM." NAME); \
321 error ("glpk: invalid value in PARAM." NAME); \
328 #define OCTAVE_GLPK_GET_INT_PARAM(NAME, VAL) \
331 octave_value tmp = PARAM.getfield (NAME); \
333 if (tmp.is_defined ()) \
335 if (! tmp.is_empty ()) \
337 VAL = tmp.int_value (); \
341 error ("glpk: invalid value in PARAM." NAME); \
347 error ("glpk: invalid value in PARAM." NAME); \
356 @deftypefn {Loadable Function} {[@var{values}] =} __glpk__ (@var{args})\n\
357 Undocumented internal function.\n\
363 #if defined (HAVE_GLPK)
365 int nrhs = args.
length ();
375 volatile int mrowsc = args(0).rows ();
377 Matrix C (args(0).matrix_value ());
381 error (
"__glpk__: invalid value of C");
394 if (args(1).is_sparse_type ())
400 error (
"__glpk__: invalid value of A");
413 error (
"__glpk__: invalid value of A");
421 rn(nz) = A.
ridx (i) + 1;
428 Matrix A (args(1).matrix_value ());
432 error (
"__glpk__: invalid value of A");
439 a.
resize (mrowsA*mrowsc+1, 0.0);
441 for (
int i = 0; i < mrowsA; i++)
443 for (
int j = 0; j < mrowsc; j++)
459 Matrix B (args(2).matrix_value ());
463 error (
"__glpk__: invalid value of B");
471 Matrix LB (args(3).matrix_value ());
475 error (
"__glpk__: invalid value of LB");
483 for (
int i = 0; i < mrowsc; i++)
496 Matrix UB (args(4).matrix_value ());
500 error (
"__glpk__: invalid value of UB");
507 for (
int i = 0; i < mrowsc; i++)
520 charMatrix CTYPE (args(5).char_matrix_value ());
524 error (
"__glpk__: invalid value of CTYPE");
531 charMatrix VTYPE (args(6).char_matrix_value ());
535 error (
"__glpk__: invalid value of VARTYPE");
540 volatile int isMIP = 0;
541 for (
int i = 0; i < mrowsc ; i++)
543 if (VTYPE(i,0) ==
'I')
554 double SENSE = args(7).scalar_value ();
558 error (
"__glpk__: invalid value of SENSE");
572 error (
"__glpk__: invalid value of PARAM");
587 error (
"__glpk__: PARAM.msglev must be 0 (no output) or 1 (error and warning messages only [default]) or 2 (normal output) or 3 (full output)");
592 volatile int scale = 16;
594 if (scale < 0 || scale > 128)
596 error (
"__glpk__: PARAM.scale must either be 128 (automatic selection of scaling options), or a bitwise or of: 1 (geometric mean scaling), 16 (equilibration scaling), 32 (round scale factors to power of two), 64 (skip if problem is well scaled");
605 error (
"__glpk__: PARAM.dual must be 1 (use two-phase primal simplex [default]) or 2 (use two-phase dual simplex) or 3 (use two-phase dual simplex, and if it fails, switch to the primal simplex)");
614 error (
"__glpk__: PARAM.price must be 17 (textbook pricing) or 34 (steepest edge pricing [default])");
631 error (
"__glpk__: PARAM.branch must be 1 (first fractional variable) or 2 (last fractional variable) or 3 (most fractional variable) or 4 (heuristic by Driebeck and Tomlin [default]) or 5 (hybrid pseudocost heuristic)");
640 error (
"__glpk__: PARAM.btrack must be 1 (depth first search) or 2 (breadth first search) or 3 (best local bound) or 4 (best projection heuristic [default]");
649 error (
"__glpk__: PARAM.presol must be 0 (do NOT use LP presolver) or 1 (use LP presolver [default])");
654 volatile int lpsolver = 1;
656 if (lpsolver < 1 || lpsolver > 2)
658 error (
"__glpk__: PARAM.lpsolver must be 1 (simplex method) or 2 (interior point method)");
667 error (
"__glpk__: PARAM.rtest must be 17 (standard ratio test) or 34 (Harris' two-pass ratio test [default])");
678 volatile int save_pb = 0;
680 save_pb = save_pb != 0;
720 volatile int errnum = 0;
722 int jmpret = setjmp (
mark);
736 extra.
assign (
"lambda", lambda);
737 extra.assign (
"redcosts", redcosts);
740 extra.assign (
"time", time);
741 extra.assign (
"status", status);
octave_idx_type cols(void) const
octave_idx_type rows(void) const
OCTINTERP_API void print_usage(void)
octave_idx_type length(void) const
F77_RET_T const octave_idx_type Complex * A
int glpk(int sense, int n, int m, double *c, int nz, int *rn, int *cn, double *a, double *b, char *ctype, int *freeLB, double *lb, int *freeUB, double *ub, int *vartype, int isMIP, int lpsolver, int save_pb, int scale, const control_params *par, double *xmin, double *fmin, int *status, double *lambda, double *redcosts, double *time)
void error(const char *fmt,...)
Complex xmin(const Complex &x, const Complex &y)
octave_idx_type * cidx(void)
octave_idx_type rows(void) const
void gripe_not_supported(const char *fcn)
octave_idx_type nnz(void) const
#define OCTAVE_GLPK_GET_INT_PARAM(NAME, VAL)
#define OCTAVE_GLPK_GET_REAL_PARAM(NAME, VAL)
void resize(const dim_vector &dv, const T &rfv)
Handles the reference counting for all the derived classes.
charNDArray max(char d, const charNDArray &m)
octave_idx_type length(void) const
Number of elements in the array.
octave_idx_type * ridx(void)
F77_RET_T const octave_idx_type Complex const octave_idx_type Complex * B
void assign(const std::string &k, const octave_value &val)
void scale(Matrix &m, double x, double y, double z)
#define DEFUN_DLD(name, args_name, nargout_name, doc)
const T * fortran_vec(void) const
void resize(octave_idx_type n, const double &rfv=0)