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 deba@458: #include deba@458: #include "test_tools.h" deba@458: #include deba@458: deba@458: #ifdef HAVE_CONFIG_H deba@458: #include deba@458: #endif deba@458: deba@458: #ifdef HAVE_GLPK deba@458: #include deba@458: #endif deba@458: deba@458: #ifdef HAVE_CPLEX deba@458: #include deba@458: #endif deba@458: deba@458: #ifdef HAVE_SOPLEX deba@458: #include deba@458: #endif deba@458: deba@458: using namespace lemon; deba@458: deba@458: void lpTest(LpSolverBase & lp) deba@458: { deba@458: deba@458: deba@458: deba@458: typedef LpSolverBase LP; deba@458: deba@458: std::vector x(10); deba@458: // for(int i=0;i<10;i++) x.push_back(lp.addCol()); deba@458: lp.addColSet(x); deba@458: lp.colLowerBound(x,1); deba@458: lp.colUpperBound(x,1); deba@458: lp.colBounds(x,1,2); deba@458: #ifndef GYORSITAS deba@458: deba@458: std::vector y(10); deba@458: lp.addColSet(y); deba@458: deba@458: lp.colLowerBound(y,1); deba@458: lp.colUpperBound(y,1); deba@458: lp.colBounds(y,1,2); deba@458: deba@458: std::map z; deba@458: deba@458: z.insert(std::make_pair(12,INVALID)); deba@458: z.insert(std::make_pair(2,INVALID)); deba@458: z.insert(std::make_pair(7,INVALID)); deba@458: z.insert(std::make_pair(5,INVALID)); deba@458: deba@458: lp.addColSet(z); deba@458: deba@458: lp.colLowerBound(z,1); deba@458: lp.colUpperBound(z,1); deba@458: lp.colBounds(z,1,2); deba@458: deba@458: { deba@458: LP::Expr e,f,g; deba@458: LP::Col p1,p2,p3,p4,p5; deba@458: LP::Constr c; deba@458: deba@458: p1=lp.addCol(); deba@458: p2=lp.addCol(); deba@458: p3=lp.addCol(); deba@458: p4=lp.addCol(); deba@458: p5=lp.addCol(); deba@458: deba@458: e[p1]=2; deba@458: e.constComp()=12; deba@458: e[p1]+=2; deba@458: e.constComp()+=12; deba@458: e[p1]-=2; deba@458: e.constComp()-=12; deba@458: deba@458: e=2; deba@458: e=2.2; deba@458: e=p1; deba@458: e=f; deba@458: deba@458: e+=2; deba@458: e+=2.2; deba@458: e+=p1; deba@458: e+=f; deba@458: deba@458: e-=2; deba@458: e-=2.2; deba@458: e-=p1; deba@458: e-=f; deba@458: deba@458: e*=2; deba@458: e*=2.2; deba@458: e/=2; deba@458: e/=2.2; deba@458: deba@458: e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+ deba@458: (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+ deba@458: (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+ deba@458: 2.2*f+f*2.2+f/2.2+ deba@458: 2*f+f*2+f/2+ deba@458: 2.2*p1+p1*2.2+p1/2.2+ deba@458: 2*p1+p1*2+p1/2 deba@458: ); deba@458: deba@458: deba@458: c = (e <= f ); deba@458: c = (e <= 2.2); deba@458: c = (e <= 2 ); deba@458: c = (e <= p1 ); deba@458: c = (2.2<= f ); deba@458: c = (2 <= f ); deba@458: c = (p1 <= f ); deba@458: c = (p1 <= p2 ); deba@458: c = (p1 <= 2.2); deba@458: c = (p1 <= 2 ); deba@458: c = (2.2<= p2 ); deba@458: c = (2 <= p2 ); deba@458: deba@458: c = (e >= f ); deba@458: c = (e >= 2.2); deba@458: c = (e >= 2 ); deba@458: c = (e >= p1 ); deba@458: c = (2.2>= f ); deba@458: c = (2 >= f ); deba@458: c = (p1 >= f ); deba@458: c = (p1 >= p2 ); deba@458: c = (p1 >= 2.2); deba@458: c = (p1 >= 2 ); deba@458: c = (2.2>= p2 ); deba@458: c = (2 >= p2 ); deba@458: deba@458: c = (e == f ); deba@458: c = (e == 2.2); deba@458: c = (e == 2 ); deba@458: c = (e == p1 ); deba@458: c = (2.2== f ); deba@458: c = (2 == f ); deba@458: c = (p1 == f ); deba@458: //c = (p1 == p2 ); deba@458: c = (p1 == 2.2); deba@458: c = (p1 == 2 ); deba@458: c = (2.2== p2 ); deba@458: c = (2 == p2 ); deba@458: deba@458: c = (2 <= e <= 3); deba@458: c = (2 <= p1<= 3); deba@458: deba@458: c = (2 >= e >= 3); deba@458: c = (2 >= p1>= 3); deba@458: deba@458: e[x[3]]=2; deba@458: e[x[3]]=4; deba@458: e[x[3]]=1; deba@458: e.constComp()=12; deba@458: deba@458: lp.addRow(LP::INF,e,23); deba@458: lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23); deba@458: lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23); deba@458: deba@458: lp.addRow(x[1]+x[3]<=x[5]-3); deba@458: lp.addRow(-7<=x[1]+x[3]-12<=3); deba@458: lp.addRow(x[1]<=x[5]); deba@458: deba@458: std::ostringstream buf; deba@458: deba@458: deba@458: //Checking the simplify function deba@458: deba@458: // //How to check the simplify function? A map gives no information deba@458: // //on the question whether a given key is or is not stored in it, or deba@458: // //it does? deba@458: // Yes, it does, using the find() function. deba@458: e=((p1+p2)+(p1-p2)); deba@458: e.simplify(); deba@458: buf << "Coeff. of p2 should be 0"; deba@458: // std::cout<0, buf.str()); deba@458: deba@458: tolerance=0.02; deba@458: e.simplify(tolerance); deba@458: buf << "Coeff. of p2 should be 0"; deba@458: check(e.find(p2)==e.end(), buf.str()); deba@458: deba@458: deba@458: } deba@458: deba@458: { deba@458: LP::DualExpr e,f,g; deba@458: LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID, deba@458: p4 = INVALID, p5 = INVALID; deba@458: deba@458: e[p1]=2; deba@458: e[p1]+=2; deba@458: e[p1]-=2; deba@458: deba@458: e=p1; deba@458: e=f; deba@458: deba@458: e+=p1; deba@458: e+=f; deba@458: deba@458: e-=p1; deba@458: e-=f; deba@458: deba@458: e*=2; deba@458: e*=2.2; deba@458: e/=2; deba@458: e/=2.2; deba@458: deba@458: e=((p1+p2)+(p1-p2)+ deba@458: (p1+f)+(f+p1)+(f+g)+ deba@458: (p1-f)+(f-p1)+(f-g)+ deba@458: 2.2*f+f*2.2+f/2.2+ deba@458: 2*f+f*2+f/2+ deba@458: 2.2*p1+p1*2.2+p1/2.2+ deba@458: 2*p1+p1*2+p1/2 deba@458: ); deba@458: } deba@458: deba@458: #endif deba@458: } deba@458: deba@458: void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat, deba@458: double exp_opt) { deba@458: using std::string; deba@458: lp.solve(); deba@458: //int decimal,sign; deba@458: std::ostringstream buf; deba@458: buf << "Primalstatus should be: " << int(stat); deba@458: deba@458: // itoa(stat,buf1, 10); deba@458: check(lp.primalStatus()==stat, buf.str()); deba@458: deba@458: if (stat == LpSolverBase::OPTIMAL) { deba@458: std::ostringstream sbuf; deba@458: sbuf << "Wrong optimal value: the right optimum is " << exp_opt; deba@458: check(std::abs(lp.primalValue()-exp_opt) < 1e-3, sbuf.str()); deba@458: //+ecvt(exp_opt,2) deba@458: } deba@458: } deba@458: deba@458: void aTest(LpSolverBase & lp) deba@458: { deba@458: typedef LpSolverBase LP; deba@458: deba@458: //The following example is very simple deba@458: deba@458: typedef LpSolverBase::Row Row; deba@458: typedef LpSolverBase::Col Col; deba@458: deba@458: deba@458: Col x1 = lp.addCol(); deba@458: Col x2 = lp.addCol(); deba@458: deba@458: deba@458: //Constraints deba@458: Row upright=lp.addRow(x1+x2 <=1); deba@458: lp.addRow(x1+x2 >=-1); deba@458: lp.addRow(x1-x2 <=1); deba@458: lp.addRow(x1-x2 >=-1); deba@458: //Nonnegativity of the variables deba@458: lp.colLowerBound(x1, 0); deba@458: lp.colLowerBound(x2, 0); deba@458: //Objective function deba@458: lp.obj(x1+x2); deba@458: deba@458: lp.max(); deba@458: deba@458: //Testing the problem retrieving routines deba@458: check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!"); deba@458: check(lp.isMax(),"This is a maximization!"); deba@458: check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!"); deba@458: // std::cout<objCoeff(x1)==1,"First term should be 1 in the obj function!"); deba@458: check(clp->isMax(),"This is a maximization!"); deba@458: check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!"); deba@458: // std::cout<colLowerBound(x1)==0, deba@458: "The lower bound for variable x1 should be 0."); deba@458: check( clp->colUpperBound(x1)==LpSolverBase::INF, deba@458: "The upper bound for variable x1 should be infty."); deba@458: deba@458: clp->getRowBounds(upright,lb,ub); deba@458: check( lb==-LpSolverBase::INF, deba@458: "The lower bound for the first row should be -infty."); deba@458: check( ub==1,"The upper bound for the first row should be 1."); deba@458: e = clp->row(upright); deba@458: check( e.size() == 2, "The row retrieval gives back wrong expression."); deba@458: check( e[x1] == 1, "The first coefficient should 1."); deba@458: check( e[x2] == 1, "The second coefficient should 1."); deba@458: deba@458: de = clp->col(x1); deba@458: check( de.size() == 4, "The col retrieval gives back wrong expression."); deba@458: check( de[upright] == 1, "The first coefficient should 1."); deba@458: deba@458: delete clp; deba@458: deba@458: //Maximization of x1+x2 deba@458: //over the triangle with vertices (0,0) (0,1) (1,0) deba@458: double expected_opt=1; deba@458: solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt); deba@458: deba@458: //Minimization deba@458: lp.min(); deba@458: expected_opt=0; deba@458: solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt); deba@458: deba@458: //Vertex (-1,0) instead of (0,0) deba@458: lp.colLowerBound(x1, -LpSolverBase::INF); deba@458: expected_opt=-1; deba@458: solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt); deba@458: deba@458: //Erase one constraint and return to maximization deba@458: lp.eraseRow(upright); deba@458: lp.max(); deba@458: expected_opt=LpSolverBase::INF; deba@458: solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt); deba@458: deba@458: //Infeasibilty deba@458: lp.addRow(x1+x2 <=-2); deba@458: solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt); deba@458: deba@458: //Change problem and forget to solve deba@458: lp.min(); deba@458: check(lp.primalStatus()==LpSolverBase::UNDEFINED, deba@458: "Primalstatus should be UNDEFINED"); deba@458: deba@458: deba@458: // lp.solve(); deba@458: // if (lp.primalStatus()==LpSolverBase::OPTIMAL){ deba@458: // std::cout<< "Z = "<