help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-glpk] [PATCH] Output variable map for MPS files


From: Michael Clark
Subject: [Help-glpk] [PATCH] Output variable map for MPS files
Date: Mon, 15 Mar 2004 09:18:55 +0800
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040312 Debian/1.6-3

Hi All,

Here's a small patch I have been using to make it easier
to associate the autogenerated MPS variable names back to
the original model variables names when exporting MPS files.

It adds a '--wmap <filename>' option to glpsol that writes a
file with a map between the MPS var names and the model names.

This then makes it easy to map the variables in solver output
back to the original model variables where previously there
was no easily deterministic way to do this, at least that I
was aware of.

Regards,
Michael.
diff -ur glpk-4.4/examples/glpsol.c glpk-4.4-modified/examples/glpsol.c
--- glpk-4.4/examples/glpsol.c  2004-01-17 17:00:00.000000000 +0800
+++ glpk-4.4-modified/examples/glpsol.c 2004-03-15 09:03:02.000000000 +0800
@@ -96,6 +96,10 @@
 /* name of output text file, to which the problem should be written in
    MPS format; NULL means no MPS output */
 
+static char *out_map = NULL;
+/* name of output text file, to which the map of variable names to
+   autogenerated MPS variable names should be written to */
+
 static char *out_lpt = NULL;
 /* name of output text file, to which the problem should be written in
    CPLEX LP format; NULL means no CPLEX LP output */
@@ -194,6 +198,7 @@
          "columns");
       print("   --wmps filename   write problem to filename in MPS form"
          "at");
+      print("   --wmap filename   write MPS variable map");
       print("   --wlpt filename   write problem to filename in CPLEX LP"
          " format");
       print("   --wtxt filename   write problem to filename in plain te"
@@ -374,6 +379,18 @@
             }
             out_mps = argv[k];
          }
+         else if (p("--wmap"))
+         {  k++;
+            if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
+            {  print("No variable map output file specified");
+               exit(EXIT_FAILURE);
+            }
+            if (out_map != NULL)
+            {  print("Only one variable map output file allowed");
+               exit(EXIT_FAILURE);
+            }
+            out_map = argv[k];
+         }
          else if (p("--wlpt"))
          {  k++;
             if (k == argc || argv[k][0] == '\0' || argv[k][0] == '-')
@@ -466,6 +483,7 @@
       if (out_sol != NULL) remove(out_sol);
       if (out_bnds != NULL) remove(out_bnds);
       if (out_mps != NULL) remove(out_mps);
+      if (out_map != NULL) remove(out_map);
       if (out_lpt != NULL) remove(out_lpt);
       if (out_txt != NULL) remove(out_txt);
       /* read problem from the input file */
@@ -508,6 +526,14 @@
       }
       /* change optimization direction (if required) */
       if (dir != 0) lpx_set_obj_dir(lp, dir);
+      /* write MPS variable map */
+      if (out_map != NULL)
+      {  ret = lpx_write_map(lp, out_map);
+         if (ret != 0)
+         {  print("Unable to write variable map");
+            exit(EXIT_FAILURE);
+         }
+      }
       /* write problem in MPS format (if required) */
       if (out_mps != NULL)
       {  lpx_set_int_parm(lp, LPX_K_MPSORIG, orig);
diff -ur glpk-4.4/src/glplpx8a.c glpk-4.4-modified/src/glplpx8a.c
--- glpk-4.4/src/glplpx8a.c     2004-01-17 17:00:00.000000000 +0800
+++ glpk-4.4-modified/src/glplpx8a.c    2004-03-15 09:03:25.000000000 +0800
@@ -53,6 +53,11 @@
 -- If no error occurred, the routine returns a pointer to the created
 -- problem object. Otherwise the routine returns NULL. */
 
+static char *_row_name_mps(LPX *lp, int i, char name[8+1]);
+static char *_col_name_mps(LPX *lp, int j, char name[8+1]);
+static char *_row_name_lpx(LPX *lp, int i, char name[255+1]);
+static char *_col_name_lpx(LPX *lp, int j, char name[255+1]);
+
 struct mps_info
 {     /* transit information passed to the routine mps_mat */
       MPS *mps;
@@ -303,7 +308,10 @@
 -- If the operation was successful, the routine returns zero. Otherwise
 -- the routine prints an error message and returns non-zero. */
 
-static char *row_name(LPX *lp, int i, char rname[8+1])
+#define row_name _row_name_mps
+#define col_name _col_name_mps
+
+static char *_row_name_mps(LPX *lp, int i, char rname[8+1])
 {     char *str = NULL;
       if (lpx_get_int_parm(lp, LPX_K_MPSORIG))
       {  if (i == 0)
@@ -319,7 +327,7 @@
       return rname;
 }
 
-static char *col_name(LPX *lp, int j, char cname[8+1])
+static char *_col_name_mps(LPX *lp, int j, char cname[8+1])
 {     char *str = NULL;
       if (lpx_get_int_parm(lp, LPX_K_MPSORIG))
       {  str = lpx_get_col_name(lp, j);
@@ -357,6 +365,49 @@
       return NULL; /* make the compiler happy */
 }
 
+int lpx_write_map(LPX *lp, char *fname)
+{     FILE *fp;
+      int nrows, ncols, i, j;
+      char mps_rname[8+1], mps_cname[8+1];
+      char full_rname[255+1], full_cname[255+1];
+      print("lpx_write_map: writing variable map to `%s'...", fname);
+      /* open the output text file */
+      fp = ufopen(fname, "w");
+      if (fp == NULL)
+      {  print("lpx_write_map: can't create `%s' - %s", 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);
+      /* write ROWS map */
+      for (i = 1; i <= nrows; i++)
+      {   fprintf(fp, "%s %s\n",
+                 _row_name_mps(lp, i, mps_rname),
+                 _row_name_lpx(lp, i, full_rname));
+      }
+      /* write COLUMNS map */
+      for (j = 1; j <= ncols; j++)
+      {  fprintf(fp, "%s %s\n",
+                _col_name_mps(lp, j, mps_cname),
+                _col_name_lpx(lp, j, full_cname));
+      }
+      /* close the output text file */
+      fflush(fp);
+      if (ferror(fp))
+      {  print("lpx_write_map: can't write to `%s' - %s", fname,
+            strerror(errno));
+         goto fail;
+      }
+      ufclose(fp);
+      /* return to the calling program */
+      return 0;
+fail: /* the operation failed */
+      if (fp != NULL) ufclose(fp);
+      return 1;
+}
+
 int lpx_write_mps(LPX *lp, char *fname)
 {     FILE *fp;
       int wide = lpx_get_int_parm(lp, LPX_K_MPSWIDE);
@@ -1016,10 +1067,12 @@
 -- If the operation is successful, the routine returns zero. Otherwise
 -- the routine prints an error message and returns non-zero. */
 
-#define row_name row_name1
-#define col_name col_name1
+#undef row_name
+#undef col_name
+#define row_name _row_name_lpx
+#define col_name _col_name_lpx
 
-static char *row_name(LPX *lp, int i, char name[255+1])
+static char *_row_name_lpx(LPX *lp, int i, char name[255+1])
 {     char *str;
       str = lpx_get_row_name(lp, i);
       if (str == NULL)
@@ -1029,7 +1082,7 @@
       return name;
 }
 
-static char *col_name(LPX *lp, int j, char name[255+1])
+static char *_col_name_lpx(LPX *lp, int j, char name[255+1])
 {     char *str;
       str = lpx_get_col_name(lp, j);
       if (str == NULL)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]