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

OsiRowCutTest.cpp

00001 // Copyright (C) 2000, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 #if defined(_MSC_VER)
00004 // Turn off compiler warning about long names
00005 #  pragma warning(disable:4786)
00006 #endif
00007 
00008 #include <cassert>
00009 #include <cfloat>
00010 
00011 #include "OsiRowCut.hpp"
00012 #include "CoinFloatEqual.hpp"
00013 
00014 #ifdef NDEBUG
00015 #undef NDEBUG
00016 #endif
00017 
00018 //--------------------------------------------------------------------------
00019 void
00020 OsiRowCutUnitTest(const OsiSolverInterface * baseSiP,
00021                   const std::string & mpsDir )
00022 {
00023 
00024   CoinRelFltEq eq;
00025 
00026   // Test default constructor
00027   {
00028     OsiRowCut r;
00029     assert( r.row_.getIndices()==NULL );
00030     assert( r.row_.getElements()==NULL );
00031     assert( r.lb_==-/*std::numeric_limits<double>::max()*/DBL_MAX );
00032     assert( r.ub_== /*std::numeric_limits<double>::max()*/DBL_MAX );
00033   }
00034 
00035   // Test set and get methods
00036   const int ne = 4;
00037   int inx[ne] = { 1, 3, 4, 7 };
00038   double el[ne] = { 1.2, 3.4, 5.6, 7.8 };
00039   {
00040     OsiRowCut r;    
00041     assert( r.row().getNumElements()==0 );
00042     assert( r.effectiveness()==0. );
00043     //assert( r.timesUsed()==0 );
00044     //assert( r.timesTested()==0 );
00045 
00046     // Test setting getting bounds
00047     r.setLb(65.432);
00048     r.setUb(123.45);
00049     assert( r.lb()==65.432 );
00050     assert( r.ub()==123.45 );
00051 
00052     // Test setting/getting of effectiveness,timesUsed,timesTested
00053     r.setEffectiveness(45.);
00054     assert( r.effectiveness()==45. );
00055 #if 0
00056     r.setTimesUsed(11);
00057     assert( r.timesUsed()==11 );
00058     r.incrementTimesUsed();
00059     assert( r.timesUsed()==12 );
00060     r.setTimesTested(111);
00061     assert( r.timesTested()==111 );
00062     r.incrementTimesTested();
00063     assert( r.timesTested()==112 );
00064 #endif
00065     
00066     // Test setting/getting elements with int* & float* vectors
00067     r.setRow( ne, inx, el );
00068     assert( r.row().getNumElements()==ne );
00069     for ( int i=0; i<ne; i++ ) {
00070       assert( r.row().getIndices()[i] == inx[i] );
00071       assert( r.row().getElements()[i] == el[i] );
00072     }
00073   } 
00074 
00075   // Test sense, rhs, range
00076   {
00077     {
00078       OsiRowCut r;
00079       assert( r.sense() == 'N' );
00080       assert( r.rhs() == 0.0 );
00081       assert( r.range() == 0.0 );
00082     }
00083     {
00084       OsiRowCut r;
00085       r.setLb(65.432);
00086       assert( r.sense() == 'G' );
00087       assert( r.rhs() == 65.432 );
00088       assert( r.range() == 0.0 );
00089     }
00090     {
00091       OsiRowCut r;
00092       r.setLb(65.432);
00093       r.setUb(65.432);
00094       assert( r.sense() == 'E' );
00095       assert( r.rhs() == 65.432 );
00096       assert( r.range() == 0.0 );
00097     }
00098     {
00099       OsiRowCut r;
00100       r.setUb(123.45);
00101       assert( r.sense() == 'L' );
00102       assert( r.rhs() == 123.45 );
00103       assert( r.range() == 0.0 );
00104     }
00105     {
00106       OsiRowCut r;
00107       r.setLb(65.432);
00108       r.setUb(123.45);
00109       assert( r.sense() == 'R' );
00110       assert( r.rhs() == 123.45 );
00111       assert( eq(r.range(),123.45 - 65.432) );
00112     }
00113   }
00114   
00115   // Test copy constructor and assignment operator
00116   {
00117     OsiRowCut rhs;
00118     {
00119       OsiRowCut r;
00120       OsiRowCut rC1(r);
00121       assert( rC1.row().getNumElements()==r.row().getNumElements() );
00122       assert( rC1.row().getIndices()==r.row().getIndices() );
00123       assert( rC1.row().getElements()==r.row().getElements() );
00124       assert( rC1.lb()==r.lb() );
00125       assert( rC1.ub()==r.ub() );
00126       
00127       r.setLb(65.432);
00128       r.setUb(123.45);
00129       r.setRow( ne, inx, el );
00130       r.setEffectiveness(123.);
00131       
00132       assert( rC1.row().getNumElements()!=r.row().getNumElements() );
00133       assert( rC1.row().getIndices()!=r.row().getIndices() );
00134       assert( rC1.row().getElements()!=r.row().getElements() );
00135       assert( rC1.lb()!=r.lb() );
00136       assert( rC1.ub()!=r.ub() );
00137       assert( rC1.effectiveness()!=r.effectiveness() );
00138       
00139       OsiRowCut rC2(r);
00140       assert( rC2.row().getNumElements()==r.row().getNumElements() );
00141       for( int i=0; i<r.row().getNumElements(); i++ ){
00142         assert( rC2.row().getIndices()[i]==r.row().getIndices()[i] );
00143         assert( rC2.row().getElements()[i]==r.row().getElements()[i] );
00144       }
00145       assert( rC2.lb()==r.lb() );
00146       assert( rC2.ub()==r.ub() );       
00147       assert( rC2.effectiveness()==r.effectiveness() );
00148       
00149       rhs=rC2;
00150     }
00151     // Test that rhs has correct values even though lhs has gone out of scope
00152     assert( rhs.row().getNumElements()==ne );
00153     for ( int i=0; i<ne; i++ ) {
00154       assert( rhs.row().getIndices()[i] == inx[i] );
00155       assert( rhs.row().getElements()[i] == el[i] );
00156     }  
00157     assert( rhs.effectiveness()==123. );
00158     assert( rhs.lb()==65.432 ); 
00159     assert( rhs.ub()==123.45 ); 
00160   }
00161 
00162   // Test setting row with packed vector
00163   {
00164     CoinPackedVector r;
00165     r.setVector(ne,inx,el);
00166 
00167     OsiRowCut rc;
00168     assert( rc.row()!=r );
00169     rc.setRow(r);
00170     assert( rc.row()==r );
00171   }
00172 
00173   // Test operator==
00174   {
00175     CoinPackedVector r;
00176     r.setVector(ne,inx,el);
00177 
00178     OsiRowCut rc;
00179     rc.setRow(r);
00180     rc.setEffectiveness(2.);
00181     rc.setLb(3.0);
00182     rc.setUb(4.0);
00183 
00184     {
00185       OsiRowCut c(rc);
00186       assert(   c == rc  );
00187       assert( !(c != rc) );
00188       assert( !(c  < rc)  );
00189       assert( !(rc < c )  );
00190       assert( !(c  > rc)  );
00191       assert( !(rc > c )  );
00192     }
00193     {
00194       OsiRowCut c(rc);      
00195       const int ne1 = 4;
00196       int inx1[ne] = { 1, 3, 4, 7 };
00197       double el1[ne] = { 1.2, 3.4, 5.6, 7.9 };
00198       c.setRow(ne1,inx1,el1);
00199       assert( !(c == rc) );
00200       assert(   c != rc  );
00201       assert( !(rc < c)  );
00202     }
00203     {
00204       OsiRowCut c(rc); 
00205       c.setEffectiveness(3.);
00206       assert( !(c == rc) );
00207       assert(   c != rc  );
00208       assert(   rc < c   );
00209       assert( !(c < rc)  );
00210       assert( !(rc > c)  );
00211       assert(   c > rc  );
00212     }
00213     {
00214       OsiRowCut c(rc); 
00215       c.setLb(4.0);
00216       assert( !(c == rc) );
00217       assert(   c != rc  );
00218     }
00219     {
00220       OsiRowCut c(rc); 
00221       c.setUb(5.0);
00222       assert( !(c == rc) );
00223       assert(   c != rc  );
00224     }
00225   }
00226   {
00227     // Test consistent
00228     OsiSolverInterface * imP = baseSiP->clone();
00229     std::string fn = mpsDir+"exmip1";
00230     imP->readMps(fn.c_str(),"mps");
00231 
00232     OsiRowCut c;      
00233     const int ne = 3;
00234     int inx[ne] = { -1, 5, 4 };
00235     double el[ne] = { 1., 1., 1. };
00236     c.setRow(ne,inx,el);
00237     assert( !c.consistent() );;
00238     
00239     inx[0]=5;
00240 #if 0
00241     c.setRow(ne,inx,el);
00242     assert( !c.consistent() );
00243 #else
00244     bool errorThrown = false;
00245     try {
00246        c.setRow(ne,inx,el);
00247     }
00248     catch (CoinError e) {
00249        errorThrown = true;
00250     }
00251     assert(errorThrown);
00252 #endif
00253     
00254     inx[0]=3;
00255     c.setRow(ne,inx,el);
00256     assert( c.consistent() );
00257     
00258     c.setLb(5.);
00259     c.setUb(5.);
00260     assert( c.consistent() );
00261 
00262     c.setLb(5.5);
00263     assert( c.consistent() );
00264     assert( c.infeasible(*imP) );
00265     delete imP;
00266   }
00267   {
00268     // Test consistent(IntegerModel) method.
00269     OsiSolverInterface * imP = baseSiP->clone();
00270     std::string fn = mpsDir+"exmip1";
00271     imP->readMps(fn.c_str(),"mps");
00272     
00273     OsiRowCut cut;    
00274     const int ne = 3;
00275     int inx[ne] = { 3, 5, 4 };
00276     double el[ne] = { 1., 1., 1. };
00277     cut.setRow(ne,inx,el);
00278     assert( cut.consistent() );
00279     
00280     inx[0]=7;
00281     cut.setRow(ne,inx,el);
00282     assert( cut.consistent(*imP) );
00283     
00284     inx[0]=8;
00285     cut.setRow(ne,inx,el);
00286     assert(  cut.consistent() ); 
00287     assert( !cut.consistent(*imP) );
00288     delete imP;
00289   }
00290   {
00291     //Test infeasible(im) method
00292     OsiSolverInterface * imP = baseSiP->clone();
00293     std::string fn = mpsDir+"exmip1";
00294     imP->readMps(fn.c_str(),"mps");
00295     
00296     OsiRowCut cut;   
00297     const int ne = 3;
00298     int inx[ne] = { 3, 5, 4 };
00299     double el[ne] = { 1., 1., 1. };
00300     cut.setRow(ne,inx,el);
00301     assert( !cut.infeasible(*imP) );
00302     
00303     OsiRowCut c1;
00304     assert( !c1.infeasible(*imP) );
00305     assert( c1.consistent() );
00306     assert( c1.consistent(*imP) );
00307     delete imP;
00308   }
00309   {
00310     //Test violation
00311 
00312     double solution[]={1.0,1.0,1.0,2.0,2.0,2.0};
00313     OsiRowCut cut;   
00314     const int ne = 3;
00315     int inx[ne] = { 3, 5, 4 };
00316     double el[ne] = { 1., 1., 1. };
00317     cut.setRow(ne,inx,el);
00318     cut.setUb(5.);
00319     assert( cut.violated(solution) );
00320 
00321     cut.setLb(5.);
00322     cut.setUb(10.);
00323     assert( !cut.violated(solution) );
00324 
00325     cut.setLb(6.1);
00326     assert( cut.violated(solution) );
00327 
00328 
00329   }
00330 }
00331 

Generated on Wed Dec 3 14:35:33 2003 for Osi by doxygen 1.3.5