test/mip_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 24 Apr 2009 11:54:48 +0200
changeset 617 4137ef9aacc6
parent 567 3314f58e7b25
child 627 20dac2104519
permissions -rw-r--r--
Fix and uniform the usage of Graph and Parent typedefs (#268)

- Rename Graph typedefs to GraphType in the implementation of graph
maps and MapExtender to prevent conflicts (especially using VS).
They are not public.
- Make Parent typedefs private in all classes.
- Replace Digraph with Graph in some places
(fix faulty renamings of the script).
- Use Graph and Digraph typedefs (more) consequently.
     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 #ifdef HAVE_CONFIG_H
    22 #include <lemon/config.h>
    23 #endif
    24 
    25 #ifdef HAVE_CPLEX
    26 #include <lemon/cplex.h>
    27 #endif
    28 
    29 #ifdef HAVE_GLPK
    30 #include <lemon/glpk.h>
    31 #endif
    32 
    33 #ifdef HAVE_CBC
    34 #include <lemon/cbc.h>
    35 #endif
    36 
    37 
    38 using namespace lemon;
    39 
    40 void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat,
    41                    double exp_opt) {
    42   using std::string;
    43 
    44   mip.solve();
    45   //int decimal,sign;
    46   std::ostringstream buf;
    47   buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type());
    48 
    49 
    50   //  itoa(stat,buf1, 10);
    51   check(mip.type()==stat, buf.str());
    52 
    53   if (stat ==  MipSolver::OPTIMAL) {
    54     std::ostringstream sbuf;
    55     buf << "Wrong optimal value: the right optimum is " << exp_opt;
    56     check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str());
    57     //+ecvt(exp_opt,2)
    58   }
    59 }
    60 
    61 void aTest(MipSolver& mip)
    62 {
    63   //The following example is very simple
    64 
    65 
    66   typedef MipSolver::Row Row;
    67   typedef MipSolver::Col Col;
    68 
    69 
    70   Col x1 = mip.addCol();
    71   Col x2 = mip.addCol();
    72 
    73 
    74   //Objective function
    75   mip.obj(x1);
    76 
    77   mip.max();
    78 
    79   //Unconstrained optimization
    80   mip.solve();
    81   //Check it out!
    82 
    83   //Constraints
    84   mip.addRow(2 * x1 + x2 <= 2);
    85   Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
    86 
    87   //Nonnegativity of the variable x1
    88   mip.colLowerBound(x1, 0);
    89 
    90 
    91   //Maximization of x1
    92   //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
    93   double expected_opt=4.0/5.0;
    94   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
    95 
    96 
    97   //Restrict x2 to integer
    98   mip.colType(x2,MipSolver::INTEGER);
    99   expected_opt=1.0/2.0;
   100   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   101 
   102 
   103   //Restrict both to integer
   104   mip.colType(x1,MipSolver::INTEGER);
   105   expected_opt=0;
   106   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   107 
   108   //Erase a variable
   109   mip.erase(x2);
   110   mip.rowUpperBound(y2, 8);
   111   expected_opt=1;
   112   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
   113 
   114 }
   115 
   116 
   117 template<class MIP>
   118 void cloneTest()
   119 {
   120 
   121   MIP* mip = new MIP();
   122   MIP* mipnew = mip->newSolver();
   123   MIP* mipclone = mip->cloneSolver();
   124   delete mip;
   125   delete mipnew;
   126   delete mipclone;
   127 }
   128 
   129 int main()
   130 {
   131 
   132 #ifdef HAVE_GLPK
   133   {
   134     GlpkMip mip1;
   135     aTest(mip1);
   136     cloneTest<GlpkMip>();
   137   }
   138 #endif
   139 
   140 #ifdef HAVE_CPLEX
   141   try {
   142     CplexMip mip2;
   143     aTest(mip2);
   144     cloneTest<CplexMip>();
   145   } catch (CplexEnv::LicenseError& error) {
   146     check(false, error.what());
   147   }
   148 #endif
   149 
   150 #ifdef HAVE_CBC
   151   {
   152     CbcMip mip1;
   153     aTest(mip1);
   154     cloneTest<CbcMip>();
   155   }
   156 #endif
   157 
   158   return 0;
   159 
   160 }