/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2008
* 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/lp_glpk.h>
#include <lemon/lp_cplex.h>
#include <lemon/lp_soplex.h>
void lpTest(LpSolverBase & 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,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);
//Checking the simplify function
// //How to check the simplify function? A map gives no information
// //on the question whether a given key is or is not stored in it, or
// Yes, it does, using the find() function.
buf << "Coeff. of p2 should be 0";
// std::cout<<e[p1]<<e[p2]<<e[p3]<<std::endl;
check(e.find(p2)==e.end(), buf.str());
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(e.find(p2)==e.end(), buf.str());
LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID,
p4 = INVALID, p5 = INVALID;
void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat,
buf << "Primalstatus should be: " << int(stat);
check(lp.primalStatus()==stat, buf.str());
if (stat == LpSolverBase::OPTIMAL) {
sbuf << "Wrong optimal value: the right optimum is " << exp_opt;
check(std::abs(lp.primalValue()-exp_opt) < 1e-3, sbuf.str());
void aTest(LpSolverBase & lp)
//The following example is very simple
typedef LpSolverBase::Row Row;
typedef LpSolverBase::Col Col;
Row upright=lp.addRow(x1+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.isMax(),"This is a maximization!");
check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
// std::cout<<lp.colLowerBound(x1)<<std::endl;
check( lp.colLowerBound(x1)==0,
"The lower bound for variable x1 should be 0.");
check( lp.colUpperBound(x1)==LpSolverBase::INF,
"The upper bound for variable x1 should be infty.");
LpSolverBase::Value lb,ub;
lp.getRowBounds(upright,lb,ub);
check( lb==-LpSolverBase::INF,
"The lower bound for the first row should be -infty.");
check( ub==1,"The upper bound for the first row should be 1.");
LpSolverBase::Expr e = lp.row(upright);
check( e.size() == 2, "The row retrieval gives back wrong expression.");
check( e[x1] == 1, "The first coefficient should 1.");
check( e[x2] == 1, "The second coefficient should 1.");
LpSolverBase::DualExpr de = lp.col(x1);
check( de.size() == 4, "The col retrieval gives back wrong expression.");
check( de[upright] == 1, "The first coefficient should 1.");
LpSolverBase* clp = lp.copyLp();
//Testing the problem retrieving routines
check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
check(clp->isMax(),"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)==LpSolverBase::INF,
"The upper bound for variable x1 should be infty.");
clp->getRowBounds(upright,lb,ub);
check( lb==-LpSolverBase::INF,
"The lower bound for the first row should be -infty.");
check( ub==1,"The upper bound for the first row should be 1.");
check( e.size() == 2, "The row retrieval gives back wrong expression.");
check( e[x1] == 1, "The first coefficient should 1.");
check( e[x2] == 1, "The second coefficient should 1.");
check( de.size() == 4, "The col retrieval gives back wrong expression.");
check( de[upright] == 1, "The first coefficient should 1.");
//over the triangle with vertices (0,0) (0,1) (1,0)
solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
//Vertex (-1,0) instead of (0,0)
lp.colLowerBound(x1, -LpSolverBase::INF);
solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
//Erase one constraint and return to maximization
expected_opt=LpSolverBase::INF;
solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
//Change problem and forget to solve
check(lp.primalStatus()==LpSolverBase::UNDEFINED,
"Primalstatus should be UNDEFINED");
// if (lp.primalStatus()==LpSolverBase::OPTIMAL){
// std::cout<< "Z = "<<lp.primalValue()
// << " (error = " << lp.primalValue()-expected_opt
// << "); x1 = "<<lp.primal(x1)
// << "; x2 = "<<lp.primal(x2)
// std::cout<<lp.primalStatus()<<std::endl;
// std::cout<<"Optimal solution not found!"<<std::endl;
LpGlpk lp_glpk1,lp_glpk2;
LpCplex lp_cplex1,lp_cplex2;
LpSoplex lp_soplex1,lp_soplex2;