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.
1.1 --- a/lemon/mip_glpk.cc Mon Jul 17 11:56:17 2006 +0000
1.2 +++ b/lemon/mip_glpk.cc Tue Jul 18 11:11:54 2006 +0000
1.3 @@ -30,7 +30,7 @@
1.4 lpx_set_class(lp,LPX_MIP);
1.5 }
1.6
1.7 - void MipGlpk::_colType(int i, ColTypes col_type){
1.8 + void MipGlpk::_colType(int i, MipGlpk::ColTypes col_type){
1.9 switch (col_type){
1.10 case INTEGER:
1.11 lpx_set_col_kind(lp,i,LPX_IV);
1.12 @@ -38,12 +38,12 @@
1.13 case REAL:
1.14 lpx_set_col_kind(lp,i,LPX_CV);
1.15 break;
1.16 - default:
1.17 + default:;
1.18 //FIXME problem
1.19 }
1.20 }
1.21
1.22 - ColTypes MipGlpk::_colType(int i){
1.23 + MipGlpk::ColTypes MipGlpk::_colType(int i){
1.24 switch (lpx_get_col_kind(lp,i)){
1.25 case LPX_IV:
1.26 return INTEGER;//Or binary
2.1 --- a/test/mip_test.cc Mon Jul 17 11:56:17 2006 +0000
2.2 +++ b/test/mip_test.cc Tue Jul 18 11:11:54 2006 +0000
2.3 @@ -1,12 +1,82 @@
2.4 #include <lemon/lp.h>
2.5 +#include "test_tools.h"
2.6
2.7 using namespace lemon;
2.8
2.9 -int main(){
2.10 +void solveAndCheck(Mip& lp, LpSolverBase::SolutionStatus stat,
2.11 + double exp_opt) {
2.12 + using std::string;
2.13 + lp.solve();
2.14 + //int decimal,sign;
2.15 + std::ostringstream buf;
2.16 + buf << "Primalstatus should be: " << int(stat)<<" and it is "<<int(lp.primalStatus());
2.17
2.18 + // itoa(stat,buf1, 10);
2.19 + check(lp.primalStatus()==stat, buf.str());
2.20 +
2.21 + if (stat == LpSolverBase::OPTIMAL) {
2.22 + std::ostringstream buf;
2.23 + buf << "Wrong optimal value: the right optimum is " << exp_opt;
2.24 + check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str());
2.25 + //+ecvt(exp_opt,2)
2.26 + }
2.27 +}
2.28 +
2.29 +void aTest(Mip& mip)
2.30 +{
2.31 + //The following example is very simple
2.32 +
2.33 + typedef Mip::Row Row;
2.34 + typedef Mip::Col Col;
2.35 +
2.36 +
2.37 + Col x1 = mip.addCol();
2.38 + Col x2 = mip.addCol();
2.39 +
2.40 +
2.41 +
2.42 + //Constraints
2.43 + mip.addRow(2*x1+x2 <=2);
2.44 + mip.addRow(x1-2*x2 <=0);
2.45 +
2.46 + //Nonnegativity of the variable x1
2.47 + mip.colLowerBound(x1, 0);
2.48 +
2.49 +
2.50 +
2.51 + //Objective function
2.52 + mip.setObj(x1);
2.53 +
2.54 + mip.max();
2.55 +
2.56 +
2.57 + //Maximization of x1
2.58 + //over the triangle with vertices
2.59 + double expected_opt=4.0/5.0;
2.60 + solveAndCheck(mip, Mip::OPTIMAL, expected_opt);
2.61 +
2.62 + //Restrict x2 to integer
2.63 + mip.colType(x2,Mip::INTEGER);
2.64 + expected_opt=1.0/2.0;
2.65 + solveAndCheck(mip, Mip::OPTIMAL, expected_opt);
2.66 +
2.67 +
2.68 + //Restrict both to integer
2.69 + mip.colType(x1,Mip::INTEGER);
2.70 + expected_opt=0;
2.71 + solveAndCheck(mip, Mip::OPTIMAL, expected_opt);
2.72 +
2.73 +
2.74 +
2.75 +}
2.76 +
2.77 +
2.78 +int main()
2.79 +{
2.80
2.81 #ifdef HAVE_GLPK
2.82 - //This needs some thinking
2.83 + MipGlpk mip1;
2.84 + aTest(mip1);
2.85 #endif
2.86
2.87 return 0;