lemon/lp_base.h
changeset 1184 3c00344f49c9
parent 1076 38c432e01489
child 1130 0759d974de81
     1.1 --- a/lemon/lp_base.h	Mon Jul 16 16:21:40 2018 +0200
     1.2 +++ b/lemon/lp_base.h	Wed Oct 17 19:14:07 2018 +0200
     1.3 @@ -2,7 +2,7 @@
     1.4   *
     1.5   * This file is a part of LEMON, a generic C++ optimization library.
     1.6   *
     1.7 - * Copyright (C) 2003-2010
     1.8 + * Copyright (C) 2003-2013
     1.9   * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10   * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11   *
    1.12 @@ -1007,6 +1007,36 @@
    1.13  
    1.14    public:
    1.15  
    1.16 +    ///Unsupported file format exception
    1.17 +    class UnsupportedFormatError : public Exception
    1.18 +    {
    1.19 +      std::string _format;
    1.20 +      mutable std::string _what;
    1.21 +    public:
    1.22 +      explicit UnsupportedFormatError(std::string format) throw()
    1.23 +        : _format(format) { }
    1.24 +      virtual ~UnsupportedFormatError() throw() {}
    1.25 +      virtual const char* what() const throw() {
    1.26 +        try {
    1.27 +          _what.clear();
    1.28 +          std::ostringstream oss;
    1.29 +          oss << "lemon::UnsupportedFormatError: " << _format;
    1.30 +          _what = oss.str();
    1.31 +        }
    1.32 +        catch (...) {}
    1.33 +        if (!_what.empty()) return _what.c_str();
    1.34 +        else return "lemon::UnsupportedFormatError";
    1.35 +      }
    1.36 +    };
    1.37 +
    1.38 +  protected:
    1.39 +    virtual void _write(std::string, std::string format) const
    1.40 +    {
    1.41 +      throw UnsupportedFormatError(format);
    1.42 +    }
    1.43 +
    1.44 +  public:
    1.45 +
    1.46      /// Virtual destructor
    1.47      virtual ~LpBase() {}
    1.48  
    1.49 @@ -1555,12 +1585,27 @@
    1.50      ///Set the sense to maximization
    1.51      void min() { _setSense(MIN); }
    1.52  
    1.53 -    ///Clears the problem
    1.54 +    ///Clear the problem
    1.55      void clear() { _clear(); rows.clear(); cols.clear(); }
    1.56  
    1.57 -    /// Sets the message level of the solver
    1.58 +    /// Set the message level of the solver
    1.59      void messageLevel(MessageLevel level) { _messageLevel(level); }
    1.60  
    1.61 +    /// Write the problem to a file in the given format
    1.62 +
    1.63 +    /// This function writes the problem to a file in the given format.
    1.64 +    /// Different solver backends may support different formats.
    1.65 +    /// Trying to write in an unsupported format will trigger
    1.66 +    /// \ref UnsupportedFormatError. For the supported formats,
    1.67 +    /// visit the documentation of the base class of the related backends
    1.68 +    /// (\ref CplexBase, \ref GlpkBase etc.)
    1.69 +    /// \param file The file path
    1.70 +    /// \param format The output file format.
    1.71 +    void write(std::string file, std::string format = "MPS") const
    1.72 +    {
    1.73 +      _write(file.c_str(),format.c_str());
    1.74 +    }
    1.75 +
    1.76      ///@}
    1.77  
    1.78    };