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

CglSimpleRoundingTest.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 #ifdef NDEBUG
00009 #undef NDEBUG
00010 #endif
00011 
00012 #include <cassert>
00013 
00014 #include "CglSimpleRounding.hpp" 
00015 #include <stdio.h>
00016 
00017 //--------------------------------------------------------------------------
00018 // test the simple rounding cut generators methods.
00019 void
00020 CglSimpleRoundingUnitTest(
00021   const OsiSolverInterface * baseSiP,
00022   const std::string mpsDir )
00023 {
00024 
00025   // Test default constructor
00026   {
00027     CglSimpleRounding cg;
00028   }
00029 
00030   // Test copy & assignment
00031   {
00032     CglSimpleRounding rhs;
00033     {
00034       CglSimpleRounding cg;
00035       CglSimpleRounding cgC(cg);
00036       rhs=cg;
00037     }
00038   }
00039 
00040   // Test gcd and gcdn
00041   {
00042     CglSimpleRounding cg;
00043     int v = cg.gcd(122,356);
00044     assert(v==2);
00045     v=cg.gcd(356,122);
00046     assert(v==2);
00047     v=cg.gcd(54,67);
00048     assert(v==1);
00049     v=cg.gcd(67,54);
00050     assert(v==1);
00051     v=cg.gcd(485,485);
00052     assert(v==485);
00053     v=cg.gcd(17*13,17*23);
00054     assert( v==17);
00055     v=cg.gcd(17*13*5,17*23);
00056     assert( v==17);
00057     v=cg.gcd(17*13*23,17*23);
00058     assert(v==17*23);
00059 
00060     int a[4] = {12, 20, 32, 400};
00061     v= cg.gcdv(4,a);
00062     assert(v== 4);
00063     int b[4] = {782, 4692, 51, 2754};
00064     v= cg.gcdv(4,b);
00065     assert(v== 17);
00066     int c[4] = {50, 40, 30, 10};
00067     v= cg.gcdv(4,c);
00068     assert(v== 10);
00069   }
00070 
00071 
00072   // Test generate cuts method on exmip1.5.mps
00073   {
00074     CglSimpleRounding cg;
00075     
00076     OsiSolverInterface * siP = baseSiP->clone();
00077     std::string fn = mpsDir+"exmip1.5";
00078     siP->readMps(fn.c_str(),"mps");
00079     OsiCuts cuts;
00080     cg.generateCuts(*siP,cuts);
00081 
00082     // there should be 3 cuts
00083     int nRowCuts = cuts.sizeRowCuts();
00084     assert(nRowCuts==3);
00085 
00086     // get the last "sr"=simple rounding cut that was derived
00087     OsiRowCut srRowCut2 = cuts.rowCut(2); 
00088     CoinPackedVector srRowCutPV2 = srRowCut2.row();
00089 
00090     // this is what the last cut should look like: i.e. the "solution"
00091     const int solSize = 2;
00092     int solCols[solSize]={2,3};
00093     double solCoefs[solSize]={5.0, 4.0};
00094     OsiRowCut solRowCut;
00095     solRowCut.setRow(solSize,solCols,solCoefs);
00096     solRowCut.setLb(-DBL_MAX);
00097     solRowCut.setUb(2.0);
00098 
00099     // Test for equality between the derived cut and the solution cut
00100 
00101     // Note: testing two OsiRowCuts are equal invokes testing two
00102     // CoinPackedVectors are equal which invokes testing two doubles
00103     // are equal.  Usually not a good idea to test that two doubles are equal, 
00104     // but in this cut the "doubles" represent integer values.
00105     assert(srRowCut2 == solRowCut);
00106 
00107     delete siP;
00108   }
00109 
00110   // Test generate cuts method on p0033
00111   {
00112     CglSimpleRounding cg;
00113     
00114     OsiSolverInterface * siP = baseSiP->clone();
00115     std::string fn = mpsDir+"p0033";
00116     siP->readMps(fn.c_str(),"mps");
00117     OsiCuts cuts;
00118     cg.generateCuts(*siP,cuts);
00119 
00120     // p0033 is the optimal solution to p0033
00121     int objIndices[14] = { 
00122        0,  6,  7,  9, 13, 17, 18,
00123       22, 24, 25, 26, 27, 28, 29 };
00124     CoinPackedVector p0033(14,objIndices,1.0);
00125 
00126     // test that none of the generated cuts
00127     // chops off the optimal solution
00128     int nRowCuts = cuts.sizeRowCuts();
00129     OsiRowCut rcut;
00130     CoinPackedVector rpv;
00131     int i;
00132     for (i=0; i<nRowCuts; i++){
00133       rcut = cuts.rowCut(i);
00134       rpv = rcut.row();
00135       double p0033Sum = (rpv*p0033).sum();
00136       double rcutub = rcut.ub();
00137       assert (p0033Sum <= rcutub);
00138     }
00139 
00140     // test that the cuts improve the 
00141     // lp objective function value
00142     siP->initialSolve();
00143     double lpRelaxBefore=siP->getObjValue();
00144     OsiSolverInterface::ApplyCutsReturnCode rc = siP->applyCuts(cuts);
00145     siP->resolve();
00146     double lpRelaxAfter=siP->getObjValue(); 
00147 #ifdef CGL_DEBUG
00148     printf("\n\nOrig LP min=%f\n",lpRelaxBefore);
00149     printf("Final LP min=%f\n\n",lpRelaxAfter);
00150 #endif
00151     assert( lpRelaxBefore < lpRelaxAfter );
00152 
00153     delete siP;
00154 
00155   }
00156 
00157 
00158 }
00159 

Generated on Wed Dec 3 14:34:56 2003 for Cgl by doxygen 1.3.5