예제 #1
0
int lpx_ipt_status(glp_prob *lp)
{     /* retrieve status of interior-point solution */
      int status;
      switch (glp_ipt_status(lp))
      {  case GLP_UNDEF:  status = LPX_T_UNDEF;  break;
         case GLP_OPT:    status = LPX_T_OPT;    break;
         default:         xassert(lp != lp);
      }
      return status;
}
예제 #2
0
파일: lp.c 프로젝트: danielriosgarza/pyglpk
static PyObject* LPX_solver_interior(LPXObject *self) {
	int retval = lpx_interior(LP);
	int status = glp_ipt_status(LP);
	if (retval != LPX_E_FAULT)
		self->last_solver = 1;
	if (retval == LPX_E_OK && status != GLP_OPT)
		return glpstatus2string(status);
	else
		return solver_retval_to_message(retval);
}
예제 #3
0
파일: lp.c 프로젝트: kleptog/pyglpk
static PyObject* LPX_getstatus(LPXObject *self, void *closure) {
  int status;
  switch (self->last_solver) {
  case -1:
  case 0: status=glp_get_status(LP); break;
  case 1: status=glp_ipt_status(LP); break;
  case 2: status=glp_mip_status(LP); break;
  default: 
    PyErr_SetString(PyExc_RuntimeError,
		    "bad internal state for last solver identifier");
    return NULL;
  }
  return glpstatus2string(status);
}
예제 #4
0
int glp_print_ipt(glp_prob *P, const char *fname)
{   /* write interior-point solution in printable format */
    glp_file *fp;
    GLPROW *row;
    GLPCOL *col;
    int i, j, t, ae_ind, re_ind, ret;
    double ae_max, re_max;
    xprintf("Writing interior-point solution to '%s'...\n", fname);
    fp = glp_open(fname, "w");
    if (fp == NULL)
    {   xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
        ret = 1;
        goto done;
    }
    xfprintf(fp, "%-12s%s\n", "Problem:",
             P->name == NULL ? "" : P->name);
    xfprintf(fp, "%-12s%d\n", "Rows:", P->m);
    xfprintf(fp, "%-12s%d\n", "Columns:", P->n);
    xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz);
    t = glp_ipt_status(P);
    xfprintf(fp, "%-12s%s\n", "Status:",
             t == GLP_OPT    ? "OPTIMAL" :
             t == GLP_UNDEF  ? "UNDEFINED" :
             t == GLP_INFEAS ? "INFEASIBLE (INTERMEDIATE)" :
             t == GLP_NOFEAS ? "INFEASIBLE (FINAL)" : "???");
    xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
             P->obj == NULL ? "" : P->obj,
             P->obj == NULL ? "" : " = ", P->ipt_obj,
             P->dir == GLP_MIN ? "MINimum" :
             P->dir == GLP_MAX ? "MAXimum" : "???");
    xfprintf(fp, "\n");
    xfprintf(fp, "   No.   Row name        Activity     Lower bound  "
             " Upper bound    Marginal\n");
    xfprintf(fp, "------ ------------    ------------- ------------- "
             "------------- -------------\n");
    for (i = 1; i <= P->m; i++)
    {   row = P->row[i];
        xfprintf(fp, "%6d ", i);
        if (row->name == NULL || strlen(row->name) <= 12)
            xfprintf(fp, "%-12s ", row->name == NULL ? "" : row->name);
        else
            xfprintf(fp, "%s\n%20s", row->name, "");
        xfprintf(fp, "%3s", "");
        xfprintf(fp, "%13.6g ",
                 fabs(row->pval) <= 1e-9 ? 0.0 : row->pval);
        if (row->type == GLP_LO || row->type == GLP_DB ||
                row->type == GLP_FX)
            xfprintf(fp, "%13.6g ", row->lb);
        else
            xfprintf(fp, "%13s ", "");
        if (row->type == GLP_UP || row->type == GLP_DB)
            xfprintf(fp, "%13.6g ", row->ub);
        else
            xfprintf(fp, "%13s ", row->type == GLP_FX ? "=" : "");
        if (fabs(row->dval) <= 1e-9)
            xfprintf(fp, "%13s", "< eps");
        else
            xfprintf(fp, "%13.6g ", row->dval);
        xfprintf(fp, "\n");
    }
    xfprintf(fp, "\n");
    xfprintf(fp, "   No. Column name       Activity     Lower bound  "
             " Upper bound    Marginal\n");
    xfprintf(fp, "------ ------------    ------------- ------------- "
             "------------- -------------\n");
    for (j = 1; j <= P->n; j++)
    {   col = P->col[j];
        xfprintf(fp, "%6d ", j);
        if (col->name == NULL || strlen(col->name) <= 12)
            xfprintf(fp, "%-12s ", col->name == NULL ? "" : col->name);
        else
            xfprintf(fp, "%s\n%20s", col->name, "");
        xfprintf(fp, "%3s", "");
        xfprintf(fp, "%13.6g ",
                 fabs(col->pval) <= 1e-9 ? 0.0 : col->pval);
        if (col->type == GLP_LO || col->type == GLP_DB ||
                col->type == GLP_FX)
            xfprintf(fp, "%13.6g ", col->lb);
        else
            xfprintf(fp, "%13s ", "");
        if (col->type == GLP_UP || col->type == GLP_DB)
            xfprintf(fp, "%13.6g ", col->ub);
        else
            xfprintf(fp, "%13s ", col->type == GLP_FX ? "=" : "");
        if (fabs(col->dval) <= 1e-9)
            xfprintf(fp, "%13s", "< eps");
        else
            xfprintf(fp, "%13.6g ", col->dval);
        xfprintf(fp, "\n");
    }
    xfprintf(fp, "\n");
    xfprintf(fp, "Karush-Kuhn-Tucker optimality conditions:\n");
    xfprintf(fp, "\n");
    glp_check_kkt(P, GLP_IPT, GLP_KKT_PE, &ae_max, &ae_ind, &re_max,
                  &re_ind);
    xfprintf(fp, "KKT.PE: max.abs.err = %.2e on row %d\n",
             ae_max, ae_ind);
    xfprintf(fp, "        max.rel.err = %.2e on row %d\n",
             re_max, re_ind);
    xfprintf(fp, "%8s%s\n", "",
             re_max <= 1e-9 ? "High quality" :
             re_max <= 1e-6 ? "Medium quality" :
             re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS WRONG");
    xfprintf(fp, "\n");
    glp_check_kkt(P, GLP_IPT, GLP_KKT_PB, &ae_max, &ae_ind, &re_max,
                  &re_ind);
    xfprintf(fp, "KKT.PB: max.abs.err = %.2e on %s %d\n",
             ae_max, ae_ind <= P->m ? "row" : "column",
             ae_ind <= P->m ? ae_ind : ae_ind - P->m);
    xfprintf(fp, "        max.rel.err = %.2e on %s %d\n",
             re_max, re_ind <= P->m ? "row" : "column",
             re_ind <= P->m ? re_ind : re_ind - P->m);
    xfprintf(fp, "%8s%s\n", "",
             re_max <= 1e-9 ? "High quality" :
             re_max <= 1e-6 ? "Medium quality" :
             re_max <= 1e-3 ? "Low quality" : "PRIMAL SOLUTION IS INFEASIBL"
             "E");
    xfprintf(fp, "\n");
    glp_check_kkt(P, GLP_IPT, GLP_KKT_DE, &ae_max, &ae_ind, &re_max,
                  &re_ind);
    xfprintf(fp, "KKT.DE: max.abs.err = %.2e on column %d\n",
             ae_max, ae_ind == 0 ? 0 : ae_ind - P->m);
    xfprintf(fp, "        max.rel.err = %.2e on column %d\n",
             re_max, re_ind == 0 ? 0 : re_ind - P->m);
    xfprintf(fp, "%8s%s\n", "",
             re_max <= 1e-9 ? "High quality" :
             re_max <= 1e-6 ? "Medium quality" :
             re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS WRONG");
    xfprintf(fp, "\n");
    glp_check_kkt(P, GLP_IPT, GLP_KKT_DB, &ae_max, &ae_ind, &re_max,
                  &re_ind);
    xfprintf(fp, "KKT.DB: max.abs.err = %.2e on %s %d\n",
             ae_max, ae_ind <= P->m ? "row" : "column",
             ae_ind <= P->m ? ae_ind : ae_ind - P->m);
    xfprintf(fp, "        max.rel.err = %.2e on %s %d\n",
             re_max, re_ind <= P->m ? "row" : "column",
             re_ind <= P->m ? re_ind : re_ind - P->m);
    xfprintf(fp, "%8s%s\n", "",
             re_max <= 1e-9 ? "High quality" :
             re_max <= 1e-6 ? "Medium quality" :
             re_max <= 1e-3 ? "Low quality" : "DUAL SOLUTION IS INFEASIBLE")
    ;
    xfprintf(fp, "\n");
    xfprintf(fp, "End of output\n");
#if 0 /* FIXME */
    xfflush(fp);
#endif
    if (glp_ioerr(fp))
    {   xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
        ret = 1;
        goto done;
    }
    ret = 0;
done:
    if (fp != NULL) glp_close(fp);
    return ret;
}
예제 #5
0
파일: glpkcc.cpp 프로젝트: lenhattan86/EPLT
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, char *save_filename, char *filetype, 
         double *xmin, double *fmin, double *status,
      	 double *lambda, double *redcosts, double *time, double *mem)
{
  int typx = 0;
  int method;

  clock_t t_start = clock();

  // Obsolete
  //lib_set_fault_hook (NULL, glpk_fault_hook);

  //Redirect standard output
  if (glpIntParam[0] > 1) glp_term_hook (glpk_print_hook, NULL);
  else glp_term_hook (NULL, NULL);

  //-- Create an empty LP/MILP object
  glp_prob *lp = glp_create_prob ();

  //-- Set the sense of optimization
  if (sense == 1)
    glp_set_obj_dir (lp, GLP_MIN);
  else
    glp_set_obj_dir (lp, GLP_MAX);

  //-- Define the number of unknowns and their domains.
  glp_add_cols (lp, n);
  for (int i = 0; i < n; i++)
  {
    //-- Define type of the structural variables
    if (! freeLB[i] && ! freeUB[i])
      glp_set_col_bnds (lp, i+1, GLP_DB, lb[i], ub[i]);
    else
	  {
      if (! freeLB[i] && freeUB[i])
        glp_set_col_bnds (lp, i+1, GLP_LO, lb[i], ub[i]);
      else
      {
        if (freeLB[i] && ! freeUB[i])
		      glp_set_col_bnds (lp, i+1, GLP_UP, lb[i], ub[i]);
	      else
		      glp_set_col_bnds (lp, i+1, GLP_FR, lb[i], ub[i]);
	    }
	  }
  
  // -- Set the objective coefficient of the corresponding
  // -- structural variable. No constant term is assumed.
  glp_set_obj_coef(lp,i+1,c[i]);

  if (isMIP)
    glp_set_col_kind (lp, i+1, vartype[i]);
  }

  glp_add_rows (lp, m);

  for (int i = 0; i < m; i++)
  {
    /*  If the i-th row has no lower bound (types F,U), the
        corrispondent parameter will be ignored.
        If the i-th row has no upper bound (types F,L), the corrispondent
        parameter will be ignored.
        If the i-th row is of S type, the i-th LB is used, but
        the i-th UB is ignored.
    */

    switch (ctype[i])
    {
      case 'F': typx = GLP_FR; break;
      // upper bound
	    case 'U': typx = GLP_UP; break;
      // lower bound
	    case 'L': typx = GLP_LO; break;
      // fixed constraint
	    case 'S': typx = GLP_FX; break;
      // double-bounded variable
      case 'D': typx = GLP_DB; break;
	  }
      
    glp_set_row_bnds (lp, i+1, typx, b[i], b[i]);

  }
  // Load constraint matrix A
  glp_load_matrix (lp, nz, rn, cn, a);

  // Save problem
  if (save_pb) {
    if (!strcmp(filetype,"cplex")){
      if (lpx_write_cpxlp (lp, save_filename) != 0) {
	        mexErrMsgTxt("glpkcc: unable to write the problem");
	        longjmp (mark, -1);
      }
    }else{
      if (!strcmp(filetype,"fixedmps")){
        if (lpx_write_mps (lp, save_filename) != 0) {
          mexErrMsgTxt("glpkcc: unable to write the problem");
	        longjmp (mark, -1);  
        }
      }else{
        if (!strcmp(filetype,"freemps")){
          if (lpx_write_freemps (lp, save_filename) != 0) {
            mexErrMsgTxt("glpkcc: unable to write the problem");
	          longjmp (mark, -1);
          }
        }else{// plain text
          if (lpx_print_prob (lp, save_filename) != 0) {
            mexErrMsgTxt("glpkcc: unable to write the problem");
	          longjmp (mark, -1);
          } 
        } 
      }    
    } 
  }
  //-- scale the problem data (if required)
  if (glpIntParam[1] && (! glpIntParam[16] || lpsolver != 1))
    lpx_scale_prob (lp);

  //-- build advanced initial basis (if required)
  if (lpsolver == 1 && ! glpIntParam[16])
    lpx_adv_basis (lp);

  glp_smcp sParam;
  glp_init_smcp(&sParam);
  
  //-- set control parameters
  if (lpsolver==1){
    //remap of control parameters for simplex method
    sParam.msg_lev=glpIntParam[0];	// message level
    // simplex method: primal/dual
    if (glpIntParam[2]==0) sParam.meth=GLP_PRIMAL;		
    else sParam.meth=GLP_DUALP;
    // pricing technique
    if (glpIntParam[3]==0) sParam.pricing=GLP_PT_STD;
    else sParam.pricing=GLP_PT_PSE;
    //sParam.r_test not available
    sParam.tol_bnd=glpRealParam[1];	// primal feasible tollerance
    sParam.tol_dj=glpRealParam[2];	// dual feasible tollerance
    sParam.tol_piv=glpRealParam[3];	// pivot tollerance
    sParam.obj_ll=glpRealParam[4];	// lower limit
    sParam.obj_ul=glpRealParam[5];	// upper limit
    // iteration limit
    if (glpIntParam[5]==-1) sParam.it_lim=INT_MAX;
    else sParam.it_lim=glpIntParam[5];   
    // time limit
    if (glpRealParam[6]==-1) sParam.tm_lim=INT_MAX;
    else sParam.tm_lim=(int) glpRealParam[6];	
    sParam.out_frq=glpIntParam[7];	// output frequency
    sParam.out_dly=(int) glpRealParam[7];	// output delay
    // presolver
    if (glpIntParam[16]) sParam.presolve=GLP_ON;
    else sParam.presolve=GLP_OFF;
  }else{
	for(int i = 0; i < NIntP; i++)
		lpx_set_int_parm (lp, IParam[i], glpIntParam[i]);
		
	for (int i = 0; i < NRealP; i++)
		lpx_set_real_parm (lp, RParam[i], glpRealParam[i]);
  }
  

  // Choose simplex method ('S') or interior point method ('T') to solve the problem
  if (lpsolver == 1)
    method = 'S';
  else
    method = 'T';
	
  int errnum;

  switch (method){
    case 'S': {
      if (isMIP){
	    method = 'I';
	    errnum = lpx_intopt (lp);
      }
      else{
		errnum = glp_simplex(lp, &sParam);
		errnum += 100; //this is to avoid ambiguity in the return codes.
	  }
    }
    break;

    case 'T': errnum = lpx_interior(lp); break;

    default:  xassert (method != method);
  }

  /*  errnum assumes the following results:
      errnum = 0 <=> No errors
      errnum = 1 <=> Iteration limit exceeded.
      errnum = 2 <=> Numerical problems with basis matrix.
  */
  if (errnum == LPX_E_OK || errnum==100){
    // Get status and object value
    if (isMIP)
    {
      *status = glp_mip_status (lp);
      *fmin = glp_mip_obj_val (lp);
    }
    else
    {
      if (lpsolver == 1)
      {
        *status = glp_get_status (lp);
        *fmin = glp_get_obj_val (lp);
	    }
      else
      {
        *status = glp_ipt_status (lp);
        *fmin = glp_ipt_obj_val (lp);
	    }
    }
    // Get optimal solution (if exists)
    if (isMIP)
    {
      for (int i = 0; i < n; i++)
        xmin[i] = glp_mip_col_val (lp, i+1);
    }
    else
    {
      /* Primal values */
      for (int i = 0; i < n; i++)
      {
        if (lpsolver == 1)
          xmin[i] = glp_get_col_prim (lp, i+1);
        else
		      xmin[i] = glp_ipt_col_prim (lp, i+1);
      }
      /* Dual values */
      for (int i = 0; i < m; i++)
      {
        if (lpsolver == 1) lambda[i] = glp_get_row_dual (lp, i+1);
	     else lambda[i] = glp_ipt_row_dual (lp, i+1);
      }
      /* Reduced costs */
      for (int i = 0; i < glp_get_num_cols (lp); i++)
      {
        if (lpsolver == 1) redcosts[i] = glp_get_col_dual (lp, i+1);
        else redcosts[i] = glp_ipt_col_dual (lp, i+1);
      }
    }

    *time = (clock () - t_start) / CLOCKS_PER_SEC;
    
   	glp_ulong tpeak;
    lib_mem_usage(NULL, NULL, NULL, &tpeak);
    *mem=(double)(4294967296.0 * tpeak.hi + tpeak.lo) / (1024);
       
	  glp_delete_prob (lp);
    return 0;
  }

  glp_delete_prob (lp);

  *status = errnum;

  return errnum;
}
예제 #6
0
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, char *save_filename, char *filetype,
         double *xmin, double *fmin, double *status,
      	 double *lambda, double *redcosts, double *time, double *mem)
{
  int typx = 0;
  int method;

  clock_t t_start = clock();

  //Redirect standard output
  if (glpIntParam[0] > 1) glp_term_hook (glpk_print_hook, NULL);
  else glp_term_hook (NULL, NULL);

  //-- Create an empty LP/MILP object
  LPX *lp = lpx_create_prob ();

  //-- Set the sense of optimization
  if (sense == 1)
    glp_set_obj_dir (lp, GLP_MIN);
  else
    glp_set_obj_dir (lp, GLP_MAX);

  //-- Define the number of unknowns and their domains.
  glp_add_cols (lp, n);
  for (int i = 0; i < n; i++)
  {
    //-- Define type of the structural variables
    if (! freeLB[i] && ! freeUB[i]) {
      if ( lb[i] == ub[i] )
        glp_set_col_bnds (lp, i+1, GLP_FX, lb[i], ub[i]);
      else
        glp_set_col_bnds (lp, i+1, GLP_DB, lb[i], ub[i]);
    }
    else
	  {
      if (! freeLB[i] && freeUB[i])
        glp_set_col_bnds (lp, i+1, GLP_LO, lb[i], ub[i]);
      else
      {
        if (freeLB[i] && ! freeUB[i])
		      glp_set_col_bnds (lp, i+1, GLP_UP, lb[i], ub[i]);
	      else
		      glp_set_col_bnds (lp, i+1, GLP_FR, lb[i], ub[i]);
	    }
	  }

  // -- Set the objective coefficient of the corresponding
  // -- structural variable. No constant term is assumed.
  glp_set_obj_coef(lp,i+1,c[i]);

  if (isMIP)
    glp_set_col_kind (lp, i+1, vartype[i]);
  }

  glp_add_rows (lp, m);

  for (int i = 0; i < m; i++)
  {
    /*  If the i-th row has no lower bound (types F,U), the
        corrispondent parameter will be ignored.
        If the i-th row has no upper bound (types F,L), the corrispondent
        parameter will be ignored.
        If the i-th row is of S type, the i-th LB is used, but
        the i-th UB is ignored.
    */

    switch (ctype[i])
    {
      case 'F': typx = GLP_FR; break;
      // upper bound
	  case 'U': typx = GLP_UP; break;
      // lower bound
	  case 'L': typx = GLP_LO; break;
      // fixed constraint
	  case 'S': typx = GLP_FX; break;
      // double-bounded variable
      case 'D': typx = GLP_DB; break;
	}

    if ( typx == GLP_DB && -b[i] < b[i]) {
        glp_set_row_bnds (lp, i+1, typx, -b[i], b[i]);
    }
    else if(typx == GLP_DB && -b[i] == b[i]) {
        glp_set_row_bnds (lp, i+1, GLP_FX, b[i], b[i]);
    }
    else {
    // this should be glp_set_row_bnds (lp, i+1, typx, -b[i], b[i]);
        glp_set_row_bnds (lp, i+1, typx, b[i], b[i]);
    }

  }
  // Load constraint matrix A
  glp_load_matrix (lp, nz, rn, cn, a);

  // Save problem
  if (save_pb) {
    if (!strcmp(filetype,"cplex")){
      if (glp_write_lp (lp, NULL, save_filename) != 0) {
	        mexErrMsgTxt("glpk: unable to write the problem");
	        longjmp (mark, -1);
      }
    }else{
      if (!strcmp(filetype,"fixedmps")){
        if (glp_write_mps (lp, GLP_MPS_DECK, NULL, save_filename) != 0) {
            mexErrMsgTxt("glpk: unable to write the problem");
	        longjmp (mark, -1);
        }
      }else{
        if (!strcmp(filetype,"freemps")){
          if (glp_write_mps (lp, GLP_MPS_FILE, NULL, save_filename) != 0) {
              mexErrMsgTxt("glpk: unable to write the problem");
	          longjmp (mark, -1);
          }
        }else{// plain text
          if (lpx_print_prob (lp, save_filename) != 0) {
              mexErrMsgTxt("glpk: unable to write the problem");
	          longjmp (mark, -1);
          }
        }
      }
    }
  }
  //-- scale the problem data (if required)
  if (! glpIntParam[16] || lpsolver != 1) {
    switch ( glpIntParam[1] ) {
        case ( 0 ): glp_scale_prob( lp, GLP_SF_SKIP ); break;
        case ( 1 ): glp_scale_prob( lp, GLP_SF_GM ); break;
        case ( 2 ): glp_scale_prob( lp, GLP_SF_EQ ); break;
        case ( 3 ): glp_scale_prob( lp, GLP_SF_AUTO  ); break;
        case ( 4 ): glp_scale_prob( lp, GLP_SF_2N ); break;
        default :
            mexErrMsgTxt("glpk: unrecognized scaling option");
            longjmp (mark, -1);
    }
  }
  else {
    /* do nothing? or unscale?
        glp_unscale_prob( lp );
    */
  }

  //-- build advanced initial basis (if required)
  if (lpsolver == 1 && ! glpIntParam[16])
    glp_adv_basis (lp, 0);

  glp_smcp sParam;
  glp_init_smcp(&sParam);

  //-- set control parameters for simplex/exact method
  if (lpsolver == 1 || lpsolver == 3){
    //remap of control parameters for simplex method
    sParam.msg_lev=glpIntParam[0];	// message level

    // simplex method: primal/dual
    switch ( glpIntParam[2] ) {
        case 0: sParam.meth=GLP_PRIMAL; break;
        case 1: sParam.meth=GLP_DUAL;   break;
        case 2: sParam.meth=GLP_DUALP;  break;
        default:
            mexErrMsgTxt("glpk: unrecognized primal/dual method");
            longjmp (mark, -1);
    }

    // pricing technique
    if (glpIntParam[3]==0) sParam.pricing=GLP_PT_STD;
    else sParam.pricing=GLP_PT_PSE;

    // ratio test
    if (glpIntParam[20]==0) sParam.r_test = GLP_RT_STD;
    else sParam.r_test=GLP_RT_HAR;

    //tollerances
    sParam.tol_bnd=glpRealParam[1];	// primal feasible tollerance
    sParam.tol_dj=glpRealParam[2];	// dual feasible tollerance
    sParam.tol_piv=glpRealParam[3];	// pivot tollerance
    sParam.obj_ll=glpRealParam[4];	// lower limit
    sParam.obj_ul=glpRealParam[5];	// upper limit

    // iteration limit
    if (glpIntParam[5]==-1) sParam.it_lim=INT_MAX;
    else sParam.it_lim=glpIntParam[5];

    // time limit
    if (glpRealParam[6]==-1) sParam.tm_lim=INT_MAX;
    else sParam.tm_lim=(int) glpRealParam[6];
    sParam.out_frq=glpIntParam[7];	// output frequency
    sParam.out_dly=(int) glpRealParam[7];	// output delay
    // presolver
    if (glpIntParam[16]) sParam.presolve=GLP_ON;
    else sParam.presolve=GLP_OFF;
  }else{
	for(int i = 0; i < NIntP; i++) {
        // skip assinging ratio test or
        if ( i == 18 || i == 20) continue;
		lpx_set_int_parm (lp, IParam[i], glpIntParam[i]);
    }

	for (int i = 0; i < NRealP; i++) {
		lpx_set_real_parm (lp, RParam[i], glpRealParam[i]);
    }
  }

  //set MIP params if MIP....
  glp_iocp iParam;
  glp_init_iocp(&iParam);

  if ( isMIP ){
    method = 'I';

    switch (glpIntParam[0]) { //message level
         case 0:  iParam.msg_lev = GLP_MSG_OFF;   break;
         case 1:  iParam.msg_lev = GLP_MSG_ERR;   break;
         case 2:  iParam.msg_lev = GLP_MSG_ON;    break;
         case 3:  iParam.msg_lev = GLP_MSG_ALL;   break;
         default:  mexErrMsgTxt("glpk: msg_lev bad param");
    }
    switch (glpIntParam[14]) { //branching param
         case 0:  iParam.br_tech = GLP_BR_FFV;    break;
         case 1:  iParam.br_tech = GLP_BR_LFV;    break;
         case 2:  iParam.br_tech = GLP_BR_MFV;    break;
         case 3:  iParam.br_tech = GLP_BR_DTH;    break;
         default: mexErrMsgTxt("glpk: branch bad param");
    }
    switch (glpIntParam[15]) { //backtracking heuristic
        case 0:  iParam.bt_tech = GLP_BT_DFS;    break;
        case 1:  iParam.bt_tech = GLP_BT_BFS;    break;
        case 2:  iParam.bt_tech = GLP_BT_BLB;    break;
        case 3:  iParam.bt_tech = GLP_BT_BPH;    break;
        default: mexErrMsgTxt("glpk: backtrack bad param");
    }

    if (  glpRealParam[8] > 0.0 && glpRealParam[8] < 1.0 )
        iParam.tol_int = glpRealParam[8];  // absolute tolorence
    else
        mexErrMsgTxt("glpk: tolint must be between 0 and 1");

    iParam.tol_obj = glpRealParam[9];  // relative tolarence
    iParam.mip_gap = glpRealParam[10]; // realative gap tolerance

    // set time limit for mip
    if ( glpRealParam[6] < 0.0 || glpRealParam[6] > 1e6 )
       iParam.tm_lim = INT_MAX;
    else
       iParam.tm_lim = (int)(1000.0 * glpRealParam[6] );

    // Choose Cutsets for mip
    // shut all cuts off, then start over....
    iParam.gmi_cuts = GLP_OFF;
    iParam.mir_cuts = GLP_OFF;
    iParam.cov_cuts = GLP_OFF;
    iParam.clq_cuts = GLP_OFF;

    switch( glpIntParam[17] ) {
        case 0: break;
        case 1: iParam.gmi_cuts = GLP_ON; break;
        case 2: iParam.mir_cuts = GLP_ON; break;
        case 3: iParam.cov_cuts = GLP_ON; break;
        case 4: iParam.clq_cuts = GLP_ON; break;
        case 5: iParam.clq_cuts = GLP_ON;
                iParam.gmi_cuts = GLP_ON;
                iParam.mir_cuts = GLP_ON;
                iParam.cov_cuts = GLP_ON;
                iParam.clq_cuts = GLP_ON; break;
        default: mexErrMsgTxt("glpk: cutset bad param");
    }

    switch( glpIntParam[18] ) { // pre-processing for mip
        case 0: iParam.pp_tech = GLP_PP_NONE; break;
        case 1: iParam.pp_tech = GLP_PP_ROOT; break;
        case 2: iParam.pp_tech = GLP_PP_ALL;  break;
        default:  mexErrMsgTxt("glpk: pprocess bad param");
    }

    if (glpIntParam[16])  iParam.presolve=GLP_ON;
    else                  iParam.presolve=GLP_OFF;

    if (glpIntParam[19])  iParam.binarize = GLP_ON;
    else                  iParam.binarize = GLP_OFF;

  }
  else {
     /* Choose simplex method ('S')
     or interior point method ('T')
     or Exact method          ('E')
     to solve the problem  */
    switch (lpsolver) {
      case 1: method = 'S'; break;
      case 2: method = 'T'; break;
      case 3: method = 'E'; break;
      default:
            mexErrMsgTxt("glpk:  lpsolver != lpsolver");
            longjmp (mark, -1);
    }
  }

	// now run the problem...
	int errnum = 0;

	switch (method) {
	case 'I':
		errnum = glp_intopt( lp, &iParam );
		errnum += 200; //this is to avoid ambiguity in the return codes.
		break;

	case 'S':
		errnum = glp_simplex(lp, &sParam);
		errnum += 100; //this is to avoid ambiguity in the return codes.
		break;

	case 'T':
		errnum = glp_interior(lp, NULL );
		errnum += 300; //this is to avoid ambiguity in the return codes.
		break;

	case 'E':
		errnum = glp_exact(lp, &sParam);
		errnum += 100; //this is to avoid ambiguity in the return codes.
		break;

	default:  /*xassert (method != method); */
		mexErrMsgTxt("glpk: method != method");
		longjmp (mark, -1);
	}

    if (errnum==100 || errnum==200 || errnum==300 || errnum==106 || errnum==107 || errnum==108 || errnum==109 || errnum==209 || errnum==214 || errnum==308) {

    // Get status and object value
    if (isMIP) {
      *status = glp_mip_status (lp);
      *fmin = glp_mip_obj_val (lp);
    }
    else {

      if (lpsolver == 1 || lpsolver == 3) {
        *status = glp_get_status (lp);
        *fmin = glp_get_obj_val (lp);
	  }
      else {
        *status = glp_ipt_status (lp);
        *fmin = glp_ipt_obj_val (lp);
	  }
    }

    // Get optimal solution (if exists)
    if (isMIP) {

      for (int i = 0; i < n; i++)
        xmin[i] = glp_mip_col_val (lp, i+1);
    }
    else {

      /* Primal values */
      for (int i = 0; i < n; i++) {

        if (lpsolver == 1 || lpsolver == 3)
              xmin[i] = glp_get_col_prim (lp, i+1);
        else
		      xmin[i] = glp_ipt_col_prim (lp, i+1);
      }

      /* Dual values */
      for (int i = 0; i < m; i++) {

        if (lpsolver == 1 || lpsolver == 3)
            lambda[i] = glp_get_row_dual (lp, i+1);
	    else
            lambda[i] = glp_ipt_row_dual (lp, i+1);
      }

      /* Reduced costs */
      for (int i = 0; i < glp_get_num_cols (lp); i++) {

        if (lpsolver == 1 || lpsolver == 3)
            redcosts[i] = glp_get_col_dual (lp, i+1);
        else
            redcosts[i] = glp_ipt_col_dual (lp, i+1);
      }

    }

    *time = (clock () - t_start) / CLOCKS_PER_SEC;

    size_t tpeak;
    glp_mem_usage(NULL, NULL, NULL, &tpeak);
    *mem=((double) tpeak) / (1024);

	lpx_delete_prob(lp);

    return 0;
  }
  else {
   // printf("errnum is %d\n", errnum);
  }

  lpx_delete_prob(lp);

  /* this shouldn't be nessiary with glp_deleted_prob, but try it
  if we have weird behavior again... */
  glp_free_env();


  *status = errnum;

  return errnum;
}
예제 #7
0
bool glpk_wrapper::is_sat() {
    if (solver_type == SIMPLEX || solver_type == EXACT) {
        int status = glp_get_status(lp);
        if (status == GLP_UNDEF || changed) {
            glp_smcp parm;
            glp_init_smcp(&parm);
            parm.msg_lev = GLP_MSG_OFF;
            // always try first the normal simple (get close to an optimal solution in double precision)
            int solved = glp_simplex(lp, &parm);
            // TODO(dzufferey) should we always fall back on exact when the normal simplex failed ?
            if (solver_type == EXACT || solved != 0) {
                solved = glp_exact(lp, &parm);
            }
            if (solved != 0) {
                switch (solved) {
                    case GLP_EBADB:
                        throw std::runtime_error("GLPK simplex failed: GLP_EBADB");
                    case GLP_ESING:
                        throw std::runtime_error("GLPK simplex failed: GLP_ESING");
                    case GLP_ECOND:
                        throw std::runtime_error("GLPK simplex failed: GLP_ECOND");
                    case GLP_EBOUND:
                        throw std::runtime_error("GLPK simplex failed: GLP_EBOUND");
                    case GLP_EFAIL:
                        throw std::runtime_error("GLPK simplex failed: GLP_EFAIL");
                    case GLP_EOBJLL:
                        throw std::runtime_error("GLPK simplex failed: GLP_EOBJLL");
                    case GLP_EOBJUL:
                        throw std::runtime_error("GLPK simplex failed: GLP_EOBJUL");
                    case GLP_EITLIM:
                        throw std::runtime_error("GLPK simplex failed: GLP_EITLIM");
                    case GLP_ETMLIM:
                        throw std::runtime_error("GLPK simplex failed: GLP_ETMLIM");
                    case GLP_ENOPFS:
                        throw std::runtime_error("GLPK simplex failed: GLP_ENOPFS");
                    case GLP_ENODFS:
                        throw std::runtime_error("GLPK simplex failed: GLP_ENODFS");
                    default:
                        throw std::runtime_error("GLPK simplex failed");
                }
            }
            status = glp_get_status(lp);
            changed = false;
        }
        return (status == GLP_OPT || status == GLP_FEAS || status == GLP_UNBND);
    } else {
        assert(solver_type == INTERIOR);
        int status = glp_ipt_status(lp);
        if (status == GLP_UNDEF || changed) {
            glp_iptcp parm;
            glp_init_iptcp(&parm);
            parm.msg_lev = GLP_MSG_OFF;
            int solved = glp_interior(lp, &parm);
            if (solved != 0) {
                switch (solved) {
                    case GLP_EFAIL:
                        throw std::runtime_error("GLPK interior-point failed: GLP_EFAIL");
                    case GLP_ENOCVG:
                        throw std::runtime_error("GLPK interior-point failed: GLP_ENOCVG");
                    case GLP_EOBJUL:
                        throw std::runtime_error("GLPK interior-point failed: GLP_EOBJUL");
                    case GLP_EITLIM:
                        throw std::runtime_error("GLPK interior-point failed: GLP_EITLIM");
                    case GLP_EINSTAB:
                        throw std::runtime_error("GLPK interior-point failed: GLP_EINSTAB");
                    default:
                        throw std::runtime_error("GLPK interior-point failed");
                }
            }
            status = glp_ipt_status(lp);
            changed = false;
        }
        return status == GLP_OPT;
    }
}