Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

network.cpp

00001 // Copyright (C) 2002, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 
00004 
00005 // The point of this example is to show how to create a model without
00006 // using mps files.
00007 
00008 // This reads a network problem created by netgen which can be
00009 // downloaded from www.netlib.org/lp/generators/netgen
00010 // This is a generator due to Darwin Klingman
00011 
00012 #include "ClpSimplex.hpp"
00013 #include "ClpFactorization.hpp"
00014 #include "ClpNetworkMatrix.hpp"
00015 #include <stdio.h>
00016 #include <assert.h>
00017 #include <cmath>
00018 
00019 int main (int argc, const char *argv[])
00020 {
00021   int numberColumns;
00022   int numberRows;
00023   
00024   FILE * fp;
00025   if ( argc>1 ) {
00026     fp = fopen(argv[1],"r");
00027     if (!fp) {
00028       fprintf(stderr,"Unable to open file %s\n",argv[1]);
00029       exit(1);
00030     }
00031   } else {
00032     fp = fopen("input.130","r");
00033     if (!fp) {
00034       fprintf(stderr,"Unable to open file input.l30 in Samples directory\n");
00035       exit(1);
00036     }
00037   }
00038   int problem;
00039   char temp[100];
00040   // read and skip 
00041   fscanf(fp,"%s",temp);
00042   assert (!strcmp(temp,"BEGIN"));
00043   fscanf(fp,"%*s %*s %d %d %*s %*s %d %*s",&problem, &numberRows, 
00044          &numberColumns);
00045   // scan down to SUPPLY
00046   while (fgets(temp,100,fp)) {
00047     if (!strncmp(temp,"SUPPLY",6))
00048       break;
00049   }
00050   if (strncmp(temp,"SUPPLY",6)) {
00051     fprintf(stderr,"Unable to find SUPPLY\n");
00052     exit(2);
00053   }
00054   // get space for rhs
00055   double * lower = new double[numberRows];
00056   double * upper = new double[numberRows];
00057   int i;
00058   for (i=0;i<numberRows;i++) {
00059     lower[i]=0.0;
00060     upper[i]=0.0;
00061   }
00062   // ***** Remember to convert to C notation
00063   while (fgets(temp,100,fp)) {
00064     int row;
00065     int value;
00066     if (!strncmp(temp,"ARCS",4))
00067       break;
00068     sscanf(temp,"%d %d",&row,&value);
00069     upper[row-1]=-value;
00070     lower[row-1]=-value;
00071   }
00072   if (strncmp(temp,"ARCS",4)) {
00073     fprintf(stderr,"Unable to find ARCS\n");
00074     exit(2);
00075   }
00076   // number of columns may be underestimate
00077   int * head = new int[2*numberColumns];
00078   int * tail = new int[2*numberColumns];
00079   int * ub = new int[2*numberColumns];
00080   int * cost = new int[2*numberColumns];
00081   // ***** Remember to convert to C notation
00082   numberColumns=0;
00083   while (fgets(temp,100,fp)) {
00084     int iHead;
00085     int iTail;
00086     int iUb;
00087     int iCost;
00088     if (!strncmp(temp,"DEMAND",6))
00089       break;
00090     sscanf(temp,"%d %d %d %d",&iHead,&iTail,&iCost,&iUb);
00091     iHead--;
00092     iTail--;
00093     head[numberColumns]=iHead;
00094     tail[numberColumns]=iTail;
00095     ub[numberColumns]=iUb;
00096     cost[numberColumns]=iCost;
00097     numberColumns++;
00098   }
00099   if (strncmp(temp,"DEMAND",6)) {
00100     fprintf(stderr,"Unable to find DEMAND\n");
00101     exit(2);
00102   }
00103   // ***** Remember to convert to C notation
00104   while (fgets(temp,100,fp)) {
00105     int row;
00106     int value;
00107     if (!strncmp(temp,"END",3))
00108       break;
00109     sscanf(temp,"%d %d",&row,&value);
00110     upper[row-1]=value;
00111     lower[row-1]=value;
00112   }
00113   if (strncmp(temp,"END",3)) {
00114     fprintf(stderr,"Unable to find END\n");
00115     exit(2);
00116   }
00117   printf("Problem %d has %d rows and %d columns\n",problem,
00118          numberRows,numberColumns);
00119   fclose(fp);
00120   ClpSimplex  model;
00121   // now build model - we have rhs so build columns - two elements
00122   // per column
00123 
00124   double * objective =new double[numberColumns];
00125   double * lowerColumn = new double[numberColumns];
00126   double * upperColumn = new double[numberColumns];
00127 
00128   double * element = new double [2*numberColumns];
00129   int * start = new int[numberColumns+1];
00130   int * row = new int[2*numberColumns];
00131   start[numberColumns]=2*numberColumns;
00132   for (i=0;i<numberColumns;i++) {
00133     start[i]=2*i;
00134     element[2*i]=-1.0;
00135     element[2*i+1]=1.0;
00136     row[2*i]=head[i];
00137     row[2*i+1]=tail[i];
00138     lowerColumn[i]=0.0;
00139     upperColumn[i]=ub[i];
00140     objective[i]=cost[i];
00141   }
00142     // Create Packed Matrix
00143   CoinPackedMatrix matrix;
00144   int * lengths = NULL;
00145   matrix.assignMatrix(true,numberRows,numberColumns,
00146                       2*numberColumns,element,row,start,lengths);
00147   ClpNetworkMatrix network(matrix);
00148   // load model
00149 
00150   model.loadProblem(network,
00151                     lowerColumn,upperColumn,objective,
00152                     lower,upper);
00153 
00154   delete [] lower;
00155   delete [] upper;
00156   delete [] head;
00157   delete [] tail;
00158   delete [] ub;
00159   delete [] cost;
00160   delete [] objective;
00161   delete [] lowerColumn;
00162   delete [] upperColumn;
00163   delete [] element;
00164   delete [] start;
00165   delete [] row;
00166 
00167   /* The confusing flow below is in to exercise both dual and primal
00168      when ClpNetworkMatrix storage used.
00169 
00170      For practical use just one call e.g. model.dual(); would be used.
00171 
00172      If network then factorization scheme is changed
00173      to be much faster.
00174      
00175      Still not as fast as a real network code, but more flexible
00176   */
00177   model.factorization()->maximumPivots(200+model.numberRows()/100);
00178   model.factorization()->maximumPivots(1000);
00179   //model.factorization()->maximumPivots(1);
00180   if (model.numberRows()<50)
00181     model.messageHandler()->setLogLevel(63);
00182   model.dual();
00183   model.setOptimizationDirection(-1);
00184   //model.messageHandler()->setLogLevel(63);
00185   model.primal();
00186   model.setOptimizationDirection(1);
00187   model.primal();
00188   return 0;
00189 }    

Generated on Wed Dec 3 14:37:36 2003 for CLP by doxygen 1.3.5