コード例 #1
0
ファイル: glplpx01.c プロジェクト: BohanHsu/developer
double lpx_get_row_ub(glp_prob *lp, int i)
{     /* retrieve row upper bound */
      double ub;
      ub = glp_get_row_ub(lp, i);
      if (ub == +DBL_MAX) ub = 0.0;
      return ub;
}
コード例 #2
0
ファイル: glpk_wrapper.cpp プロジェクト: jadecastro/dreal3
double glpk_wrapper::get_row_value(int i) {
    double cstr_value = 0;
    if (solver_type ==  SIMPLEX || solver_type == EXACT) {
        int cstr_status = glp_get_row_stat(lp, i);
        if (cstr_status == GLP_BS) {  // basic variable;
            cstr_status = glp_get_row_prim(lp, i);  // or glp_get_row_dual
        } else if (cstr_status == GLP_NL || cstr_status == GLP_NS) {  // non-basic variable on its lower bound, non-basic fixed variable.
            cstr_value = glp_get_row_lb(lp, i);
        } else if (cstr_status == GLP_NU) {  //  non-basic variable on its upper bound
            cstr_value = glp_get_row_ub(lp, i);
        }
        // TODO(dzufferey): should we do something for GLP_NF — non-basic free (unbounded) variable;
    } else {
        cstr_value = glp_ipt_row_prim(lp, i);  // or glp_ipt_row_dual
    }
    return cstr_value;
}
コード例 #3
0
int glp_print_ranges(glp_prob *P, int len, const int list[],
                     int flags, const char *fname)
{   /* print sensitivity analysis report */
    glp_file *fp = NULL;
    GLPROW *row;
    GLPCOL *col;
    int m, n, pass, k, t, numb, type, stat, var1, var2, count, page,
        ret;
    double lb, ub, slack, coef, prim, dual, value1, value2, coef1,
           coef2, obj1, obj2;
    const char *name, *limit;
    char buf[13+1];
    /* sanity checks */
    if (P == NULL || P->magic != GLP_PROB_MAGIC)
        xerror("glp_print_ranges: P = %p; invalid problem object\n",
               P);
    m = P->m, n = P->n;
    if (len < 0)
        xerror("glp_print_ranges: len = %d; invalid list length\n",
               len);
    if (len > 0)
    {   if (list == NULL)
            xerror("glp_print_ranges: list = %p: invalid parameter\n",
                   list);
        for (t = 1; t <= len; t++)
        {   k = list[t];
            if (!(1 <= k && k <= m+n))
                xerror("glp_print_ranges: list[%d] = %d; row/column numb"
                       "er out of range\n", t, k);
        }
    }
    if (flags != 0)
        xerror("glp_print_ranges: flags = %d; invalid parameter\n",
               flags);
    if (fname == NULL)
        xerror("glp_print_ranges: fname = %p; invalid parameter\n",
               fname);
    if (glp_get_status(P) != GLP_OPT)
    {   xprintf("glp_print_ranges: optimal basic solution required\n");
        ret = 1;
        goto done;
    }
    if (!glp_bf_exists(P))
    {   xprintf("glp_print_ranges: basis factorization required\n");
        ret = 2;
        goto done;
    }
    /* start reporting */
    xprintf("Write sensitivity analysis report to '%s'...\n", fname);
    fp = glp_open(fname, "w");
    if (fp == NULL)
    {   xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
        ret = 3;
        goto done;
    }
    page = count = 0;
    for (pass = 1; pass <= 2; pass++)
        for (t = 1; t <= (len == 0 ? m+n : len); t++)
        {   if (t == 1) count = 0;
            k = (len == 0 ? t : list[t]);
            if (pass == 1 && k > m || pass == 2 && k <= m)
                continue;
            if (count == 0)
            {   xfprintf(fp, "GLPK %-4s - SENSITIVITY ANALYSIS REPORT%73sPa"
                         "ge%4d\n", glp_version(), "", ++page);
                xfprintf(fp, "\n");
                xfprintf(fp, "%-12s%s\n", "Problem:",
                         P->name == NULL ? "" : P->name);
                xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
                         P->obj == NULL ? "" : P->obj,
                         P->obj == NULL ? "" : " = ", P->obj_val,
                         P->dir == GLP_MIN ? "MINimum" :
                         P->dir == GLP_MAX ? "MAXimum" : "???");
                xfprintf(fp, "\n");
                xfprintf(fp, "%6s %-12s %2s %13s %13s %13s  %13s %13s %13s "
                         "%s\n", "No.", pass == 1 ? "Row name" : "Column name",
                         "St", "Activity", pass == 1 ? "Slack" : "Obj coef",
                         "Lower bound", "Activity", "Obj coef", "Obj value at",
                         "Limiting");
                xfprintf(fp, "%6s %-12s %2s %13s %13s %13s  %13s %13s %13s "
                         "%s\n", "", "", "", "", "Marginal", "Upper bound",
                         "range", "range", "break point", "variable");
                xfprintf(fp, "------ ------------ -- ------------- --------"
                         "----- -------------  ------------- ------------- ------"
                         "------- ------------\n");
            }
            if (pass == 1)
            {   numb = k;
                xassert(1 <= numb && numb <= m);
                row = P->row[numb];
                name = row->name;
                type = row->type;
                lb = glp_get_row_lb(P, numb);
                ub = glp_get_row_ub(P, numb);
                coef = 0.0;
                stat = row->stat;
                prim = row->prim;
                if (type == GLP_FR)
                    slack = - prim;
                else if (type == GLP_LO)
                    slack = lb - prim;
                else if (type == GLP_UP || type == GLP_DB || type == GLP_FX)
                    slack = ub - prim;
                dual = row->dual;
            }
            else
            {   numb = k - m;
                xassert(1 <= numb && numb <= n);
                col = P->col[numb];
                name = col->name;
                lb = glp_get_col_lb(P, numb);
                ub = glp_get_col_ub(P, numb);
                coef = col->coef;
                stat = col->stat;
                prim = col->prim;
                slack = 0.0;
                dual = col->dual;
            }
            if (stat != GLP_BS)
            {   glp_analyze_bound(P, k, &value1, &var1, &value2, &var2);
                if (stat == GLP_NF)
                    coef1 = coef2 = coef;
                else if (stat == GLP_NS)
                    coef1 = -DBL_MAX, coef2 = +DBL_MAX;
                else if (stat == GLP_NL && P->dir == GLP_MIN ||
                         stat == GLP_NU && P->dir == GLP_MAX)
                    coef1 = coef - dual, coef2 = +DBL_MAX;
                else
                    coef1 = -DBL_MAX, coef2 = coef - dual;
                if (value1 == -DBL_MAX)
                {   if (dual < -1e-9)
                        obj1 = +DBL_MAX;
                    else if (dual > +1e-9)
                        obj1 = -DBL_MAX;
                    else
                        obj1 = P->obj_val;
                }
                else
                    obj1 = P->obj_val + dual * (value1 - prim);
                if (value2 == +DBL_MAX)
                {   if (dual < -1e-9)
                        obj2 = -DBL_MAX;
                    else if (dual > +1e-9)
                        obj2 = +DBL_MAX;
                    else
                        obj2 = P->obj_val;
                }
                else
                    obj2 = P->obj_val + dual * (value2 - prim);
            }
            else
            {   glp_analyze_coef(P, k, &coef1, &var1, &value1, &coef2,
                                 &var2, &value2);
                if (coef1 == -DBL_MAX)
                {   if (prim < -1e-9)
                        obj1 = +DBL_MAX;
                    else if (prim > +1e-9)
                        obj1 = -DBL_MAX;
                    else
                        obj1 = P->obj_val;
                }
                else
                    obj1 = P->obj_val + (coef1 - coef) * prim;
                if (coef2 == +DBL_MAX)
                {   if (prim < -1e-9)
                        obj2 = -DBL_MAX;
                    else if (prim > +1e-9)
                        obj2 = +DBL_MAX;
                    else
                        obj2 = P->obj_val;
                }
                else
                    obj2 = P->obj_val + (coef2 - coef) * prim;
            }
            /*** first line ***/
            /* row/column number */
            xfprintf(fp, "%6d", numb);
            /* row/column name */
            xfprintf(fp, " %-12.12s", name == NULL ? "" : name);
            if (name != NULL && strlen(name) > 12)
                xfprintf(fp, "%s\n%6s %12s", name+12, "", "");
            /* row/column status */
            xfprintf(fp, " %2s",
                     stat == GLP_BS ? "BS" : stat == GLP_NL ? "NL" :
                     stat == GLP_NU ? "NU" : stat == GLP_NF ? "NF" :
                     stat == GLP_NS ? "NS" : "??");
            /* row/column activity */
            xfprintf(fp, " %s", format(buf, prim));
            /* row slack, column objective coefficient */
            xfprintf(fp, " %s", format(buf, k <= m ? slack : coef));
            /* row/column lower bound */
            xfprintf(fp, " %s", format(buf, lb));
            /* row/column activity range */
            xfprintf(fp, "  %s", format(buf, value1));
            /* row/column objective coefficient range */
            xfprintf(fp, " %s", format(buf, coef1));
            /* objective value at break point */
            xfprintf(fp, " %s", format(buf, obj1));
            /* limiting variable name */
            if (var1 != 0)
            {   if (var1 <= m)
                    limit = glp_get_row_name(P, var1);
                else
                    limit = glp_get_col_name(P, var1 - m);
                if (limit != NULL)
                    xfprintf(fp, " %s", limit);
            }
            xfprintf(fp, "\n");
            /*** second line ***/
            xfprintf(fp, "%6s %-12s %2s %13s", "", "", "", "");
            /* row/column reduced cost */
            xfprintf(fp, " %s", format(buf, dual));
            /* row/column upper bound */
            xfprintf(fp, " %s", format(buf, ub));
            /* row/column activity range */
            xfprintf(fp, "  %s", format(buf, value2));
            /* row/column objective coefficient range */
            xfprintf(fp, " %s", format(buf, coef2));
            /* objective value at break point */
            xfprintf(fp, " %s", format(buf, obj2));
            /* limiting variable name */
            if (var2 != 0)
            {   if (var2 <= m)
                    limit = glp_get_row_name(P, var2);
                else
                    limit = glp_get_col_name(P, var2 - m);
                if (limit != NULL)
                    xfprintf(fp, " %s", limit);
            }
            xfprintf(fp, "\n");
            xfprintf(fp, "\n");
            /* print 10 items per page */
            count = (count + 1) % 10;
        }
    xfprintf(fp, "End of report\n");
