alpar@1956: /* -*- C++ -*- alpar@1956: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@1956: * Copyright (C) 2003-2006 alpar@1956: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1956: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@1956: * alpar@1956: * Permission to use, modify and distribute this software is granted alpar@1956: * provided that this copyright notice appears in all copies. For alpar@1956: * precise terms see the accompanying LICENSE file. alpar@1956: * alpar@1956: * This software is provided "AS IS" with no warranty of any kind, alpar@1956: * express or implied, and with no claim as to its suitability for any alpar@1956: * purpose. alpar@1956: * alpar@1956: */ alpar@1956: athos@1543: #include athos@1543: #include athos@1473: #include "test_tools.h" athos@2264: #include athos@1542: ladanyi@1387: #ifdef HAVE_CONFIG_H ladanyi@1387: #include ladanyi@1387: #endif ladanyi@1387: ladanyi@1387: #ifdef HAVE_GLPK ladanyi@1387: #include ladanyi@1437: #endif ladanyi@1437: ladanyi@1437: #ifdef HAVE_CPLEX ladanyi@1387: #include ladanyi@1387: #endif alpar@1254: deba@2314: // #ifdef HAVE_SOPLEX deba@2314: // #include deba@2314: // #endif deba@2314: alpar@1254: using namespace lemon; alpar@1254: alpar@1263: void lpTest(LpSolverBase & lp) alpar@1254: { athos@1508: athos@1508: athos@1508: alpar@1263: typedef LpSolverBase LP; alpar@1256: alpar@1309: std::vector x(10); alpar@1309: // for(int i=0;i<10;i++) x.push_back(lp.addCol()); alpar@1309: lp.addColSet(x); alpar@1895: lp.colLowerBound(x,1); alpar@1895: lp.colUpperBound(x,1); alpar@1895: lp.colBounds(x,1,2); athos@1508: #ifndef GYORSITAS athos@1508: alpar@1256: std::vector y(10); alpar@1256: lp.addColSet(y); alpar@1256: alpar@1895: lp.colLowerBound(y,1); alpar@1895: lp.colUpperBound(y,1); alpar@1895: lp.colBounds(y,1,2); alpar@1895: alpar@1256: std::map z; alpar@1256: alpar@1256: z.insert(std::make_pair(12,INVALID)); alpar@1256: z.insert(std::make_pair(2,INVALID)); alpar@1256: z.insert(std::make_pair(7,INVALID)); alpar@1256: z.insert(std::make_pair(5,INVALID)); alpar@1895: alpar@1256: lp.addColSet(z); alpar@1256: alpar@1895: lp.colLowerBound(z,1); alpar@1895: lp.colUpperBound(z,1); alpar@1895: lp.colBounds(z,1,2); alpar@1895: alpar@1445: { alpar@1445: LP::Expr e,f,g; alpar@1445: LP::Col p1,p2,p3,p4,p5; alpar@1445: LP::Constr c; alpar@1445: alpar@1484: p1=lp.addCol(); alpar@1484: p2=lp.addCol(); alpar@1484: p3=lp.addCol(); alpar@1484: p4=lp.addCol(); alpar@1484: p5=lp.addCol(); alpar@1484: alpar@1445: e[p1]=2; alpar@1445: e.constComp()=12; alpar@1445: e[p1]+=2; alpar@1445: e.constComp()+=12; alpar@1445: e[p1]-=2; alpar@1445: e.constComp()-=12; alpar@1445: alpar@1445: e=2; alpar@1445: e=2.2; alpar@1445: e=p1; alpar@1445: e=f; alpar@1445: alpar@1445: e+=2; alpar@1445: e+=2.2; alpar@1445: e+=p1; alpar@1445: e+=f; alpar@1445: alpar@1445: e-=2; alpar@1445: e-=2.2; alpar@1445: e-=p1; alpar@1445: e-=f; alpar@1445: alpar@1445: e*=2; alpar@1445: e*=2.2; alpar@1445: e/=2; alpar@1445: e/=2.2; alpar@1445: alpar@1445: e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+ alpar@1445: (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+ alpar@1445: (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+ alpar@1445: 2.2*f+f*2.2+f/2.2+ alpar@1445: 2*f+f*2+f/2+ alpar@1445: 2.2*p1+p1*2.2+p1/2.2+ alpar@1445: 2*p1+p1*2+p1/2 alpar@1445: ); alpar@1256: alpar@1445: alpar@1445: c = (e <= f ); alpar@1445: c = (e <= 2.2); alpar@1445: c = (e <= 2 ); alpar@1445: c = (e <= p1 ); alpar@1445: c = (2.2<= f ); alpar@1445: c = (2 <= f ); alpar@1445: c = (p1 <= f ); alpar@1445: c = (p1 <= p2 ); alpar@1445: c = (p1 <= 2.2); alpar@1445: c = (p1 <= 2 ); alpar@1445: c = (2.2<= p2 ); alpar@1445: c = (2 <= p2 ); alpar@1445: alpar@1445: c = (e >= f ); alpar@1445: c = (e >= 2.2); alpar@1445: c = (e >= 2 ); alpar@1445: c = (e >= p1 ); alpar@1445: c = (2.2>= f ); alpar@1445: c = (2 >= f ); alpar@1445: c = (p1 >= f ); alpar@1445: c = (p1 >= p2 ); alpar@1445: c = (p1 >= 2.2); alpar@1445: c = (p1 >= 2 ); alpar@1445: c = (2.2>= p2 ); alpar@1445: c = (2 >= p2 ); alpar@1445: alpar@1445: c = (e == f ); alpar@1445: c = (e == 2.2); alpar@1445: c = (e == 2 ); alpar@1445: c = (e == p1 ); alpar@1445: c = (2.2== f ); alpar@1445: c = (2 == f ); alpar@1445: c = (p1 == f ); alpar@1445: //c = (p1 == p2 ); alpar@1445: c = (p1 == 2.2); alpar@1445: c = (p1 == 2 ); alpar@1445: c = (2.2== p2 ); alpar@1445: c = (2 == p2 ); alpar@1445: alpar@1445: c = (2 <= e <= 3); alpar@1445: c = (2 <= p1<= 3); alpar@1445: alpar@1445: c = (2 >= e >= 3); alpar@1445: c = (2 >= p1>= 3); alpar@1445: alpar@1445: e[x[3]]=2; alpar@1445: e[x[3]]=4; alpar@1445: e[x[3]]=1; alpar@1445: e.constComp()=12; alpar@1445: alpar@1445: lp.addRow(LP::INF,e,23); alpar@1445: lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23); alpar@1445: lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23); alpar@1445: alpar@1445: lp.addRow(x[1]+x[3]<=x[5]-3); alpar@1445: lp.addRow(-7<=x[1]+x[3]-12<=3); alpar@1445: lp.addRow(x[1]<=x[5]); athos@2264: athos@2264: std::ostringstream buf; athos@2264: athos@2264: athos@2264: //Checking the simplify function athos@2264: athos@2264: // //How to check the simplify function? A map gives no information athos@2264: // //on the question whether a given key is or is not stored in it, or athos@2264: // //it does? athos@2264: // Yes, it does, using the find() function. athos@2264: e=((p1+p2)+(p1-p2)); athos@2264: e.simplify(); athos@2264: buf << "Coeff. of p2 should be 0"; athos@2264: // std::cout<0, buf.str()); athos@2264: athos@2264: tolerance=0.02; athos@2264: e.simplify(tolerance); athos@2264: buf << "Coeff. of p2 should be 0"; athos@2264: check(e.find(p2)==e.end(), buf.str()); athos@2264: athos@2264: alpar@1445: } alpar@1272: alpar@1445: { alpar@1445: LP::DualExpr e,f,g; alpar@1445: LP::Row p1,p2,p3,p4,p5; alpar@1445: alpar@1445: e[p1]=2; alpar@1445: e[p1]+=2; alpar@1445: e[p1]-=2; alpar@1445: alpar@1445: e=p1; alpar@1445: e=f; alpar@1445: alpar@1445: e+=p1; alpar@1445: e+=f; alpar@1445: alpar@1445: e-=p1; alpar@1445: e-=f; alpar@1445: alpar@1445: e*=2; alpar@1445: e*=2.2; alpar@1445: e/=2; alpar@1445: e/=2.2; alpar@1445: alpar@1493: e=((p1+p2)+(p1-p2)+ alpar@1445: (p1+f)+(f+p1)+(f+g)+ alpar@1445: (p1-f)+(f-p1)+(f-g)+ alpar@1445: 2.2*f+f*2.2+f/2.2+ alpar@1445: 2*f+f*2+f/2+ alpar@1445: 2.2*p1+p1*2.2+p1/2.2+ alpar@1445: 2*p1+p1*2+p1/2 alpar@1445: ); alpar@1445: } alpar@1272: athos@1508: #endif alpar@1264: } alpar@1264: athos@1542: void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat, athos@1543: double exp_opt) { athos@1543: using std::string; athos@1542: lp.solve(); athos@1542: //int decimal,sign; athos@1543: std::ostringstream buf; athos@1543: buf << "Primalstatus should be: " << int(stat); athos@1543: athos@1542: // itoa(stat,buf1, 10); athos@1543: check(lp.primalStatus()==stat, buf.str()); athos@1543: athos@1543: if (stat == LpSolverBase::OPTIMAL) { athos@1543: std::ostringstream buf; athos@1543: buf << "Wrong optimal value: the right optimum is " << exp_opt; athos@1543: check(std::abs(lp.primalValue()-exp_opt) < 1e-3, buf.str()); athos@1542: //+ecvt(exp_opt,2) athos@1542: } athos@1542: } athos@1542: athos@1473: void aTest(LpSolverBase & lp) athos@1473: { athos@1473: typedef LpSolverBase LP; athos@1473: athos@1542: //The following example is very simple athos@1473: athos@1473: typedef LpSolverBase::Row Row; athos@1473: typedef LpSolverBase::Col Col; athos@1473: athos@1473: athos@1473: Col x1 = lp.addCol(); athos@1473: Col x2 = lp.addCol(); athos@1473: athos@1473: athos@1473: //Constraints athos@1542: Row upright=lp.addRow(x1+x2 <=1); athos@1542: lp.addRow(x1+x2 >=-1); athos@1542: lp.addRow(x1-x2 <=1); athos@1542: lp.addRow(x1-x2 >=-1); athos@1473: //Nonnegativity of the variables athos@1473: lp.colLowerBound(x1, 0); athos@1473: lp.colLowerBound(x2, 0); athos@1473: //Objective function athos@1542: lp.setObj(x1+x2); athos@1473: athos@1473: lp.max(); athos@1473: athos@2324: athos@2324: //Testing the problem retrieving routines athos@2324: check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!"); athos@2324: check(lp.is_max(),"This is a maximization!"); athos@2328: check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!"); athos@2328: // std::cout<