Ejemplo n.º 1
0
int glp_write_mip(glp_prob *mip, const char *fname)
{   XFILE *fp;
    int i, j, ret = 0;
    xprintf("Writing MIP solution to `%s'...\n", fname);
    fp = xfopen(fname, "w");
    if (fp == NULL)
    {   xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
        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);
    xfflush(fp);
    if (xferror(fp))
    {   xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
        ret = 1;
        goto done;
    }
    xprintf("%d lines were written\n", 2 + mip->m + mip->n);
done:
    if (fp != NULL) xfclose(fp);
    return ret;
}
Ejemplo n.º 2
0
int glp_write_graph(glp_graph *G, const char *fname)
{     XFILE *fp;
      glp_vertex *v;
      glp_arc *a;
      int i, count, ret;
      xprintf("Writing graph to `%s'...\n", fname);
      fp = xfopen(fname, "w"), count = 0;
      if (fp == NULL)
      {  xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
         ret = 1;
         goto done;
      }
      xfprintf(fp, "%d %d\n", G->nv, G->na), count++;
      for (i = 1; i <= G->nv; i++)
      {  v = G->v[i];
         for (a = v->out; a != NULL; a = a->t_next)
            xfprintf(fp, "%d %d\n", a->tail->i, a->head->i), count++;
      }
      xfflush(fp);
      if (xferror(fp))
      {  xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
         ret = 1;
         goto done;
      }
      xprintf("%d lines were written\n", count);
      ret = 0;
done: if (fp != NULL) xfclose(fp);
      return ret;
}
Ejemplo n.º 3
0
int glp_write_maxflow(glp_graph *G, int s, int t, int a_cap,
      const char *fname)
{     XFILE *fp;
      glp_vertex *v;
      glp_arc *a;
      int i, count = 0, ret;
      double cap;
      if (!(1 <= s && s <= G->nv))
         xerror("glp_write_maxflow: s = %d; source node number out of r"
            "ange\n", s);
      if (!(1 <= t && t <= G->nv))
         xerror("glp_write_maxflow: t = %d: sink node number out of ran"
            "ge\n", t);
      if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
         xerror("glp_write_mincost: a_cap = %d; invalid offset\n",
            a_cap);
      xprintf("Writing maximum flow problem data to `%s'...\n",
         fname);
      fp = xfopen(fname, "w");
      if (fp == NULL)
      {  xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
         ret = 1;
         goto done;
      }
      xfprintf(fp, "c %s\n",
         G->name == NULL ? "unknown" : G->name), count++;
      xfprintf(fp, "p max %d %d\n", G->nv, G->na), count++;
      xfprintf(fp, "n %d s\n", s), count++;
      xfprintf(fp, "n %d t\n", t), count++;
      for (i = 1; i <= G->nv; i++)
      {  v = G->v[i];
         for (a = v->out; a != NULL; a = a->t_next)
         {  if (a_cap >= 0)
               memcpy(&cap, (char *)a->data + a_cap, sizeof(double));
            else
               cap = DBL_MAX;
            xfprintf(fp, "a %d %d %.*g\n",
               a->tail->i, a->head->i, DBL_DIG, cap), count++;
         }
      }
      xfprintf(fp, "c eof\n"), count++;
      xfflush(fp);
      if (xferror(fp))
      {  xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
         ret = 1;
         goto done;
      }
      xprintf("%d lines were written\n", count);
      ret = 0;
done: if (fp != NULL) xfclose(fp);
      return ret;
}
Ejemplo n.º 4
0
Archivo: net.c Proyecto: cinience/saker
static int  get_if_stats(const char *if_name, MIB_IFROW *pIfRow) {
    DWORD       dwSize, dwRetVal, i;
    int     ret = UGERR;
    /* variables used for GetIfTable and GetIfEntry */
    MIB_IFTABLE *pIfTable = NULL;
    MIB_IFROW   TmpIfRow;
    /* Allocate memory for our pointers. */
    dwSize = sizeof(MIB_IFTABLE);
    pIfTable = (MIB_IFTABLE *)zmalloc( dwSize);

    /* Before calling GetIfEntry, we call GetIfTable to make
       sure there are entries to get and retrieve the interface index.
       Make an initial call to GetIfTable to get the necessary size into dwSize */
    if (ERROR_INSUFFICIENT_BUFFER == GetIfTable(pIfTable, &dwSize, 0))
        pIfTable = (MIB_IFTABLE *)zrealloc(pIfTable, dwSize);

    /* Make a second call to GetIfTable to get the actual data we want. */
    if (NO_ERROR != (dwRetVal = GetIfTable(pIfTable, &dwSize, 0))) {
        LOG_DEBUG("GetIfTable failed with error: %s", xerrmsg());
        goto clean;
    }

    memset(pIfRow,0,sizeof(MIB_IFROW));
    for (i = 0; i < pIfTable->dwNumEntries; i++) {
        memset(&TmpIfRow,0,sizeof(MIB_IFROW));
        TmpIfRow.dwIndex = pIfTable->table[i].dwIndex;
        if (NO_ERROR != (dwRetVal = GetIfEntry(&TmpIfRow))) {
            LOG_DEBUG("GetIfEntry failed with error: %s",
                      xerrmsg());
            continue;
        }

        /* ignore loopback addr */
        if (0 == strcmp(if_name, TmpIfRow.bDescr))
            continue;
        pIfRow->dwInOctets+=TmpIfRow.dwInOctets;
        pIfRow->dwOutOctets+=TmpIfRow.dwOutOctets;
        pIfRow->dwInErrors+=TmpIfRow.dwInErrors;
        pIfRow->dwOutErrors+=TmpIfRow.dwOutErrors;
        pIfRow->dwInUcastPkts+=TmpIfRow.dwInUcastPkts;
        pIfRow->dwInNUcastPkts+=TmpIfRow.dwInNUcastPkts;
        pIfRow->dwOutUcastPkts+=TmpIfRow.dwOutUcastPkts;
        pIfRow->dwOutNUcastPkts+=TmpIfRow.dwOutNUcastPkts;
        ret = UGOK;
    }
clean:
    zfree(pIfTable);
    return ret;
}
Ejemplo n.º 5
0
static void next_char(glp_data *data)
{     /* read next character */
      int c;
      if (data->c == XEOF)
         glp_sdf_error(data, "unexpected end of file\n");
      else if (data->c == '\n')
         data->count++;
      c = xfgetc(data->fp);
      if (c < 0)
      {  if (xferror(data->fp))
            glp_sdf_error(data, "read error - %s\n", xerrmsg());
         else if (data->c == '\n')
            c = XEOF;
         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;
}
Ejemplo n.º 6
0
void open_input(MPL *mpl, char *file)
{     mpl->line = 0;
      mpl->c = '\n';
      mpl->token = 0;
      mpl->imlen = 0;
      mpl->image[0] = '\0';
      mpl->value = 0.0;
      mpl->b_token = T_EOF;
      mpl->b_imlen = 0;
      mpl->b_image[0] = '\0';
      mpl->b_value = 0.0;
      mpl->f_dots = 0;
      mpl->f_scan = 0;
      mpl->f_token = 0;
      mpl->f_imlen = 0;
      mpl->f_image[0] = '\0';
      mpl->f_value = 0.0;
      memset(mpl->context, ' ', CONTEXT_SIZE);
      mpl->c_ptr = 0;
      xassert(mpl->in_fp == NULL);
      mpl->in_fp = xfopen(file, "r");
      if (mpl->in_fp == NULL)
         error(mpl, "unable to open %s - %s", file, xerrmsg());
      mpl->in_file = file;
      /* scan the very first character */
      get_char(mpl);
      /* scan the very first token */
      get_token(mpl);
      return;
}
Ejemplo n.º 7
0
glp_data *glp_sdf_open_file(const char *fname)
{     /* open plain data file */
      glp_data *data = NULL;
      XFILE *fp;
      jmp_buf jump;
      fp = xfopen(fname, "r");
      if (fp == NULL)
      {  xprintf("Unable to open `%s' - %s\n", fname, xerrmsg());
         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;
}
Ejemplo n.º 8
0
void flush_output(MPL *mpl)
{     xassert(mpl->out_fp != NULL);
      if (mpl->out_fp != (void *)stdout)
      {  xfflush(mpl->out_fp);
         if (xferror(mpl->out_fp))
            error(mpl, "write error on %s - %s", mpl->out_file,
               xerrmsg());
      }
      return;
}
Ejemplo n.º 9
0
int read_char(MPL *mpl)
{     int c;
      xassert(mpl->in_fp != NULL);
      c = xfgetc(mpl->in_fp);
      if (c < 0)
      {  if (xferror(mpl->in_fp))
            error(mpl, "read error on %s - %s", mpl->in_file,
               xerrmsg());
         c = EOF;
      }
      return c;
}
Ejemplo n.º 10
0
int glp_write_sol(glp_prob *lp, const char *fname)
{   XFILE *fp;
    int i, j, ret = 0;
    xprintf("Writing basic solution to `%s'...\n", fname);
    fp = xfopen(fname, "w");
    if (fp == NULL)
    {   xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
        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);
    }
    xfflush(fp);
    if (xferror(fp))
    {   xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
        ret = 1;
        goto done;
    }
    xprintf("%d lines were written\n", 2 + lp->m + lp->n);
done:
    if (fp != NULL) xfclose(fp);
    return ret;
}
Ejemplo n.º 11
0
void open_output(MPL *mpl, char *file)
{     xassert(mpl->out_fp == NULL);
      if (file == NULL)
      {  file = "<stdout>";
         mpl->out_fp = (void *)stdout;
      }
      else
      {  mpl->out_fp = xfopen(file, "w");
         if (mpl->out_fp == NULL)
            error(mpl, "unable to create %s - %s", file, xerrmsg());
      }
      mpl->out_file = xmalloc(strlen(file)+1);
      strcpy(mpl->out_file, file);
      return;
}
Ejemplo n.º 12
0
static void read_char(struct csa *csa)
{     /* read character from input text file */
      int c;
      if (csa->c == '\n') csa->count++;
      c = xfgetc(csa->fp);
      if (c < 0)
      {  if (xferror(csa->fp))
            error(csa, "read error - %s", xerrmsg());
         else if (csa->c == '\n')
            error(csa, "unexpected end of file");
         else
         {  warning(csa, "missing final end of line");
            c = '\n';
         }
      }
      else if (c == '\n')
         ;
      else if (isspace(c))
         c = ' ';
      else if (iscntrl(c))
         error(csa, "invalid control character 0x%02X", c);
      csa->c = c;
      return;
}
Ejemplo n.º 13
0
int glp_print_mip(glp_prob *P, const char *fname)
{   /* write MIP solution in printable format */
    XFILE *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 = xfopen(fname, "w");
    if (fp == NULL)
    {   xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
        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");
    xfflush(fp);
    if (xferror(fp))
    {   xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
        ret = 1;
        goto done;
    }
    ret = 0;
done:
    if (fp != NULL) xfclose(fp);
    return ret;
}
Ejemplo n.º 14
0
int glp_print_ipt(glp_prob *P, const char *fname)
{   /* write interior-point solution in printable format */
    XFILE *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 = xfopen(fname, "w");
    if (fp == NULL)
    {   xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
        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");
    xfflush(fp);
    if (xferror(fp))
    {   xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
        ret = 1;
        goto done;
    }
    ret = 0;
done:
    if (fp != NULL) xfclose(fp);
    return ret;
}
Ejemplo n.º 15
0
int glp_print_ranges(glp_prob *P, int len, const int list[],
                     int flags, const char *fname)
{   /* print sensitivity analysis report */
    XFILE *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 = xfopen(fname, "w");
    if (fp == NULL)
    {   xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
        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");
    xfflush(fp);
    if (xferror(fp))
    {   xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
        ret = 4;
        goto done;
    }
    ret = 0;
done:
    if (fp != NULL) xfclose(fp);
    return ret;
}
Ejemplo n.º 16
0
int glp_read_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap,
      int a_cost, const char *fname)
{     struct csa _csa, *csa = &_csa;
      glp_vertex *v;
      glp_arc *a;
      int i, j, k, nv, na, ret = 0;
      double rhs, low, cap, cost;
      char *flag = NULL;
      if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double))
         xerror("glp_read_mincost: v_rhs = %d; invalid offset\n",
            v_rhs);
      if (a_low >= 0 && a_low > G->a_size - (int)sizeof(double))
         xerror("glp_read_mincost: a_low = %d; invalid offset\n",
            a_low);
      if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
         xerror("glp_read_mincost: a_cap = %d; invalid offset\n",
            a_cap);
      if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
         xerror("glp_read_mincost: a_cost = %d; invalid offset\n",
            a_cost);
      glp_erase_graph(G, G->v_size, G->a_size);
      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 min-cost flow problem data from `%s'...\n",
         fname);
      csa->fp = xfopen(fname, "r");
      if (csa->fp == NULL)
      {  xprintf("Unable to open `%s' - %s\n", fname, xerrmsg());
         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, "min") != 0)
         error(csa, "wrong problem designator; `min' expected");
      read_field(csa);
      if (!(str2int(csa->field, &nv) == 0 && nv >= 0))
         error(csa, "number of nodes missing or invalid");
      read_field(csa);
      if (!(str2int(csa->field, &na) == 0 && na >= 0))
         error(csa, "number of arcs missing or invalid");
      xprintf("Flow network has %d node%s and %d arc%s\n",
         nv, nv == 1 ? "" : "s", na, na == 1 ? "" : "s");
      if (nv > 0) glp_add_vertices(G, nv);
      end_of_line(csa);
      /* read node descriptor lines */
      flag = xcalloc(1+nv, sizeof(char));
      memset(&flag[1], 0, nv * sizeof(char));
      if (v_rhs >= 0)
      {  rhs = 0.0;
         for (i = 1; i <= nv; i++)
         {  v = G->v[i];
            memcpy((char *)v->data + v_rhs, &rhs, sizeof(double));
         }
      }
      for (;;)
      {  read_designator(csa);
         if (strcmp(csa->field, "n") != 0) break;
         read_field(csa);
         if (str2int(csa->field, &i) != 0)
            error(csa, "node number missing or invalid");
         if (!(1 <= i && i <= nv))
            error(csa, "node number %d out of range", i);
         if (flag[i])
            error(csa, "duplicate descriptor of node %d", i);
         read_field(csa);
         if (str2num(csa->field, &rhs) != 0)
            error(csa, "node supply/demand missing or invalid");
         check_int(csa, rhs);
         if (v_rhs >= 0)
         {  v = G->v[i];
            memcpy((char *)v->data + v_rhs, &rhs, sizeof(double));
         }
         flag[i] = 1;
         end_of_line(csa);
      }
      xfree(flag), flag = NULL;
      /* read arc descriptor lines */
      for (k = 1; k <= na; k++)
      {  if (k > 1) read_designator(csa);
         if (strcmp(csa->field, "a") != 0)
            error(csa, "wrong line designator; `a' expected");
         read_field(csa);
         if (str2int(csa->field, &i) != 0)
            error(csa, "starting node number missing or invalid");
         if (!(1 <= i && i <= nv))
            error(csa, "starting node number %d out of range", i);
         read_field(csa);
         if (str2int(csa->field, &j) != 0)
            error(csa, "ending node number missing or invalid");
         if (!(1 <= j && j <= nv))
            error(csa, "ending node number %d out of range", j);
         read_field(csa);
         if (!(str2num(csa->field, &low) == 0 && low >= 0.0))
            error(csa, "lower bound of arc flow missing or invalid");
         check_int(csa, low);
         read_field(csa);
         if (!(str2num(csa->field, &cap) == 0 && cap >= low))
            error(csa, "upper bound of arc flow missing or invalid");
         check_int(csa, cap);
         read_field(csa);
         if (str2num(csa->field, &cost) != 0)
            error(csa, "per-unit cost of arc flow missing or invalid");
         check_int(csa, cost);
         a = glp_add_arc(G, i, j);
         if (a_low >= 0)
            memcpy((char *)a->data + a_low, &low, sizeof(double));
         if (a_cap >= 0)
            memcpy((char *)a->data + a_cap, &cap, sizeof(double));
         if (a_cost >= 0)
            memcpy((char *)a->data + a_cost, &cost, sizeof(double));
         end_of_line(csa);
      }
      xprintf("%d lines were read\n", csa->count);
