[Lemon-commits] [lemon_svn] athos: r2863 - in hugo/trunk: lemon test

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 21:50:52 CET 2006


Author: athos
Date: Tue Jul 18 13:11:54 2006
New Revision: 2863

Modified:
   hugo/trunk/lemon/mip_glpk.cc
   hugo/trunk/test/mip_test.cc

Log:
Some tests added to the test file mip_test.cc. One problem is the verbosity of the mip solver in glpk which I couldn't find how to kill.

Modified: hugo/trunk/lemon/mip_glpk.cc
==============================================================================
--- hugo/trunk/lemon/mip_glpk.cc	(original)
+++ hugo/trunk/lemon/mip_glpk.cc	Tue Jul 18 13:11:54 2006
@@ -30,7 +30,7 @@
     lpx_set_class(lp,LPX_MIP);
   }
 
-  void MipGlpk::_colType(int i, ColTypes col_type){
+  void MipGlpk::_colType(int i, MipGlpk::ColTypes col_type){
     switch (col_type){
       case INTEGER:
 	lpx_set_col_kind(lp,i,LPX_IV);
@@ -38,12 +38,12 @@
       case REAL:
 	lpx_set_col_kind(lp,i,LPX_CV);
 	break;
-      default:
+    default:;
         //FIXME problem
     }
   }
   
-  ColTypes MipGlpk::_colType(int i){
+  MipGlpk::ColTypes MipGlpk::_colType(int i){
     switch (lpx_get_col_kind(lp,i)){
     case LPX_IV:
       return INTEGER;//Or binary

Modified: hugo/trunk/test/mip_test.cc
==============================================================================
--- hugo/trunk/test/mip_test.cc	(original)
+++ hugo/trunk/test/mip_test.cc	Tue Jul 18 13:11:54 2006
@@ -1,12 +1,82 @@
 #include <lemon/lp.h>
+#include "test_tools.h"
 
 using namespace lemon;
 
-int main(){
+void solveAndCheck(Mip& lp, LpSolverBase::SolutionStatus stat, 
+		   double exp_opt) {
+  using std::string;
+  lp.solve();
+  //int decimal,sign;
+  std::ostringstream buf;
+  buf << "Primalstatus should be: " << int(stat)<<" and it is "<<int(lp.primalStatus());
+
+  //  itoa(stat,buf1, 10);
+  check(lp.primalStatus()==stat, buf.str());
+
+  if (stat ==  LpSolverBase::OPTIMAL) {
+    std::ostringstream buf;
+    buf << "Wrong optimal value: the right optimum is " << exp_opt; 
+    check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str());
+    //+ecvt(exp_opt,2)
+  }
+}
+
+void aTest(Mip& mip)
+{
+ //The following example is very simple
+
+  typedef Mip::Row Row;
+  typedef Mip::Col Col;
+
+
+  Col x1 = mip.addCol();
+  Col x2 = mip.addCol();
+
+
+
+  //Constraints
+  mip.addRow(2*x1+x2 <=2);  
+  mip.addRow(x1-2*x2 <=0);  
+
+  //Nonnegativity of the variable x1
+  mip.colLowerBound(x1, 0);
+
+
+
+  //Objective function
+  mip.setObj(x1);
+
+  mip.max();
+
+
+  //Maximization of x1
+  //over the triangle with vertices
+  double expected_opt=4.0/5.0;
+  solveAndCheck(mip, Mip::OPTIMAL, expected_opt);
+
+  //Restrict x2 to integer
+  mip.colType(x2,Mip::INTEGER);  
+  expected_opt=1.0/2.0;
+  solveAndCheck(mip, Mip::OPTIMAL, expected_opt);
+
+
+  //Restrict both to integer
+  mip.colType(x1,Mip::INTEGER);  
+  expected_opt=0;
+  solveAndCheck(mip, Mip::OPTIMAL, expected_opt);
+
+ 
+
+}
+
 
+int main() 
+{
 
 #ifdef HAVE_GLPK
-  //This needs some thinking
+  MipGlpk mip1;
+  aTest(mip1);
 #endif
 
   return 0;



More information about the Lemon-commits mailing list