[Lemon-commits] Alpar Juttner: Add file export funcionality to L...
Lemon HG
hg at lemon.cs.elte.hu
Wed Jul 24 11:11:12 CEST 2013
details: http://lemon.cs.elte.hu/hg/lemon/rev/1782aa72495a
changeset: 1231:1782aa72495a
user: Alpar Juttner <alpar [at] cs.elte.hu>
date: Fri Jul 20 17:09:01 2012 +0200
description:
Add file export funcionality to LpBase (#457)
diffstat:
lemon/cplex.cc | 11 +++++++++++
lemon/cplex.h | 15 +++++++++++++++
lemon/glpk.cc | 11 +++++++++++
lemon/glpk.h | 15 +++++++++++++++
lemon/lp_base.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++--
lemon/lp_skeleton.cc | 2 ++
lemon/lp_skeleton.h | 5 +++++
7 files changed, 106 insertions(+), 2 deletions(-)
diffs (215 lines):
diff -r caf16813b1e8 -r 1782aa72495a lemon/cplex.cc
--- a/lemon/cplex.cc Sat Jul 21 10:18:57 2012 +0200
+++ b/lemon/cplex.cc Fri Jul 20 17:09:01 2012 +0200
@@ -492,6 +492,17 @@
_message_enabled ? CPX_ON : CPX_OFF);
}
+ void CplexBase::_write(std::string file, std::string format) const
+ {
+ if(format == "MPS" || format == "LP")
+ CPXwriteprob(cplexEnv(), cplexLp(), file.c_str(), format.c_str());
+ else if(format == "SOL")
+ CPXsolwrite(cplexEnv(), cplexLp(), file.c_str());
+ else throw UnsupportedFormatError(format);
+ }
+
+
+
// CplexLp members
CplexLp::CplexLp()
diff -r caf16813b1e8 -r 1782aa72495a lemon/cplex.h
--- a/lemon/cplex.h Sat Jul 21 10:18:57 2012 +0200
+++ b/lemon/cplex.h Fri Jul 20 17:09:01 2012 +0200
@@ -150,6 +150,8 @@
bool _message_enabled;
+ void _write(std::string file, std::string format) const;
+
public:
/// Returns the used \c CplexEnv instance
@@ -170,6 +172,19 @@
/// Returns the cplex problem object
const cpxlp* cplexLp() const { return _prob; }
+#ifdef DOXYGEN
+ /// Write the problem or the solution to a file in the given format
+
+ /// This function writes the problem or the solution
+ /// to a file in the given format.
+ /// Trying to write in an unsupported format will trigger
+ /// \ref UnsupportedFormatError.
+ /// \param file The file path
+ /// \param format The output file format.
+ /// Supportted formats are "MPS", "LP" and "SOL".
+ void write(std::string file, std::string format = "MPS") const {}
+#endif
+
};
/// \brief Interface for the CPLEX LP solver
diff -r caf16813b1e8 -r 1782aa72495a lemon/glpk.cc
--- a/lemon/glpk.cc Sat Jul 21 10:18:57 2012 +0200
+++ b/lemon/glpk.cc Fri Jul 20 17:09:01 2012 +0200
@@ -582,6 +582,15 @@
}
}
+ void GlpkBase::_write(std::string file, std::string format) const
+ {
+ if(format == "MPS")
+ glp_write_mps(lp, GLP_MPS_FILE, 0, file.c_str());
+ else if(format == "LP")
+ glp_write_lp(lp, 0, file.c_str());
+ else throw UnsupportedFormatError(format);
+ }
+
GlpkBase::FreeEnvHelper GlpkBase::freeEnvHelper;
// GlpkLp members
@@ -998,4 +1007,6 @@
const char* GlpkMip::_solverName() const { return "GlpkMip"; }
+
+
} //END OF NAMESPACE LEMON
diff -r caf16813b1e8 -r 1782aa72495a lemon/glpk.h
--- a/lemon/glpk.h Sat Jul 21 10:18:57 2012 +0200
+++ b/lemon/glpk.h Fri Jul 20 17:09:01 2012 +0200
@@ -115,6 +115,8 @@
virtual void _messageLevel(MessageLevel level);
+ virtual void _write(std::string file, std::string format) const;
+
private:
static void freeEnv();
@@ -144,6 +146,19 @@
///Returns the variable identifier understood by GLPK.
int lpxCol(Col c) const { return cols(id(c)); }
+#ifdef DOXYGEN
+ /// Write the problem or the solution to a file in the given format
+
+ /// This function writes the problem or the solution
+ /// to a file in the given format.
+ /// Trying to write in an unsupported format will trigger
+ /// \ref UnsupportedFormatError.
+ /// \param file The file path
+ /// \param format The output file format.
+ /// Supportted formats are "MPS" and "LP".
+ void write(std::string file, std::string format = "MPS") const {}
+#endif
+
};
/// \brief Interface for the GLPK LP solver
diff -r caf16813b1e8 -r 1782aa72495a lemon/lp_base.h
--- a/lemon/lp_base.h Sat Jul 21 10:18:57 2012 +0200
+++ b/lemon/lp_base.h Fri Jul 20 17:09:01 2012 +0200
@@ -1007,6 +1007,36 @@
public:
+ ///\e
+ class UnsupportedFormatError : public Exception
+ {
+ std::string _format;
+ mutable std::string _what;
+ public:
+ explicit UnsupportedFormatError(std::string format) throw()
+ : _format(format) { }
+ virtual ~UnsupportedFormatError() throw() {}
+ virtual const char* what() const throw() {
+ try {
+ _what.clear();
+ std::ostringstream oss;
+ oss << "lemon::UnsupportedFormatError: " << _format;
+ _what = oss.str();
+ }
+ catch (...) {}
+ if (!_what.empty()) return _what.c_str();
+ else return "lemon::UnsupportedFormatError";
+ }
+ };
+
+ protected:
+ virtual void _write(std::string, std::string format) const
+ {
+ throw UnsupportedFormatError(format);
+ }
+
+ public:
+
/// Virtual destructor
virtual ~LpBase() {}
@@ -1555,12 +1585,27 @@
///Set the sense to maximization
void min() { _setSense(MIN); }
- ///Clears the problem
+ ///Clear the problem
void clear() { _clear(); rows.clear(); cols.clear(); }
- /// Sets the message level of the solver
+ /// Set the message level of the solver
void messageLevel(MessageLevel level) { _messageLevel(level); }
+ /// Write the problem to a file in the given format
+
+ /// This function writes the problem to a file in the given format.
+ /// Different solver backends may support different formats.
+ /// Trying to write in an unsupported format will trigger
+ /// \ref UnsupportedFormatError. For the supported formats,
+ /// visit the documentation of the base class of the related backends
+ /// (\ref CplexBase, \ref GlpkBase etc.)
+ /// \param file The file path
+ /// \param format The output file format.
+ void write(std::string file, std::string format = "MPS") const
+ {
+ _write(file.c_str(),format.c_str());
+ }
+
///@}
};
diff -r caf16813b1e8 -r 1782aa72495a lemon/lp_skeleton.cc
--- a/lemon/lp_skeleton.cc Sat Jul 21 10:18:57 2012 +0200
+++ b/lemon/lp_skeleton.cc Fri Jul 20 17:09:01 2012 +0200
@@ -91,6 +91,8 @@
void SkeletonSolverBase::_messageLevel(MessageLevel) {}
+ void SkeletonSolverBase::_write(std::string, std::string) const {}
+
LpSkeleton::SolveExitStatus LpSkeleton::_solve() { return SOLVED; }
LpSkeleton::Value LpSkeleton::_getPrimal(int) const { return 0; }
diff -r caf16813b1e8 -r 1782aa72495a lemon/lp_skeleton.h
--- a/lemon/lp_skeleton.h Sat Jul 21 10:18:57 2012 +0200
+++ b/lemon/lp_skeleton.h Fri Jul 20 17:09:01 2012 +0200
@@ -144,6 +144,10 @@
///\e
virtual void _messageLevel(MessageLevel);
+
+ ///\e
+ virtual void _write(std::string file, std::string format) const;
+
};
/// \brief Skeleton class for an LP solver interface
@@ -222,6 +226,7 @@
///\e
virtual const char* _solverName() const;
+
};
} //namespace lemon
More information about the Lemon-commits
mailing list