⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glpmps01.c

📁 著名的大规模线性规划求解器源码GLPK.C语言版本,可以修剪.内有详细帮助文档.
💻 C
📖 第 1 页 / 共 4 页
字号:
            double lb, ub, rhs;            if (i == 0)               typx = LPX_FR, lb = ub = 0.0;            else               lpx_get_row_bnds(lp, i, &typx, &lb, &ub);            switch (typx)            {  case LPX_FR:                  rhs = (make_obj && i == 0 ? obj[0] : 0.0); break;               case LPX_LO:                  rhs = lb; break;               case LPX_UP:                  rhs = ub; break;               case LPX_DB:                  rhs = (ub > 0.0 ? lb : ub); break;               case LPX_FX:                  rhs = lb; break;               default:                  xassert(typx != typx);            }            if (rhs == 0.0) continue;            if (!flag) xfprintf(fp, "RHS\n"), flag = 1;            if (nl)                xfprintf(fp, "    %-8s  ", vname);            else                xfprintf(fp, "   ");            xfprintf(fp, "%-8s  %12s",               row_name(lp, i, rname), mps_numb(rhs, numb));            if (wide) nl = 1 - nl;            if (nl) xfprintf(fp, "\n");         }         if (!nl) xfprintf(fp, "\n");      }      xfree(obj);      /* write RANGES section */      flag = 0;      {  int nl = 1;         strcpy(vname, frei ? "" : "RNG1");         for (i = 1; i <= nrows; i++)         {  int typx;            double lb, ub, rng;            lpx_get_row_bnds(lp, i, &typx, &lb, &ub);            if (typx != LPX_DB) continue;            if (!flag) xfprintf(fp, "RANGES\n"), flag = 1;            if (nl)                xfprintf(fp, "    %-8s  ", vname);            else                xfprintf(fp, "   ");            rng = (ub > 0.0 ? ub - lb : lb - ub);            xfprintf(fp, "%-8s  %12s",               row_name(lp, i, rname), mps_numb(rng, numb));            if (wide) nl = 1 - nl;            if (nl) xfprintf(fp, "\n");         }         if (!nl) xfprintf(fp, "\n");      }      /* write BOUNDS section */      flag = 0;      {  strcpy(vname, frei ? "" : "BND1");         for (j = 1; j <= ncols; j++)         {  int typx;            double lb, ub;            lpx_get_col_bnds(lp, j, &typx, &lb, &ub);            if (typx == LPX_LO && lb == 0.0) continue;            if (!flag) xfprintf(fp, "BOUNDS\n"), flag = 1;            switch (typx)            {  case LPX_FR:                  xfprintf(fp, " FR %-8s  %-8s\n", vname,                     col_name(lp, j, cname));                  break;               case LPX_LO:                  xfprintf(fp, " LO %-8s  %-8s  %12s\n", vname,                     col_name(lp, j, cname), mps_numb(lb, numb));                  break;               case LPX_UP:                  xfprintf(fp, " MI %-8s  %-8s\n", vname,                     col_name(lp, j, cname));                  xfprintf(fp, " UP %-8s  %-8s  %12s\n", vname,                     col_name(lp, j, cname), mps_numb(ub, numb));                  break;               case LPX_DB:                  if (lb != 0.0)                  xfprintf(fp, " LO %-8s  %-8s  %12s\n", vname,                     col_name(lp, j, cname), mps_numb(lb, numb));                  xfprintf(fp, " UP %-8s  %-8s  %12s\n", vname,                     col_name(lp, j, cname), mps_numb(ub, numb));                  break;               case LPX_FX:                  xfprintf(fp, " FX %-8s  %-8s  %12s\n", vname,                     col_name(lp, j, cname), mps_numb(lb, numb));                  break;               default:                  xassert(typx != typx);            }         }      }      /* write ENDATA indicator card */      xfprintf(fp, "ENDATA\n");      /* close the output text file */      xfflush(fp);      if (xferror(fp))      {  xprintf("glp_write_mps: write error on `%s' - %s\n", fname,            strerror(errno));         goto fail;      }      xfclose(fp);      /* return to the calling program */      return 0;fail: /* the operation failed */      if (fp != NULL) xfclose(fp);      return 1;}/*------------------------------------------------------------------------ read_bas - read LP basis in fixed MPS format.---- *Synopsis*---- #include "glpmps.h"-- int read_bas(LPX *lp, char *fname);---- *Description*---- The routine read_bas reads LP basis in fixed MPS format from an-- input text file whose name is the character string fname.---- *Returns*---- If the operation was successful, the routine returns zero. Otherwise-- the routine prints an error message and returns non-zero. */int read_bas(LPX *lp, const char *fname){     struct dsa _dsa, *dsa = &_dsa;      int i, j;      dsa->lp = lp;      dsa->fname = fname;      dsa->fp = NULL;      dsa->count = 0;      dsa->obj = 0;      xprintf("lpx_read_bas: reading LP basis from `%s'...\n",         dsa->fname);      dsa->fp = xfopen(dsa->fname, "r");      if (dsa->fp == NULL)      {  xprintf("lpx_read_bas: unable to open `%s' - %s\n",            dsa->fname, strerror(errno));         goto fail;      }      lpx_create_index(dsa->lp);      /* read NAME indicator card */      if (read_card(dsa)) goto fail;      if (memcmp(dsa->card, "NAME ", 5) != 0)      {  xprintf("%s:%d: NAME indicator card missing\n", dsa->fname,            dsa->count);         goto fail;      }      /* build the "standard" basis of all slacks */      lpx_std_basis(dsa->lp);loop: /* read and split next data card */      if (read_card(dsa)) goto fail;      if (dsa->card[0] != ' ') goto fini;      if (split_card(dsa)) goto fail;      /* check indicator in field 1 */      if (!(strcmp(dsa->f1, "XL") == 0 || strcmp(dsa->f1, "XU") == 0 ||            strcmp(dsa->f1, "LL") == 0 || strcmp(dsa->f1, "UL") == 0))      {  xprintf("%s:%d: invalid indicator in field 1\n", dsa->fname,            dsa->count);         goto fail;      }      /* scan column name in field 2 */      if (dsa->f2[0] == '\0')      {  xprintf("%s:%d: missing column name in field 2\n", dsa->fname,            dsa->count);         goto fail;      }      j = lpx_find_col(dsa->lp, dsa->f2);      if (j == 0)      {  xprintf("%s:%d: column %s not found\n", dsa->fname, dsa->count,            dsa->f2);         goto fail;      }      /* process field 3 */      if (dsa->f1[0] == 'X')      {  /* scan row name in field 3 */         if (dsa->f3[0] == '\0')         {  xprintf("%s:%d: missing row name in field 3\n", dsa->fname,               dsa->count);            goto fail;         }         i = lpx_find_row(dsa->lp, dsa->f3);         if (i == 0)         {  xprintf("%s:%d: row %s not found\n", dsa->fname, dsa->count,               dsa->f3);            goto fail;         }      }      else      {  /* field 3 must be blank */         if (dsa->f3[0] != '\0')         {  xprintf("%s:%d: invalid data card; field 3 must be blank\n",               dsa->fname, dsa->count);            goto fail;         }         i = 0;      }      /* fields 4-6 must be blank */      if (!(dsa->f4[0] == '\0' && dsa->f5[0] == '\0' &&            dsa->f6[0] == '\0'))      {  xprintf("%s:%d: invalid data card; fields 4-6 must be blank\n",            dsa->fname, dsa->count);         goto fail;      }      /* apply a "patch" */      if (dsa->f1[0] == 'X')      {  lpx_set_row_stat(dsa->lp, i, dsa->f1[1] == 'L' ? LPX_NL :            LPX_NU);         lpx_set_col_stat(dsa->lp, j, LPX_BS);      }      else         lpx_set_col_stat(dsa->lp, j, dsa->f1[0] == 'L' ? LPX_NL :            LPX_NU);      goto loop;fini: /* check ENDATA indicator card */      if (memcmp(dsa->card, "ENDATA ", 7) != 0)      {  xprintf("%s:%d: ENDATA indicator card missing\n", dsa->fname,            dsa->count);         goto fail;      }      xprintf("lpx_read_bas: %d cards were read\n", dsa->count);      xfclose(dsa->fp);      lpx_delete_index(dsa->lp);      return 0;fail: if (dsa->fp != NULL) xfclose(dsa->fp);      lpx_delete_index(dsa->lp);      return 1;}/*------------------------------------------------------------------------ write_bas - write LP basis in fixed MPS format.---- *Synopsis*---- #include "glpmps.h"-- int write_bas(LPX *lp, char *fname);---- *Description*---- The routine write_bas writes current LP basis in fixed MPS format to-- an output text file whose name is the character string fname.---- *Returns*---- If the operation was successful, the routine returns zero. Otherwise-- the routine prints an error message and returns non-zero. */int write_bas(LPX *lp, const char *fname){     XFILE *fp;      int nrows, ncols, i, j, rtype, ctype, rstat, cstat;      char rname[8+1], cname[8+1];      xprintf("lpx_write_bas: writing LP basis to `%s'...\n", fname);      /* open the output text file */      fp = xfopen(fname, "w");      if (fp == NULL)      {  xprintf("lpx_write_bas: unable to create `%s' - %s\n", fname,            strerror(errno));         goto fail;      }      /* determine number of rows and number of columns */      nrows = lpx_get_num_rows(lp);      ncols = lpx_get_num_cols(lp);      /* the problem should contain at least one row and one column */      if (!(nrows > 0 && ncols > 0))         xerror("lpx_write_bas: problem has no rows/columns\n");      /* write comment cards (if required) */      if (lpx_get_int_parm(lp, LPX_K_MPSINFO))      {  int dir, status;         double obj;         const char *name;         /* problem name and statistics */         name = lpx_get_prob_name(lp);         if (name == NULL) name = "UNKNOWN";         xfprintf(fp, "* Problem:    %.31s\n", name);         xfprintf(fp, "* Rows:       %d\n", nrows);         xfprintf(fp, "* Columns:    %d\n", ncols);         xfprintf(fp, "* Non-zeros:  %d\n", lpx_get_num_nz(lp));         /* solution status */         status = lpx_get_status(lp);         xfprintf(fp, "* Status:     %s\n",            status == LPX_OPT    ? "OPTIMAL" :            status == LPX_FEAS   ? "FEASIBLE" :            status == LPX_INFEAS ? "INFEASIBLE (INTERMEDIATE)" :            status == LPX_NOFEAS ? "INFEASIBLE (FINAL)" :            status == LPX_UNBND  ? "UNBOUNDED" :            status == LPX_UNDEF  ? "UNDEFINED" : "???");         /* objective function */         name = lpx_get_obj_name(lp);         dir = lpx_get_obj_dir(lp);         obj = lpx_get_obj_val(lp);         xfprintf(fp, "* Objective:  %s%s%.10g %s\n",            name == NULL ? "" : name,            name == NULL ? "" : " = ", obj,            dir == LPX_MIN ? "(MINimum)" :            dir == LPX_MAX ? "(MAXimum)" : "(" "???" ")");         xfprintf(fp, "* Format:     Fixed MPS\n");         xfprintf(fp, "*\n");      }      /* write NAME indicator card */      {  const char *name = lpx_get_prob_name(lp);         if (name == NULL)            xfprintf(fp, "NAME\n");         else            xfprintf(fp, "NAME          %.8s\n", name);      }      /* write information about which columns should be made basic         and which rows should be made non-basic */      i = j = 0;      for (;;)      {  /* find a next non-basic row */         for (i++; i <= nrows; i++)         {  lpx_get_row_info(lp, i, &rstat, NULL, NULL);            if (rstat != LPX_BS) break;         }         /* find a next basic column */         for (j++; j <= ncols; j++)         {  lpx_get_col_info(lp, j, &cstat, NULL, NULL);            if (cstat == LPX_BS) break;         }         /* if all non-basic rows and basic columns have been written,            break the loop */         if (i > nrows && j > ncols) break;         /* since the basis is valid, there must be nor extra non-basic            rows nor extra basic columns */         xassert(i <= nrows && j <= ncols);         /* write the pair (basic column, non-basic row) */         lpx_get_row_bnds(lp, i, &rtype, NULL, NULL);         xfprintf(fp, " %s %-8s  %s\n",            (rtype == LPX_DB && rstat == LPX_NU) ? "XU" : "XL",            col_name(lp, j, cname), row_name(lp, i, rname));      }      /* write information about statuses of double-bounded non-basic         columns */      for (j = 1; j <= ncols; j++)      {  lpx_get_col_bnds(lp, j, &ctype, NULL, NULL);         lpx_get_col_info(lp, j, &cstat, NULL, NULL);         if (ctype == LPX_DB && cstat != LPX_BS)            xfprintf(fp, " %s %s\n",               cstat == LPX_NU ? "UL" : "LL", col_name(lp, j, cname));      }      /* write ENDATA indcator card */      xfprintf(fp, "ENDATA\n");      /* close the output text file */      xfflush(fp);      if (xferror(fp))      {  xprintf("lpx_write_bas: write error on `%s' - %s\n", fname,            strerror(errno));         goto fail;      }      xfclose(fp);      /* return to the calling program */      return 0;fail: /* the operation failed */      if (fp != NULL) xfclose(fp);      return 1;}/* eof */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -