/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2009
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
#include <lemon/lp_skeleton.h>
#include <lemon/tolerance.h>
#include <lemon/config.h>
#include <lemon/soplex.h>
void lpTest(LpSolver& lp)
std::vector<LP::Col> x(10);
// for(int i=0;i<10;i++) x.push_back(lp.addCol());
std::vector<LP::Col> y(10);
z.insert(std::make_pair(12,INVALID));
z.insert(std::make_pair(2,INVALID));
z.insert(std::make_pair(7,INVALID));
z.insert(std::make_pair(5,INVALID));
e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
(f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
(f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
lp.addRow(-LP::INF,e,23);
lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
lp.addRow(-LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
lp.addRow(x[1]+x[3]<=x[5]-3);
lp.addRow((-7<=x[1]+x[3]-12)<=3);
e=((p1+p2)+(p1-0.99*p2));
//e.prettyPrint(std::cout);
//(e<=2).prettyPrint(std::cout);
buf << "Coeff. of p2 should be 0.01";
check(e[p2]>0, buf.str());
buf << "Coeff. of p2 should be 0";
check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str());
LP* lpnew = lp.newSolver();
LP* lpclone = lp.cloneSolver();
LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID,
p4 = INVALID, p5 = INVALID;
void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
check(lp.primalType()==stat, buf.str());
if (stat == LpSolver::OPTIMAL) {
sbuf << "Wrong optimal value (" << lp.primal() <<") with "
<< lp.solverName() <<"\n the right optimum is " << exp_opt;
check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
void aTest(LpSolver & lp)
//The following example is very simple
typedef LpSolver::Row Row;
typedef LpSolver::Col Col;
Row upright=lp.addRow(x1+2*x2 <=1);
//Nonnegativity of the variables
//Testing the problem retrieving routines
check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
check(lp.sense() == lp.MAX,"This is a maximization!");
check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
check(lp.colLowerBound(x1)==0,
"The lower bound for variable x1 should be 0.");
check(lp.colUpperBound(x1)==LpSolver::INF,
"The upper bound for variable x1 should be infty.");
check(lp.rowLowerBound(upright) == -LpSolver::INF,
"The lower bound for the first row should be -infty.");
check(lp.rowUpperBound(upright)==1,
"The upper bound for the first row should be 1.");
LpSolver::Expr e = lp.row(upright);
check(e[x1] == 1, "The first coefficient should 1.");
check(e[x2] == 2, "The second coefficient should 1.");
lp.row(upright, x1+x2 <=1);
check(e[x1] == 1, "The first coefficient should 1.");
check(e[x2] == 1, "The second coefficient should 1.");
LpSolver::DualExpr de = lp.col(x1);
check( de[upright] == 1, "The first coefficient should 1.");
LpSolver* clp = lp.cloneSolver();
//Testing the problem retrieving routines
check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
check(clp->sense() == clp->MAX,"This is a maximization!");
check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
// std::cout<<lp.colLowerBound(x1)<<std::endl;
check(clp->colLowerBound(x1)==0,
"The lower bound for variable x1 should be 0.");
check(clp->colUpperBound(x1)==LpSolver::INF,
"The upper bound for variable x1 should be infty.");
check(lp.rowLowerBound(upright)==-LpSolver::INF,
"The lower bound for the first row should be -infty.");
check(lp.rowUpperBound(upright)==1,
"The upper bound for the first row should be 1.");
check(e[x1] == 1, "The first coefficient should 1.");
check(e[x2] == 1, "The second coefficient should 1.");
check(de[upright] == 1, "The first coefficient should 1.");
//over the triangle with vertices (0,0) (0,1) (1,0)
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
//Vertex (-1,0) instead of (0,0)
lp.colLowerBound(x1, -LpSolver::INF);
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
//Erase one constraint and return to maximization
expected_opt=LpSolver::INF;
solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
LP* lpnew = lp->newSolver();
LP* lpclone = lp->cloneSolver();
GlpkLp lp_glpk1,lp_glpk2;
CplexLp lp_cplex1,lp_cplex2;
} catch (CplexEnv::LicenseError& error) {
check(false, error.what());
SoplexLp lp_soplex1,lp_soplex2;