mt_test.ex.c

Go to the documentation of this file.
00001 /* MTgomory "multi tableau gomory cut" provides an implementation for gomory
00002  * cuts derived from multiple tableau rows in the spirit of the work of
00003  * Andersen et al (IPCO 2007), Cornuejols (es presented in George Nemhauser
00004  * Birthday Conference in Atlanta 2007) and Gomory (presented in the same
00005  * conference).
00006  *
00007  * Copyright (C) 2007 Daniel Espinoza.
00008  * 
00009  * This library is free software; you can redistribute it and/or modify it
00010  * under the terms of the GNU Lesser General Public License as published by the
00011  * Free Software Foundation; either version 2.1 of the License, or (at your
00012  * option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful, but 
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00016  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00017  * License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with this library; if not, write to the Free Software Foundation,
00021  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00022  * */
00023 /* ========================================================================= */
00024 #include <getopt.h>
00025 #include <math.h>
00026 #include "eg_mem.h"
00027 #include "eg_macros.h"
00028 #include "mt_gomory.h"
00029 #include "mt_tableau.h"
00030 #include "eg_io.h"
00031 /** @file 
00032  * @ingroup MTgomory */
00033 /** @addtogroup MTgomory */
00034 /** @{ */
00035 /* ========================================================================= */
00036 /** @brief global option variables */
00037 static int mt_mode=0;
00038 static char tb_file[1024]="tableau.txt";
00039 /* ========================================================================= */
00040 /** @brief basic usage function */
00041 int static usage(char*prog)
00042 {
00043   fprintf(stderr,"Usage:\n%s [options]",prog);
00044   fprintf(stderr,"Options:\n");
00045   fprintf(stderr,"  -m n  Mode of testing: 0 for tableau reading testing,\n");
00046   fprintf(stderr,"        Default: %d\n",mt_mode);
00047   fprintf(stderr,"  -t f  File containing a tableau row, Default %s\n",tb_file);
00048   return 0;
00049 }
00050 /* ========================================================================= */
00051 /** @brief parsing code */
00052 int static parse_args(int argc,char**argv)
00053 {
00054   int c;
00055   while((c=getopt(argc,argv,"f:m:"))!=EOF)
00056   {
00057     switch(c)
00058     {
00059       case 'f':
00060         snprintf(tb_file,1023,"%s",optarg);
00061         break;
00062       case 'm':
00063         mt_mode = atoi(optarg);
00064         break;
00065       default:
00066         usage(argv[0]);
00067         exit(1);
00068     }
00069   }
00070   /* test consistency if arguments */
00071   if(mt_mode != 0)
00072   {
00073     usage(argv[0]);
00074     exit(1);
00075   }
00076   return 0;
00077 }
00078 /* ========================================================================= */
00079 /** @brief test #MTgomory_cut function, by reading from a file a tableau, and
00080  * giving it to the function, print the resulting cut, and return */
00081 int static test_MTgomory_cut(void)
00082 {
00083   int rval = 0;
00084   register int i;
00085   int*rowbeg = 0, *rowind = 0, nrows = 0, ncols = 0;
00086   double* rowval = 0, *f=0, *cutval=0, *work=0, *base = 0;
00087   FILE*input = EGsfopen(tb_file,"r");
00088   /* now read the file */
00089   rval = MTreadRTableau( input, &nrows, &ncols, &rowval, &rowind, 
00090                          &rowbeg, &f);
00091   CHECKRVALG(rval,CLEANUP);
00092   /* now do the cutting */
00093   cutval = EGsMalloc(double,ncols);
00094   work = EGsMalloc(double,ncols+1);
00095   base = EGsMalloc(double,nrows);
00096   for( i = nrows ; i-- ; ) base[i] = 1.0;
00097   rval = MTgomoryCut( nrows, ncols, rowval, rowind, rowbeg, f, base,
00098                       cutval, work);
00099   CHECKRVALG(rval,CLEANUP);
00100   /* now we display the cut */
00101   fprintf(stdout, "The cut is:\n");
00102   for( i = 0 ; i < ncols ; i++ )
00103     fprintf(stdout,"%s %.5lf x_%d ", (cutval[i]>=0) ? "+":"-", 
00104             fabs(cutval[i]), i);
00105   fprintf(stdout," >= 1\n");
00106   CLEANUP:
00107   EGfree(rowbeg);
00108   EGfree(rowind);
00109   EGfree(rowval);
00110   EGfree(f);
00111   EGfree(cutval);
00112   EGfree(work);
00113   EGfree(base);
00114   fclose(input);
00115   return rval;
00116 }
00117 /* ========================================================================= */
00118 /** @brief a simple example on its usage */
00119 int main(int argc,char**argv)
00120 {
00121   int rval = 0;
00122   parse_args(argc,argv);
00123   switch(mt_mode)
00124   {
00125     case 0:
00126       rval = test_MTgomory_cut();
00127       CHECKRVALG(rval,CLEANUP);
00128       break;
00129   }
00130   CLEANUP:
00131   return rval;
00132 }
00133 
00134 /* ========================================================================= */
00135 /** @} */
00136 /* end of mt_test.ex.c */

Generated on Wed Nov 21 09:38:13 2007 for MTgomory by  doxygen 1.4.6