lemon/lp_base.h
changeset 1231 1782aa72495a
parent 1143 38e1d4383262
child 1250 97d978243703
     1.1 --- a/lemon/lp_base.h	Sat Jul 21 10:18:57 2012 +0200
     1.2 +++ b/lemon/lp_base.h	Fri Jul 20 17:09:01 2012 +0200
     1.3 @@ -1007,6 +1007,36 @@
     1.4  
     1.5    public:
     1.6  
     1.7 +    ///\e
     1.8 +    class UnsupportedFormatError : public Exception
     1.9 +    {
    1.10 +      std::string _format;
    1.11 +      mutable std::string _what;
    1.12 +    public:
    1.13 +      explicit UnsupportedFormatError(std::string format) throw()
    1.14 +        : _format(format) { }
    1.15 +      virtual ~UnsupportedFormatError() throw() {}
    1.16 +      virtual const char* what() const throw() {
    1.17 +        try {
    1.18 +          _what.clear();
    1.19 +          std::ostringstream oss;
    1.20 +          oss << "lemon::UnsupportedFormatError: " << _format;
    1.21 +          _what = oss.str();
    1.22 +        }
    1.23 +        catch (...) {}
    1.24 +        if (!_what.empty()) return _what.c_str();
    1.25 +        else return "lemon::UnsupportedFormatError";
    1.26 +      }
    1.27 +    };
    1.28 +    
    1.29 +  protected:
    1.30 +    virtual void _write(std::string, std::string format) const
    1.31 +    {
    1.32 +      throw UnsupportedFormatError(format);
    1.33 +    }
    1.34 +    
    1.35 +  public:
    1.36 +
    1.37      /// Virtual destructor
    1.38      virtual ~LpBase() {}
    1.39  
    1.40 @@ -1555,12 +1585,27 @@
    1.41      ///Set the sense to maximization
    1.42      void min() { _setSense(MIN); }
    1.43  
    1.44 -    ///Clears the problem
    1.45 +    ///Clear the problem
    1.46      void clear() { _clear(); rows.clear(); cols.clear(); }
    1.47  
    1.48 -    /// Sets the message level of the solver
    1.49 +    /// Set the message level of the solver
    1.50      void messageLevel(MessageLevel level) { _messageLevel(level); }
    1.51  
    1.52 +    /// Write the problem to a file in the given format
    1.53 +
    1.54 +    /// This function writes the problem to a file in the given format.
    1.55 +    /// Different solver backends may support different formats.
    1.56 +    /// Trying to write in an unsupported format will trigger
    1.57 +    /// \ref UnsupportedFormatError. For the supported formats,
    1.58 +    /// visit the documentation of the base class of the related backends
    1.59 +    /// (\ref CplexBase, \ref GlpkBase etc.)
    1.60 +    /// \param file The file path
    1.61 +    /// \param format The output file format.
    1.62 +    void write(std::string file, std::string format = "MPS") const
    1.63 +    {
    1.64 +      _write(file.c_str(),format.c_str());
    1.65 +    }
    1.66 +
    1.67      ///@}
    1.68  
    1.69    };