test/mip_test.cc
changeset 576 745e182d0139
parent 551 9d0d7e20f76d
child 575 3cbddc293cf9
equal deleted inserted replaced
8:b82ac25d67cd 9:4db2009adddd
    16  *
    16  *
    17  */
    17  */
    18 
    18 
    19 #include "test_tools.h"
    19 #include "test_tools.h"
    20 
    20 
    21 
       
    22 #ifdef HAVE_CONFIG_H
    21 #ifdef HAVE_CONFIG_H
    23 #include <lemon/config.h>
    22 #include <lemon/config.h>
    24 #endif
    23 #endif
    25 
    24 
    26 #ifdef HAVE_CPLEX
    25 #ifdef HAVE_CPLEX
    27 #include <lemon/cplex.h>
    26 #include <lemon/cplex.h>
    28 #endif
    27 #endif
    29 
    28 
    30 #ifdef HAVE_GLPK
    29 #ifdef HAVE_GLPK
    31 #include <lemon/glpk.h>
    30 #include <lemon/glpk.h>
       
    31 #endif
       
    32 
       
    33 #ifdef HAVE_CBC
       
    34 #include <lemon/cbc.h>
    32 #endif
    35 #endif
    33 
    36 
    34 
    37 
    35 using namespace lemon;
    38 using namespace lemon;
    36 
    39 
    55   }
    58   }
    56 }
    59 }
    57 
    60 
    58 void aTest(MipSolver& mip)
    61 void aTest(MipSolver& mip)
    59 {
    62 {
    60  //The following example is very simple
    63   //The following example is very simple
    61 
    64 
    62 
    65 
    63   typedef MipSolver::Row Row;
    66   typedef MipSolver::Row Row;
    64   typedef MipSolver::Col Col;
    67   typedef MipSolver::Col Col;
    65 
       
    66 
    68 
    67 
    69 
    68   Col x1 = mip.addCol();
    70   Col x1 = mip.addCol();
    69   Col x2 = mip.addCol();
    71   Col x2 = mip.addCol();
    70 
    72 
    72   //Objective function
    74   //Objective function
    73   mip.obj(x1);
    75   mip.obj(x1);
    74 
    76 
    75   mip.max();
    77   mip.max();
    76 
    78 
    77 
       
    78   //Unconstrained optimization
    79   //Unconstrained optimization
    79   mip.solve();
    80   mip.solve();
    80   //Check it out!
    81   //Check it out!
    81 
    82 
    82   //Constraints
    83   //Constraints
    83   mip.addRow(2*x1+x2 <=2);
    84   mip.addRow(2 * x1 + x2 <= 2);
    84   mip.addRow(x1-2*x2 <=0);
    85   Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
    85 
    86 
    86   //Nonnegativity of the variable x1
    87   //Nonnegativity of the variable x1
    87   mip.colLowerBound(x1, 0);
    88   mip.colLowerBound(x1, 0);
       
    89 
    88 
    90 
    89   //Maximization of x1
    91   //Maximization of x1
    90   //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
    92   //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
    91   double expected_opt=4.0/5.0;
    93   double expected_opt=4.0/5.0;
    92   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
    94   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
       
    95 
    93 
    96 
    94   //Restrict x2 to integer
    97   //Restrict x2 to integer
    95   mip.colType(x2,MipSolver::INTEGER);
    98   mip.colType(x2,MipSolver::INTEGER);
    96   expected_opt=1.0/2.0;
    99   expected_opt=1.0/2.0;
    97   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   100   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   100   //Restrict both to integer
   103   //Restrict both to integer
   101   mip.colType(x1,MipSolver::INTEGER);
   104   mip.colType(x1,MipSolver::INTEGER);
   102   expected_opt=0;
   105   expected_opt=0;
   103   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   106   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   104 
   107 
   105 
   108   //Erase a variable
       
   109   mip.erase(x2);
       
   110   mip.rowUpperBound(y2, 8);
       
   111   expected_opt=1;
       
   112   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   106 
   113 
   107 }
   114 }
       
   115 
   108 
   116 
   109 template<class MIP>
   117 template<class MIP>
   110 void cloneTest()
   118 void cloneTest()
   111 {
   119 {
   112 
   120 
   142     std::cerr << "Cplex license check failed, lp check skipped" << std::endl;
   150     std::cerr << "Cplex license check failed, lp check skipped" << std::endl;
   143 #endif
   151 #endif
   144   }
   152   }
   145 #endif
   153 #endif
   146 
   154 
       
   155 #ifdef HAVE_CBC
       
   156   {
       
   157     CbcMip mip1;
       
   158     aTest(mip1);
       
   159     cloneTest<CbcMip>();
       
   160   }
       
   161 #endif
       
   162 
   147   return 0;
   163   return 0;
   148 
   164 
   149 }
   165 }