done: if (ret) glp_erase_graph(G, G->v_size, G->a_size);
      if (csa->fp != NULL) xfclose(csa->fp);
      if (flag != NULL) xfree(flag);
      return ret;
}
Ejemplo n.º 17
0
static void *db_iodbc_open_int(TABDCA *dca, int mode, const char
   **sqllines)
{
   struct db_odbc    *sql;
   SQLRETURN          ret;
   SQLCHAR FAR       *dsn;
   SQLCHAR            info[256];
   SQLSMALLINT        colnamelen;
   SQLSMALLINT        nullable;
   SQLSMALLINT        scale;
   const char        *arg;
   int                narg;
   int                i, j;
   int                total;

   if (libodbc == NULL)
   {
      xprintf("No loader for shared ODBC library available\n");
      return NULL;
   }

   if (h_odbc == NULL)
   {
      h_odbc = xdlopen(libodbc);
      if (h_odbc == NULL)
      {  xprintf("unable to open library %s\n", libodbc);
         xprintf("%s\n", xerrmsg());
         return NULL;
      }
   }

   sql = (struct db_odbc *) xmalloc(sizeof(struct db_odbc));
   if (sql == NULL)
         return NULL;

   sql->mode  = mode;
   sql->hdbc  = NULL;
   sql->henv  = NULL;
   sql->hstmt = NULL;
   sql->query = NULL;
   narg = mpl_tab_num_args(dca);

   dsn = (SQLCHAR FAR *) mpl_tab_get_arg(dca, 2);
   /* allocate an environment handle */
   ret = dl_SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE,
      &(sql->henv));
   /* set attribute to enable application to run as ODBC 3.0
      application */
   ret = dl_SQLSetEnvAttr(sql->henv, SQL_ATTR_ODBC_VERSION,
      (void *) SQL_OV_ODBC3, 0);
   /* allocate a connection handle */
   ret = dl_SQLAllocHandle(SQL_HANDLE_DBC, sql->henv, &(sql->hdbc));
   /* connect */
   ret = dl_SQLDriverConnect(sql->hdbc, NULL, dsn, SQL_NTS, NULL, 0,
      NULL, SQL_DRIVER_COMPLETE);
   if (SQL_SUCCEEDED(ret))
   {  /* output information about data base connection */
      xprintf("Connected to ");
      dl_SQLGetInfo(sql->hdbc, SQL_DBMS_NAME, (SQLPOINTER)info,
         sizeof(info), NULL);
      xprintf("%s ", info);
      dl_SQLGetInfo(sql->hdbc, SQL_DBMS_VER, (SQLPOINTER)info,
         sizeof(info), NULL);
      xprintf("%s - ", info);
      dl_SQLGetInfo(sql->hdbc, SQL_DATABASE_NAME, (SQLPOINTER)info,
         sizeof(info), NULL);
      xprintf("%s\n", info);
   }
   else
   {  /* describe error */
      xprintf("Failed to connect\n");
      extract_error("SQLDriverConnect", sql->hdbc, SQL_HANDLE_DBC);
      dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
      dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
      xfree(sql);
      return NULL;
   }
   /* set AUTOCOMMIT on*/
   ret = dl_SQLSetConnectAttr(sql->hdbc, SQL_ATTR_AUTOCOMMIT,
      (SQLPOINTER)SQL_AUTOCOMMIT_ON, 0);
   /* allocate a statement handle */
   ret = dl_SQLAllocHandle(SQL_HANDLE_STMT, sql->hdbc, &(sql->hstmt));

   /* initialization queries */
   for(j = 0; sqllines[j+1] != NULL; j++)
   {
      sql->query = (SQLCHAR *) sqllines[j];
      xprintf("%s\n", sql->query);
      ret = dl_SQLExecDirect(sql->hstmt, sql->query, SQL_NTS);
      switch (ret)
      {
         case SQL_SUCCESS:
         case SQL_SUCCESS_WITH_INFO:
         case SQL_NO_DATA_FOUND:
            break;
         default:
            xprintf("db_iodbc_open: Query\n\"%s\"\nfailed.\n",
               sql->query);
            extract_error("SQLExecDirect", sql->hstmt, SQL_HANDLE_STMT);
            dl_SQLFreeHandle(SQL_HANDLE_STMT, sql->hstmt);
            dl_SQLDisconnect(sql->hdbc);
            dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
            dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
            xfree(sql);
            return NULL;
      }
      /* commit statement */
      dl_SQLEndTran(SQL_HANDLE_ENV, sql->henv, SQL_COMMIT);
   }

   if ( sql->mode == 'R' )
   {  sql->nf = mpl_tab_num_flds(dca);
      for(j = 0; sqllines[j] != NULL; j++)
         arg = sqllines[j];
      total = strlen(arg);
      if (total > 7 && 0 == strncmp(arg, "SELECT ", 7))
      {
         total = strlen(arg);
         sql->query = xmalloc( (total+1) * sizeof(char));
         strcpy (sql->query, arg);
      }
      else
      {
         sql->query = db_generate_select_stmt(dca);
      }
      xprintf("%s\n", sql->query);
      if (dl_SQLExecDirect(sql->hstmt, sql->query, SQL_NTS) !=
         SQL_SUCCESS)
      {
         xprintf("db_iodbc_open: Query\n\"%s\"\nfailed.\n", sql->query);
         extract_error("SQLExecDirect", sql->hstmt, SQL_HANDLE_STMT);
         dl_SQLFreeHandle(SQL_HANDLE_STMT, sql->hstmt);
         dl_SQLDisconnect(sql->hdbc);
         dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
         dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
         xfree(sql->query);
            xfree(sql);
         return NULL;
      }
      xfree(sql->query);
      /* determine number of result columns */
      ret = dl_SQLNumResultCols(sql->hstmt, &sql->nresultcols);
      total = sql->nresultcols;
      if (total > SQL_FIELD_MAX)
      {  xprintf("db_iodbc_open: Too many fields (> %d) in query.\n"
            "\"%s\"\n", SQL_FIELD_MAX, sql->query);
         dl_SQLFreeHandle(SQL_HANDLE_STMT, sql->hstmt);
         dl_SQLDisconnect(sql->hdbc);
         dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
         dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
         xfree(sql->query);
         return NULL;
      }
      for (i = 1; i <= total; i++)
      {  /* return a set of attributes for a column */
         ret = dl_SQLDescribeCol(sql->hstmt, (SQLSMALLINT) i,
            sql->colname[i], SQL_FDLEN_MAX,
            &colnamelen, &(sql->coltype[i]), &(sql->collen[i]), &scale,
            &nullable);
         sql->isnumeric[i] = is_numeric(sql->coltype[i]);
         /* bind columns to program vars, converting all types to CHAR*/
         if (sql->isnumeric[i])
         {  dl_SQLBindCol(sql->hstmt, i, SQL_DOUBLE, sql->data[i],
               SQL_FDLEN_MAX, &(sql->outlen[i]));
         } else
         {  dl_SQLBindCol(sql->hstmt, i, SQL_CHAR, sql->data[i],
               SQL_FDLEN_MAX, &(sql->outlen[i]));
         }
         for (j = sql->nf; j >= 1; j--)
         {  if (strcmp(mpl_tab_get_name(dca, j), sql->colname[i]) == 0)
            break;
         }
         sql->ref[i] = j;
      }
   }
   else if ( sql->mode == 'W' )
   {  for(j = 0; sqllines[j] != NULL; j++)
         arg = sqllines[j];
      if (  NULL != strchr(arg, '?') )
      {
         total = strlen(arg);
         sql->query = xmalloc( (total+1) * sizeof(char));
         strcpy (sql->query, arg);
         }
      else
      {
         sql->query = db_generate_insert_stmt(dca);
      }
      xprintf("%s\n", sql->query);
   }
   return sql;
}
Ejemplo n.º 18
0
int glp_write_mincost(glp_graph *G, int v_rhs, int a_low, int a_cap,
      int a_cost, const char *fname)
{     XFILE *fp;
      glp_vertex *v;
      glp_arc *a;
      int i, count = 0, ret;
      double rhs, low, cap, cost;
      if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double))
         xerror("glp_write_mincost: v_rhs = %d; invalid offset\n",
            v_rhs);
      if (a_low >= 0 && a_low > G->a_size - (int)sizeof(double))
         xerror("glp_write_mincost: a_low = %d; invalid offset\n",
            a_low);
      if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
         xerror("glp_write_mincost: a_cap = %d; invalid offset\n",
            a_cap);
      if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
         xerror("glp_write_mincost: a_cost = %d; invalid offset\n",
            a_cost);
      xprintf("Writing min-cost flow problem data to `%s'...\n",
         fname);
      fp = xfopen(fname, "w");
      if (fp == NULL)
      {  xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
         ret = 1;
         goto done;
      }
      xfprintf(fp, "c %s\n",
         G->name == NULL ? "unknown" : G->name), count++;
      xfprintf(fp, "p min %d %d\n", G->nv, G->na), count++;
      if (v_rhs >= 0)
      {  for (i = 1; i <= G->nv; i++)
         {  v = G->v[i];
            memcpy(&rhs, (char *)v->data + v_rhs, sizeof(double));
            if (rhs != 0.0)
               xfprintf(fp, "n %d %.*g\n", i, DBL_DIG, rhs), count++;
         }
      }
      for (i = 1; i <= G->nv; i++)
      {  v = G->v[i];
         for (a = v->out; a != NULL; a = a->t_next)
         {  if (a_low >= 0)
               memcpy(&low, (char *)a->data + a_low, sizeof(double));
            else
               low = 0.0;
            if (a_cap >= 0)
               memcpy(&cap, (char *)a->data + a_cap, sizeof(double));
            else
               cap = DBL_MAX;
            if (a_cost >= 0)
               memcpy(&cost, (char *)a->data + a_cost, sizeof(double));
            else
               cost = 0.0;
            xfprintf(fp, "a %d %d %.*g %.*g %.*g\n",
               a->tail->i, a->head->i, DBL_DIG, low, DBL_DIG, cap,
               DBL_DIG, cost), count++;
         }
      }
      xfprintf(fp, "c eof\n"), count++;
      xfflush(fp);
      if (xferror(fp))
      {  xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
         ret = 1;
         goto done;
      }
      xprintf("%d lines were written\n", count);
      ret = 0;
