test/mip_test.cc
author Balazs Dezso <deba@inf.elte.hu>
Sun, 14 Feb 2010 23:10:24 +0100
changeset 900 5100072d83ca
parent 674 20dac2104519
child 795 4792459983d0
permissions -rw-r--r--
Use void* like LPX object (#337)
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2009
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #include "test_tools.h"
    20 
    21 #include <lemon/config.h>
    22 
    23 #ifdef LEMON_HAVE_CPLEX
    24 #include <lemon/cplex.h>
    25 #endif
    26 
    27 #ifdef LEMON_HAVE_GLPK
    28 #include <lemon/glpk.h>
    29 #endif
    30 
    31 #ifdef LEMON_HAVE_CBC
    32 #include <lemon/cbc.h>
    33 #endif
    34 
    35 
    36 using namespace lemon;
    37 
    38 void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat,
    39                    double exp_opt) {
    40   using std::string;
    41 
    42   mip.solve();
    43   //int decimal,sign;
    44   std::ostringstream buf;
    45   buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type());
    46 
    47 
    48   //  itoa(stat,buf1, 10);
    49   check(mip.type()==stat, buf.str());
    50 
    51   if (stat ==  MipSolver::OPTIMAL) {
    52     std::ostringstream sbuf;
    53     buf << "Wrong optimal value: the right optimum is " << exp_opt;
    54     check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str());
    55     //+ecvt(exp_opt,2)
    56   }
    57 }
    58 
    59 void aTest(MipSolver& mip)
    60 {
    61   //The following example is very simple
    62 
    63 
    64   typedef MipSolver::Row Row;
    65   typedef MipSolver::Col Col;
    66 
    67 
    68   Col x1 = mip.addCol();
    69   Col x2 = mip.addCol();
    70 
    71 
    72   //Objective function
    73   mip.obj(x1);
    74 
    75   mip.max();
    76 
    77   //Unconstrained optimization
    78   mip.solve();
    79   //Check it out!
    80 
    81   //Constraints
    82   mip.addRow(2 * x1 + x2 <= 2);
    83   Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
    84 
    85   //Nonnegativity of the variable x1
    86   mip.colLowerBound(x1, 0);
    87 
    88 
    89   //Maximization of x1
    90   //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
    91   double expected_opt=4.0/5.0;
    92   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
    93 
    94 
    95   //Restrict x2 to integer
    96   mip.colType(x2,MipSolver::INTEGER);
    97   expected_opt=1.0/2.0;
    98   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
    99 
   100 
   101   //Restrict both to integer
   102   mip.colType(x1,MipSolver::INTEGER);
   103   expected_opt=0;
   104   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   105 
   106   //Erase a variable
   107   mip.erase(x2);
   108   mip.rowUpperBound(y2, 8);
   109   expected_opt=1;
   110   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   111 
   112 }
   113 
   114 
   115 template<class MIP>
   116 void cloneTest()
   117 {
   118 
   119   MIP* mip = new MIP();
   120   MIP* mipnew = mip->newSolver();
   121   MIP* mipclone = mip->cloneSolver();
   122   delete mip;
   123   delete mipnew;
   124   delete mipclone;
   125 }
   126 
   127 int main()
   128 {
   129 
   130 #ifdef LEMON_HAVE_GLPK
   131   {
   132     GlpkMip mip1;
   133     aTest(mip1);
   134     cloneTest<GlpkMip>();
   135   }
   136 #endif
   137 
   138 #ifdef LEMON_HAVE_CPLEX
   139   try {
   140     CplexMip mip2;
   141     aTest(mip2);
   142     cloneTest<CplexMip>();
   143   } catch (CplexEnv::LicenseError& error) {
   144     check(false, error.what());
   145   }
   146 #endif
   147 
   148 #ifdef LEMON_HAVE_CBC
   149   {
   150     CbcMip mip1;
   151     aTest(mip1);
   152     cloneTest<CbcMip>();
   153   }
   154 #endif
   155 
   156   return 0;
   157 
   158 }