deba@481: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@481: * deba@481: * This file is a part of LEMON, a generic C++ optimization library. deba@481: * deba@481: * Copyright (C) 2003-2008 deba@481: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@481: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@481: * deba@481: * Permission to use, modify and distribute this software is granted deba@481: * provided that this copyright notice appears in all copies. For deba@481: * precise terms see the accompanying LICENSE file. deba@481: * deba@481: * This software is provided "AS IS" with no warranty of any kind, deba@481: * express or implied, and with no claim as to its suitability for any deba@481: * purpose. deba@481: * deba@481: */ deba@481: deba@481: #include deba@481: #include deba@481: #include "test_tools.h" deba@481: #include deba@481: deba@481: #ifdef HAVE_CONFIG_H deba@481: #include deba@481: #endif deba@481: deba@481: #ifdef HAVE_GLPK deba@481: #include deba@481: #endif deba@481: deba@481: #ifdef HAVE_CPLEX deba@481: #include deba@481: #endif deba@481: deba@481: #ifdef HAVE_SOPLEX deba@481: #include deba@481: #endif deba@481: deba@481: using namespace lemon; deba@481: deba@481: void lpTest(LpSolverBase & lp) deba@481: { deba@481: deba@481: deba@481: deba@481: typedef LpSolverBase LP; deba@481: deba@481: std::vector x(10); deba@481: // for(int i=0;i<10;i++) x.push_back(lp.addCol()); deba@481: lp.addColSet(x); deba@481: lp.colLowerBound(x,1); deba@481: lp.colUpperBound(x,1); deba@481: lp.colBounds(x,1,2); deba@481: #ifndef GYORSITAS deba@481: deba@481: std::vector y(10); deba@481: lp.addColSet(y); deba@481: deba@481: lp.colLowerBound(y,1); deba@481: lp.colUpperBound(y,1); deba@481: lp.colBounds(y,1,2); deba@481: deba@481: std::map z; deba@481: deba@481: z.insert(std::make_pair(12,INVALID)); deba@481: z.insert(std::make_pair(2,INVALID)); deba@481: z.insert(std::make_pair(7,INVALID)); deba@481: z.insert(std::make_pair(5,INVALID)); deba@481: deba@481: lp.addColSet(z); deba@481: deba@481: lp.colLowerBound(z,1); deba@481: lp.colUpperBound(z,1); deba@481: lp.colBounds(z,1,2); deba@481: deba@481: { deba@481: LP::Expr e,f,g; deba@481: LP::Col p1,p2,p3,p4,p5; deba@481: LP::Constr c; deba@481: deba@481: p1=lp.addCol(); deba@481: p2=lp.addCol(); deba@481: p3=lp.addCol(); deba@481: p4=lp.addCol(); deba@481: p5=lp.addCol(); deba@481: deba@481: e[p1]=2; deba@481: e.constComp()=12; deba@481: e[p1]+=2; deba@481: e.constComp()+=12; deba@481: e[p1]-=2; deba@481: e.constComp()-=12; deba@481: deba@481: e=2; deba@481: e=2.2; deba@481: e=p1; deba@481: e=f; deba@481: deba@481: e+=2; deba@481: e+=2.2; deba@481: e+=p1; deba@481: e+=f; deba@481: deba@481: e-=2; deba@481: e-=2.2; deba@481: e-=p1; deba@481: e-=f; deba@481: deba@481: e*=2; deba@481: e*=2.2; deba@481: e/=2; deba@481: e/=2.2; deba@481: deba@481: e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+ deba@481: (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+ deba@481: (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+ deba@481: 2.2*f+f*2.2+f/2.2+ deba@481: 2*f+f*2+f/2+ deba@481: 2.2*p1+p1*2.2+p1/2.2+ deba@481: 2*p1+p1*2+p1/2 deba@481: ); deba@481: deba@481: deba@481: c = (e <= f ); deba@481: c = (e <= 2.2); deba@481: c = (e <= 2 ); deba@481: c = (e <= p1 ); deba@481: c = (2.2<= f ); deba@481: c = (2 <= f ); deba@481: c = (p1 <= f ); deba@481: c = (p1 <= p2 ); deba@481: c = (p1 <= 2.2); deba@481: c = (p1 <= 2 ); deba@481: c = (2.2<= p2 ); deba@481: c = (2 <= p2 ); deba@481: deba@481: c = (e >= f ); deba@481: c = (e >= 2.2); deba@481: c = (e >= 2 ); deba@481: c = (e >= p1 ); deba@481: c = (2.2>= f ); deba@481: c = (2 >= f ); deba@481: c = (p1 >= f ); deba@481: c = (p1 >= p2 ); deba@481: c = (p1 >= 2.2); deba@481: c = (p1 >= 2 ); deba@481: c = (2.2>= p2 ); deba@481: c = (2 >= p2 ); deba@481: deba@481: c = (e == f ); deba@481: c = (e == 2.2); deba@481: c = (e == 2 ); deba@481: c = (e == p1 ); deba@481: c = (2.2== f ); deba@481: c = (2 == f ); deba@481: c = (p1 == f ); deba@481: //c = (p1 == p2 ); deba@481: c = (p1 == 2.2); deba@481: c = (p1 == 2 ); deba@481: c = (2.2== p2 ); deba@481: c = (2 == p2 ); deba@481: deba@481: c = (2 <= e <= 3); deba@481: c = (2 <= p1<= 3); deba@481: deba@481: c = (2 >= e >= 3); deba@481: c = (2 >= p1>= 3); deba@481: deba@481: e[x[3]]=2; deba@481: e[x[3]]=4; deba@481: e[x[3]]=1; deba@481: e.constComp()=12; deba@481: deba@481: lp.addRow(LP::INF,e,23); deba@481: lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23); deba@481: lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23); deba@481: deba@481: lp.addRow(x[1]+x[3]<=x[5]-3); deba@481: lp.addRow(-7<=x[1]+x[3]-12<=3); deba@481: lp.addRow(x[1]<=x[5]); deba@481: deba@481: std::ostringstream buf; deba@481: deba@481: deba@481: //Checking the simplify function deba@481: deba@481: // //How to check the simplify function? A map gives no information deba@481: // //on the question whether a given key is or is not stored in it, or deba@481: // //it does? deba@481: // Yes, it does, using the find() function. deba@481: e=((p1+p2)+(p1-p2)); deba@481: e.simplify(); deba@481: buf << "Coeff. of p2 should be 0"; deba@481: // std::cout<0, buf.str()); deba@481: deba@481: tolerance=0.02; deba@481: e.simplify(tolerance); deba@481: buf << "Coeff. of p2 should be 0"; deba@481: check(e.find(p2)==e.end(), buf.str()); deba@481: deba@481: deba@481: } deba@481: deba@481: { deba@481: LP::DualExpr e,f,g; deba@481: LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID, deba@481: p4 = INVALID, p5 = INVALID; deba@481: deba@481: e[p1]=2; deba@481: e[p1]+=2; deba@481: e[p1]-=2; deba@481: deba@481: e=p1; deba@481: e=f; deba@481: deba@481: e+=p1; deba@481: e+=f; deba@481: deba@481: e-=p1; deba@481: e-=f; deba@481: deba@481: e*=2; deba@481: e*=2.2; deba@481: e/=2; deba@481: e/=2.2; deba@481: deba@481: e=((p1+p2)+(p1-p2)+ deba@481: (p1+f)+(f+p1)+(f+g)+ deba@481: (p1-f)+(f-p1)+(f-g)+ deba@481: 2.2*f+f*2.2+f/2.2+ deba@481: 2*f+f*2+f/2+ deba@481: 2.2*p1+p1*2.2+p1/2.2+ deba@481: 2*p1+p1*2+p1/2 deba@481: ); deba@481: } deba@481: deba@481: #endif deba@481: } deba@481: deba@481: void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat, deba@481: double exp_opt) { deba@481: using std::string; deba@481: lp.solve(); deba@481: //int decimal,sign; deba@481: std::ostringstream buf; deba@481: buf << "Primalstatus should be: " << int(stat); deba@481: deba@481: // itoa(stat,buf1, 10); deba@481: check(lp.primalStatus()==stat, buf.str()); deba@481: deba@481: if (stat == LpSolverBase::OPTIMAL) { deba@481: std::ostringstream sbuf; deba@481: sbuf << "Wrong optimal value: the right optimum is " << exp_opt; deba@481: check(std::abs(lp.primalValue()-exp_opt) < 1e-3, sbuf.str()); deba@481: //+ecvt(exp_opt,2) deba@481: } deba@481: } deba@481: deba@481: void aTest(LpSolverBase & lp) deba@481: { deba@481: typedef LpSolverBase LP; deba@481: deba@481: //The following example is very simple deba@481: deba@481: typedef LpSolverBase::Row Row; deba@481: typedef LpSolverBase::Col Col; deba@481: deba@481: deba@481: Col x1 = lp.addCol(); deba@481: Col x2 = lp.addCol(); deba@481: deba@481: deba@481: //Constraints deba@481: Row upright=lp.addRow(x1+x2 <=1); deba@481: lp.addRow(x1+x2 >=-1); deba@481: lp.addRow(x1-x2 <=1); deba@481: lp.addRow(x1-x2 >=-1); deba@481: //Nonnegativity of the variables deba@481: lp.colLowerBound(x1, 0); deba@481: lp.colLowerBound(x2, 0); deba@481: //Objective function deba@481: lp.obj(x1+x2); deba@481: deba@481: lp.max(); deba@481: deba@481: //Testing the problem retrieving routines deba@481: check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!"); deba@481: check(lp.isMax(),"This is a maximization!"); deba@481: check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!"); deba@481: // std::cout<objCoeff(x1)==1,"First term should be 1 in the obj function!"); deba@481: check(clp->isMax(),"This is a maximization!"); deba@481: check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!"); deba@481: // std::cout<colLowerBound(x1)==0, deba@481: "The lower bound for variable x1 should be 0."); deba@481: check( clp->colUpperBound(x1)==LpSolverBase::INF, deba@481: "The upper bound for variable x1 should be infty."); deba@481: deba@481: clp->getRowBounds(upright,lb,ub); deba@481: check( lb==-LpSolverBase::INF, deba@481: "The lower bound for the first row should be -infty."); deba@481: check( ub==1,"The upper bound for the first row should be 1."); deba@481: e = clp->row(upright); deba@481: check( e.size() == 2, "The row retrieval gives back wrong expression."); deba@481: check( e[x1] == 1, "The first coefficient should 1."); deba@481: check( e[x2] == 1, "The second coefficient should 1."); deba@481: deba@481: de = clp->col(x1); deba@481: check( de.size() == 4, "The col retrieval gives back wrong expression."); deba@481: check( de[upright] == 1, "The first coefficient should 1."); deba@481: deba@481: delete clp; deba@481: deba@481: //Maximization of x1+x2 deba@481: //over the triangle with vertices (0,0) (0,1) (1,0) deba@481: double expected_opt=1; deba@481: solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt); deba@481: deba@481: //Minimization deba@481: lp.min(); deba@481: expected_opt=0; deba@481: solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt); deba@481: deba@481: //Vertex (-1,0) instead of (0,0) deba@481: lp.colLowerBound(x1, -LpSolverBase::INF); deba@481: expected_opt=-1; deba@481: solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt); deba@481: deba@481: //Erase one constraint and return to maximization deba@481: lp.eraseRow(upright); deba@481: lp.max(); deba@481: expected_opt=LpSolverBase::INF; deba@481: solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt); deba@481: deba@481: //Infeasibilty deba@481: lp.addRow(x1+x2 <=-2); deba@481: solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt); deba@481: deba@481: //Change problem and forget to solve deba@481: lp.min(); deba@481: check(lp.primalStatus()==LpSolverBase::UNDEFINED, deba@481: "Primalstatus should be UNDEFINED"); deba@481: deba@481: deba@481: // lp.solve(); deba@481: // if (lp.primalStatus()==LpSolverBase::OPTIMAL){ deba@481: // std::cout<< "Z = "<