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

driver2.cpp

00001 // Copyright (C) 2002, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 
00004 #include "ClpSimplex.hpp"
00005 #include "ClpPresolve.hpp"
00006 #include "CoinHelperFunctions.hpp"
00007 #include "CoinTime.hpp"
00008 #include "ClpDualRowSteepest.hpp"
00009 #include "ClpPrimalColumnSteepest.hpp"
00010 #include <iomanip>
00011 // This driver shows how to trap messages - this is just as in unitTest.cpp
00012 // ****** THis code is similar to MyMessageHandler.hpp and MyMessagehandler.cpp
00013 #include "CoinMessageHandler.hpp"
00014 
00022 class ClpSimplex;
00023 
00024 class MyMessageHandler : public CoinMessageHandler {
00025   
00026 public:
00029   virtual int print();
00031 
00033 
00034   const ClpSimplex * model() const;
00035   void setModel(ClpSimplex * model);
00037 
00041   MyMessageHandler();
00043   MyMessageHandler(ClpSimplex * model,
00044                            FILE * userPointer=NULL);
00046   virtual ~MyMessageHandler();
00048 
00052   MyMessageHandler(const MyMessageHandler&);
00054   MyMessageHandler(const CoinMessageHandler&);
00055   
00056   MyMessageHandler& operator=(const MyMessageHandler&);
00058   virtual CoinMessageHandler * clone() const ;
00060    
00061     
00062 protected:
00066 
00067   ClpSimplex * model_;
00069 };
00070 
00071 
00072 //#############################################################################
00073 // Constructors / Destructor / Assignment
00074 //#############################################################################
00075 
00076 //-------------------------------------------------------------------
00077 // Default Constructor 
00078 //-------------------------------------------------------------------
00079 MyMessageHandler::MyMessageHandler () 
00080   : CoinMessageHandler(),
00081     model_(NULL)
00082 {
00083 }
00084 
00085 //-------------------------------------------------------------------
00086 // Copy constructor 
00087 //-------------------------------------------------------------------
00088 MyMessageHandler::MyMessageHandler (const MyMessageHandler & rhs) 
00089 : CoinMessageHandler(rhs),
00090     model_(rhs.model_)
00091 {  
00092 }
00093 
00094 MyMessageHandler::MyMessageHandler (const CoinMessageHandler & rhs) 
00095   : CoinMessageHandler(),
00096     model_(NULL)
00097 {  
00098 }
00099 
00100 // Constructor with pointer to model
00101 MyMessageHandler::MyMessageHandler(ClpSimplex * model,
00102                FILE * userPointer)
00103   : CoinMessageHandler(),
00104     model_(model)
00105 {
00106 }
00107 
00108 //-------------------------------------------------------------------
00109 // Destructor 
00110 //-------------------------------------------------------------------
00111 MyMessageHandler::~MyMessageHandler ()
00112 {
00113 }
00114 
00115 //----------------------------------------------------------------
00116 // Assignment operator 
00117 //-------------------------------------------------------------------
00118 MyMessageHandler &
00119 MyMessageHandler::operator=(const MyMessageHandler& rhs)
00120 {
00121   if (this != &rhs) {
00122     CoinMessageHandler::operator=(rhs);
00123     model_ = rhs.model_;
00124   }
00125   return *this;
00126 }
00127 //-------------------------------------------------------------------
00128 // Clone
00129 //-------------------------------------------------------------------
00130 CoinMessageHandler * MyMessageHandler::clone() const
00131 {
00132   return new MyMessageHandler(*this);
00133 }
00134 // Print out values from first 20 messages
00135 static int times=0;
00136 int 
00137 MyMessageHandler::print()
00138 {
00139   // You could have added a callback flag if you had wanted - see Clp_C_Interface.c
00140   times++;
00141   if (times<=20) {
00142     int messageNumber = currentMessage().externalNumber();
00143     if (currentSource()!="Clp")
00144       messageNumber += 1000000;
00145     int i;
00146     int nDouble=numberDoubleFields();
00147     printf("%d doubles - ",nDouble);
00148     for (i=0;i<nDouble;i++)
00149       printf("%g ",doubleValue(i));
00150     printf("\n");;
00151     int nInt=numberIntFields();
00152     printf("%d ints - ",nInt);
00153     for (i=0;i<nInt;i++)
00154       printf("%d ",intValue(i));
00155     printf("\n");;
00156     int nString=numberStringFields();
00157     printf("%d strings - ",nString);
00158     for (i=0;i<nString;i++)
00159       printf("%s ",stringValue(i).c_str());
00160     printf("\n");;
00161   }
00162   return CoinMessageHandler::print();
00163 }
00164 const ClpSimplex *
00165 MyMessageHandler::model() const
00166 {
00167   return model_;
00168 }
00169 void 
00170 MyMessageHandler::setModel(ClpSimplex * model)
00171 {
00172   model_ = model;
00173 }
00174 
00175 int main (int argc, const char *argv[])
00176 {
00177   ClpSimplex  model;
00178   // Message handler
00179   MyMessageHandler messageHandler(&model);
00180   std::cout<<"Testing derived message handler"<<std::endl;
00181   model.passInMessageHandler(&messageHandler);
00182   int status;
00183   // Keep names when reading an mps file
00184   if (argc<2)
00185     status=model.readMps("../../Mps/Sample/p0033.mps",true);
00186   else
00187     status=model.readMps(argv[1],true);
00188 
00189   if (status) {
00190     fprintf(stderr,"Bad readMps %s\n",argv[1]);
00191     fprintf(stdout,"Bad readMps %s\n",argv[1]);
00192     exit(1);
00193   }
00194 
00195   double time1 = CoinCpuTime();
00196   /*
00197     This driver shows how to do presolve.by hand (rather than with initialSolve)
00198   */
00199   ClpSimplex * model2;
00200   ClpPresolve pinfo;
00201   int numberPasses=5; // can change this
00202   /* Use a tolerance of 1.0e-8 for feasibility, treat problem as 
00203      not being integer, do "numberpasses" passes and throw away names
00204      in presolved model */
00205   model2 = pinfo.presolvedModel(model,1.0e-8,false,numberPasses,false);
00206   if (!model2) {
00207     fprintf(stderr,"ClpPresolve says %s is infeasible with tolerance of %g\n",
00208             argv[1],1.0e-8);
00209     fprintf(stdout,"ClpPresolve says %s is infeasible with tolerance of %g\n",
00210             argv[1],1.0e-8);
00211     // model was infeasible - maybe try again with looser tolerances
00212     model2 = pinfo.presolvedModel(model,1.0e-7,false,numberPasses,false);
00213     if (!model2) {
00214       fprintf(stderr,"ClpPresolve says %s is infeasible with tolerance of %g\n",
00215               argv[1],1.0e-7);
00216       fprintf(stdout,"ClpPresolve says %s is infeasible with tolerance of %g\n",
00217               argv[1],1.0e-7);
00218       exit(2);
00219     }
00220   }
00221   // change factorization frequency from 200
00222   model2->setFactorizationFrequency(100+model2->numberRows()/50);
00223   if (argc<3 ||!strstr(argv[2],"primal")) {
00224     // Use the dual algorithm unless user said "primal"
00225     /* faster if bounds tightened as then dual can flip variables
00226         to other bound to stay dual feasible.  We can trash the bounds as 
00227         this model is going to be thrown away
00228     */
00229     int numberInfeasibilities = model2->tightenPrimalBounds();
00230     if (numberInfeasibilities)
00231       std::cout<<"** Analysis indicates model infeasible"
00232                <<std::endl;
00233     model2->crash(1000.0,2);
00234     ClpDualRowSteepest steep(1);
00235     model2->setDualRowPivotAlgorithm(steep);
00236     model2->dual();
00237   } else {
00238     ClpPrimalColumnSteepest steep(1);
00239     model2->setPrimalColumnPivotAlgorithm(steep);
00240     model2->primal();
00241   }
00242   pinfo.postsolve(true);
00243 
00244   int numberIterations=model2->numberIterations();;
00245   delete model2;
00246   /* After this postsolve model should be optimal.
00247      We can use checkSolution and test feasibility */
00248   model.checkSolution();
00249   if (model.numberDualInfeasibilities()||
00250       model.numberPrimalInfeasibilities()) 
00251     printf("%g dual %g(%d) Primal %g(%d)\n",
00252            model.objectiveValue(),
00253            model.sumDualInfeasibilities(),
00254            model.numberDualInfeasibilities(),
00255            model.sumPrimalInfeasibilities(),
00256            model.numberPrimalInfeasibilities());
00257   // But resolve for safety
00258   model.primal(1);
00259 
00260   numberIterations += model.numberIterations();;
00261   // for running timing tests
00262   std::cout<<argv[1]<<" Objective "<<model.objectiveValue()<<" took "<<
00263     numberIterations<<" iterations and "<<
00264     CoinCpuTime()-time1<<" seconds"<<std::endl;
00265 
00266   std::string modelName;
00267   model.getStrParam(ClpProbName,modelName);
00268   std::cout<<"Model "<<modelName<<" has "<<model.numberRows()<<" rows and "<<
00269     model.numberColumns()<<" columns"<<std::endl;
00270 
00271   // remove this to print solution
00272 
00273   exit(0);
00274 
00275   /*
00276     Now to print out solution.  The methods used return modifiable
00277     arrays while the alternative names return const pointers -
00278     which is of course much more virtuous.
00279 
00280     This version just does non-zero columns
00281   
00282    */
00283 #if 0
00284   int numberRows = model.numberRows();
00285 
00286   // Alternatively getRowActivity()
00287   double * rowPrimal = model.primalRowSolution();
00288   // Alternatively getRowPrice()
00289   double * rowDual = model.dualRowSolution();
00290   // Alternatively getRowLower()
00291   double * rowLower = model.rowLower();
00292   // Alternatively getRowUpper()
00293   double * rowUpper = model.rowUpper();
00294   // Alternatively getRowObjCoefficients()
00295   double * rowObjective = model.rowObjective();
00296     
00297   // If we have not kept names (parameter to readMps) this will be 0
00298   assert(model.lengthNames());
00299 
00300   // Row names
00301   const std::vector<std::string> * rowNames = model.rowNames();
00302 
00303 
00304   int iRow;
00305 
00306   std::cout<<"                       Primal          Dual         Lower         Upper        (Cost)"
00307            <<std::endl;
00308 
00309   for (iRow=0;iRow<numberRows;iRow++) {
00310     double value;
00311     std::cout<<std::setw(6)<<iRow<<" "<<std::setw(8)<<(*rowNames)[iRow];
00312     value = rowPrimal[iRow];
00313     if (fabs(value)<1.0e5)
00314       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00315     else
00316       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00317     value = rowDual[iRow];
00318     if (fabs(value)<1.0e5)
00319       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00320     else
00321       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00322     value = rowLower[iRow];
00323     if (fabs(value)<1.0e5)
00324       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00325     else
00326       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00327     value = rowUpper[iRow];
00328     if (fabs(value)<1.0e5)
00329       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00330     else
00331       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00332     if (rowObjective) {
00333       value = rowObjective[iRow];
00334       if (fabs(value)<1.0e5)
00335         std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00336       else
00337         std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00338     }
00339     std::cout<<std::endl;
00340   }
00341 #endif
00342   std::cout<<"--------------------------------------"<<std::endl;
00343 
00344   // Columns
00345 
00346   int numberColumns = model.numberColumns();
00347 
00348   // Alternatively getColSolution()
00349   double * columnPrimal = model.primalColumnSolution();
00350   // Alternatively getReducedCost()
00351   double * columnDual = model.dualColumnSolution();
00352   // Alternatively getColLower()
00353   double * columnLower = model.columnLower();
00354   // Alternatively getColUpper()
00355   double * columnUpper = model.columnUpper();
00356   // Alternatively getObjCoefficients()
00357   double * columnObjective = model.objective();
00358     
00359   // If we have not kept names (parameter to readMps) this will be 0
00360   assert(model.lengthNames());
00361 
00362   // Column names
00363   const std::vector<std::string> * columnNames = model.columnNames();
00364 
00365 
00366   int iColumn;
00367   
00368   std::cout<<"                       Primal          Dual         Lower         Upper          Cost"
00369            <<std::endl;
00370 
00371   for (iColumn=0;iColumn<numberColumns;iColumn++) {
00372     double value;
00373     value = columnPrimal[iColumn];
00374     if (fabs(value)>1.0e-8) {
00375       std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<(*columnNames)[iColumn];
00376       if (fabs(value)<1.0e5)
00377         std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00378       else
00379         std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00380       value = columnDual[iColumn];
00381       if (fabs(value)<1.0e5)
00382         std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00383       else
00384         std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00385       value = columnLower[iColumn];
00386       if (fabs(value)<1.0e5)
00387         std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00388       else
00389         std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00390       value = columnUpper[iColumn];
00391       if (fabs(value)<1.0e5)
00392         std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00393       else
00394         std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00395       value = columnObjective[iColumn];
00396       if (fabs(value)<1.0e5)
00397         std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00398       else
00399         std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00400       
00401       std::cout<<std::endl;
00402     }
00403   }
00404   std::cout<<"--------------------------------------"<<std::endl;
00405 
00406   return 0;
00407 }    

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