COIN-OR::LEMON - Graph Library

Ticket #326: 326-lp-mip-unify-d4c3ba9ac811.patch

File 326-lp-mip-unify-d4c3ba9ac811.patch, 14.1 KB (added by Peter Kovacs, 9 years ago)
  • lemon/lp_base.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1266443462 -3600
    # Node ID d4c3ba9ac81125b315d7cf33ff057f0b074a457d
    # Parent  5100072d83caa82eaf0be1a84a27e5d1917f2efd
    Unify the interface of MipSolver and LpSolver (#326)
    
     - Add sol() (without parameters) to MipSolver as an alias for solValue(),
    since we have primal() instead of primalValue() in LpSolver.
     - Add primal() (3 overloaded variants) and primalValue() to MipSolver as
    aliases for sol() and solValue().
     - Rename ColTypes to ColType in MipSolver and keep the old name as an
    obsolete typedef.
     - Add primalValue() to LpSolver as an alias for primal() (without
    parameters).
     - Doc improvements and unifications.
    
    diff --git a/lemon/lp_base.h b/lemon/lp_base.h
    a b  
    146146
    147147    ///Iterator for iterate over the columns of an LP problem
    148148
    149     /// Its usage is quite simple, for example you can count the number
     149    /// Its usage is quite simple, for example, you can count the number
    150150    /// of columns in an LP \c lp:
    151151    ///\code
    152152    /// int count=0;
     
    241241
    242242    ///Iterator for iterate over the rows of an LP problem
    243243
    244     /// Its usage is quite simple, for example you can count the number
     244    /// Its usage is quite simple, for example, you can count the number
    245245    /// of rows in an LP \c lp:
    246246    ///\code
    247247    /// int count=0;
     
    943943    virtual int _addCol() = 0;
    944944    virtual int _addRow() = 0;
    945945
     946    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
     947      int row = _addRow();
     948      _setRowCoeffs(row, b, e);
     949      _setRowLowerBound(row, l);
     950      _setRowUpperBound(row, u);
     951      return row;
     952    }
     953
    946954    virtual void _eraseCol(int col) = 0;
    947955    virtual void _eraseRow(int row) = 0;
    948956
     
    12071215    ///\param u is the upper bound (\ref INF means no bound)
    12081216    ///\return The created row.
    12091217    Row addRow(Value l,const Expr &e, Value u) {
    1210       Row r=addRow();
    1211       row(r,l,e,u);
     1218      Row r;
     1219      e.simplify();
     1220      r._id = _addRowId(_addRow(l - *e, ExprIterator(e.comps.begin(), cols),
     1221                                ExprIterator(e.comps.end(), cols), u - *e));
    12121222      return r;
    12131223    }
    12141224
     
    12171227    ///\param c is a linear expression (see \ref Constr)
    12181228    ///\return The created row.
    12191229    Row addRow(const Constr &c) {
    1220       Row r=addRow();
    1221       row(r,c);
     1230      Row r;
     1231      c.expr().simplify();
     1232      r._id = _addRowId(_addRow(c.lowerBounded()?c.lowerBound()-*c.expr():-INF,
     1233                                ExprIterator(c.expr().comps.begin(), cols),
     1234                                ExprIterator(c.expr().comps.end(), cols),
     1235                                c.upperBounded()?c.upperBound()-*c.expr():INF));
    12221236      return r;
    12231237    }
    12241238    ///Erase a column (i.e a variable) from the LP
     
    17761790  ///
    17771791  /// \brief Common base class for LP solvers
    17781792  ///
    1779   /// This class is an abstract base class for LP solvers. This class
     1793  /// This class is an abstract base class for LP solvers. It
    17801794  /// provides a full interface for set and modify an LP problem,
    17811795  /// solve it and retrieve the solution. You can use one of the
    17821796  /// descendants as a concrete implementation, or the \c Lp
     
    17861800  class LpSolver : virtual public LpBase {
    17871801  public:
    17881802
    1789     /// The problem types for primal and dual problems
     1803    /// \brief The problem types for primal and dual problems
     1804    ///
     1805    /// The problem types for primal and dual problems.
    17901806    enum ProblemType {
    17911807      /// = 0. Feasible solution hasn't been found (but may exist).
    17921808      UNDEFINED = 0,
     
    18001816      UNBOUNDED = 4
    18011817    };
    18021818
    1803     ///The basis status of variables
     1819    /// \brief The basis status of variables
     1820    ///
     1821    /// The basis status of variables.
    18041822    enum VarStatus {
    18051823      /// The variable is in the basis
    18061824      BASIC,
     
    18431861
    18441862    ///@{
    18451863
    1846     ///\e Solve the LP problem at hand
     1864    ///\brief Solve the specified LP problem
    18471865    ///
     1866    ///This function solves the specified LP problem.
    18481867    ///\return The result of the optimization procedure. Possible
    18491868    ///values and their meanings can be found in the documentation of
    18501869    ///\ref SolveExitStatus.
     
    18561875
    18571876    ///@{
    18581877
    1859     /// The type of the primal problem
     1878    /// Return the type of the primal problem
     1879
     1880    /// This function returns the type of the primal problem.
     1881    /// \see ProblemType
    18601882    ProblemType primalType() const {
    18611883      return _getPrimalType();
    18621884    }
    18631885
    1864     /// The type of the dual problem
     1886    /// Return the type of the dual problem
     1887
     1888    /// This function returns the type of the dual problem.
     1889    /// \see ProblemType
    18651890    ProblemType dualType() const {
    18661891      return _getDualType();
    18671892    }
    18681893
     1894    /// Return the primal value of the objective function
     1895
     1896    /// This function returns the primal value of the objective function.
     1897    /// \return
     1898    /// - \ref INF or -\ref INF means either infeasibility or unboundedness
     1899    ///   of the primal problem, depending on whether we minimize or maximize.
     1900    /// - \ref NaN if no primal solution is found.
     1901    /// - The (finite) objective value if an optimal solution is found.
     1902    Value primal() const { return _getPrimalValue()+obj_const_comp;}
     1903
    18691904    /// Return the primal value of the column
    18701905
    1871     /// Return the primal value of the column.
     1906    /// This function returns the primal value of the given column.
    18721907    /// \pre The problem is solved.
    18731908    Value primal(Col c) const { return _getPrimal(cols(id(c))); }
    18741909
    18751910    /// Return the primal value of the expression
    18761911
    1877     /// Return the primal value of the expression, i.e. the dot
    1878     /// product of the primal solution and the expression.
     1912    /// This function returns the primal value of the given expression,
     1913    /// i.e. the dot product of the primal solution and the expression.
    18791914    /// \pre The problem is solved.
    18801915    Value primal(const Expr& e) const {
    18811916      double res = *e;
     
    18841919      }
    18851920      return res;
    18861921    }
    1887     /// Returns a component of the primal ray
     1922
     1923    /// Return the primal value of the objective function
     1924   
     1925    /// This function returns the primal value of the objective function.
     1926    /// It is an alias for \ref primal().
     1927    Value primalValue() const { return _getPrimalValue()+obj_const_comp;}
     1928
     1929    /// Return a component of the primal ray
    18881930   
    18891931    /// The primal ray is solution of the modified primal problem,
    18901932    /// where we change each finite bound to 0, and we looking for a
     
    19011943
    19021944    /// Return the dual value of the row
    19031945
    1904     /// Return the dual value of the row.
     1946    /// This function returns the dual value of the given row.
    19051947    /// \pre The problem is solved.
    19061948    Value dual(Row r) const { return _getDual(rows(id(r))); }
    19071949
    19081950    /// Return the dual value of the dual expression
    19091951
    1910     /// Return the dual value of the dual expression, i.e. the dot
    1911     /// product of the dual solution and the dual expression.
     1952    /// This function returns the dual value of the given dual expression,
     1953    /// i.e. the dot product of the dual solution and the dual expression.
    19121954    /// \pre The problem is solved.
    19131955    Value dual(const DualExpr& e) const {
    19141956      double res = 0.0;
     
    19181960      return res;
    19191961    }
    19201962
    1921     /// Returns a component of the dual ray
     1963    /// Return a component of the dual ray
    19221964   
    19231965    /// The dual ray is solution of the modified primal problem, where
    19241966    /// we change each finite bound to 0 (i.e. the objective function
     
    19351977
    19361978    /// Return the basis status of the column
    19371979
     1980    /// This function returns the basis status of the column.
    19381981    /// \see VarStatus
    19391982    VarStatus colStatus(Col c) const { return _getColStatus(cols(id(c))); }
    19401983
    19411984    /// Return the basis status of the row
    19421985
     1986    /// This function returns the basis status of the row.
    19431987    /// \see VarStatus
    19441988    VarStatus rowStatus(Row r) const { return _getRowStatus(rows(id(r))); }
    1945 
    1946     ///The value of the objective function
    1947 
    1948     ///\return
    1949     ///- \ref INF or -\ref INF means either infeasibility or unboundedness
    1950     /// of the primal problem, depending on whether we minimize or maximize.
    1951     ///- \ref NaN if no primal solution is found.
    1952     ///- The (finite) objective value if an optimal solution is found.
    1953     Value primal() const { return _getPrimalValue()+obj_const_comp;}
    19541989    ///@}
    19551990
    1956   protected:
    1957 
    19581991  };
    19591992
    19601993
     
    19621995  ///
    19631996  /// \brief Common base class for MIP solvers
    19641997  ///
    1965   /// This class is an abstract base class for MIP solvers. This class
     1998  /// This class is an abstract base class for MIP solvers. It
    19661999  /// provides a full interface for set and modify an MIP problem,
    19672000  /// solve it and retrieve the solution. You can use one of the
    19682001  /// descendants as a concrete implementation, or the \c Lp
     
    19722005  class MipSolver : virtual public LpBase {
    19732006  public:
    19742007
    1975     /// The problem types for MIP problems
     2008    /// \brief The problem types for MIP problems
     2009    ///
     2010    /// The problem types for MIP problems.
    19762011    enum ProblemType {
    19772012      /// = 0. Feasible solution hasn't been found (but may exist).
    19782013      UNDEFINED = 0,
     
    19962031
    19972032    ///@{
    19982033
    1999     /// Solve the MIP problem at hand
     2034    ///\brief Solve the specified MIP problem
    20002035    ///
     2036    ///This function solves the specified MIP problem.
    20012037    ///\return The result of the optimization procedure. Possible
    20022038    ///values and their meanings can be found in the documentation of
    20032039    ///\ref SolveExitStatus.
     
    20082044    ///\name Set Column Type
    20092045    ///@{
    20102046
    2011     ///Possible variable (column) types (e.g. real, integer, binary etc.)
    2012     enum ColTypes {
     2047    /// The column types for MIP problems.
     2048
     2049    /// The column (variable) types for MIP problems.
     2050    ///
     2051    enum ColType {
    20132052      /// = 0. Continuous variable (default).
    20142053      REAL = 0,
    20152054      /// = 1. Integer variable.
    20162055      INTEGER = 1
    20172056    };
    20182057
     2058    /// \brief The column types for MIP problems. It is an obsolete alias for
     2059    /// \ref ColType.
     2060    typedef ColType ColTypes;
     2061
    20192062    ///Sets the type of the given column to the given type
    20202063
    2021     ///Sets the type of the given column to the given type.
     2064    ///This function sets the type of the given column to the given type.
    20222065    ///
    20232066    void colType(Col c, ColTypes col_type) {
    20242067      _setColType(cols(id(c)),col_type);
     
    20262069
    20272070    ///Gives back the type of the column.
    20282071
    2029     ///Gives back the type of the column.
     2072    ///This function gives back the type of the column.
    20302073    ///
    20312074    ColTypes colType(Col c) const {
    20322075      return _getColType(cols(id(c)));
     
    20372080
    20382081    ///@{
    20392082
    2040     /// The type of the MIP problem
     2083    /// Return the type of the MIP problem
     2084
     2085    /// This function returns the type of the MIP problem.
     2086    /// \see ProblemType
    20412087    ProblemType type() const {
    20422088      return _getType();
    20432089    }
    20442090
    2045     /// Return the value of the row in the solution
     2091    /// Return the value of the objective function
     2092   
     2093    /// This function returns value of the objective function.
     2094    /// \return
     2095    /// - \ref INF or -\ref INF means either infeasibility or unboundedness
     2096    ///   of the problem, depending on whether we minimize or maximize.
     2097    /// - \ref NaN if no primal solution is found.
     2098    /// - The (finite) objective value if an optimal solution is found.
     2099    Value sol() const { return _getSolValue()+obj_const_comp;}
    20462100
    2047     ///  Return the value of the row in the solution.
     2101    /// Return the value of the column
     2102
     2103    /// This function returns the value of the given column in the solution.
    20482104    /// \pre The problem is solved.
    20492105    Value sol(Col c) const { return _getSol(cols(id(c))); }
    20502106
    2051     /// Return the value of the expression in the solution
     2107    /// Return the value of the expression
    20522108
    2053     /// Return the value of the expression in the solution, i.e. the
    2054     /// dot product of the solution and the expression.
     2109    /// This function returns the value of the given expression in the solution,
     2110    /// i.e. the dot product of the solution and the expression.
    20552111    /// \pre The problem is solved.
    20562112    Value sol(const Expr& e) const {
    20572113      double res = *e;
     
    20602116      }
    20612117      return res;
    20622118    }
    2063     ///The value of the objective function
     2119
     2120    /// Return the value of the objective function
    20642121   
    2065     ///\return
    2066     ///- \ref INF or -\ref INF means either infeasibility or unboundedness
    2067     /// of the problem, depending on whether we minimize or maximize.
    2068     ///- \ref NaN if no primal solution is found.
    2069     ///- The (finite) objective value if an optimal solution is found.
     2122    /// This function returns the value of the objective function.
     2123    /// It is an alias for \ref sol().
    20702124    Value solValue() const { return _getSolValue()+obj_const_comp;}
     2125
     2126    /// Return the type of the MIP problem
     2127
     2128    /// This function returns the type of the MIP problem.
     2129    /// It is an alias for \ref type().
     2130    /// \see ProblemType
     2131    ProblemType primalType() const {
     2132      return _getType();
     2133    }
     2134
     2135    /// Return the value of the objective function
     2136   
     2137    /// This function returns value of the objective function.
     2138    /// It is an alias for \ref sol().
     2139    Value primal() const { return _getSolValue()+obj_const_comp;}
     2140
     2141    /// Return the value of the column
     2142
     2143    /// This function returns the value of the given column in the solution.
     2144    /// It is an alias for the corresponding \ref sol() function.
     2145    /// \pre The problem is solved.
     2146    Value primal(Col c) const { return _getSol(cols(id(c))); }
     2147
     2148    /// Return the value of the expression
     2149
     2150    /// This function returns the value of the given expression in the solution,
     2151    /// i.e. the dot product of the solution and the expression.
     2152    /// It is an alias for the corresponding \ref sol() function.
     2153    /// \pre The problem is solved.
     2154    Value primal(const Expr& e) const {
     2155      double res = *e;
     2156      for (Expr::ConstCoeffIt c(e); c != INVALID; ++c) {
     2157        res += *c * sol(c);
     2158      }
     2159      return res;
     2160    }
     2161
     2162    /// Return the value of the objective function
     2163   
     2164    /// This function returns the value of the objective function.
     2165    /// It is an alias for \ref sol().
     2166    Value primalValue() const { return _getSolValue()+obj_const_comp;}
     2167
    20712168    ///@}
    20722169
    20732170  protected: