deba@458: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@458: * deba@458: * This file is a part of LEMON, a generic C++ optimization library. deba@458: * deba@458: * Copyright (C) 2003-2008 deba@458: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@458: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@458: * deba@458: * Permission to use, modify and distribute this software is granted deba@458: * provided that this copyright notice appears in all copies. For deba@458: * precise terms see the accompanying LICENSE file. deba@458: * deba@458: * This software is provided "AS IS" with no warranty of any kind, deba@458: * express or implied, and with no claim as to its suitability for any deba@458: * purpose. deba@458: * deba@458: */ deba@458: deba@458: #include "test_tools.h" deba@458: deba@458: deba@458: #ifdef HAVE_CONFIG_H deba@458: #include <lemon/config.h> deba@458: #endif deba@458: deba@458: #ifdef HAVE_CPLEX alpar@461: #include <lemon/cplex.h> deba@458: #endif deba@458: deba@458: #ifdef HAVE_GLPK alpar@461: #include <lemon/glpk.h> deba@458: #endif deba@458: deba@458: deba@458: using namespace lemon; deba@458: deba@459: void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat, deba@458: double exp_opt) { deba@458: using std::string; deba@458: deba@459: mip.solve(); deba@458: //int decimal,sign; deba@458: std::ostringstream buf; deba@459: buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type()); deba@458: deba@458: deba@458: // itoa(stat,buf1, 10); deba@459: check(mip.type()==stat, buf.str()); deba@458: deba@459: if (stat == MipSolver::OPTIMAL) { deba@458: std::ostringstream sbuf; deba@458: buf << "Wrong optimal value: the right optimum is " << exp_opt; deba@459: check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str()); deba@458: //+ecvt(exp_opt,2) deba@458: } deba@458: } deba@458: deba@459: void aTest(MipSolver& mip) deba@458: { deba@458: //The following example is very simple deba@458: deba@458: deba@459: typedef MipSolver::Row Row; deba@459: typedef MipSolver::Col Col; deba@458: deba@458: deba@458: deba@458: Col x1 = mip.addCol(); deba@458: Col x2 = mip.addCol(); deba@458: deba@458: deba@458: //Objective function deba@458: mip.obj(x1); deba@458: deba@458: mip.max(); deba@458: deba@458: deba@458: //Unconstrained optimization deba@458: mip.solve(); deba@458: //Check it out! deba@458: deba@458: //Constraints deba@458: mip.addRow(2*x1+x2 <=2); deba@458: mip.addRow(x1-2*x2 <=0); deba@458: deba@458: //Nonnegativity of the variable x1 deba@458: mip.colLowerBound(x1, 0); deba@458: deba@458: //Maximization of x1 deba@458: //over the triangle with vertices (0,0),(4/5,2/5),(0,2) deba@458: double expected_opt=4.0/5.0; deba@459: solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); deba@458: deba@458: //Restrict x2 to integer deba@459: mip.colType(x2,MipSolver::INTEGER); deba@458: expected_opt=1.0/2.0; deba@459: solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); deba@458: deba@458: deba@458: //Restrict both to integer deba@459: mip.colType(x1,MipSolver::INTEGER); deba@458: expected_opt=0; deba@459: solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); deba@458: deba@458: deba@458: deba@458: } deba@458: deba@458: deba@458: int main() deba@458: { deba@458: deba@458: #ifdef HAVE_GLPK deba@459: { alpar@462: GlpkMip mip1; deba@459: aTest(mip1); deba@459: } deba@458: #endif deba@458: deba@458: #ifdef HAVE_CPLEX deba@459: try { alpar@462: CplexMip mip2; deba@459: aTest(mip2); deba@459: } catch (CplexEnv::LicenseError& error) { deba@459: #ifdef LEMON_FORCE_CPLEX_CHECK deba@459: check(false, error.what()); deba@459: #else deba@459: std::cerr << error.what() << std::endl; deba@459: std::cerr << "Cplex license check failed, lp check skipped" << std::endl; deba@459: #endif deba@459: } deba@458: #endif deba@458: deba@458: return 0; deba@458: deba@458: }