1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
 
     3  * This file is a part of LEMON, a generic C++ optimization library.
 
     5  * Copyright (C) 2003-2009
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     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.
 
    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
 
    19 #include "test_tools.h"
 
    22 #include <lemon/config.h>
 
    26 #include <lemon/cplex.h>
 
    30 #include <lemon/glpk.h>
 
    34 #include <lemon/cbc.h>
 
    38 using namespace lemon;
 
    40 void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat,
 
    46   std::ostringstream buf;
 
    47   buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type());
 
    50   //  itoa(stat,buf1, 10);
 
    51   check(mip.type()==stat, buf.str());
 
    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());
 
    61 void aTest(MipSolver& mip)
 
    63   //The following example is very simple
 
    66   typedef MipSolver::Row Row;
 
    67   typedef MipSolver::Col Col;
 
    70   Col x1 = mip.addCol();
 
    71   Col x2 = mip.addCol();
 
    79   //Unconstrained optimization
 
    84   mip.addRow(2 * x1 + x2 <= 2);
 
    85   Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
 
    87   //Nonnegativity of the variable x1
 
    88   mip.colLowerBound(x1, 0);
 
    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);
 
    97   //Restrict x2 to integer
 
    98   mip.colType(x2,MipSolver::INTEGER);
 
   100   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
 
   103   //Restrict both to integer
 
   104   mip.colType(x1,MipSolver::INTEGER);
 
   106   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
 
   110   mip.rowUpperBound(y2, 8);
 
   112   solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
 
   121   MIP* mip = new MIP();
 
   122   MIP* mipnew = mip->newSolver();
 
   123   MIP* mipclone = mip->cloneSolver();
 
   136     cloneTest<GlpkMip>();
 
   144     cloneTest<CplexMip>();
 
   145   } catch (CplexEnv::LicenseError& error) {
 
   146 #ifdef LEMON_FORCE_CPLEX_CHECK
 
   147     check(false, error.what());
 
   149     std::cerr << error.what() << std::endl;
 
   150     std::cerr << "Cplex license check failed, lp check skipped" << std::endl;