#if 0 /* FIXME */
    xfflush(fp);
#endif
    if (glp_ioerr(fp))
    {   xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
        ret = 4;
        goto done;
    }
    ret = 0;
done:
    if (fp != NULL) glp_close(fp);
    return ret;
}
コード例 #4
0
ファイル: ppl_lpsol.c プロジェクト: hnxiao/ppl
static void
solve(char* file_name) {
  ppl_Constraint_System_t ppl_cs;
#ifndef NDEBUG
  ppl_Constraint_System_t ppl_cs_copy;
#endif
  ppl_Generator_t optimum_location;
  ppl_Linear_Expression_t ppl_le;
  int dimension, row, num_rows, column, nz, i, j, type;
  int* coefficient_index;
  double lb, ub;
  double* coefficient_value;
  mpq_t rational_lb, rational_ub;
  mpq_t* rational_coefficient;
  mpq_t* objective;
  ppl_Linear_Expression_t ppl_objective_le;
  ppl_Coefficient_t optimum_n;
  ppl_Coefficient_t optimum_d;
  mpq_t optimum;
  mpz_t den_lcm;
  int optimum_found;
  glp_mpscp glpk_mpscp;

  glpk_lp = glp_create_prob();
  glp_init_mpscp(&glpk_mpscp);

  if (verbosity == 0) {
    /* FIXME: find a way to suppress output from glp_read_mps. */
  }

#ifdef PPL_LPSOL_SUPPORTS_TIMINGS

  if (print_timings)
    start_clock();

#endif /* defined(PPL_LPSOL_SUPPORTS_TIMINGS) */

  if (glp_read_mps(glpk_lp, GLP_MPS_FILE, &glpk_mpscp, file_name) != 0)
    fatal("cannot read MPS file `%s'", file_name);

#ifdef PPL_LPSOL_SUPPORTS_TIMINGS

  if (print_timings) {
    fprintf(stderr, "Time to read the input file: ");
    print_clock(stderr);
    fprintf(stderr, " s\n");
    start_clock();
  }

#endif /* defined(PPL_LPSOL_SUPPORTS_TIMINGS) */

  glpk_lp_num_int = glp_get_num_int(glpk_lp);

  if (glpk_lp_num_int > 0 && !no_mip && !use_simplex)
     fatal("the enumeration solving method can not handle MIP problems");

  dimension = glp_get_num_cols(glpk_lp);

  /* Read variables constrained to be integer. */
  if (glpk_lp_num_int > 0 && !no_mip && use_simplex) {
    if (verbosity >= 4)
      fprintf(output_file, "Integer variables:\n");
    integer_variables = (ppl_dimension_type*)
      malloc((glpk_lp_num_int + 1)*sizeof(ppl_dimension_type));
    for (i = 0, j = 0; i < dimension; ++i) {
      int col_kind = glp_get_col_kind(glpk_lp, i+1);
      if (col_kind == GLP_IV || col_kind == GLP_BV) {
        integer_variables[j] = i;
        if (verbosity >= 4) {
          ppl_io_fprint_variable(output_file, i);
          fprintf(output_file, " ");
        }
        ++j;
      }
    }
  }
  coefficient_index = (int*) malloc((dimension+1)*sizeof(int));
  coefficient_value = (double*) malloc((dimension+1)*sizeof(double));
  rational_coefficient = (mpq_t*) malloc((dimension+1)*sizeof(mpq_t));


  ppl_new_Constraint_System(&ppl_cs);

  mpq_init(rational_lb);
  mpq_init(rational_ub);
  for (i = 1; i <= dimension; ++i)
    mpq_init(rational_coefficient[i]);

  mpz_init(den_lcm);

  if (verbosity >= 4)
    fprintf(output_file, "\nConstraints:\n");

  /* Set up the row (ordinary) constraints. */
  num_rows = glp_get_num_rows(glpk_lp);
  for (row = 1; row <= num_rows; ++row) {
    /* Initialize the least common multiple computation. */
    mpz_set_si(den_lcm, 1);
    /* Set `nz' to the number of non-zero coefficients. */
    nz = glp_get_mat_row(glpk_lp, row, coefficient_index, coefficient_value);
    for (i = 1; i <= nz; ++i) {
      set_mpq_t_from_double(rational_coefficient[i], coefficient_value[i]);
      /* Update den_lcm. */
      mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_coefficient[i]));
    }

    lb = glp_get_row_lb(glpk_lp, row);
    ub = glp_get_row_ub(glpk_lp, row);

    set_mpq_t_from_double(rational_lb, lb);
    set_mpq_t_from_double(rational_ub, ub);

    mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_lb));
    mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_ub));

    ppl_new_Linear_Expression_with_dimension(&ppl_le, dimension);

    for (i = 1; i <= nz; ++i) {
      mpz_mul(tmp_z, den_lcm, mpq_numref(rational_coefficient[i]));
      mpz_divexact(tmp_z, tmp_z, mpq_denref(rational_coefficient[i]));
      ppl_assign_Coefficient_from_mpz_t(ppl_coeff, tmp_z);
      ppl_Linear_Expression_add_to_coefficient(ppl_le, coefficient_index[i]-1,
                                               ppl_coeff);
    }

    type = glp_get_row_type(glpk_lp, row);
    add_constraints(ppl_le, type, rational_lb, rational_ub, den_lcm, ppl_cs);

    ppl_delete_Linear_Expression(ppl_le);
  }

  free(coefficient_value);
  for (i = 1; i <= dimension; ++i)
    mpq_clear(rational_coefficient[i]);
  free(rational_coefficient);
  free(coefficient_index);

#ifndef NDEBUG
  ppl_new_Constraint_System_from_Constraint_System(&ppl_cs_copy, ppl_cs);
#endif

  /*
    FIXME: here we could build the polyhedron and minimize it before
    adding the variable bounds.
  */

  /* Set up the columns constraints, i.e., variable bounds. */
  for (column = 1; column <= dimension; ++column) {

    lb = glp_get_col_lb(glpk_lp, column);
    ub = glp_get_col_ub(glpk_lp, column);

    set_mpq_t_from_double(rational_lb, lb);
    set_mpq_t_from_double(rational_ub, ub);

    /* Initialize the least common multiple computation. */
    mpz_set_si(den_lcm, 1);
    mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_lb));
    mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_ub));

    ppl_new_Linear_Expression_with_dimension(&ppl_le, dimension);
    ppl_assign_Coefficient_from_mpz_t(ppl_coeff, den_lcm);
    ppl_Linear_Expression_add_to_coefficient(ppl_le, column-1, ppl_coeff);

    type = glp_get_col_type(glpk_lp, column);
    add_constraints(ppl_le, type, rational_lb, rational_ub, den_lcm, ppl_cs);

    ppl_delete_Linear_Expression(ppl_le);
  }

  mpq_clear(rational_ub);
  mpq_clear(rational_lb);

  /* Deal with the objective function. */
  objective = (mpq_t*) malloc((dimension+1)*sizeof(mpq_t));

  /* Initialize the least common multiple computation. */
  mpz_set_si(den_lcm, 1);

  mpq_init(objective[0]);
  set_mpq_t_from_double(objective[0], glp_get_obj_coef(glpk_lp, 0));
  for (i = 1; i <= dimension; ++i) {
    mpq_init(objective[i]);
    set_mpq_t_from_double(objective[i], glp_get_obj_coef(glpk_lp, i));
    /* Update den_lcm. */
    mpz_lcm(den_lcm, den_lcm, mpq_denref(objective[i]));
  }

  /* Set the ppl_objective_le to be the objective function. */
  ppl_new_Linear_Expression_with_dimension(&ppl_objective_le, dimension);
  /* Set value for objective function's inhomogeneous term. */
  mpz_mul(tmp_z, den_lcm, mpq_numref(objective[0]));
  mpz_divexact(tmp_z, tmp_z, mpq_denref(objective[0]));
  ppl_assign_Coefficient_from_mpz_t(ppl_coeff, tmp_z);
  ppl_Linear_Expression_add_to_inhomogeneous(ppl_objective_le, ppl_coeff);
  /* Set values for objective function's variable coefficients. */
  for (i = 1; i <= dimension; ++i) {
    mpz_mul(tmp_z, den_lcm, mpq_numref(objective[i]));
    mpz_divexact(tmp_z, tmp_z, mpq_denref(objective[i]));
    ppl_assign_Coefficient_from_mpz_t(ppl_coeff, tmp_z);
    ppl_Linear_Expression_add_to_coefficient(ppl_objective_le, i-1, ppl_coeff);
  }

  if (verbosity >= 4) {
    fprintf(output_file, "Objective function:\n");
    if (mpz_cmp_si(den_lcm, 1) != 0)
      fprintf(output_file, "(");
    ppl_io_fprint_Linear_Expression(output_file, ppl_objective_le);
  }

  for (i = 0; i <= dimension; ++i)
    mpq_clear(objective[i]);
  free(objective);

  if (verbosity >= 4) {
    if (mpz_cmp_si(den_lcm, 1) != 0) {
      fprintf(output_file, ")/");
      mpz_out_str(output_file, 10, den_lcm);
    }
    fprintf(output_file, "\n%s\n",
            (maximize ? "Maximizing." : "Minimizing."));
  }

  ppl_new_Coefficient(&optimum_n);
  ppl_new_Coefficient(&optimum_d);
  ppl_new_Generator_zero_dim_point(&optimum_location);

  optimum_found = use_simplex
    ? solve_with_simplex(ppl_cs,
                         ppl_objective_le,
                         optimum_n,
                         optimum_d,
                         optimum_location)
    : solve_with_generators(ppl_cs,
                            ppl_objective_le,
                            optimum_n,
                            optimum_d,
                            optimum_location);

  ppl_delete_Linear_Expression(ppl_objective_le);

  if (glpk_lp_num_int > 0)
      free(integer_variables);

  if (optimum_found) {
    mpq_init(optimum);
    ppl_Coefficient_to_mpz_t(optimum_n, tmp_z);
    mpq_set_num(optimum, tmp_z);
    ppl_Coefficient_to_mpz_t(optimum_d, tmp_z);
    mpz_mul(tmp_z, tmp_z, den_lcm);
    mpq_set_den(optimum, tmp_z);
    if (verbosity == 1)
      fprintf(output_file, "Optimized problem.\n");
    if (verbosity >= 2)
      fprintf(output_file, "Optimum value: %.10g\n", mpq_get_d(optimum));
    if (verbosity >= 3) {
      fprintf(output_file, "Optimum location:\n");
      ppl_Generator_divisor(optimum_location, ppl_coeff);
      ppl_Coefficient_to_mpz_t(ppl_coeff, tmp_z);
      for (i = 0; i < dimension; ++i) {
        mpz_set(mpq_denref(tmp1_q), tmp_z);
        ppl_Generator_coefficient(optimum_location, i, ppl_coeff);
        ppl_Coefficient_to_mpz_t(ppl_coeff, mpq_numref(tmp1_q));
        ppl_io_fprint_variable(output_file, i);
        fprintf(output_file, " = %.10g\n", mpq_get_d(tmp1_q));
      }
    }
#ifndef NDEBUG
    {
      ppl_Polyhedron_t ph;
      unsigned int relation;
      ppl_new_C_Polyhedron_recycle_Constraint_System(&ph, ppl_cs_copy);
      ppl_delete_Constraint_System(ppl_cs_copy);
      relation = ppl_Polyhedron_relation_with_Generator(ph, optimum_location);
      ppl_delete_Polyhedron(ph);
      assert(relation == PPL_POLY_GEN_RELATION_SUBSUMES);
    }
#endif
    maybe_check_results(PPL_MIP_PROBLEM_STATUS_OPTIMIZED,
                        mpq_get_d(optimum));
    mpq_clear(optimum);
  }

  ppl_delete_Constraint_System(ppl_cs);
  ppl_delete_Coefficient(optimum_d);
  ppl_delete_Coefficient(optimum_n);
  ppl_delete_Generator(optimum_location);

  glp_delete_prob(glpk_lp);
}
コード例 #5
0
ファイル: glpios02.c プロジェクト: Shicheng-Guo/methylFlow
int ios_preprocess_node(glp_tree *tree, int max_pass)
{     glp_prob *mip = tree->mip;
      int m = mip->m;
      int n = mip->n;
      int i, j, nrs, *num, ret = 0;
      double *L, *U, *l, *u;
      /* the current subproblem must exist */
      xassert(tree->curr != NULL);
      /* determine original row bounds */
      L = xcalloc(1+m, sizeof(double));
      U = xcalloc(1+m, sizeof(double));
      switch (mip->mip_stat)
      {  case GLP_UNDEF:
            L[0] = -DBL_MAX, U[0] = +DBL_MAX;
            break;
         case GLP_FEAS:
            switch (mip->dir)
            {  case GLP_MIN:
                  L[0] = -DBL_MAX, U[0] = mip->mip_obj - mip->c0;
                  break;
               case GLP_MAX:
                  L[0] = mip->mip_obj - mip->c0, U[0] = +DBL_MAX;
                  break;
               default:
                  xassert(mip != mip);
            }
            break;
         default:
            xassert(mip != mip);
      }
      for (i = 1; i <= m; i++)
      {  L[i] = glp_get_row_lb(mip, i);
         U[i] = glp_get_row_ub(mip, i);
      }
      /* determine original column bounds */
      l = xcalloc(1+n, sizeof(double));
      u = xcalloc(1+n, sizeof(double));
      for (j = 1; j <= n; j++)
      {  l[j] = glp_get_col_lb(mip, j);
         u[j] = glp_get_col_ub(mip, j);
      }
      /* build the initial list of rows to be analyzed */
      nrs = m + 1;
      num = xcalloc(1+nrs, sizeof(int));
      for (i = 1; i <= nrs; i++) num[i] = i - 1;
      /* perform basic preprocessing */
      if (basic_preprocessing(mip , L, U, l, u, nrs, num, max_pass))
      {  ret = 1;
         goto done;
      }
      /* set new actual (relaxed) row bounds */
      for (i = 1; i <= m; i++)
      {  /* consider only non-active rows to keep dual feasibility */
         if (glp_get_row_stat(mip, i) == GLP_BS)
         {  if (L[i] == -DBL_MAX && U[i] == +DBL_MAX)
               glp_set_row_bnds(mip, i, GLP_FR, 0.0, 0.0);
            else if (U[i] == +DBL_MAX)
               glp_set_row_bnds(mip, i, GLP_LO, L[i], 0.0);
            else if (L[i] == -DBL_MAX)
               glp_set_row_bnds(mip, i, GLP_UP, 0.0, U[i]);
         }
      }
      /* set new actual (tightened) column bounds */
      for (j = 1; j <= n; j++)
      {  int type;
         if (l[j] == -DBL_MAX && u[j] == +DBL_MAX)
            type = GLP_FR;
         else if (u[j] == +DBL_MAX)
            type = GLP_LO;
         else if (l[j] == -DBL_MAX)
            type = GLP_UP;
         else if (l[j] != u[j])
            type = GLP_DB;
         else
            type = GLP_FX;
         glp_set_col_bnds(mip, j, type, l[j], u[j]);
      }
done: /* free working arrays and return */
      xfree(L);
      xfree(U);
      xfree(l);
      xfree(u);
      xfree(num);
      return ret;
}
コード例 #6
0
ファイル: glpk.c プロジェクト: joshcc3/HaskellMiniProjects
double c_glp_get_row_ub (glp_prob *lp, int i){
	return glp_get_row_ub (lp, i);
}
コード例 #7
0
ファイル: Rglpk_read_file.c プロジェクト: TraME-Project/Rglpk
// retrieve all missing values of LP/MILP
void Rglpk_retrieve_MP_from_file (char **file, int *type,
				  int *lp_n_constraints,
				  int *lp_n_objective_vars,
				  double *lp_objective_coefficients,
				  int *lp_constraint_matrix_i,
				  int *lp_constraint_matrix_j,
				  double *lp_constraint_matrix_values,
				  int *lp_direction_of_constraints,
				  double *lp_right_hand_side,
				  double *lp_left_hand_side,
				  int *lp_objective_var_is_integer,
				  int *lp_objective_var_is_binary,
				  int *lp_bounds_type,
				  double *lp_bounds_lower,
				  double *lp_bounds_upper,
				  int *lp_ignore_first_row,
				  int *lp_verbosity,
				  char **lp_constraint_names,
				  char **lp_objective_vars_names
				  ) {
  extern glp_prob *lp;
  glp_tran *tran;
  const char *str; 
  
  int i, j, lp_column_kind, tmp;
  int ind_offset, status;

  // Turn on/off Terminal Output
  if (*lp_verbosity==1)
    glp_term_out(GLP_ON);
  else
    glp_term_out(GLP_OFF);

  // create problem object
  if (lp)
    glp_delete_prob(lp);
  lp = glp_create_prob();

  // read file -> gets stored as an GLPK problem object 'lp'
  // which file type do we have?
  switch (*type){
  case 1: 
    // Fixed (ancient) MPS Format, param argument currently NULL
    status = glp_read_mps(lp, GLP_MPS_DECK, NULL, *file);
    break;
  case 2:
    // Free (modern) MPS format, param argument currently NULL
    status = glp_read_mps(lp, GLP_MPS_FILE, NULL, *file);
    break;
  case 3:
    // CPLEX LP Format
    status = glp_read_lp(lp, NULL, *file);
    break;
  case 4:
    // MATHPROG Format (based on lpx_read_model function)
    tran = glp_mpl_alloc_wksp();

    status = glp_mpl_read_model(tran, *file, 0);

    if (!status) {
        status = glp_mpl_generate(tran, NULL);
        if (!status) {
            glp_mpl_build_prob(tran, lp);
        }
    }
    glp_mpl_free_wksp(tran);
    break;    
  } 

  // if file read successfully glp_read_* returns zero
  if ( status != 0 ) {
    glp_delete_prob(lp);
    lp = NULL;
    error("Reading file %c failed.", *file);
  }
  
  if(*lp_verbosity==1)
    Rprintf("Retrieve column specific data ...\n");

  if(glp_get_num_cols(lp) != *lp_n_objective_vars) {
    glp_delete_prob(lp);
    lp = NULL;
    error("The number of columns is not as specified");
  }

  // retrieve column specific data (values, bounds and type)
  for (i = 0; i < *lp_n_objective_vars; i++) {
    lp_objective_coefficients[i] = glp_get_obj_coef(lp, i+1);
    
    // Note that str must not be freed befor we have returned
    // from the .C call in R! 
    str = glp_get_col_name(lp, i+1);    
    if (str){
      lp_objective_vars_names[i] = (char *) str;
    }
    
    lp_bounds_type[i]            = glp_get_col_type(lp, i+1);
    lp_bounds_lower[i]           = glp_get_col_lb  (lp, i+1);
    lp_bounds_upper[i]           = glp_get_col_ub  (lp, i+1);
    lp_column_kind               = glp_get_col_kind(lp, i+1);
    // set to TRUE if objective variable is integer or binary  
    switch (lp_column_kind){
    case GLP_IV: 
      lp_objective_var_is_integer[i] = 1;
      break;
    case GLP_BV:
      lp_objective_var_is_binary[i] = 1;
      break;
    }
  }
  
  ind_offset = 0;

  if(*lp_verbosity==1)
    Rprintf("Retrieve row specific data ...\n");

  if(glp_get_num_rows(lp) != *lp_n_constraints) {
    glp_delete_prob(lp);
    lp = NULL;
    error("The number of rows is not as specified");
  }

  // retrieve row specific data (right hand side, direction of constraints)
  for (i = *lp_ignore_first_row; i < *lp_n_constraints; i++) {
    lp_direction_of_constraints[i] = glp_get_row_type(lp, i+1);
    
    str = glp_get_row_name(lp, i + 1);
    if (str) { 
      lp_constraint_names[i] = (char *) str;
    }
    
    // the right hand side. Note we don't allow for double bounded or
    // free auxiliary variables 
    if( lp_direction_of_constraints[i] == GLP_LO )
      lp_right_hand_side[i] = glp_get_row_lb(lp, i+1);
    if( lp_direction_of_constraints[i] == GLP_UP )
      lp_right_hand_side[i] = glp_get_row_ub(lp, i+1);
    if( lp_direction_of_constraints[i] == GLP_FX )
      lp_right_hand_side[i] = glp_get_row_lb(lp, i+1);
    if( lp_direction_of_constraints[i] == GLP_DB  ){
      lp_right_hand_side[i] = glp_get_row_ub(lp, i+1);
      lp_left_hand_side[i] =  glp_get_row_lb(lp, i+1);
    }

    tmp = glp_get_mat_row(lp, i+1, &lp_constraint_matrix_j[ind_offset-1],
			           &lp_constraint_matrix_values[ind_offset-1]);
    if (tmp > 0)
      for (j = 0; j < tmp; j++)
	lp_constraint_matrix_i[ind_offset+j] = i+1;
	ind_offset += tmp; 
  }
  
  if(*lp_verbosity==1)
    Rprintf("Done.\n");

}
コード例 #8
0
ファイル: glplpx03.c プロジェクト: Shicheng-Guo/methylFlow
int lpx_write_pb(LPX *lp, const char *fname, int normalized,
      int binarize)
{
  FILE* fp;
  int m,n,i,j,k,o,nonfree=0, obj_dir, dbl, *ndx, row_type, emptylhs=0;
  double coeff, *val, bound, constant/*=0.0*/;
  char* objconstname = "dummy_one";
  char* emptylhsname = "dummy_zero";

  /* Variables needed for possible binarization */
  /*LPX* tlp;*/
  IPP *ipp = NULL;
  /*tlp=lp;*/

  if(binarize) /* Transform integer variables to binary ones */
    {
      ipp = ipp_create_wksp();
      ipp_load_orig(ipp, lp);
      ipp_binarize(ipp);
      lp = ipp_build_prob(ipp);
    }
  fp = fopen(fname, "w");

  if(fp!= NULL)
    {
      xprintf(
          "lpx_write_pb: writing problem in %sOPB format to `%s'...\n",
              (normalized?"normalized ":""), fname);

      m = glp_get_num_rows(lp);
      n = glp_get_num_cols(lp);
      for(i=1;i<=m;i++)
        {
          switch(glp_get_row_type(lp,i))
            {
            case GLP_LO:
            case GLP_UP:
            case GLP_FX:
              {
                nonfree += 1;
                break;
              }
            case GLP_DB:
              {
                nonfree += 2;
                break;
              }
            }
        }
      constant=glp_get_obj_coef(lp,0);
      fprintf(fp,"* #variables = %d #constraints = %d\n",
         n + (constant == 0?1:0), nonfree + (constant == 0?1:0));
      /* Objective function */
      obj_dir = glp_get_obj_dir(lp);
      fprintf(fp,"min: ");
      for(i=1;i<=n;i++)
        {
          coeff = glp_get_obj_coef(lp,i);
          if(coeff != 0.0)
            {
              if(obj_dir == GLP_MAX)
                coeff=-coeff;
              if(normalized)
                fprintf(fp, " %d x%d", (int)coeff, i);
              else
                fprintf(fp, " %d*%s", (int)coeff,
                  glp_get_col_name(lp,i));

            }
        }
      if(constant)
        {
          if(normalized)
            fprintf(fp, " %d x%d", (int)constant, n+1);
          else
            fprintf(fp, " %d*%s", (int)constant, objconstname);
        }
      fprintf(fp,";\n");

      if(normalized && !binarize)  /* Name substitution */
        {
          fprintf(fp,"* Variable name substitution:\n");
          for(j=1;j<=n;j++)
            {
              fprintf(fp, "* x%d = %s\n", j, glp_get_col_name(lp,j));
            }
          if(constant)
            fprintf(fp, "* x%d = %s\n", n+1, objconstname);
        }

      ndx = xcalloc(1+n, sizeof(int));
      val = xcalloc(1+n, sizeof(double));

      /* Constraints */
      for(j=1;j<=m;j++)
        {
          row_type=glp_get_row_type(lp,j);
          if(row_type!=GLP_FR)
            {
              if(row_type == GLP_DB)
                {
                  dbl=2;
                  row_type = GLP_UP;
                }
              else
                {
                  dbl=1;
                }
              k=glp_get_mat_row(lp, j, ndx, val);
              for(o=1;o<=dbl;o++)
                {
                  if(o==2)
                    {
                      row_type = GLP_LO;
                    }
                  if(k==0) /* Empty LHS */
                    {
                      emptylhs = 1;
                      if(normalized)
                        {
                          fprintf(fp, "0 x%d ", n+2);
                        }
                      else
                        {
                          fprintf(fp, "0*%s ", emptylhsname);
                        }
                    }

                  for(i=1;i<=k;i++)
                    {
                      if(val[i] != 0.0)
                        {

                          if(normalized)
                            {
                              fprintf(fp, "%d x%d ",
              (row_type==GLP_UP)?(-(int)val[i]):((int)val[i]), ndx[i]);
                            }
                          else
                            {
                              fprintf(fp, "%d*%s ", (int)val[i],
                                      glp_get_col_name(lp,ndx[i]));
                            }
                        }
                    }
                  switch(row_type)
                    {
                    case GLP_LO:
                      {
                        fprintf(fp, ">=");
                        bound = glp_get_row_lb(lp,j);
                        break;
                      }
                    case GLP_UP:
                      {
                        if(normalized)
                          {
                            fprintf(fp, ">=");
                            bound = -glp_get_row_ub(lp,j);
                          }
                        else
                          {
                            fprintf(fp, "<=");
                            bound = glp_get_row_ub(lp,j);
                          }

                        break;
                      }
                    case GLP_FX:
                      {
                        fprintf(fp, "=");
                        bound = glp_get_row_lb(lp,j);
                        break;
                      }
                    }
                  fprintf(fp," %d;\n",(int)bound);
                }
            }
        }
      xfree(ndx);
      xfree(val);

      if(constant)
        {
          xprintf(
        "lpx_write_pb: adding constant objective function variable\n");

          if(normalized)
            fprintf(fp, "1 x%d = 1;\n", n+1);
          else
            fprintf(fp, "1*%s = 1;\n", objconstname);
        }
      if(emptylhs)
        {
          xprintf(
            "lpx_write_pb: adding dummy variable for empty left-hand si"
            "de constraint\n");

          if(normalized)
            fprintf(fp, "1 x%d = 0;\n", n+2);
          else
            fprintf(fp, "1*%s = 0;\n", emptylhsname);
        }

    }
  else
    {
      xprintf("Problems opening file for writing: %s\n", fname);
      return(1);
    }
  fflush(fp);
  if (ferror(fp))
    {  xprintf("lpx_write_pb: can't write to `%s' - %s\n", fname,
               strerror(errno));
    goto fail;
    }
  fclose(fp);


  if(binarize)
    {
      /* delete the resultant problem object */
      if (lp != NULL) lpx_delete_prob(lp);
      /* delete MIP presolver workspace */
      if (ipp != NULL) ipp_delete_wksp(ipp);
      /*lp=tlp;*/
    }
  return 0;
 fail: if (fp != NULL) fclose(fp);
  return 1;
}