コード例 #1
0
static void parse_integer(struct dsa *dsa)
{     int j, binary;
      /* parse the keyword 'general', 'integer', or 'binary' */
      if (dsa->token == T_GENERAL)
         binary = 0, scan_token(dsa);
      else if (dsa->token == T_INTEGER)
         binary = 0, scan_token(dsa);
      else if (dsa->token == T_BINARY)
         binary = 1, scan_token(dsa);
      else
         xassert(dsa != dsa);
      /* parse list of variables (may be empty) */
      while (dsa->token == T_NAME)
      {  /* find the corresponding column */
         j = find_col(dsa, dsa->image);
         /* change kind of the variable */
#if 0
         lpx_set_class(dsa->lp, LPX_MIP);
#endif
         lpx_set_col_kind(dsa->lp, j, LPX_IV);
         /* set 0-1 bounds for the binary variable */
         if (binary)
         {  set_lower_bound(dsa, j, 0.0);
            set_upper_bound(dsa, j, 1.0);
         }
         scan_token(dsa);
      }
      return;
}
コード例 #2
0
ファイル: GLPKapi.cpp プロジェクト: ModelSEED/Model-SEED-core
int GLPKLoadVariables(MFAVariable* InVariable, bool RelaxIntegerVariables,bool UseTightBounds) {
	if (GLPKModel == NULL) {
		FErrorFile() << "Could not add variable because GLPK object does not exist." << endl;
		FlushErrorFile();
		return FAIL;
	}

	int NumColumns = lpx_get_num_cols(GLPKModel);

	if (InVariable->Index >= NumColumns) {
		lpx_add_cols(GLPKModel, 1);
		string Name = GetMFAVariableName(InVariable);
		char* Temp = new char[Name.length()+1];
		strcpy(Temp,Name.data());
		lpx_set_col_name(GLPKModel,InVariable->Index+1,Temp);
	}


	double LowerBound = InVariable->LowerBound;
	double UpperBound = InVariable->UpperBound;
	if (UseTightBounds) {
		LowerBound = InVariable->Min;
		UpperBound = InVariable->Max;
	}
	if (LowerBound != UpperBound) {
		lpx_set_col_bnds(GLPKModel, InVariable->Index+1, LPX_DB, InVariable->LowerBound, InVariable->UpperBound);
	} else {
		lpx_set_col_bnds(GLPKModel, InVariable->Index+1, LPX_FX, InVariable->LowerBound, InVariable->UpperBound);
	}

	if (InVariable->Binary && !RelaxIntegerVariables) {
		lpx_set_class(GLPKModel, LPX_MIP);
		lpx_set_col_kind(GLPKModel, InVariable->Index+1,LPX_IV);
	}

	return SUCCESS;
}
コード例 #3
0
ファイル: glplpx8d.c プロジェクト: ChrisMoreton/Test3
LPX *lpx_extract_prob(void *_mpl)
{     MPL *mpl = _mpl;
      LPX *lp;
      int m, n, i, j, t, kind, type, len, *ind;
      double lb, ub, *val;
      /* create problem instance */
      lp = lpx_create_prob();
      /* set problem name */
      lpx_set_prob_name(lp, mpl_get_prob_name(mpl));
      /* build rows (constraints) */
      m = mpl_get_num_rows(mpl);
      if (m > 0) lpx_add_rows(lp, m);
      for (i = 1; i <= m; i++)
      {  /* set row name */
         lpx_set_row_name(lp, i, mpl_get_row_name(mpl, i));
         /* set row bounds */
         type = mpl_get_row_bnds(mpl, i, &lb, &ub);
         switch (type)
         {  case MPL_FR: type = LPX_FR; break;
            case MPL_LO: type = LPX_LO; break;
            case MPL_UP: type = LPX_UP; break;
            case MPL_DB: type = LPX_DB; break;
            case MPL_FX: type = LPX_FX; break;
            default: insist(type != type);
         }
         if (type == LPX_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb)))
         {  type = LPX_FX;
            if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub;
         }
         lpx_set_row_bnds(lp, i, type, lb, ub);
         /* warn about non-zero constant term */
         if (mpl_get_row_c0(mpl, i) != 0.0)
            print("lpx_read_model: row %s; constant term %.12g ignored",
               mpl_get_row_name(mpl, i), mpl_get_row_c0(mpl, i));
      }
      /* build columns (variables) */
      n = mpl_get_num_cols(mpl);
      if (n > 0) lpx_add_cols(lp, n);
      for (j = 1; j <= n; j++)
      {  /* set column name */
         lpx_set_col_name(lp, j, mpl_get_col_name(mpl, j));
         /* set column kind */
         kind = mpl_get_col_kind(mpl, j);
         switch (kind)
         {  case MPL_NUM:
               break;
            case MPL_INT:
            case MPL_BIN:
               lpx_set_class(lp, LPX_MIP);
               lpx_set_col_kind(lp, j, LPX_IV);
               break;
            default:
               insist(kind != kind);
         }
         /* set column bounds */
         type = mpl_get_col_bnds(mpl, j, &lb, &ub);
         switch (type)
         {  case MPL_FR: type = LPX_FR; break;
            case MPL_LO: type = LPX_LO; break;
            case MPL_UP: type = LPX_UP; break;
            case MPL_DB: type = LPX_DB; break;
            case MPL_FX: type = LPX_FX; break;
            default: insist(type != type);
         }
         if (kind == MPL_BIN)
         {  if (type == LPX_FR || type == LPX_UP || lb < 0.0) lb = 0.0;
            if (type == LPX_FR || type == LPX_LO || ub > 1.0) ub = 1.0;
            type = LPX_DB;
         }
         if (type == LPX_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb)))
         {  type = LPX_FX;
            if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub;
         }
         lpx_set_col_bnds(lp, j, type, lb, ub);
      }
      /* load the constraint matrix */
      ind = ucalloc(1+n, sizeof(int));
      val = ucalloc(1+n, sizeof(double));
      for (i = 1; i <= m; i++)
      {  len = mpl_get_mat_row(mpl, i, ind, val);
         lpx_set_mat_row(lp, i, len, ind, val);
      }
      /* build objective function (the first objective is used) */
      for (i = 1; i <= m; i++)
      {  kind = mpl_get_row_kind(mpl, i);
         if (kind == MPL_MIN || kind == MPL_MAX)
         {  /* set objective name */
            lpx_set_obj_name(lp, mpl_get_row_name(mpl, i));
            /* set optimization direction */
            lpx_set_obj_dir(lp, kind == MPL_MIN ? LPX_MIN : LPX_MAX);
            /* set constant term */
            lpx_set_obj_coef(lp, 0, mpl_get_row_c0(mpl, i));
            /* set objective coefficients */
            len = mpl_get_mat_row(mpl, i, ind, val);
            for (t = 1; t <= len; t++)
               lpx_set_obj_coef(lp, ind[t], val[t]);
            break;
         }
      }
      /* free working arrays */
      ufree(ind);
      ufree(val);
      /* bring the problem object to the calling program */
      return lp;
}
コード例 #4
0
ファイル: glpipp01.c プロジェクト: BohanHsu/developer
LPX *ipp_build_prob(IPP *ipp)
{     LPX *prob;
      IPPROW *row;
      IPPCOL *col;
      IPPAIJ *aij;
      int i, j, type, len, *ind;
      double *val;
      /* create problem object */
      prob = lpx_create_prob();
#if 0
      lpx_set_class(prob, LPX_MIP);
#endif
      /* the resultant problem should have the same optimization sense
         as the original problem */
      lpx_set_obj_dir(prob, ipp->orig_dir);
      /* set the constant term of the objective function */
      lpx_set_obj_coef(prob, 0,
         ipp->orig_dir == LPX_MIN ? + ipp->c0 : - ipp->c0);
      /* copy rows of the resultant problem */
      for (row = ipp->row_ptr; row != NULL; row = row->next)
      {  i = lpx_add_rows(prob, 1);
         if (row->lb == -DBL_MAX && row->ub == +DBL_MAX)
            type = LPX_FR;
         else if (row->ub == +DBL_MAX)
            type = LPX_LO;
         else if (row->lb == -DBL_MAX)
            type = LPX_UP;
         else if (row->lb != row->ub)
            type = LPX_DB;
         else
            type = LPX_FX;
         lpx_set_row_bnds(prob, i, type, row->lb, row->ub);
         row->temp = i;
      }
      /* copy columns of the resultant problem */
      ind = xcalloc(1+lpx_get_num_rows(prob), sizeof(int));
      val = xcalloc(1+lpx_get_num_rows(prob), sizeof(double));
      for (col = ipp->col_ptr; col != NULL; col = col->next)
      {  j = lpx_add_cols(prob, 1);
         if (col->i_flag) lpx_set_col_kind(prob, j, LPX_IV);
         if (col->lb == -DBL_MAX && col->ub == +DBL_MAX)
            type = LPX_FR;
         else if (col->ub == +DBL_MAX)
            type = LPX_LO;
         else if (col->lb == -DBL_MAX)
            type = LPX_UP;
         else if (col->lb != col->ub)
            type = LPX_DB;
         else
            type = LPX_FX;
         lpx_set_col_bnds(prob, j, type, col->lb, col->ub);
         lpx_set_obj_coef(prob, j,
            ipp->orig_dir == LPX_MIN ? + col->c : - col->c);
         /* copy constraint coefficients */
         len = 0;
         for (aij = col->ptr; aij != NULL; aij = aij->c_next)
         {  len++;
            ind[len] = aij->row->temp;
            val[len] = aij->val;
         }
         lpx_set_mat_col(prob, j, len, ind, val);
      }
      xfree(ind);
      xfree(val);
      return prob;
}