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

defaults.cpp

00001 // Copyright (C) 2002, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 
00004 #include "ClpSimplex.hpp"
00005 #include "ClpPrimalColumnSteepest.hpp"
00006 #include "ClpDualRowSteepest.hpp"
00007 #include <iomanip>
00008 
00009 int main (int argc, const char *argv[])
00010 {
00011   ClpSimplex  model;
00012   int status;
00013   // Keep names
00014   if (argc<2)
00015     status=model.readMps("../../Mps/Sample/p0033.mps",true);
00016   else
00017     status=model.readMps(argv[1],true);
00018   /*
00019     This driver is similar to minimum.cpp, but it
00020     sets all parameter values to their defaults.  The purpose of this
00021     is to give a list of most of the methods that the end user will need.
00022 
00023     There are also some more methods as for OsiSolverInterface e.g.
00024     some loadProblem methods and deleteRows and deleteColumns.
00025 
00026     Often two methods do the same thing, one having a name I like
00027     while the other adheres to OsiSolverInterface standards
00028   */
00029 
00030   // Use exact devex ( a variant of steepest edge)
00031   ClpPrimalColumnSteepest primalSteepest;
00032   model.setPrimalColumnPivotAlgorithm(primalSteepest);
00033 
00034 
00035   int integerValue;
00036   double value;
00037 
00038   // Infeasibility cost
00039   value = model.infeasibilityCost();
00040   std::cout<<"Default value of infeasibility cost is "<<value<<std::endl;
00041   model.setInfeasibilityCost(value);
00042 
00043   if (!status) {
00044     model.primal();
00045   }
00046   // Number of rows and columns - also getNumRows, getNumCols
00047   std::string modelName;
00048   model.getStrParam(ClpProbName,modelName);
00049   std::cout<<"Model "<<modelName<<" has "<<model.numberRows()<<" rows and "<<
00050     model.numberColumns()<<" columns"<<std::endl;
00051 
00052   /* Some parameters as in OsiSolverParameters.  ObjectiveLimits
00053      are not active yet.  dualTolerance, setDualTolerance,
00054      primalTolerance and setPrimalTolerance may be used as well */
00055 
00056   model.getDblParam(ClpDualObjectiveLimit,value);
00057   std::cout<<"Value of ClpDualObjectiveLimit is "<<value<<std::endl;
00058   model.getDblParam(ClpPrimalObjectiveLimit,value);
00059   std::cout<<"Value of ClpPrimalObjectiveLimit is "<<value<<std::endl;
00060   model.getDblParam(ClpDualTolerance,value);
00061   std::cout<<"Value of ClpDualTolerance is "<<value<<std::endl;
00062   model.getDblParam(ClpPrimalTolerance,value);
00063   std::cout<<"Value of ClpPrimalTolerance is "<<value<<std::endl;
00064   model.getDblParam(ClpObjOffset,value);
00065   std::cout<<"Value of ClpObjOffset is "<<value<<std::endl;
00066 
00067   // setDblParam(ClpPrimalTolerance) is same as this
00068   model.getDblParam(ClpPrimalTolerance,value);
00069   model.setPrimalTolerance(value);
00070 
00071   model.setDualTolerance( model.dualTolerance()) ;
00072 
00073   // Other Param stuff
00074 
00075   // Can also use maximumIterations
00076   model.getIntParam(ClpMaxNumIteration,integerValue);
00077   std::cout<<"Value of ClpMaxNumIteration is "<<integerValue<<std::endl;
00078   model.setMaximumIterations(integerValue);
00079 
00080   // Not sure this works yet
00081   model.getIntParam(ClpMaxNumIterationHotStart,integerValue);
00082   std::cout<<"Value of ClpMaxNumIterationHotStart is "
00083            <<integerValue<<std::endl;
00084 
00085   // Can also use getIterationCount and getObjValue
00086   /* Status of problem:
00087       0 - optimal
00088       1 - primal infeasible
00089       2 - dual infeasible
00090       3 - stopped on iterations etc
00091       4 - stopped due to errors
00092   */
00093   std::cout<<"Model status is "<<model.status()<<" after "
00094            <<model.numberIterations()<<" iterations - objective is "
00095            <<model.objectiveValue()<<std::endl;
00096 
00097   assert(!model.isAbandoned());
00098   assert(model.isProvenOptimal());
00099   assert(!model.isProvenPrimalInfeasible());
00100   assert(!model.isProvenDualInfeasible());
00101   assert(!model.isPrimalObjectiveLimitReached());
00102   assert(!model.isDualObjectiveLimitReached());
00103   assert(!model.isIterationLimitReached());
00104 
00105 
00106   // Things to help you determine if optimal
00107   assert(model.primalFeasible());
00108   assert(!model.numberPrimalInfeasibilities());
00109   assert(model.sumPrimalInfeasibilities()<1.0e-7);
00110   assert(model.dualFeasible());
00111   assert(!model.numberDualInfeasibilities());
00112   assert(model.sumDualInfeasibilities()<1.0e-7);
00113 
00114   // Save warm start and set to all slack
00115   unsigned char * basis1 = model.statusCopy();
00116   model.createStatus();
00117 
00118   // Now create another model and do hot start
00119   ClpSimplex model2=model;
00120   model2.copyinStatus(basis1);
00121   delete [] basis1;
00122 
00123   // Check model has not got basis (should iterate)
00124   model.dual();
00125   
00126   // Can use getObjSense
00127   model2.setOptimizationDirection(model.optimizationDirection());
00128 
00129   // Can use scalingFlag() to check if scaling on
00130   // But set up scaling
00131   model2.scaling();
00132 
00133   // Could play with sparse factorization on/off
00134   model2.setSparseFactorization(model.sparseFactorization());
00135 
00136   // Sets row pivot choice algorithm in dual
00137   ClpDualRowSteepest dualSteepest;
00138   model2.setDualRowPivotAlgorithm(dualSteepest);
00139 
00140   // Dual bound (i.e. dual infeasibility cost)
00141   value = model.dualBound();
00142   std::cout<<"Default value of dual bound is "<<value<<std::endl;
00143   model.setDualBound(value);
00144 
00145   // Do some deafult message handling
00146   // To see real use - see OsiOslSolverInterfaceTest.cpp
00147   CoinMessageHandler handler;
00148   model2.passInMessageHandler(& handler);
00149   model2.newLanguage(CoinMessages::us_en);
00150 
00151   //Increase level of detail
00152   model2.setLogLevel(4);
00153 
00154   // solve
00155   model2.dual();
00156   // flip direction twice and solve
00157   model2.setOptimizationDirection(-1);
00158   model2.dual();
00159   model2.setOptimizationDirection(1);
00160   //Decrease level of detail
00161   model2.setLogLevel(1);
00162   model2.dual();
00163 
00164   /*
00165     Now for getting at information.  This will not deal with:
00166 
00167     ClpMatrixBase * rowCopy() and ClpMatrixbase * clpMatrix()
00168 
00169     nor with
00170 
00171     double * infeasibilityRay() and double * unboundedRay()
00172     (NULL returned if none/wrong)
00173     Up to user to use delete [] on these arrays.
00174 
00175    */
00176 
00177 
00178   /*
00179     Now to print out solution.  The methods used return modifiable
00180     arrays while the alternative names return const pointers -
00181     which is of course much more virtuous
00182   
00183    */
00184 
00185   int numberRows = model2.numberRows();
00186 
00187   // Alternatively getRowActivity()
00188   double * rowPrimal = model2.primalRowSolution();
00189   // Alternatively getRowPrice()
00190   double * rowDual = model2.dualRowSolution();
00191   // Alternatively getRowLower()
00192   double * rowLower = model2.rowLower();
00193   // Alternatively getRowUpper()
00194   double * rowUpper = model2.rowUpper();
00195   // Alternatively getRowObjCoefficients()
00196   double * rowObjective = model2.rowObjective();
00197     
00198   // If we have not kept names (parameter to readMps) this will be 0
00199   assert(model2.lengthNames());
00200 
00201   // Row names
00202   const std::vector<std::string> * rowNames = model2.rowNames();
00203 
00204 
00205   int iRow;
00206 
00207   std::cout<<"                       Primal          Dual         Lower         Upper        (Cost)"
00208            <<std::endl;
00209 
00210   for (iRow=0;iRow<numberRows;iRow++) {
00211     double value;
00212     std::cout<<std::setw(6)<<iRow<<" "<<std::setw(8)<<(*rowNames)[iRow];
00213     value = rowPrimal[iRow];
00214     if (fabs(value)<1.0e5)
00215       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00216     else
00217       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00218     value = rowDual[iRow];
00219     if (fabs(value)<1.0e5)
00220       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00221     else
00222       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00223     value = rowLower[iRow];
00224     if (fabs(value)<1.0e5)
00225       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00226     else
00227       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00228     value = rowUpper[iRow];
00229     if (fabs(value)<1.0e5)
00230       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00231     else
00232       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00233     if (rowObjective) {
00234       value = rowObjective[iRow];
00235       if (fabs(value)<1.0e5)
00236         std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00237       else
00238         std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00239     }
00240     std::cout<<std::endl;
00241   }
00242   std::cout<<"--------------------------------------"<<std::endl;
00243 
00244   // Columns
00245 
00246   int numberColumns = model2.numberColumns();
00247 
00248   // Alternatively getColSolution()
00249   double * columnPrimal = model2.primalColumnSolution();
00250   // Alternatively getReducedCost()
00251   double * columnDual = model2.dualColumnSolution();
00252   // Alternatively getColLower()
00253   double * columnLower = model2.columnLower();
00254   // Alternatively getColUpper()
00255   double * columnUpper = model2.columnUpper();
00256   // Alternatively getObjCoefficients()
00257   double * columnObjective = model2.objective();
00258     
00259   // If we have not kept names (parameter to readMps) this will be 0
00260   assert(model2.lengthNames());
00261 
00262   // Column names
00263   const std::vector<std::string> * columnNames = model2.columnNames();
00264 
00265 
00266   int iColumn;
00267   
00268   std::cout<<"                       Primal          Dual         Lower         Upper          Cost"
00269            <<std::endl;
00270 
00271   for (iColumn=0;iColumn<numberColumns;iColumn++) {
00272     double value;
00273     std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<(*columnNames)[iColumn];
00274     value = columnPrimal[iColumn];
00275     if (fabs(value)<1.0e5)
00276       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00277     else
00278       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00279     value = columnDual[iColumn];
00280     if (fabs(value)<1.0e5)
00281       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00282     else
00283       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00284     value = columnLower[iColumn];
00285     if (fabs(value)<1.0e5)
00286       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00287     else
00288       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00289     value = columnUpper[iColumn];
00290     if (fabs(value)<1.0e5)
00291       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00292     else
00293       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00294     value = columnObjective[iColumn];
00295     if (fabs(value)<1.0e5)
00296       std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
00297     else
00298       std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
00299 
00300     std::cout<<std::endl;
00301   }
00302   std::cout<<"--------------------------------------"<<std::endl;
00303 
00304   std::cout<<resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
00305 
00306   // Now matrix
00307   CoinPackedMatrix * matrix = model2.matrix();
00308 
00309   const double * element = matrix->getElements();
00310   const int * row = matrix->getIndices();
00311   const int * start = matrix->getVectorStarts();
00312   const int * length = matrix->getVectorLengths();
00313 
00314   for (iColumn=0;iColumn<numberColumns;iColumn++) {
00315     std::cout<<"Column "<<iColumn;
00316     int j;
00317     for (j=start[iColumn];j<start[iColumn]+length[iColumn];j++) 
00318       std::cout<<" ( "<<row[j]<<", "<<element[j]<<")";
00319     std::cout<<std::endl;
00320   }
00321   return 0;
00322 }    

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