[Lemon-commits] Alpar Juttner: Merge #640
Lemon HG
hg at lemon.cs.elte.hu
Thu Feb 25 10:24:25 CET 2021
details: http://lemon.cs.elte.hu/hg/lemon/rev/eba5aa390aca
changeset: 1429:eba5aa390aca
user: Alpar Juttner <alpar [at] cs.elte.hu>
date: Thu Jan 21 18:58:37 2021 +0100
description:
Merge #640
diffstat:
cmake/FindILOG.cmake | 2 +-
lemon/clp.cc | 18 ++++++++++++++----
lemon/clp.h | 2 ++
lemon/cplex.cc | 2 +-
test/lp_test.cc | 33 +++++++++++++++++++++++++++++++++
test/mip_test.cc | 11 ++++++++---
6 files changed, 59 insertions(+), 9 deletions(-)
diffs (194 lines):
diff --git a/cmake/FindILOG.cmake b/cmake/FindILOG.cmake
--- a/cmake/FindILOG.cmake
+++ b/cmake/FindILOG.cmake
@@ -96,7 +96,7 @@
SET(ILOG_LIBRARIES ${ILOG_CPLEX_LIBRARY} ${ILOG_CONCERT_LIBRARY})
IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
# SET(CPLEX_LIBRARIES "${CPLEX_LIBRARIES};m;pthread")
- SET(ILOG_LIBRARIES ${ILOG_LIBRARIES} "m" "pthread")
+ SET(ILOG_LIBRARIES ${ILOG_LIBRARIES} "m" "pthread" "dl")
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
ENDIF(ILOG_FOUND)
diff --git a/lemon/clp.cc b/lemon/clp.cc
--- a/lemon/clp.cc
+++ b/lemon/clp.cc
@@ -227,14 +227,14 @@
}
ClpLp::Value ClpLp::_getCoeff(int ix, int jx) const {
- CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
- CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
+ CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[jx];
+ CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[jx];
const int* indices = _prob->clpMatrix()->getIndices();
const double* elements = _prob->clpMatrix()->getElements();
- const int* it = std::lower_bound(indices + begin, indices + end, jx);
- if (it != indices + end && *it == jx) {
+ const int* it = std::lower_bound(indices + begin, indices + end, ix);
+ if (it != indices + end && *it == ix) {
return elements[it - indices];
} else {
return 0.0;
@@ -461,4 +461,14 @@
}
}
+ void ClpLp::_write(std::string file, std::string format) const
+ {
+ if(format == "LP")
+ _prob->writeLp(file.c_str(), "", 1e-5, 10, 5,
+ sense()==ClpLp::MIN?1:-1,
+ true
+ );
+ else throw UnsupportedFormatError(format);
+ }
+
} //END OF NAMESPACE LEMON
diff --git a/lemon/clp.h b/lemon/clp.h
--- a/lemon/clp.h
+++ b/lemon/clp.h
@@ -139,6 +139,8 @@
virtual void _messageLevel(MessageLevel);
+ void _write(std::string file, std::string format) const;
+
public:
///Solves LP with primal simplex method.
diff --git a/lemon/cplex.cc b/lemon/cplex.cc
--- a/lemon/cplex.cc
+++ b/lemon/cplex.cc
@@ -158,7 +158,7 @@
} else {
const char s = 'R';
double len = ub - lb;
- CPXaddrows(cplexEnv(), _prob, 0, 1, values.size(), &ub, &s,
+ CPXaddrows(cplexEnv(), _prob, 0, 1, values.size(), &lb, &s,
&rmatbeg, &indices.front(), &values.front(), 0, 0);
CPXchgrngval(cplexEnv(), _prob, 1, &i, &len);
}
diff --git a/test/lp_test.cc b/test/lp_test.cc
--- a/test/lp_test.cc
+++ b/test/lp_test.cc
@@ -339,6 +339,7 @@
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.coeff(upright,x2)==2,"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,
@@ -424,6 +425,34 @@
delete lpclone;
}
+template<class LP>
+void rangeConstraintTest()
+{
+ LP lp;
+ // Add two columns (variables) to the problem
+ typename LP::Col x1 = lp.addCol();
+ typename LP::Col x2 = lp.addCol();
+ // Add rows (constraints) to the problem
+ lp.addRow(x1 - 5 <= x2);
+ lp.addRow(0 <= 2 * x1 + x2 <= 25);
+
+ // Set lower and upper bounds for the columns (variables)
+ lp.colLowerBound(x1, 0);
+ lp.colUpperBound(x2, 10);
+
+ // Specify the objective function
+ lp.max();
+ lp.obj(5 * x1 + 3 * x2);
+
+ // Solve the problem using the underlying LP solver
+ lp.solve();
+ // Print the results
+ check(lp.primalType() == LP::OPTIMAL, "Optimal solution is not found");
+ check(lp.primal() <= 67.501 && lp.primal() >= 67.499, "Wrong objective value");
+ check(lp.primal(x1) <= 7.501 && lp.primal(x1) >= 7.499, "Wrong value for x1");
+ check(lp.primal(x2) <= 10.001 && lp.primal(x2) >= 9.999, "Wrong value for x2");
+}
+
int main()
{
LpSkeleton lp_skel;
@@ -444,6 +473,7 @@
lpTest(lp_glpk1);
aTest(lp_glpk2);
cloneTest<GlpkLp>();
+ rangeConstraintTest<GlpkLp>();
}
#endif
@@ -453,6 +483,7 @@
lpTest(lp_cplex1);
aTest(lp_cplex2);
cloneTest<CplexLp>();
+ rangeConstraintTest<CplexLp>();
} catch (CplexEnv::LicenseError& error) {
check(false, error.what());
}
@@ -464,6 +495,7 @@
lpTest(lp_soplex1);
aTest(lp_soplex2);
cloneTest<SoplexLp>();
+ rangeConstraintTest<Soplex>();
}
#endif
@@ -473,6 +505,7 @@
lpTest(lp_clp1);
aTest(lp_clp2);
cloneTest<ClpLp>();
+ rangeConstraintTest<ClpLp>();
}
#endif
diff --git a/test/mip_test.cc b/test/mip_test.cc
--- a/test/mip_test.cc
+++ b/test/mip_test.cc
@@ -61,7 +61,7 @@
}
}
-void aTest(MipSolver& mip)
+void aTest(MipSolver& mip, bool solve_empty=true)
{
//The following example is very simple
@@ -80,7 +80,8 @@
mip.max();
//Unconstrained optimization
- mip.solve();
+ if(solve_empty)
+ mip.solve();
//Check it out!
//Constraints
@@ -135,7 +136,11 @@
#ifdef LEMON_HAVE_MIP
{
Mip mip1;
+#if LEMON_DEFAULT_MIP==LEMON_CBC_
+ aTest(mip1, false);
+#else
aTest(mip1);
+#endif
cloneTest<Mip>();
}
#endif
@@ -161,7 +166,7 @@
#ifdef LEMON_HAVE_CBC
{
CbcMip mip1;
- aTest(mip1);
+ aTest(mip1, false);
cloneTest<CbcMip>();
}
#endif
More information about the Lemon-commits
mailing list