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)
95 clock_t t_start = clock ();
97 glp_prob *lp = glp_create_prob ();
101 glp_set_obj_dir (lp, GLP_MIN);
103 glp_set_obj_dir (lp, GLP_MAX);
105 glp_add_cols (lp,
n);
106 for (
int i = 0; i <
n; i++)
109 if (! freeLB[i] && ! freeUB[i])
112 glp_set_col_bnds (lp, i+1, GLP_DB, lb[i], ub[i]);
114 glp_set_col_bnds (lp, i+1, GLP_FX, lb[i], ub[i]);
118 if (! freeLB[i] && freeUB[i])
119 glp_set_col_bnds (lp, i+1, GLP_LO, lb[i], ub[i]);
122 if (freeLB[i] && ! freeUB[i])
123 glp_set_col_bnds (lp, i+1, GLP_UP, lb[i], ub[i]);
125 glp_set_col_bnds (lp, i+1, GLP_FR, lb[i], ub[i]);
131 glp_set_obj_coef(lp, i+1, c[i]);
134 glp_set_col_kind (lp, i+1, vartype[i]);
137 glp_add_rows (lp,
m);
139 for (
int i = 0; i <
m; i++)
170 glp_set_row_bnds (lp, i+1, typx, b[i], b[i]);
174 glp_load_matrix (lp, nz, rn, cn, a);
178 static char tmp[] =
"outpb.lp";
179 if (glp_write_lp (lp,
nullptr, tmp) != 0)
180 error (
"__glpk__: unable to write problem");
184 if (! par.
presol || lpsolver != 1)
185 glp_scale_prob (lp,
scale);
188 if (lpsolver == 1 && ! par.
presol)
189 glp_adv_basis (lp, 0);
193 if ((! isMIP && lpsolver == 1)
194 || (isMIP && ! par.
presol))
197 glp_init_smcp (&smcp);
198 smcp.msg_lev = par.
msglev;
199 smcp.meth = par.
dual;
200 smcp.pricing = par.
price;
201 smcp.r_test = par.
rtest;
202 smcp.tol_bnd = par.
tolbnd;
203 smcp.tol_dj = par.
toldj;
204 smcp.tol_piv = par.
tolpiv;
205 smcp.obj_ll = par.
objll;
206 smcp.obj_ul = par.
objul;
207 smcp.it_lim = par.
itlim;
208 smcp.tm_lim = par.
tmlim;
209 smcp.out_frq = par.
outfrq;
210 smcp.out_dly = par.
outdly;
211 smcp.presolve = par.
presol;
212 errnum = glp_simplex (lp, &smcp);
218 glp_init_iocp (&iocp);
219 iocp.msg_lev = par.
msglev;
220 iocp.br_tech = par.
branch;
221 iocp.bt_tech = par.
btrack;
222 iocp.tol_int = par.
tolint;
223 iocp.tol_obj = par.
tolobj;
224 iocp.tm_lim = par.
tmlim;
225 iocp.out_frq = par.
outfrq;
226 iocp.out_dly = par.
outdly;
227 iocp.presolve = par.
presol;
228 errnum = glp_intopt (lp, &iocp);
231 if (! isMIP && lpsolver == 2)
234 glp_init_iptcp (&iptcp);
235 iptcp.msg_lev = par.
msglev;
236 errnum = glp_interior (lp, &iptcp);
243 status = glp_mip_status (lp);
244 fmin = glp_mip_obj_val (lp);
250 status = glp_get_status (lp);
251 fmin = glp_get_obj_val (lp);
255 status = glp_ipt_status (lp);
256 fmin = glp_ipt_obj_val (lp);
262 for (
int i = 0; i <
n; i++)
263 xmin[i] = glp_mip_col_val (lp, i+1);
268 for (
int i = 0; i <
n; i++)
271 xmin[i] = glp_get_col_prim (lp, i+1);
273 xmin[i] = glp_ipt_col_prim (lp, i+1);
277 for (
int i = 0; i <
m; i++)
280 lambda[i] = glp_get_row_dual (lp, i+1);
282 lambda[i] = glp_ipt_row_dual (lp, i+1);
286 for (
int i = 0; i < glp_get_num_cols (lp); i++)
289 redcosts[i] = glp_get_col_dual (lp, i+1);
291 redcosts[i] = glp_ipt_col_dual (lp, i+1);
296 time = (clock () - t_start) / CLOCKS_PER_SEC;
298 glp_delete_prob (lp);
312 #define OCTAVE_GLPK_GET_REAL_PARAM(NAME, VAL) \
315 octave_value tmp = PARAM.getfield (NAME); \
317 if (tmp.is_defined ()) \
319 if (! tmp.isempty ()) \
320 VAL = tmp.xscalar_value ("glpk: invalid value in PARAM" NAME); \
322 error ("glpk: invalid value in PARAM" NAME); \
327 #define OCTAVE_GLPK_GET_INT_PARAM(NAME, VAL) \
330 octave_value tmp = PARAM.getfield (NAME); \
332 if (tmp.is_defined ()) \
334 if (! tmp.isempty ()) \
335 VAL = tmp.xint_value ("glpk: invalid value in PARAM" NAME); \
337 error ("glpk: invalid value in PARAM" NAME); \
348 #if defined (HAVE_GLPK)
351 if (args.length () != 9)
355 int mrowsc = args(0).rows ();
357 Matrix C = args(0).xmatrix_value (
"__glpk__: invalid value of C");
359 double *c =
C.fortran_vec ();
368 if (args(1).issparse ())
370 SparseMatrix A = args(1).xsparse_matrix_value (
"__glpk__: invalid value of A");
380 error (
"__glpk__: invalid value of A");
386 rn(nz) =
A.ridx (i) + 1;
393 Matrix A = args(1).xmatrix_value (
"__glpk__: invalid value of A");
398 a.
resize (mrowsA*mrowsc+1, 0.0);
400 for (
int i = 0; i < mrowsA; i++)
402 for (
int j = 0; j < mrowsc; j++)
418 Matrix B = args(2).xmatrix_value (
"__glpk__: invalid value of B");
420 double *b =
B.fortran_vec ();
424 Matrix LB = args(3).xmatrix_value (
"__glpk__: invalid value of LB");
426 if (LB.
numel () < mrowsc)
427 error (
"__glpk__: invalid dimensions for LB");
433 for (
int i = 0; i < mrowsc; i++)
446 Matrix UB = args(4).xmatrix_value (
"__glpk__: invalid value of UB");
448 if (UB.
numel () < mrowsc)
449 error (
"__glpk__: invalid dimensions for UB");
454 for (
int i = 0; i < mrowsc; i++)
467 charMatrix CTYPE = args(5).xchar_matrix_value (
"__glpk__: invalid value of CTYPE");
472 charMatrix VTYPE = args(6).xchar_matrix_value (
"__glpk__: invalid value of VARTYPE");
476 for (
int i = 0; i < mrowsc ; i++)
478 if (VTYPE(i, 0) ==
'I')
489 double SENSE = args(7).xscalar_value (
"__glpk__: invalid value of SENSE");
497 octave_scalar_map PARAM = args(8).xscalar_map_value (
"__glpk__: invalid value of PARAM");
507 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)");
512 if (scale < 0 || scale > 128)
513 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");
519 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)");
525 error (
"__glpk__: PARAM.price must be 17 (textbook pricing) or 34 (steepest edge pricing [default])");
539 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)");
545 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]");
551 error (
"__glpk__: PARAM.presol must be 0 (do NOT use LP presolver) or 1 (use LP presolver [default])");
556 if (lpsolver < 1 || lpsolver > 2)
557 error (
"__glpk__: PARAM.lpsolver must be 1 (simplex method) or 2 (interior point method)");
563 error (
"__glpk__: PARAM.rtest must be 17 (standard ratio test) or 34 (Harris' two-pass ratio test [default])");
574 save_pb = save_pb != 0;
618 save_pb,
scale, par,
xmin.fortran_vec (), fmin,
626 extra.
assign (
"lambda", lambda);
627 extra.
assign (
"redcosts", redcosts);
631 extra.
assign (
"status", status);
633 return ovl (
xmin, fmin, errnum, extra);
637 octave_unused_parameter (args);
#define OCTAVE_GLPK_GET_INT_PARAM(NAME, VAL)
#define OCTAVE_GLPK_GET_REAL_PARAM(NAME, VAL)
static 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)
charNDArray max(char d, const charNDArray &m)
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
OCTARRAY_API void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
OCTARRAY_API T * fortran_vec(void)
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)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#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.