00001 /* 00002 * Copyright 2007 Martin von Gagern 00003 * 00004 * 00005 * This file is part of bande. 00006 * 00007 * bande is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 3 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * bande is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00021 00022 /** 00023 * @file 00024 * Interface of class bande::IntegerProgram. 00025 */ 00026 00027 namespace bande { 00028 00029 /** 00030 * Linear program with integer bounds, coefficients and solutions. 00031 * 00032 * This class is mainly a wrapper around LinearProgram which changes 00033 * many of its data types from double to integer. In order to not 00034 * interfere with the LP, the integral bookkeeping is done using 00035 * duplicate data structures within this IP. 00036 * 00037 * In order to keep these dual structures synchronized, there is no 00038 * direct access to the protected linear program. All access must be 00039 * made through the corresponding integer program. 00040 */ 00041 class IntegerProgram { 00042 00043 public: 00044 IntegerProgram(); 00045 virtual ~IntegerProgram(); 00046 00047 void readMps(const char* fileName); 00048 void setColLower(UndoManager& um, int col, int val); 00049 void setColUpper(UndoManager& um, int col, int val); 00050 void mkIntSolution(); 00051 bool checkIntSolution() const; 00052 00053 /** 00054 * Get the number of rows. 00055 * Each row represents zero to two inequalities, depending on the 00056 * row bounds. 00057 * @return the number of rows in the problem. 00058 */ 00059 int getNumRows() const { return lp.getNumRows(); } 00060 00061 /** 00062 * Get the number of columns. 00063 * Each column corresponds to one variable of the solution. 00064 * @return the number of columns in the problem. 00065 */ 00066 int getNumCols() const { return lp.getNumCols(); } 00067 00068 /** 00069 * Find optimal solution to current linear program. 00070 * @return whether an optimal solution has been found. 00071 */ 00072 bool solve() { return lp.solve(); } 00073 00074 /** 00075 * Get one variable of the solution. 00076 * @param index the index of the variable. 00077 * @return the value of that variable in the current solution. 00078 */ 00079 double getColSolution(int index) const { return lp.getColSolution(index); } 00080 00081 /** 00082 * Dump the current solution for debugging purposes. 00083 * @param out the output stream to which the solution will be 00084 * dumped. 00085 */ 00086 void dumpSolution(std::ostream& out = std::cerr) const { 00087 lp.dumpSolution(out); 00088 } 00089 00090 /** 00091 * Get the current lower bound of a column. 00092 * @param col the index of the column. 00093 * @return the current lower bound of that column. 00094 */ 00095 int getColLower(int col) const { return colBounds[col].first; } 00096 00097 /** 00098 * Get the current upper bound of a column. 00099 * @param col the index of the column. 00100 * @return the current upper bound of that column. 00101 */ 00102 int getColUpper(int col) const { return colBounds[col].second; } 00103 00104 /** 00105 * Get both bounds of a column. 00106 * @param col the index of the column. 00107 * @return a pair of the lower (first) and upper (second) bound of 00108 * that column. 00109 */ 00110 const std::pair<int, int>& getColBounds(int col) const { 00111 return colBounds[col]; 00112 } 00113 00114 /** 00115 * Choose a random objective function. 00116 */ 00117 void randomizeObjective() { lp.randomizeObjective(); } 00118 00119 /** 00120 * Get the name of a column. 00121 * This is also the name of the associated variable. 00122 * @param col the index of the column. 00123 * @return the name of that column. 00124 */ 00125 std::string getColName(int col) const { return lp.getColName(col); } 00126 00127 /** 00128 * Get the name of a row. 00129 * @param row the index of the row. 00130 * @return the name of that row. 00131 */ 00132 std::string getRowName(int row) const { return lp.getRowName(row); } 00133 00134 /** 00135 * Get one variable of the solution as an integer. 00136 * 00137 * This function will only return the current values after 00138 * mkIntSolution() has been called. 00139 * 00140 * @param index the index of the variable. 00141 * @return the value of that variable in the current solution. 00142 */ 00143 int getIntSolution(int index) const { return iSol[index]; } 00144 00145 protected: 00146 00147 /** 00148 * The linear program wrapped inside this integer program. 00149 */ 00150 LinearProgram lp; 00151 00152 /** 00153 * The row bounds as integers. 00154 */ 00155 std::vector< std::pair<int, int> > rowBounds; 00156 00157 /** 00158 * The column bounds as integers. 00159 */ 00160 std::vector< std::pair<int, int> > colBounds; 00161 00162 /** 00163 * The matrix elements as integers. 00164 * 00165 * The matrix elements are stored in row major mode, and can be 00166 * accessed through the m() function by giving twodimensional 00167 * coordinates. 00168 */ 00169 std::vector<int> matrix; 00170 00171 /** 00172 * The solution vector rounded to integers. 00173 * This vector is not automatically filled in, but only when 00174 * mkIntSolution() is called. 00175 */ 00176 std::vector<int> iSol; 00177 00178 int mkInt(double d); 00179 00180 /** 00181 * Provide read-write access to the integral matrix elements. 00182 * @param row the row index of the matrix element. 00183 * @param col the column index of the matrix element. 00184 * @return a writable reference to the addressed matrix element. 00185 */ 00186 int& m(int row, int col) { return matrix[row*getNumCols() + col]; } 00187 }; 00188 00189 }
1.6.0