Exemplo n.º 1
0
int glp_write_mip(glp_prob *mip, const char *fname)
{   glp_file *fp;
    int i, j, ret = 0;
    xprintf("Writing MIP 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;
    }
    /* number of rows, number of columns */
    xfprintf(fp, "%d %d\n", mip->m, mip->n);
    /* solution status, objective value */
    xfprintf(fp, "%d %.*g\n", mip->mip_stat, DBL_DIG, mip->mip_obj);
    /* rows (auxiliary variables) */
    for (i = 1; i <= mip->m; i++)
        xfprintf(fp, "%.*g\n", DBL_DIG, mip->row[i]->mipx);
    /* columns (structural variables) */
    for (j = 1; j <= mip->n; j++)
        xfprintf(fp, "%.*g\n", DBL_DIG, mip->col[j]->mipx);
#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;
    }
    xprintf("%d lines were written\n", 2 + mip->m + mip->n);
done:
    if (fp != NULL) glp_close(fp);
    return ret;
}
Exemplo n.º 2
0
Arquivo: gbp.c Projeto: kmart/gbp
static VALUE gbp_send(VALUE self, VALUE bytes)
{
    gbp_com *gbp;
    int i;
    short int status;
    VALUE ary;

    Check_Type(bytes, T_ARRAY);
    Data_Get_Struct(self, gbp_com, gbp);

    for (i = 0; i < RARRAY_LEN(bytes); i++) {
        gbp->ibuf[i] = (unsigned char) FIX2INT(RARRAY_PTR(bytes)[i]);
    }
    gbp->ibuf_len = RARRAY_LEN(bytes);

    status = TL_SendReceive(gbp->ibuf_len, gbp->ibuf, &gbp->obuf_len, gbp->obuf);
    if (status != NO_ERR) {
        rb_raise(rb_eRuntimeError, get_err_msg(status));
    }

    ary = rb_ary_new2(gbp->obuf_len);

    for (i = 0; i < gbp->obuf_len; i++) {
        rb_ary_push(ary, CHR2FIX(gbp->obuf[i]));
    }

    return ary;
}
Exemplo n.º 3
0
static void read_char(struct csa *csa)
{     /* read next character from input file */
      int c;
      xassert(csa->c != EOF);
      if (csa->c == '\n') csa->count++;
      c = glp_getc(csa->fp);
      if (c < 0)
      {  if (glp_ioerr(csa->fp))
            error(csa, "read error - %s\n", get_err_msg());
         else if (csa->c == '\n')
         {  csa->count--;
            c = EOF;
         }
         else
         {  warning(csa, "missing final end of line\n");
            c = '\n';
         }
      }
      else if (c == '\n')
         ;
      else if (isspace(c))
         c = ' ';
      else if (iscntrl(c))
         error(csa, "invalid control character 0x%02X\n", c);
      csa->c = c;
      return;
}
Exemplo n.º 4
0
glp_data *glp_sdf_open_file(const char *fname)
{     /* open plain data file */
      glp_data *data = NULL;
      glp_file *fp;
      jmp_buf jump;
      fp = glp_open(fname, "r");
      if (fp == NULL)
      {  xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
         goto done;
      }
      data = xmalloc(sizeof(glp_data));
      data->fname = xmalloc(strlen(fname)+1);
      strcpy(data->fname, fname);
      data->fp = fp;
      data->jump = NULL;
      data->count = 0;
      data->c = '\n';
      data->item[0] = '\0';
      /* read the very first character */
      if (setjmp(jump))
      {  glp_sdf_close_file(data);
         data = NULL;
         goto done;
      }
      data->jump = jump;
      next_char(data);
      data->jump = NULL;
done: return data;
}
Exemplo n.º 5
0
static void next_char(glp_data *data)
{     /* read next character */
      int c;
      if (data->c == EOF)
         glp_sdf_error(data, "unexpected end of file\n");
      else if (data->c == '\n')
         data->count++;
      c = glp_getc(data->fp);
      if (c < 0)
      {  if (glp_ioerr(data->fp))
            glp_sdf_error(data, "read error - %s\n", get_err_msg());
         else if (data->c == '\n')
            c = EOF;
         else
         {  glp_sdf_warning(data, "missing final end of line\n");
            c = '\n';
         }
      }
      else if (c == '\n')
         ;
      else if (isspace(c))
         c = ' ';
      else if (iscntrl(c))
         glp_sdf_error(data, "invalid control character 0x%02X\n", c);
      data->c = c;
      return;
}
Exemplo n.º 6
0
int glp_write_sol(glp_prob *lp, const char *fname)
{   glp_file *fp;
    int i, j, ret = 0;
    xprintf("Writing basic 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;
    }
    /* number of rows, number of columns */
    xfprintf(fp, "%d %d\n", lp->m, lp->n);
    /* primal status, dual status, objective value */
    xfprintf(fp, "%d %d %.*g\n", lp->pbs_stat, lp->dbs_stat, DBL_DIG,
             lp->obj_val);
    /* rows (auxiliary variables) */
    for (i = 1; i <= lp->m; i++)
    {   GLPROW *row = lp->row[i];
        /* status, primal value, dual value */
        xfprintf(fp, "%d %.*g %.*g\n", row->stat, DBL_DIG, row->prim,
                 DBL_DIG, row->dual);
    }
    /* columns (structural variables) */
    for (j = 1; j <= lp->n; j++)
    {   GLPCOL *col = lp->col[j];
        /* status, primal value, dual value */
        xfprintf(fp, "%d %.*g %.*g\n", col->stat, DBL_DIG, col->prim,
                 DBL_DIG, col->dual);
    }
#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;
    }
    xprintf("%d lines were written\n", 2 + lp->m + lp->n);
