280 int idx = luaL_checkinteger(L,2);
281 const char *name = luaL_checkstring(L,3);
282 double coef = luaL_checknumber(L,4);
283 const char *skind = luaL_optstring(L,5,
"real");
284 int haslb = !lua_isnoneornil(L,6);
285 int hasub = !lua_isnoneornil(L,7);
286 double lb = luaL_optnumber(L,6,0.0);
287 double ub = luaL_optnumber(L,7,0.0);
288 int type = GLP_FR, kind = GLP_CV;
291 glp_set_col_name( lp->
prob, idx, name );
292 glp_set_obj_coef( lp->
prob, idx, coef );
295 if (haslb && hasub) {
296 if (fabs(lb-ub) < 1e-5)
307 glp_set_col_bnds( lp->
prob, idx, type, lb, ub );
310 if (strcmp(skind,
"real")==0)
312 else if (strcmp(skind,
"integer")==0)
314 else if (strcmp(skind,
"binary")==0)
317 return NLUA_ERROR(L,_(
"Unknown column kind '%s'!"), skind);
318 glp_set_col_kind( lp->
prob, idx, kind );
381 luaL_checktype(L, 2, LUA_TTABLE);
382 luaL_checktype(L, 3, LUA_TTABLE);
383 luaL_checktype(L, 4, LUA_TTABLE);
388 if ((n != lua_objlen(L,3)) || (n != lua_objlen(L,4)))
389 return NLUA_ERROR(L, _(
"Table lengths don't match!"));
393 ia = calloc( n+1,
sizeof(
int) );
394 ja = calloc( n+1,
sizeof(
int) );
395 ar = calloc( n+1,
sizeof(
double) );
396 for (
size_t i=1; i<=n; i++) {
397 lua_rawgeti(L, 2, i);
398 lua_rawgeti(L, 3, i);
399 lua_rawgeti(L, 4, i);
401 ia[i] = luaL_checkinteger(L,-3);
402 ja[i] = luaL_checkinteger(L,-2);
403 ar[i] = luaL_checknumber(L,-1);
405 ia[i] = lua_tointeger(L,-3);
406 ja[i] = lua_tointeger(L,-2);
407 ar[i] = lua_tonumber(L,-1);
413 glp_load_matrix( lp->
prob, n, ia, ja, ar );
599 Uint64 starttime = SDL_GetTicks64();
603 ismip = (glp_get_num_int( lp->
prob ) > 0);
604 glp_init_smcp(&parm_smcp);
605 parm_smcp.msg_lev = GLP_MSG_ERR;
608 glp_init_iocp(&parm_iocp);
609 parm_iocp.msg_lev = GLP_MSG_ERR;
614 if (!lua_isnoneornil(L,2)) {
615 GETOPT_SMCP( meth, opt_meth, METH_DEF );
616 GETOPT_SMCP( pricing, opt_pricing, PRICING_DEF );
617 GETOPT_SMCP( r_test, opt_r_test, R_TEST_DEF );
618 GETOPT_SMCP( presolve,opt_onoff, PRESOLVE_DEF );
620 GETOPT_IOCP( br_tech, opt_br_tech, BR_TECH_DEF );
621 GETOPT_IOCP( bt_tech, opt_bt_tech, BT_TECH_DEF );
622 GETOPT_IOCP( pp_tech, opt_pp_tech, PP_TECH_DEF );
624 GETOPT_IOCP( fp_heur, opt_onoff, FP_HEUR_DEF );
626 GETOPT_IOCP( gmi_cuts, opt_onoff, GMI_CUTS_DEF );
627 GETOPT_IOCP( mir_cuts, opt_onoff, MIR_CUTS_DEF );
628 GETOPT_IOCP( cov_cuts, opt_onoff, COV_CUTS_DEF );
629 GETOPT_IOCP( clq_cuts, opt_onoff, CLQ_CUTS_DEF );
634 parm_smcp.meth = METH_DEF;
635 parm_smcp.pricing = PRICING_DEF;
636 parm_smcp.r_test = R_TEST_DEF;
637 parm_smcp.presolve= PRESOLVE_DEF;
639 parm_iocp.br_tech = BR_TECH_DEF;
640 parm_iocp.bt_tech = BT_TECH_DEF;
641 parm_iocp.pp_tech = PP_TECH_DEF;
642 parm_iocp.sr_heur = SR_HEUR_DEF;
643 parm_iocp.fp_heur = FP_HEUR_DEF;
644 parm_iocp.ps_heur = PS_HEUR_DEF;
645 parm_iocp.gmi_cuts = GMI_CUTS_DEF;
646 parm_iocp.mir_cuts = MIR_CUTS_DEF;
647 parm_iocp.cov_cuts = COV_CUTS_DEF;
648 parm_iocp.clq_cuts = CLQ_CUTS_DEF;
654 if (!ismip || !parm_iocp.presolve) {
655 ret = glp_simplex( lp->
prob, &parm_smcp );
656 if ((ret != 0) && (ret != GLP_ETMLIM)) {
658 lua_pushstring(L, linopt_error(ret));
662 ret = glp_get_status(lp->
prob);
663 if ((ret != GLP_OPT) && (ret != GLP_FEAS)) {
665 lua_pushstring(L, linopt_status(ret));
670 ret = glp_intopt( lp->
prob, &parm_iocp );
671 if ((ret != 0) && (ret != GLP_ETMLIM)) {
673 lua_pushstring(L, linopt_error(ret));
677 ret = glp_mip_status(lp->
prob);
678 if ((ret != GLP_OPT) && (ret != GLP_FEAS)) {
680 lua_pushstring(L, linopt_status(ret));
684 z = glp_get_obj_val( lp->
prob );
691 for (
int i=1; i<=lp->
ncols; i++) {
693 z = glp_mip_col_val( lp->
prob, i );
695 z = glp_get_col_prim( lp->
prob, i );
696 lua_pushnumber( L, z );
697 lua_rawseti( L, -2, i );
702 for (
int i=1; i<=lp->
nrows; i++) {
704 z = glp_mip_row_val( lp->
prob, i );
706 z = glp_get_row_prim( lp->
prob, i );
707 lua_pushnumber( L, z );
708 lua_rawseti( L, -2, i );
714 WARN(_(
"glpk: too over 1 second to optimize!"));
732 const char *fname = luaL_checkstring(L,1);
733 int glpk_format = lua_toboolean(L,2);
734 int maximize = lua_toboolean(L,3);
735 const char *dirname = PHYSFS_getRealDir( fname );
740 return NLUA_ERROR( L, _(
"Failed to read LP problem \"%s\"!"), fname );
741 SDL_asprintf( &fpath,
"%s/%s", dirname, fname );
742 lp.
prob = glp_create_prob();
743 ret = glpk_format ? glp_read_prob( lp.
prob, 0, fpath ) : glp_read_mps( lp.
prob, GLP_MPS_FILE, NULL, fpath );
746 glp_delete_prob( lp.
prob );
747 return NLUA_ERROR( L, _(
"Failed to read LP problem \"%s\"!"), fname );
752 glp_set_obj_dir( lp.
prob, GLP_MAX );
768 const char *fname = luaL_checkstring(L,2);
769 int glpk_format = lua_toboolean(L,3);
770 const char *dirname = PHYSFS_getWriteDir();
773 SDL_asprintf( &fpath,
"%s/%s", dirname, fname );
774 ret = glpk_format ? glp_write_prob( lp->
prob, 0, fpath ) : glp_write_mps( lp->
prob, GLP_MPS_FILE, NULL, fpath );
776 lua_pushboolean( L, ret==0 );