26 #if defined (HAVE_CONFIG_H)
48 #if defined (HAVE_GLPK)
52 #if defined (HAVE_GLPK_GLPK_H)
53 # include <glpk/glpk.h>
82 glpk (
int sense,
int n,
int m,
double *c,
int nz,
int *rn,
int *cn,
83 double *a,
double *b,
char *ctype,
int *freeLB,
double *lb,
84 int *freeUB,
double *ub,
int *vartype,
int isMIP,
int lpsolver,
86 double *
xmin,
double *fmin,
int *status,
87 double *lambda,
double *redcosts,
double *time)
92 clock_t t_start = clock ();
94 glp_prob *lp = glp_create_prob ();
98 glp_set_obj_dir (lp, GLP_MIN);
100 glp_set_obj_dir (lp, GLP_MAX);
102 glp_add_cols (lp,
n);
103 for (
int i = 0; i <
n; i++)
106 if (! freeLB[i] && ! freeUB[i])
109 glp_set_col_bnds (lp, i+1, GLP_DB, lb[i], ub[i]);
111 glp_set_col_bnds (lp, i+1, GLP_FX, lb[i], ub[i]);
115 if (! freeLB[i] && freeUB[i])
116 glp_set_col_bnds (lp, i+1, GLP_LO, lb[i], ub[i]);
119 if (freeLB[i] && ! freeUB[i])
120 glp_set_col_bnds (lp, i+1, GLP_UP, lb[i], ub[i]);
122 glp_set_col_bnds (lp, i+1, GLP_FR, lb[i], ub[i]);
128 glp_set_obj_coef(lp,i+1,c[i]);
131 glp_set_col_kind (lp, i+1, vartype[i]);
134 glp_add_rows (lp,
m);
136 for (
int i = 0; i <
m; i++)
167 glp_set_row_bnds (lp, i+1, typx, b[i], b[i]);
171 glp_load_matrix (lp, nz, rn, cn, a);
175 static char tmp[] =
"outpb.lp";
176 if (glp_write_lp (lp,
nullptr, tmp) != 0)
177 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);
293 *time = (clock () - t_start) / CLOCKS_PER_SEC;
295 glp_delete_prob (lp);
307 #define OCTAVE_GLPK_GET_REAL_PARAM(NAME, VAL) \
310 octave_value tmp = PARAM.getfield (NAME); \
312 if (tmp.is_defined ()) \
314 if (! tmp.isempty ()) \
315 VAL = tmp.xscalar_value ("glpk: invalid value in PARAM" NAME); \
317 error ("glpk: invalid value in PARAM" NAME); \
322 #define OCTAVE_GLPK_GET_INT_PARAM(NAME, VAL) \
325 octave_value tmp = PARAM.getfield (NAME); \
327 if (tmp.is_defined ()) \
329 if (! tmp.isempty ()) \
330 VAL = tmp.xint_value ("glpk: invalid value in PARAM" NAME); \
332 error ("glpk: invalid value in PARAM" NAME); \
343 #if defined (HAVE_GLPK)
346 if (args.length () != 9)
350 int mrowsc = args(0).rows ();
352 Matrix C = args(0).xmatrix_value (
"__glpk__: invalid value of C");
354 double *c =
C.fortran_vec ();
363 if (args(1).issparse ())
365 SparseMatrix A = args(1).xsparse_matrix_value (
"__glpk__: invalid value of A");
375 error (
"__glpk__: invalid value of A");
381 rn(nz) =
A.ridx (i) + 1;
388 Matrix A = args(1).xmatrix_value (
"__glpk__: invalid value of A");
393 a.
resize (mrowsA*mrowsc+1, 0.0);
395 for (
int i = 0; i < mrowsA; i++)
397 for (
int j = 0; j < mrowsc; j++)
413 Matrix B = args(2).xmatrix_value (
"__glpk__: invalid value of B");
415 double *b =
B.fortran_vec ();
419 Matrix LB = args(3).xmatrix_value (
"__glpk__: invalid value of LB");
421 if (LB.
numel () < mrowsc)
422 error (
"__glpk__: invalid dimensions for LB");
428 for (
int i = 0; i < mrowsc; i++)
441 Matrix UB = args(4).xmatrix_value (
"__glpk__: invalid value of UB");
443 if (UB.
numel () < mrowsc)
444 error (
"__glpk__: invalid dimensions for UB");
449 for (
int i = 0; i < mrowsc; i++)
462 charMatrix CTYPE = args(5).xchar_matrix_value (
"__glpk__: invalid value of CTYPE");
467 charMatrix VTYPE = args(6).xchar_matrix_value (
"__glpk__: invalid value of VARTYPE");
471 for (
int i = 0; i < mrowsc ; i++)
473 if (VTYPE(i,0) ==
'I')
484 double SENSE = args(7).xscalar_value (
"__glpk__: invalid value of SENSE");
492 octave_scalar_map PARAM = args(8).xscalar_map_value (
"__glpk__: invalid value of PARAM");
502 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)");
507 if (scale < 0 || scale > 128)
508 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");
514 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)");
520 error (
"__glpk__: PARAM.price must be 17 (textbook pricing) or 34 (steepest edge pricing [default])");
534 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)");
540 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]");
546 error (
"__glpk__: PARAM.presol must be 0 (do NOT use LP presolver) or 1 (use LP presolver [default])");
551 if (lpsolver < 1 || lpsolver > 2)
552 error (
"__glpk__: PARAM.lpsolver must be 1 (simplex method) or 2 (interior point method)");
558 error (
"__glpk__: PARAM.rtest must be 17 (standard ratio test) or 34 (Harris' two-pass ratio test [default])");
569 save_pb = save_pb != 0;
612 save_pb,
scale, &par,
xmin.fortran_vec (), &fmin,
620 extra.
assign (
"lambda", lambda);
621 extra.
assign (
"redcosts", redcosts);
624 extra.
assign (
"time", time);
625 extra.
assign (
"status", status);
627 return ovl (
xmin, fmin, errnum, extra);
631 octave_unused_parameter (args);
#define OCTAVE_GLPK_GET_INT_PARAM(NAME, VAL)
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)
#define OCTAVE_GLPK_GET_REAL_PARAM(NAME, VAL)
charNDArray max(char d, const charNDArray &m)
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
octave_idx_type numel(void) const
Number of elements in the array.
const T * fortran_vec(void) const
Size of the specified dimension.
void resize(octave_idx_type n, const double &rfv=0)
Vector representing the dimensions (size) of an Array.
void assign(const std::string &k, const octave_value &val)
#define DEFUN_DLD(name, args_name, nargout_name, doc)
Macro to define an at run time dynamically loadable builtin function.
OCTINTERP_API void print_usage(void)
void error(const char *fmt,...)
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
void scale(Matrix &m, double x, double y, double z)
F77_RET_T const F77_INT F77_CMPLX const F77_INT F77_CMPLX * B
F77_RET_T const F77_INT F77_CMPLX * A
octave_int< T > xmin(const octave_int< T > &x, const octave_int< T > &y)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.