done: if (fp != NULL) xfclose(fp);
      return ret;
}
Ejemplo n.º 19
0
int glp_read_maxflow(glp_graph *G, int *_s, int *_t, int a_cap,
      const char *fname)
{     struct csa _csa, *csa = &_csa;
      glp_arc *a;
      int i, j, k, s, t, nv, na, ret = 0;
      double cap;
      if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
         xerror("glp_read_maxflow: a_cap = %d; invalid offset\n",
            a_cap);
      glp_erase_graph(G, G->v_size, G->a_size);
      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 maximum flow problem data from `%s'...\n",
         fname);
      csa->fp = xfopen(fname, "r");
      if (csa->fp == NULL)
      {  xprintf("Unable to open `%s' - %s\n", fname, xerrmsg());
         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, "max") != 0)
         error(csa, "wrong problem designator; `max' expected");
      read_field(csa);
      if (!(str2int(csa->field, &nv) == 0 && nv >= 2))
         error(csa, "number of nodes missing or invalid");
      read_field(csa);
      if (!(str2int(csa->field, &na) == 0 && na >= 0))
         error(csa, "number of arcs missing or invalid");
      xprintf("Flow network has %d node%s and %d arc%s\n",
         nv, nv == 1 ? "" : "s", na, na == 1 ? "" : "s");
      if (nv > 0) glp_add_vertices(G, nv);
      end_of_line(csa);
      /* read node descriptor lines */
      s = t = 0;
      for (;;)
      {  read_designator(csa);
         if (strcmp(csa->field, "n") != 0) break;
         read_field(csa);
         if (str2int(csa->field, &i) != 0)
            error(csa, "node number missing or invalid");
         if (!(1 <= i && i <= nv))
            error(csa, "node number %d out of range", i);
         read_field(csa);
         if (strcmp(csa->field, "s") == 0)
         {  if (s > 0)
               error(csa, "only one source node allowed");
            s = i;
         }
         else if (strcmp(csa->field, "t") == 0)
         {  if (t > 0)
               error(csa, "only one sink node allowed");
            t = i;
         }
         else
            error(csa, "wrong node designator; `s' or `t' expected");
         if (s > 0 && s == t)
            error(csa, "source and sink nodes must be distinct");
         end_of_line(csa);
      }
      if (s == 0)
         error(csa, "source node descriptor missing\n");
      if (t == 0)
         error(csa, "sink node descriptor missing\n");
      if (_s != NULL) *_s = s;
      if (_t != NULL) *_t = t;
      /* read arc descriptor lines */
      for (k = 1; k <= na; k++)
      {  if (k > 1) read_designator(csa);
         if (strcmp(csa->field, "a") != 0)
            error(csa, "wrong line designator; `a' expected");
         read_field(csa);
         if (str2int(csa->field, &i) != 0)
            error(csa, "starting node number missing or invalid");
         if (!(1 <= i && i <= nv))
            error(csa, "starting node number %d out of range", i);
         read_field(csa);
         if (str2int(csa->field, &j) != 0)
            error(csa, "ending node number missing or invalid");
         if (!(1 <= j && j <= nv))
            error(csa, "ending node number %d out of range", j);
         read_field(csa);
         if (!(str2num(csa->field, &cap) == 0 && cap >= 0.0))
            error(csa, "arc capacity missing or invalid");
         check_int(csa, cap);
         a = glp_add_arc(G, i, j);
         if (a_cap >= 0)
            memcpy((char *)a->data + a_cap, &cap, sizeof(double));
         end_of_line(csa);
      }
      xprintf("%d lines were read\n", csa->count);
done: if (ret) glp_erase_graph(G, G->v_size, G->a_size);
      if (csa->fp != NULL) xfclose(csa->fp);
      return ret;
}