done:
    if (fp != NULL) glp_close(fp);
    return ret;
}
Exemplo n.º 7
0
int glp_print_mip(glp_prob *P, const char *fname)
{   /* write MIP 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 MIP 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 (%d integer, %d binary)\n", "Columns:",
             P->n, glp_get_num_int(P), glp_get_num_bin(P));
    xfprintf(fp, "%-12s%d\n", "Non-zeros:", P->nnz);
    t = glp_mip_status(P);
    xfprintf(fp, "%-12s%s\n", "Status:",
             t == GLP_OPT    ? "INTEGER OPTIMAL" :
             t == GLP_FEAS   ? "INTEGER NON-OPTIMAL" :
             t == GLP_NOFEAS ? "INTEGER EMPTY" :
             t == GLP_UNDEF  ? "INTEGER UNDEFINED" : "???");
    xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
             P->obj == NULL ? "" : P->obj,
             P->obj == NULL ? "" : " = ", P->mip_obj,
             P->dir == GLP_MIN ? "MINimum" :
             P->dir == GLP_MAX ? "MAXimum" : "???");
    xfprintf(fp, "\n");
    xfprintf(fp, "   No.   Row name        Activity     Lower bound  "
             " Upper bound\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->mipx) <= 1e-9 ? 0.0 : row->mipx);
        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 ? "=" : "");
        xfprintf(fp, "\n");
    }
    xfprintf(fp, "\n");
    xfprintf(fp, "   No. Column name       Activity     Lower bound  "
             " Upper bound\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, "%s  ",
                 col->kind == GLP_CV ? " " :
                 col->kind == GLP_IV ? "*" : "?");
        xfprintf(fp, "%13.6g ",
                 fabs(col->mipx) <= 1e-9 ? 0.0 : col->mipx);
        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 ? "=" : "");
        xfprintf(fp, "\n");
    }
    xfprintf(fp, "\n");
    xfprintf(fp, "Integer feasibility conditions:\n");
    xfprintf(fp, "\n");
    glp_check_kkt(P, GLP_MIP, 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" : "SOLUTION IS WRONG");
    xfprintf(fp, "\n");
    glp_check_kkt(P, GLP_MIP, 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" : "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;
}
Exemplo n.º 8
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;
}
Exemplo n.º 9
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;
}
Exemplo n.º 10
0
int glp_read_prob(glp_prob *P, int flags, const char *fname)
{     DMX _csa, *csa = &_csa;
      int mip, m, n, nnz, ne, i, j, k, type, kind, ret, *ln = NULL,
         *ia = NULL, *ja = NULL;
      double lb, ub, temp, *ar = NULL;
      char *rf = NULL, *cf = NULL;
      if (P == NULL || P->magic != GLP_PROB_MAGIC)
         xerror("glp_read_prob: P = %p; invalid problem object\n",
            P);
      if (flags != 0)
         xerror("glp_read_prob: flags = %d; invalid parameter\n",
            flags);
      if (fname == NULL)
         xerror("glp_read_prob: fname = %d; invalid parameter\n",
            fname);
      glp_erase_prob(P);
      if (setjmp(csa->jump))
      {  ret = 1;
         goto done;
      }
      csa->fname = fname;
      csa->fp = NULL;
      csa->count = 0;
      csa->c = '\n';
      csa->field[0] = '\0';
      csa->empty = csa->nonint = 0;
      xprintf("Reading problem data from '%s'...\n", fname);
      csa->fp = glp_open(fname, "r");
      if (csa->fp == NULL)
      {  xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
         longjmp(csa->jump, 1);
      }
      /* read problem line */
      read_designator(csa);
      if (strcmp(csa->field, "p") != 0)
         error(csa, "problem line missing or invalid");
      read_field(csa);
      if (strcmp(csa->field, "lp") == 0)
         mip = 0;
      else if (strcmp(csa->field, "mip") == 0)
         mip = 1;
      else
         error(csa, "wrong problem designator; 'lp' or 'mip' expected");
      read_field(csa);
      if (strcmp(csa->field, "min") == 0)
         glp_set_obj_dir(P, GLP_MIN);
      else if (strcmp(csa->field, "max") == 0)
         glp_set_obj_dir(P, GLP_MAX);
      else
         error(csa, "objective sense missing or invalid");
      read_field(csa);
      if (!(str2int(csa->field, &m) == 0 && m >= 0))
         error(csa, "number of rows missing or invalid");
      read_field(csa);
      if (!(str2int(csa->field, &n) == 0 && n >= 0))
         error(csa, "number of columns missing or invalid");
      read_field(csa);
      if (!(str2int(csa->field, &nnz) == 0 && nnz >= 0))
         error(csa, "number of constraint coefficients missing or inval"
            "id");
      if (m > 0)
      {  glp_add_rows(P, m);
         for (i = 1; i <= m; i++)
            glp_set_row_bnds(P, i, GLP_FX, 0.0, 0.0);
      }
      if (n > 0)
      {  glp_add_cols(P, n);
         for (j = 1; j <= n; j++)
         {  if (!mip)
               glp_set_col_bnds(P, j, GLP_LO, 0.0, 0.0);
            else
               glp_set_col_kind(P, j, GLP_BV);
         }
      }
      end_of_line(csa);
      /* allocate working arrays */
      rf = xcalloc(1+m, sizeof(char));
      memset(rf, 0, 1+m);
      cf = xcalloc(1+n, sizeof(char));
      memset(cf, 0, 1+n);
      ln = xcalloc(1+nnz, sizeof(int));
      ia = xcalloc(1+nnz, sizeof(int));
      ja = xcalloc(1+nnz, sizeof(int));
      ar = xcalloc(1+nnz, sizeof(double));
      /* read descriptor lines */
      ne = 0;
      for (;;)
      {  read_designator(csa);
         if (strcmp(csa->field, "i") == 0)
         {  /* row descriptor */
            read_field(csa);
            if (str2int(csa->field, &i) != 0)
               error(csa, "row number missing or invalid");
            if (!(1 <= i && i <= m))
               error(csa, "row number out of range");
            read_field(csa);
            if (strcmp(csa->field, "f") == 0)
               type = GLP_FR;
            else if (strcmp(csa->field, "l") == 0)
               type = GLP_LO;
            else if (strcmp(csa->field, "u") == 0)
               type = GLP_UP;
            else if (strcmp(csa->field, "d") == 0)
               type = GLP_DB;
            else if (strcmp(csa->field, "s") == 0)
               type = GLP_FX;
            else
               error(csa, "row type missing or invalid");
            if (type == GLP_LO || type == GLP_DB || type == GLP_FX)
            {  read_field(csa);
               if (str2num(csa->field, &lb) != 0)
                  error(csa, "row lower bound/fixed value missing or in"
                     "valid");
            }
            else
               lb = 0.0;
            if (type == GLP_UP || type == GLP_DB)
            {  read_field(csa);
               if (str2num(csa->field, &ub) != 0)
                  error(csa, "row upper bound missing or invalid");
            }
            else
               ub = 0.0;
            if (rf[i] & 0x01)
               error(csa, "duplicate row descriptor");
            glp_set_row_bnds(P, i, type, lb, ub), rf[i] |= 0x01;
         }
         else if (strcmp(csa->field, "j") == 0)
         {  /* column descriptor */
            read_field(csa);
            if (str2int(csa->field, &j) != 0)
               error(csa, "column number missing or invalid");
            if (!(1 <= j && j <= n))
               error(csa, "column number out of range");
            if (!mip)
               kind = GLP_CV;
            else
            {  read_field(csa);
               if (strcmp(csa->field, "c") == 0)
                  kind = GLP_CV;
               else if (strcmp(csa->field, "i") == 0)
                  kind = GLP_IV;
               else if (strcmp(csa->field, "b") == 0)
               {  kind = GLP_IV;
                  type = GLP_DB, lb = 0.0, ub = 1.0;
                  goto skip;
               }
               else
                  error(csa, "column kind missing or invalid");
            }
            read_field(csa);
            if (strcmp(csa->field, "f") == 0)
               type = GLP_FR;
            else if (strcmp(csa->field, "l") == 0)
               type = GLP_LO;
            else if (strcmp(csa->field, "u") == 0)
               type = GLP_UP;
            else if (strcmp(csa->field, "d") == 0)
               type = GLP_DB;
            else if (strcmp(csa->field, "s") == 0)
               type = GLP_FX;
            else
               error(csa, "column type missing or invalid");
            if (type == GLP_LO || type == GLP_DB || type == GLP_FX)
            {  read_field(csa);
               if (str2num(csa->field, &lb) != 0)
                  error(csa, "column lower bound/fixed value missing or"
                     " invalid");
            }
            else
               lb = 0.0;
            if (type == GLP_UP || type == GLP_DB)
            {  read_field(csa);
               if (str2num(csa->field, &ub) != 0)
                  error(csa, "column upper bound missing or invalid");
            }
            else
               ub = 0.0;
skip:       if (cf[j] & 0x01)
               error(csa, "duplicate column descriptor");
            glp_set_col_kind(P, j, kind);
            glp_set_col_bnds(P, j, type, lb, ub), cf[j] |= 0x01;
         }
         else if (strcmp(csa->field, "a") == 0)
         {  /* coefficient descriptor */
            read_field(csa);
            if (str2int(csa->field, &i) != 0)
               error(csa, "row number missing or invalid");
            if (!(0 <= i && i <= m))
               error(csa, "row number out of range");
            read_field(csa);
            if (str2int(csa->field, &j) != 0)
               error(csa, "column number missing or invalid");
            if (!((i == 0 ? 0 : 1) <= j && j <= n))
               error(csa, "column number out of range");
            read_field(csa);
            if (i == 0)
            {  if (str2num(csa->field, &temp) != 0)
                  error(csa, "objective %s missing or invalid",
                     j == 0 ? "constant term" : "coefficient");
               if (cf[j] & 0x10)
                  error(csa, "duplicate objective %s",
                     j == 0 ? "constant term" : "coefficient");
               glp_set_obj_coef(P, j, temp), cf[j] |= 0x10;
            }
            else
            {  if (str2num(csa->field, &temp) != 0)
                  error(csa, "constraint coefficient missing or invalid"
                     );
               if (ne == nnz)
                  error(csa, "too many constraint coefficient descripto"
                     "rs");
               ln[++ne] = csa->count;
               ia[ne] = i, ja[ne] = j, ar[ne] = temp;
            }
         }
         else if (strcmp(csa->field, "n") == 0)
         {  /* symbolic name descriptor */
            read_field(csa);
            if (strcmp(csa->field, "p") == 0)
            {  /* problem name */
               read_field(csa);
               if (P->name != NULL)
                  error(csa, "duplicate problem name");
               glp_set_prob_name(P, csa->field);
            }
            else if (strcmp(csa->field, "z") == 0)
            {  /* objective name */
               read_field(csa);
               if (P->obj != NULL)
                  error(csa, "duplicate objective name");
               glp_set_obj_name(P, csa->field);
            }
            else if (strcmp(csa->field, "i") == 0)
            {  /* row name */
               read_field(csa);
               if (str2int(csa->field, &i) != 0)
                  error(csa, "row number missing or invalid");
               if (!(1 <= i && i <= m))
                  error(csa, "row number out of range");
               read_field(csa);
               if (P->row[i]->name != NULL)
                  error(csa, "duplicate row name");
               glp_set_row_name(P, i, csa->field);
            }
            else if (strcmp(csa->field, "j") == 0)
            {  /* column name */
               read_field(csa);
               if (str2int(csa->field, &j) != 0)
                  error(csa, "column number missing or invalid");
               if (!(1 <= j && j <= n))
                  error(csa, "column number out of range");
               read_field(csa);
               if (P->col[j]->name != NULL)
                  error(csa, "duplicate column name");
               glp_set_col_name(P, j, csa->field);
            }
            else
               error(csa, "object designator missing or invalid");
         }
         else if (strcmp(csa->field, "e") == 0)
            break;
         else
            error(csa, "line designator missing or invalid");
         end_of_line(csa);
      }
      if (ne < nnz)
         error(csa, "too few constraint coefficient descriptors");
      xassert(ne == nnz);
      k = glp_check_dup(m, n, ne, ia, ja);
      xassert(0 <= k && k <= nnz);
      if (k > 0)
      {  csa->count = ln[k];
         error(csa, "duplicate constraint coefficient");
      }
      glp_load_matrix(P, ne, ia, ja, ar);
      /* print some statistics */
      if (P->name != NULL)
         xprintf("Problem: %s\n", P->name);
      if (P->obj != NULL)
         xprintf("Objective: %s\n", P->obj);
      xprintf("%d row%s, %d column%s, %d non-zero%s\n",
         m, m == 1 ? "" : "s", n, n == 1 ? "" : "s", nnz, nnz == 1 ?
         "" : "s");
      if (glp_get_num_int(P) > 0)
      {  int ni = glp_get_num_int(P);
         int nb = glp_get_num_bin(P);
         if (ni == 1)
         {  if (nb == 0)
               xprintf("One variable is integer\n");
            else
               xprintf("One variable is binary\n");
         }
         else
         {  xprintf("%d integer variables, ", ni);
            if (nb == 0)
               xprintf("none");
            else if (nb == 1)
               xprintf("one");
            else if (nb == ni)
               xprintf("all");
            else
               xprintf("%d", nb);
            xprintf(" of which %s binary\n", nb == 1 ? "is" : "are");
         }
      }
      xprintf("%d lines were read\n", csa->count);
      /* problem data has been successfully read */
      glp_sort_matrix(P);
      ret = 0;
done: if (csa->fp != NULL) glp_close(csa->fp);
      if (rf != NULL) xfree(rf);
      if (cf != NULL) xfree(cf);
      if (ln != NULL) xfree(ln);
      if (ia != NULL) xfree(ia);
      if (ja != NULL) xfree(ja);
      if (ar != NULL) xfree(ar);
      if (ret) glp_erase_prob(P);
      return ret;
}
Exemplo n.º 11
0
int glp_read_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname)
{     /* read problem data in CPLEX LP format */
      glp_cpxcp _parm;
      struct csa _csa, *csa = &_csa;
      int ret;
      xprintf("Reading problem data from '%s'...\n", fname);
      if (parm == NULL)
         glp_init_cpxcp(&_parm), parm = &_parm;
      /* check control parameters */
      check_parm("glp_read_lp", parm);
      /* initialize common storage area */
      csa->P = P;
      csa->parm = parm;
      csa->fname = fname;
      csa->fp = NULL;
      if (setjmp(csa->jump))
      {  ret = 1;
         goto done;
      }
      csa->count = 0;
      csa->c = '\n';
      csa->token = T_EOF;
      csa->image[0] = '\0';
      csa->imlen = 0;
      csa->value = 0.0;
      csa->n_max = 100;
      csa->ind = xcalloc(1+csa->n_max, sizeof(int));
      csa->val = xcalloc(1+csa->n_max, sizeof(double));
      csa->flag = xcalloc(1+csa->n_max, sizeof(char));
      memset(&csa->flag[1], 0, csa->n_max * sizeof(char));
      csa->lb = xcalloc(1+csa->n_max, sizeof(double));
      csa->ub = xcalloc(1+csa->n_max, sizeof(double));
#if 1 /* 27/VII-2013 */
      csa->lb_warn = csa->ub_warn = 0;
#endif
      /* erase problem object */
      glp_erase_prob(P);
      glp_create_index(P);
      /* open input CPLEX LP file */
      csa->fp = glp_open(fname, "r");
      if (csa->fp == NULL)
      {  xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
         ret = 1;
         goto done;
      }
      /* scan very first token */
      scan_token(csa);
      /* parse definition of the objective function */
      if (!(csa->token == T_MINIMIZE || csa->token == T_MAXIMIZE))
         error(csa, "'minimize' or 'maximize' keyword missing\n");
      parse_objective(csa);
      /* parse constraints section */
      if (csa->token != T_SUBJECT_TO)
         error(csa, "constraints section missing\n");
      parse_constraints(csa);
      /* parse optional bounds section */
      if (csa->token == T_BOUNDS) parse_bounds(csa);
      /* parse optional general, integer, and binary sections */
      while (csa->token == T_GENERAL ||
             csa->token == T_INTEGER ||
             csa->token == T_BINARY) parse_integer(csa);
      /* check for the keyword 'end' */
      if (csa->token == T_END)
         scan_token(csa);
      else if (csa->token == T_EOF)
         warning(csa, "keyword 'end' missing\n");
      else
         error(csa, "symbol '%s' in wrong position\n", csa->image);
      /* nothing must follow the keyword 'end' (except comments) */
      if (csa->token != T_EOF)
         error(csa, "extra symbol(s) detected beyond 'end'\n");
      /* set bounds of variables */
      {  int j, type;
         double lb, ub;
         for (j = 1; j <= P->n; j++)
         {  lb = csa->lb[j];
            ub = csa->ub[j];
            if (lb == +DBL_MAX) lb = 0.0;      /* default lb */
            if (ub == -DBL_MAX) ub = +DBL_MAX; /* default ub */
            if (lb == -DBL_MAX && ub == +DBL_MAX)
               type = GLP_FR;
            else if (ub == +DBL_MAX)
               type = GLP_LO;
            else if (lb == -DBL_MAX)
               type = GLP_UP;
            else if (lb != ub)
               type = GLP_DB;
            else
               type = GLP_FX;
            glp_set_col_bnds(csa->P, j, type, lb, ub);
         }
      }
      /* print some statistics */
      xprintf("%d row%s, %d column%s, %d non-zero%s\n",
         P->m, P->m == 1 ? "" : "s", P->n, P->n == 1 ? "" : "s",
         P->nnz, P->nnz == 1 ? "" : "s");
      if (glp_get_num_int(P) > 0)
      {  int ni = glp_get_num_int(P);
         int nb = glp_get_num_bin(P);
         if (ni == 1)
         {  if (nb == 0)
               xprintf("One variable is integer\n");
            else
               xprintf("One variable is binary\n");
         }
         else
         {  xprintf("%d integer variables, ", ni);
            if (nb == 0)
               xprintf("none");
            else if (nb == 1)
               xprintf("one");
            else if (nb == ni)
               xprintf("all");
            else
               xprintf("%d", nb);
            xprintf(" of which %s binary\n", nb == 1 ? "is" : "are");
         }
      }
      xprintf("%d lines were read\n", csa->count);
      /* problem data has been successfully read */
      glp_delete_index(P);
      glp_sort_matrix(P);
      ret = 0;
done: if (csa->fp != NULL) glp_close(csa->fp);
      xfree(csa->ind);
      xfree(csa->val);
      xfree(csa->flag);
      xfree(csa->lb);
      xfree(csa->ub);
      if (ret != 0) glp_erase_prob(P);
      return ret;
}
Exemplo n.º 12
0
int glp_write_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname)
{     /* write problem data in CPLEX LP format */
      glp_cpxcp _parm;
      struct csa _csa, *csa = &_csa;
      glp_file *fp;
      GLPROW *row;
      GLPCOL *col;
      GLPAIJ *aij;
      int i, j, len, flag, count, ret;
      char line[1000+1], term[500+1], name[255+1];
      xprintf("Writing problem data to '%s'...\n", fname);
      if (parm == NULL)
         glp_init_cpxcp(&_parm), parm = &_parm;
      /* check control parameters */
      check_parm("glp_write_lp", parm);
      /* initialize common storage area */
      csa->P = P;
      csa->parm = parm;
      /* create output CPLEX LP file */
      fp = glp_open(fname, "w"), count = 0;
      if (fp == NULL)
      {  xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
         ret = 1;
         goto done;
      }
      /* write problem name */
      xfprintf(fp, "\\* Problem: %s *\\\n",
         P->name == NULL ? "Unknown" : P->name), count++;
      xfprintf(fp, "\n"), count++;
      /* the problem should contain at least one row and one column */
      if (!(P->m > 0 && P->n > 0))
      {  xprintf("Warning: problem has no rows/columns\n");
         xfprintf(fp, "\\* WARNING: PROBLEM HAS NO ROWS/COLUMNS *\\\n"),
            count++;
         xfprintf(fp, "\n"), count++;
         goto skip;
      }
      /* write the objective function definition */
      if (P->dir == GLP_MIN)
         xfprintf(fp, "Minimize\n"), count++;
      else if (P->dir == GLP_MAX)
         xfprintf(fp, "Maximize\n"), count++;
      else
         xassert(P != P);
      row_name(csa, 0, name);
      sprintf(line, " %s:", name);
      len = 0;
      for (j = 1; j <= P->n; j++)
      {  col = P->col[j];
         if (col->coef != 0.0 || col->ptr == NULL)
         {  len++;
            col_name(csa, j, name);
            if (col->coef == 0.0)
               sprintf(term, " + 0 %s", name); /* empty column */
            else if (col->coef == +1.0)
               sprintf(term, " + %s", name);
            else if (col->coef == -1.0)
               sprintf(term, " - %s", name);
            else if (col->coef > 0.0)
               sprintf(term, " + %.*g %s", DBL_DIG, +col->coef, name);
            else
               sprintf(term, " - %.*g %s", DBL_DIG, -col->coef, name);
            if (strlen(line) + strlen(term) > 72)
               xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
            strcat(line, term);
         }
      }
      if (len == 0)
      {  /* empty objective */
         sprintf(term, " 0 %s", col_name(csa, 1, name));
         strcat(line, term);
      }
      xfprintf(fp, "%s\n", line), count++;
      if (P->c0 != 0.0)
         xfprintf(fp, "\\* constant term = %.*g *\\\n", DBL_DIG, P->c0),
            count++;
      xfprintf(fp, "\n"), count++;
      /* write the constraints section */
      xfprintf(fp, "Subject To\n"), count++;
      for (i = 1; i <= P->m; i++)
      {  row = P->row[i];
         if (row->type == GLP_FR) continue; /* skip free row */
         row_name(csa, i, name);
         sprintf(line, " %s:", name);
         /* linear form */
         for (aij = row->ptr; aij != NULL; aij = aij->r_next)
         {  col_name(csa, aij->col->j, name);
            if (aij->val == +1.0)
               sprintf(term, " + %s", name);
            else if (aij->val == -1.0)
               sprintf(term, " - %s", name);
            else if (aij->val > 0.0)
               sprintf(term, " + %.*g %s", DBL_DIG, +aij->val, name);
            else
               sprintf(term, " - %.*g %s", DBL_DIG, -aij->val, name);
            if (strlen(line) + strlen(term) > 72)
               xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
            strcat(line, term);
         }
         if (row->type == GLP_DB)
         {  /* double-bounded (ranged) constraint */
            sprintf(term, " - ~r_%d", i);
            if (strlen(line) + strlen(term) > 72)
               xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
            strcat(line, term);
         }
         else if (row->ptr == NULL)
         {  /* empty constraint */
            sprintf(term, " 0 %s", col_name(csa, 1, name));
            strcat(line, term);
         }
         /* right hand-side */
         if (row->type == GLP_LO)
            sprintf(term, " >= %.*g", DBL_DIG, row->lb);
         else if (row->type == GLP_UP)
            sprintf(term, " <= %.*g", DBL_DIG, row->ub);
         else if (row->type == GLP_DB || row->type == GLP_FX)
            sprintf(term, " = %.*g", DBL_DIG, row->lb);
         else
            xassert(row != row);
         if (strlen(line) + strlen(term) > 72)
            xfprintf(fp, "%s\n", line), line[0] = '\0', count++;
         strcat(line, term);
         xfprintf(fp, "%s\n", line), count++;
      }
      xfprintf(fp, "\n"), count++;
      /* write the bounds section */
      flag = 0;
      for (i = 1; i <= P->m; i++)
      {  row = P->row[i];
         if (row->type != GLP_DB) continue;
         if (!flag)
            xfprintf(fp, "Bounds\n"), flag = 1, count++;
         xfprintf(fp, " 0 <= ~r_%d <= %.*g\n",
            i, DBL_DIG, row->ub - row->lb), count++;
      }
      for (j = 1; j <= P->n; j++)
      {  col = P->col[j];
         if (col->type == GLP_LO && col->lb == 0.0) continue;
         if (!flag)
            xfprintf(fp, "Bounds\n"), flag = 1, count++;
         col_name(csa, j, name);
         if (col->type == GLP_FR)
            xfprintf(fp, " %s free\n", name), count++;
         else if (col->type == GLP_LO)
            xfprintf(fp, " %s >= %.*g\n",
               name, DBL_DIG, col->lb), count++;
         else if (col->type == GLP_UP)
            xfprintf(fp, " -Inf <= %s <= %.*g\n",
               name, DBL_DIG, col->ub), count++;
         else if (col->type == GLP_DB)
            xfprintf(fp, " %.*g <= %s <= %.*g\n",
               DBL_DIG, col->lb, name, DBL_DIG, col->ub), count++;
         else if (col->type == GLP_FX)
            xfprintf(fp, " %s = %.*g\n",
               name, DBL_DIG, col->lb), count++;
         else
            xassert(col != col);
      }
      if (flag) xfprintf(fp, "\n"), count++;
      /* write the integer section */
      flag = 0;
      for (j = 1; j <= P->n; j++)
      {  col = P->col[j];
         if (col->kind == GLP_CV) continue;
         xassert(col->kind == GLP_IV);
         if (!flag)
            xfprintf(fp, "Generals\n"), flag = 1, count++;
         xfprintf(fp, " %s\n", col_name(csa, j, name)), count++;
      }
      if (flag) xfprintf(fp, "\n"), count++;
skip: /* write the end keyword */
      xfprintf(fp, "End\n"), count++;
#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;
      }
      /* problem data has been successfully written */
      xprintf("%d lines were written\n", count);
      ret = 0;
done: if (fp != NULL) glp_close(fp);
      return ret;
}