Ticket #326: 326-lp-mip-unify-d4c3ba9ac811.patch
File 326-lp-mip-unify-d4c3ba9ac811.patch, 14.1 KB (added by , 15 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 146 146 147 147 ///Iterator for iterate over the columns of an LP problem 148 148 149 /// Its usage is quite simple, for example you can count the number149 /// Its usage is quite simple, for example, you can count the number 150 150 /// of columns in an LP \c lp: 151 151 ///\code 152 152 /// int count=0; … … 241 241 242 242 ///Iterator for iterate over the rows of an LP problem 243 243 244 /// Its usage is quite simple, for example you can count the number244 /// Its usage is quite simple, for example, you can count the number 245 245 /// of rows in an LP \c lp: 246 246 ///\code 247 247 /// int count=0; … … 943 943 virtual int _addCol() = 0; 944 944 virtual int _addRow() = 0; 945 945 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 946 954 virtual void _eraseCol(int col) = 0; 947 955 virtual void _eraseRow(int row) = 0; 948 956 … … 1207 1215 ///\param u is the upper bound (\ref INF means no bound) 1208 1216 ///\return The created row. 1209 1217 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)); 1212 1222 return r; 1213 1223 } 1214 1224 … … 1217 1227 ///\param c is a linear expression (see \ref Constr) 1218 1228 ///\return The created row. 1219 1229 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)); 1222 1236 return r; 1223 1237 } 1224 1238 ///Erase a column (i.e a variable) from the LP … … 1776 1790 /// 1777 1791 /// \brief Common base class for LP solvers 1778 1792 /// 1779 /// This class is an abstract base class for LP solvers. This class1793 /// This class is an abstract base class for LP solvers. It 1780 1794 /// provides a full interface for set and modify an LP problem, 1781 1795 /// solve it and retrieve the solution. You can use one of the 1782 1796 /// descendants as a concrete implementation, or the \c Lp … … 1786 1800 class LpSolver : virtual public LpBase { 1787 1801 public: 1788 1802 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. 1790 1806 enum ProblemType { 1791 1807 /// = 0. Feasible solution hasn't been found (but may exist). 1792 1808 UNDEFINED = 0, … … 1800 1816 UNBOUNDED = 4 1801 1817 }; 1802 1818 1803 ///The basis status of variables 1819 /// \brief The basis status of variables 1820 /// 1821 /// The basis status of variables. 1804 1822 enum VarStatus { 1805 1823 /// The variable is in the basis 1806 1824 BASIC, … … 1843 1861 1844 1862 ///@{ 1845 1863 1846 ///\ e Solve the LP problem at hand1864 ///\brief Solve the specified LP problem 1847 1865 /// 1866 ///This function solves the specified LP problem. 1848 1867 ///\return The result of the optimization procedure. Possible 1849 1868 ///values and their meanings can be found in the documentation of 1850 1869 ///\ref SolveExitStatus. … … 1856 1875 1857 1876 ///@{ 1858 1877 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 1860 1882 ProblemType primalType() const { 1861 1883 return _getPrimalType(); 1862 1884 } 1863 1885 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 1865 1890 ProblemType dualType() const { 1866 1891 return _getDualType(); 1867 1892 } 1868 1893 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 1869 1904 /// Return the primal value of the column 1870 1905 1871 /// Return the primal value of thecolumn.1906 /// This function returns the primal value of the given column. 1872 1907 /// \pre The problem is solved. 1873 1908 Value primal(Col c) const { return _getPrimal(cols(id(c))); } 1874 1909 1875 1910 /// Return the primal value of the expression 1876 1911 1877 /// Return the primal value of the expression, i.e. the dot1878 /// 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. 1879 1914 /// \pre The problem is solved. 1880 1915 Value primal(const Expr& e) const { 1881 1916 double res = *e; … … 1884 1919 } 1885 1920 return res; 1886 1921 } 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 1888 1930 1889 1931 /// The primal ray is solution of the modified primal problem, 1890 1932 /// where we change each finite bound to 0, and we looking for a … … 1901 1943 1902 1944 /// Return the dual value of the row 1903 1945 1904 /// Return the dual value of therow.1946 /// This function returns the dual value of the given row. 1905 1947 /// \pre The problem is solved. 1906 1948 Value dual(Row r) const { return _getDual(rows(id(r))); } 1907 1949 1908 1950 /// Return the dual value of the dual expression 1909 1951 1910 /// Return the dual value of the dual expression, i.e. the dot1911 /// 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. 1912 1954 /// \pre The problem is solved. 1913 1955 Value dual(const DualExpr& e) const { 1914 1956 double res = 0.0; … … 1918 1960 return res; 1919 1961 } 1920 1962 1921 /// Return sa component of the dual ray1963 /// Return a component of the dual ray 1922 1964 1923 1965 /// The dual ray is solution of the modified primal problem, where 1924 1966 /// we change each finite bound to 0 (i.e. the objective function … … 1935 1977 1936 1978 /// Return the basis status of the column 1937 1979 1980 /// This function returns the basis status of the column. 1938 1981 /// \see VarStatus 1939 1982 VarStatus colStatus(Col c) const { return _getColStatus(cols(id(c))); } 1940 1983 1941 1984 /// Return the basis status of the row 1942 1985 1986 /// This function returns the basis status of the row. 1943 1987 /// \see VarStatus 1944 1988 VarStatus rowStatus(Row r) const { return _getRowStatus(rows(id(r))); } 1945 1946 ///The value of the objective function1947 1948 ///\return1949 ///- \ref INF or -\ref INF means either infeasibility or unboundedness1950 /// 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;}1954 1989 ///@} 1955 1990 1956 protected:1957 1958 1991 }; 1959 1992 1960 1993 … … 1962 1995 /// 1963 1996 /// \brief Common base class for MIP solvers 1964 1997 /// 1965 /// This class is an abstract base class for MIP solvers. This class1998 /// This class is an abstract base class for MIP solvers. It 1966 1999 /// provides a full interface for set and modify an MIP problem, 1967 2000 /// solve it and retrieve the solution. You can use one of the 1968 2001 /// descendants as a concrete implementation, or the \c Lp … … 1972 2005 class MipSolver : virtual public LpBase { 1973 2006 public: 1974 2007 1975 /// The problem types for MIP problems 2008 /// \brief The problem types for MIP problems 2009 /// 2010 /// The problem types for MIP problems. 1976 2011 enum ProblemType { 1977 2012 /// = 0. Feasible solution hasn't been found (but may exist). 1978 2013 UNDEFINED = 0, … … 1996 2031 1997 2032 ///@{ 1998 2033 1999 /// Solve the MIP problem at hand2034 ///\brief Solve the specified MIP problem 2000 2035 /// 2036 ///This function solves the specified MIP problem. 2001 2037 ///\return The result of the optimization procedure. Possible 2002 2038 ///values and their meanings can be found in the documentation of 2003 2039 ///\ref SolveExitStatus. … … 2008 2044 ///\name Set Column Type 2009 2045 ///@{ 2010 2046 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 { 2013 2052 /// = 0. Continuous variable (default). 2014 2053 REAL = 0, 2015 2054 /// = 1. Integer variable. 2016 2055 INTEGER = 1 2017 2056 }; 2018 2057 2058 /// \brief The column types for MIP problems. It is an obsolete alias for 2059 /// \ref ColType. 2060 typedef ColType ColTypes; 2061 2019 2062 ///Sets the type of the given column to the given type 2020 2063 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. 2022 2065 /// 2023 2066 void colType(Col c, ColTypes col_type) { 2024 2067 _setColType(cols(id(c)),col_type); … … 2026 2069 2027 2070 ///Gives back the type of the column. 2028 2071 2029 /// Gives back the type of the column.2072 ///This function gives back the type of the column. 2030 2073 /// 2031 2074 ColTypes colType(Col c) const { 2032 2075 return _getColType(cols(id(c))); … … 2037 2080 2038 2081 ///@{ 2039 2082 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 2041 2087 ProblemType type() const { 2042 2088 return _getType(); 2043 2089 } 2044 2090 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;} 2046 2100 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. 2048 2104 /// \pre The problem is solved. 2049 2105 Value sol(Col c) const { return _getSol(cols(id(c))); } 2050 2106 2051 /// Return the value of the expression in the solution2107 /// Return the value of the expression 2052 2108 2053 /// Return the value of the expression in the solution, i.e. the2054 /// 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. 2055 2111 /// \pre The problem is solved. 2056 2112 Value sol(const Expr& e) const { 2057 2113 double res = *e; … … 2060 2116 } 2061 2117 return res; 2062 2118 } 2063 ///The value of the objective function 2119 2120 /// Return the value of the objective function 2064 2121 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(). 2070 2124 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 2071 2168 ///@} 2072 2169 2073 2170 protected: