Add file export funcionality to LpBase (#457)
authorAlpar Juttner <alpar@cs.elte.hu>
Fri, 20 Jul 2012 17:09:01 +0200
changeset 12311782aa72495a
parent 1230 caf16813b1e8
child 1232 fc3854d936f7
Add file export funcionality to LpBase (#457)
lemon/cplex.cc
lemon/cplex.h
lemon/glpk.cc
lemon/glpk.h
lemon/lp_base.h
lemon/lp_skeleton.cc
lemon/lp_skeleton.h
     1.1 --- a/lemon/cplex.cc	Sat Jul 21 10:18:57 2012 +0200
     1.2 +++ b/lemon/cplex.cc	Fri Jul 20 17:09:01 2012 +0200
     1.3 @@ -492,6 +492,17 @@
     1.4                     _message_enabled ? CPX_ON : CPX_OFF);
     1.5    }
     1.6  
     1.7 +  void CplexBase::_write(std::string file, std::string format) const
     1.8 +  {
     1.9 +    if(format == "MPS" || format == "LP")
    1.10 +      CPXwriteprob(cplexEnv(), cplexLp(), file.c_str(), format.c_str());
    1.11 +    else if(format == "SOL")
    1.12 +      CPXsolwrite(cplexEnv(), cplexLp(), file.c_str());
    1.13 +    else throw UnsupportedFormatError(format);
    1.14 +  }
    1.15 +
    1.16 +
    1.17 +
    1.18    // CplexLp members
    1.19  
    1.20    CplexLp::CplexLp()
     2.1 --- a/lemon/cplex.h	Sat Jul 21 10:18:57 2012 +0200
     2.2 +++ b/lemon/cplex.h	Fri Jul 20 17:09:01 2012 +0200
     2.3 @@ -150,6 +150,8 @@
     2.4  
     2.5      bool _message_enabled;
     2.6  
     2.7 +    void _write(std::string file, std::string format) const;
     2.8 +
     2.9    public:
    2.10  
    2.11      /// Returns the used \c CplexEnv instance
    2.12 @@ -170,6 +172,19 @@
    2.13      /// Returns the cplex problem object
    2.14      const cpxlp* cplexLp() const { return _prob; }
    2.15  
    2.16 +#ifdef DOXYGEN
    2.17 +    /// Write the problem or the solution to a file in the given format
    2.18 +
    2.19 +    /// This function writes the problem or the solution
    2.20 +    /// to a file in the given format.
    2.21 +    /// Trying to write in an unsupported format will trigger
    2.22 +    /// \ref UnsupportedFormatError.
    2.23 +    /// \param file The file path
    2.24 +    /// \param format The output file format.
    2.25 +    /// Supportted formats are "MPS", "LP" and "SOL".
    2.26 +    void write(std::string file, std::string format = "MPS") const {}
    2.27 +#endif
    2.28 +
    2.29    };
    2.30  
    2.31    /// \brief Interface for the CPLEX LP solver
     3.1 --- a/lemon/glpk.cc	Sat Jul 21 10:18:57 2012 +0200
     3.2 +++ b/lemon/glpk.cc	Fri Jul 20 17:09:01 2012 +0200
     3.3 @@ -582,6 +582,15 @@
     3.4      }
     3.5    }
     3.6  
     3.7 +  void GlpkBase::_write(std::string file, std::string format) const
     3.8 +  {
     3.9 +    if(format == "MPS")
    3.10 +      glp_write_mps(lp, GLP_MPS_FILE, 0, file.c_str());
    3.11 +    else if(format == "LP")
    3.12 +      glp_write_lp(lp, 0, file.c_str());
    3.13 +    else throw UnsupportedFormatError(format);
    3.14 +  }
    3.15 +
    3.16    GlpkBase::FreeEnvHelper GlpkBase::freeEnvHelper;
    3.17  
    3.18    // GlpkLp members
    3.19 @@ -998,4 +1007,6 @@
    3.20  
    3.21    const char* GlpkMip::_solverName() const { return "GlpkMip"; }
    3.22  
    3.23 +
    3.24 +
    3.25  } //END OF NAMESPACE LEMON
     4.1 --- a/lemon/glpk.h	Sat Jul 21 10:18:57 2012 +0200
     4.2 +++ b/lemon/glpk.h	Fri Jul 20 17:09:01 2012 +0200
     4.3 @@ -115,6 +115,8 @@
     4.4  
     4.5      virtual void _messageLevel(MessageLevel level);
     4.6  
     4.7 +    virtual void _write(std::string file, std::string format) const;
     4.8 +
     4.9    private:
    4.10  
    4.11      static void freeEnv();
    4.12 @@ -144,6 +146,19 @@
    4.13      ///Returns the variable identifier understood by GLPK.
    4.14      int lpxCol(Col c) const { return cols(id(c)); }
    4.15  
    4.16 +#ifdef DOXYGEN
    4.17 +    /// Write the problem or the solution to a file in the given format
    4.18 +    
    4.19 +    /// This function writes the problem or the solution
    4.20 +    /// to a file in the given format.
    4.21 +    /// Trying to write in an unsupported format will trigger
    4.22 +    /// \ref UnsupportedFormatError.
    4.23 +    /// \param file The file path
    4.24 +    /// \param format The output file format.
    4.25 +    /// Supportted formats are "MPS" and "LP".
    4.26 +    void write(std::string file, std::string format = "MPS") const {}
    4.27 +#endif
    4.28 +
    4.29    };
    4.30  
    4.31    /// \brief Interface for the GLPK LP solver
     5.1 --- a/lemon/lp_base.h	Sat Jul 21 10:18:57 2012 +0200
     5.2 +++ b/lemon/lp_base.h	Fri Jul 20 17:09:01 2012 +0200
     5.3 @@ -1007,6 +1007,36 @@
     5.4  
     5.5    public:
     5.6  
     5.7 +    ///\e
     5.8 +    class UnsupportedFormatError : public Exception
     5.9 +    {
    5.10 +      std::string _format;
    5.11 +      mutable std::string _what;
    5.12 +    public:
    5.13 +      explicit UnsupportedFormatError(std::string format) throw()
    5.14 +        : _format(format) { }
    5.15 +      virtual ~UnsupportedFormatError() throw() {}
    5.16 +      virtual const char* what() const throw() {
    5.17 +        try {
    5.18 +          _what.clear();
    5.19 +          std::ostringstream oss;
    5.20 +          oss << "lemon::UnsupportedFormatError: " << _format;
    5.21 +          _what = oss.str();
    5.22 +        }
    5.23 +        catch (...) {}
    5.24 +        if (!_what.empty()) return _what.c_str();
    5.25 +        else return "lemon::UnsupportedFormatError";
    5.26 +      }
    5.27 +    };
    5.28 +    
    5.29 +  protected:
    5.30 +    virtual void _write(std::string, std::string format) const
    5.31 +    {
    5.32 +      throw UnsupportedFormatError(format);
    5.33 +    }
    5.34 +    
    5.35 +  public:
    5.36 +
    5.37      /// Virtual destructor
    5.38      virtual ~LpBase() {}
    5.39  
    5.40 @@ -1555,12 +1585,27 @@
    5.41      ///Set the sense to maximization
    5.42      void min() { _setSense(MIN); }
    5.43  
    5.44 -    ///Clears the problem
    5.45 +    ///Clear the problem
    5.46      void clear() { _clear(); rows.clear(); cols.clear(); }
    5.47  
    5.48 -    /// Sets the message level of the solver
    5.49 +    /// Set the message level of the solver
    5.50      void messageLevel(MessageLevel level) { _messageLevel(level); }
    5.51  
    5.52 +    /// Write the problem to a file in the given format
    5.53 +
    5.54 +    /// This function writes the problem to a file in the given format.
    5.55 +    /// Different solver backends may support different formats.
    5.56 +    /// Trying to write in an unsupported format will trigger
    5.57 +    /// \ref UnsupportedFormatError. For the supported formats,
    5.58 +    /// visit the documentation of the base class of the related backends
    5.59 +    /// (\ref CplexBase, \ref GlpkBase etc.)
    5.60 +    /// \param file The file path
    5.61 +    /// \param format The output file format.
    5.62 +    void write(std::string file, std::string format = "MPS") const
    5.63 +    {
    5.64 +      _write(file.c_str(),format.c_str());
    5.65 +    }
    5.66 +
    5.67      ///@}
    5.68  
    5.69    };
     6.1 --- a/lemon/lp_skeleton.cc	Sat Jul 21 10:18:57 2012 +0200
     6.2 +++ b/lemon/lp_skeleton.cc	Fri Jul 20 17:09:01 2012 +0200
     6.3 @@ -91,6 +91,8 @@
     6.4  
     6.5    void SkeletonSolverBase::_messageLevel(MessageLevel) {}
     6.6  
     6.7 +  void SkeletonSolverBase::_write(std::string, std::string) const {}
     6.8 +
     6.9    LpSkeleton::SolveExitStatus LpSkeleton::_solve() { return SOLVED; }
    6.10  
    6.11    LpSkeleton::Value LpSkeleton::_getPrimal(int) const { return 0; }
     7.1 --- a/lemon/lp_skeleton.h	Sat Jul 21 10:18:57 2012 +0200
     7.2 +++ b/lemon/lp_skeleton.h	Fri Jul 20 17:09:01 2012 +0200
     7.3 @@ -144,6 +144,10 @@
     7.4  
     7.5      ///\e
     7.6      virtual void _messageLevel(MessageLevel);
     7.7 +
     7.8 +    ///\e
     7.9 +    virtual void _write(std::string file, std::string format) const;
    7.10 +
    7.11    };
    7.12  
    7.13    /// \brief Skeleton class for an LP solver interface
    7.14 @@ -222,6 +226,7 @@
    7.15  
    7.16      ///\e
    7.17      virtual const char* _solverName() const;
    7.18 +
    7.19    };
    7.20  
    7.21  } //namespace lemon