# HG changeset patch
# User Alpar Juttner <alpar@cs.elte.hu>
# Date 1342796941 -7200
# Node ID 1782aa72495ad552b05d3dbabb1c6110d3d0c09f
# Parent caf16813b1e8c0f2f0bbd4b303442620b921834c
Add file export funcionality to LpBase (#457)
diff --git a/lemon/cplex.cc b/lemon/cplex.cc
a
|
b
|
|
492 | 492 | _message_enabled ? CPX_ON : CPX_OFF); |
493 | 493 | } |
494 | 494 | |
| 495 | void CplexBase::_write(std::string file, std::string format) const |
| 496 | { |
| 497 | if(format == "MPS" || format == "LP") |
| 498 | CPXwriteprob(cplexEnv(), cplexLp(), file.c_str(), format.c_str()); |
| 499 | else if(format == "SOL") |
| 500 | CPXsolwrite(cplexEnv(), cplexLp(), file.c_str()); |
| 501 | else throw UnsupportedFormatError(format); |
| 502 | } |
| 503 | |
| 504 | |
| 505 | |
495 | 506 | // CplexLp members |
496 | 507 | |
497 | 508 | CplexLp::CplexLp() |
diff --git a/lemon/cplex.h b/lemon/cplex.h
a
|
b
|
|
150 | 150 | |
151 | 151 | bool _message_enabled; |
152 | 152 | |
| 153 | void _write(std::string file, std::string format) const; |
| 154 | |
153 | 155 | public: |
154 | 156 | |
155 | 157 | /// Returns the used \c CplexEnv instance |
… |
… |
|
170 | 172 | /// Returns the cplex problem object |
171 | 173 | const cpxlp* cplexLp() const { return _prob; } |
172 | 174 | |
| 175 | #ifdef DOXYGEN |
| 176 | /// Write the problem or the solution to a file in the given format |
| 177 | |
| 178 | /// This function writes the problem or the solution |
| 179 | /// to a file in the given format. |
| 180 | /// Trying to write in an unsupported format will trigger |
| 181 | /// \ref UnsupportedFormatError. |
| 182 | /// \param file The file path |
| 183 | /// \param format The output file format. |
| 184 | /// Supportted formats are "MPS", "LP" and "SOL". |
| 185 | void write(std::string file, std::string format = "MPS") const {} |
| 186 | #endif |
| 187 | |
173 | 188 | }; |
174 | 189 | |
175 | 190 | /// \brief Interface for the CPLEX LP solver |
diff --git a/lemon/glpk.cc b/lemon/glpk.cc
a
|
b
|
|
582 | 582 | } |
583 | 583 | } |
584 | 584 | |
| 585 | void GlpkBase::_write(std::string file, std::string format) const |
| 586 | { |
| 587 | if(format == "MPS") |
| 588 | glp_write_mps(lp, GLP_MPS_FILE, 0, file.c_str()); |
| 589 | else if(format == "LP") |
| 590 | glp_write_lp(lp, 0, file.c_str()); |
| 591 | else throw UnsupportedFormatError(format); |
| 592 | } |
| 593 | |
585 | 594 | GlpkBase::FreeEnvHelper GlpkBase::freeEnvHelper; |
586 | 595 | |
587 | 596 | // GlpkLp members |
… |
… |
|
998 | 1007 | |
999 | 1008 | const char* GlpkMip::_solverName() const { return "GlpkMip"; } |
1000 | 1009 | |
| 1010 | |
| 1011 | |
1001 | 1012 | } //END OF NAMESPACE LEMON |
diff --git a/lemon/glpk.h b/lemon/glpk.h
a
|
b
|
|
115 | 115 | |
116 | 116 | virtual void _messageLevel(MessageLevel level); |
117 | 117 | |
| 118 | virtual void _write(std::string file, std::string format) const; |
| 119 | |
118 | 120 | private: |
119 | 121 | |
120 | 122 | static void freeEnv(); |
… |
… |
|
144 | 146 | ///Returns the variable identifier understood by GLPK. |
145 | 147 | int lpxCol(Col c) const { return cols(id(c)); } |
146 | 148 | |
| 149 | #ifdef DOXYGEN |
| 150 | /// Write the problem or the solution to a file in the given format |
| 151 | |
| 152 | /// This function writes the problem or the solution |
| 153 | /// to a file in the given format. |
| 154 | /// Trying to write in an unsupported format will trigger |
| 155 | /// \ref UnsupportedFormatError. |
| 156 | /// \param file The file path |
| 157 | /// \param format The output file format. |
| 158 | /// Supportted formats are "MPS" and "LP". |
| 159 | void write(std::string file, std::string format = "MPS") const {} |
| 160 | #endif |
| 161 | |
147 | 162 | }; |
148 | 163 | |
149 | 164 | /// \brief Interface for the GLPK LP solver |
diff --git a/lemon/lp_base.h b/lemon/lp_base.h
a
|
b
|
|
1007 | 1007 | |
1008 | 1008 | public: |
1009 | 1009 | |
| 1010 | ///\e |
| 1011 | class UnsupportedFormatError : public Exception |
| 1012 | { |
| 1013 | std::string _format; |
| 1014 | mutable std::string _what; |
| 1015 | public: |
| 1016 | explicit UnsupportedFormatError(std::string format) throw() |
| 1017 | : _format(format) { } |
| 1018 | virtual ~UnsupportedFormatError() throw() {} |
| 1019 | virtual const char* what() const throw() { |
| 1020 | try { |
| 1021 | _what.clear(); |
| 1022 | std::ostringstream oss; |
| 1023 | oss << "lemon::UnsupportedFormatError: " << _format; |
| 1024 | _what = oss.str(); |
| 1025 | } |
| 1026 | catch (...) {} |
| 1027 | if (!_what.empty()) return _what.c_str(); |
| 1028 | else return "lemon::UnsupportedFormatError"; |
| 1029 | } |
| 1030 | }; |
| 1031 | |
| 1032 | protected: |
| 1033 | virtual void _write(std::string, std::string format) const |
| 1034 | { |
| 1035 | throw UnsupportedFormatError(format); |
| 1036 | } |
| 1037 | |
| 1038 | public: |
| 1039 | |
1010 | 1040 | /// Virtual destructor |
1011 | 1041 | virtual ~LpBase() {} |
1012 | 1042 | |
… |
… |
|
1555 | 1585 | ///Set the sense to maximization |
1556 | 1586 | void min() { _setSense(MIN); } |
1557 | 1587 | |
1558 | | ///Clears the problem |
| 1588 | ///Clear the problem |
1559 | 1589 | void clear() { _clear(); rows.clear(); cols.clear(); } |
1560 | 1590 | |
1561 | | /// Sets the message level of the solver |
| 1591 | /// Set the message level of the solver |
1562 | 1592 | void messageLevel(MessageLevel level) { _messageLevel(level); } |
1563 | 1593 | |
| 1594 | /// Write the problem to a file in the given format |
| 1595 | |
| 1596 | /// This function writes the problem to a file in the given format. |
| 1597 | /// Different solver backends may support different formats. |
| 1598 | /// Trying to write in an unsupported format will trigger |
| 1599 | /// \ref UnsupportedFormatError. For the supported formats, |
| 1600 | /// visit the documentation of the base class of the related backends |
| 1601 | /// (\ref CplexBase, \ref GlpkBase etc.) |
| 1602 | /// \param file The file path |
| 1603 | /// \param format The output file format. |
| 1604 | void write(std::string file, std::string format = "MPS") const |
| 1605 | { |
| 1606 | _write(file.c_str(),format.c_str()); |
| 1607 | } |
| 1608 | |
1564 | 1609 | ///@} |
1565 | 1610 | |
1566 | 1611 | }; |
diff --git a/lemon/lp_skeleton.cc b/lemon/lp_skeleton.cc
a
|
b
|
|
91 | 91 | |
92 | 92 | void SkeletonSolverBase::_messageLevel(MessageLevel) {} |
93 | 93 | |
| 94 | void SkeletonSolverBase::_write(std::string, std::string) const {} |
| 95 | |
94 | 96 | LpSkeleton::SolveExitStatus LpSkeleton::_solve() { return SOLVED; } |
95 | 97 | |
96 | 98 | LpSkeleton::Value LpSkeleton::_getPrimal(int) const { return 0; } |
diff --git a/lemon/lp_skeleton.h b/lemon/lp_skeleton.h
a
|
b
|
|
144 | 144 | |
145 | 145 | ///\e |
146 | 146 | virtual void _messageLevel(MessageLevel); |
| 147 | |
| 148 | ///\e |
| 149 | virtual void _write(std::string file, std::string format) const; |
| 150 | |
147 | 151 | }; |
148 | 152 | |
149 | 153 | /// \brief Skeleton class for an LP solver interface |
… |
… |
|
222 | 226 | |
223 | 227 | ///\e |
224 | 228 | virtual const char* _solverName() const; |
| 229 | |
225 | 230 | }; |
226 | 231 | |
227 | 232 | } //namespace lemon |