ConstrainedSolver.hh 12 KB
Newer Older
 Henrik Zimmer committed Aug 28, 2009 1 2 /*===========================================================================*\ * *  Henrik Zimmer committed Aug 28, 2009 3  * CoMISo *  Henrik Zimmer committed Aug 28, 2009 4  * Copyright (C) 2008-2009 by Computer Graphics Group, RWTH Aachen *  Henrik Zimmer committed Aug 28, 2009 5  * www.rwth-graphics.de *  Henrik Zimmer committed Aug 28, 2009 6  * *  Henrik Zimmer committed Aug 28, 2009 7 8  *---------------------------------------------------------------------------* * This file is part of CoMISo. *  Henrik Zimmer committed Aug 28, 2009 9  * *  Henrik Zimmer committed Sep 01, 2009 10 11 12 13  * CoMISo is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. *  Henrik Zimmer committed Aug 28, 2009 14  * *  Henrik Zimmer committed Aug 28, 2009 15 16 17  * CoMISo is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *  Henrik Zimmer committed Sep 01, 2009 18  * GNU General Public License for more details. *  Henrik Zimmer committed Aug 28, 2009 19  * *  Henrik Zimmer committed Sep 01, 2009 20 21  * You should have received a copy of the GNU General Public License * * along with CoMISo. If not, see . *  Henrik Zimmer committed Aug 28, 2009 22 23  * * \*===========================================================================*/  Henrik Zimmer committed Aug 28, 2009 24 25   Henrik Zimmer committed Aug 26, 2009 26 27 28 29 30 31 32 33 34 35 36 37 //============================================================================= // // CLASS ConstrainedSolver // //============================================================================= #ifndef ACG_CONSTRAINEDSOLVER_HH #define ACG_CONSTRAINEDSOLVER_HH //== INCLUDES =================================================================  Henrik Zimmer committed Aug 26, 2009 38 #include  Henrik Zimmer committed Aug 26, 2009 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284  #include "GMM_Tools.hh" #include "MISolver.hh" #include //== FORWARDDECLARATIONS ====================================================== //== DEFINES ================================================================== #define ROUND(x) ((x)<0?int((x)-0.5):int((x)+0.5)) //== NAMESPACES =============================================================== namespace ACG { //== CLASS DEFINITION ========================================================= /** \class ConstrainedSolver ConstrainedSolver.hh Takes a linear (symmetric) system of equations and a set of linear constraints and solves it. */ class COMISODLLEXPORT ConstrainedSolver { public: typedef gmm::csc_matrix CSCMatrix; /// default Constructor ConstrainedSolver() { } /// Destructor ~ConstrainedSolver() { } /** @name Contrained solvers * Functions to solve constrained linear systems of the form Ax=b (stemming from quadratic energies). * The constraints can be linear constraints of the form \f$x_1*c_1+ \cdots +x_n*c_n=c \f$ as well as integer constraints \f$x_i\in \mathbf{Z}\f$. * The system is then solved with these constraints in mind. For solving the system the Mixed-Integer Solver \a MISolver is used. */ /*@{*/ /// Quadratic matrix constrained solver /** * Takes a system of the form Ax=b, a constraint matrix C and a set of variables _to_round to be rounded to integers. \f$A\in \mathbf{R}^{n\times n}\f$ * @param _constraints row matrix with rows of the form \f$[c_1, c_2, \cdots, c_n, c_{n+1}] \f$ corresponding to the linear equation \f$c_1*x_1+\cdots+c_n*x_n + c_{n+1}=0 \f$. * @param _A nxn-dimensional column matrix of the system * @param _x n-dimensional variable vector * @param _rhs n-dimensional right hand side. * @param _idx_to_round indices i of variables x_i that shall be rounded * @param _reg_factor regularization factor. Helps unstable, low rank system to become solvable. Adds \f$\_reg\_factor*mean(trace(_A))*Id \f$ to A. * @param _show_miso_settings should the (QT) dialog of the Mixed-Integer solver pop up? * @param _show_timings shall some timings be printed? */ template void solve( RMatrixT& _constraints, CMatrixT& _A, VectorT& _x, VectorT& _rhs, VectorIT& _idx_to_round, double _reg_factor = 0.0, bool _show_miso_settings = true, bool _show_timings = true ); /// Non-Quadratic matrix constrained solver /** * Same as above, but performs the elimination of the constraints directly on the B matrix of \f$x^\top B^\top Bx \f$, where B has m rows (equations) and (n+1) columns \f$[x_1, x_2, \dots, \x_n, -rhs] \f$. \note This function might be more efficient in some cases, but generally the solver for the quadratic matrix above is a safer bet. Needs further testing. * \note Internally the \f$A=B^\top B \f$ matrix is formed. * @param _constraints row matrix with rows of the form \f$[c_1, c_2, \cdots, c_n, c_{n+1}] \f$ corresponding to the linear equation \f$c_1*x_1+\cdots+c_n*x_n + c_{n+1}=0 \f$. * @param _B mx(n+1)-dimensional column matrix of the system * @param _x n-dimensional variable vector * @param _idx_to_round indices i of variables x_i that shall be rounded * @param _reg_factor regularization factor. Helps unstable, low rank system to become solvable. * @param _show_miso_settings should the (QT) dialog of the Mixed-Integer solver pop up? * @param _show_timings shall some timings be printed? */ template void solve( RMatrixT& _constraints, RMatrixT& _B, VectorT& _x, VectorIT& _idx_to_round, double _reg_factor = 0.0, bool _show_miso_settings = true, bool _show_timings = true ); /*@}*/ /** @name Eliminate constraints * Functions to eliminate (or integrate) linear constraints from an equation system. These functions are used internally by the \a solve functions. */ /*@{*/ /// Make constraints independent /** * This function performs a Gauss elimination on the constraint matrix making the constraints easier to eliminate. * \note A certain amount of independence of the constraints is assumed. * \note contradicting constraints will be ignored. * \warning care must be taken when non-trivial constraints occur where some of the variables contain integer-variables (to be rounded) as the optimal result might not always occur. * @param _constraints row matrix with constraints * @param _idx_to_round indices of variables to be rounded (these must be considered.) * @param _c_elim the "returned" vector of variable indices and the order in which the can be eliminated. */ template void make_constraints_independent( RMatrixT& _constraints, VectorIT& _idx_to_round, std::vector& _c_elim ); /// Eliminate constraints on a factored matrix B /** * \note Constraints are assumed to have been made independent by \a make_constraints_independent. * @param _constraints row matrix with constraints (n+1 columns) * @param _B system row matrix mx(n+1) * @param _idx_to_round indices to be rounded * @param _c_elim the indices of the variables to be eliminated. * @param _new_idx the created re-indexing map. new_idx[i] = -1 means x_i eliminated, new_idx[i] = j means x_i is now at index j. * @param _Bcol resulting (smaller) column matrix to be used for future computations. (e.g. convert to CSC and solve) */ template void eliminate_constraints( gmm::row_matrix& _constraints, gmm::row_matrix& _B, VectorIT& _idx_to_round, std::vector& _c_elim, std::vector& _new_idx, gmm::col_matrix& _Bcol); /// Eliminate constraints on a quadratic matrix A /** * \note Constraints are assumed to have been made independent by \a make_constraints_independent. * \note _x must have correct size (same as _rhs) * @param _constraints row matrix with constraints (n+1 columns) * @param _A system row matrix nxn) * @param _x variable vector * @param _rhs right hand side * @param _idx_to_round indices to be rounded * @param _c_elim the indices of the variables to be eliminated. * @param _new_idx the created re-indexing map. new_idx[i] = -1 means x_i eliminated, new_idx[i] = j means x_i is now at index j. * @param _Acsc resulting (smaller) column (csc) matrix to be used for future computations. */ template void eliminate_constraints( gmm::row_matrix& _constraints, gmm::col_matrix& _A, std::vector& _x, std::vector& _rhs, VectorIT& _idx_to_round, std::vector& _c_elim, std::vector& _new_idx, CSCMatrixT& _Acsc); /// Restore a solution vector to the un-eliminated size /** * @param _constraints row matrix with constraints (n+1 columns) * @param _x solution vector to reduced/eliminated system (result will also be written here) * @param _c_elim vector of eliminated indices * @param _new_idx re-indexing vector */ template void restore_eliminated_vars( RMatrixT& _constraints, VectorT& _x, std::vector& _c_elim, std::vector& _new_idx); /*@}*/ /** @name Verify the result. * Functions to verify the result of the constrained solver. Are the constraints met, are the correct variables correctly rounded ... */ /*@{*/ template double verify_constrained_system( const RMatrixT& _conditions, const CMatrixT& _A, const VectorT& _x, const VectorT& _rhs); template double verify_constrained_system_round( const RMatrixT& _conditions, const CMatrixT& _A, const VectorT& _x, const VectorT& _rhs, const VectorIT& _idx_to_round); template void verify_mi_factored( const RMatrixT& _conditions, const RMatrixT& _B, const VectorT& _x, const VectorIT& _idx_to_round ); /*@}*/ private: template void add_row( int _row_i, double _coeff, RowT _row, MatrixT& _mat ); template void add_row_simultaneously( int _row_i, double _coeff, RowT _row, RMatrixT& _rmat, CMatrixT& _cmat ); template double setup_and_solve_system( CMatrixT& _B, VectorT& _x, VectorIT& _idx_to_round, double _reg_factor, bool _show_miso_settings); // warning: order of replacement not the same as in _columns (internal sort) template void eliminate_columns( CMatrixT& _M, const std::vector< int >& _columns); private: /// Copy constructor (not used) ConstrainedSolver(const ConstrainedSolver& _rhs); /// Assignment operator (not used) ConstrainedSolver& operator=(const ConstrainedSolver& _rhs); }; //============================================================================= } // namespace ACG //============================================================================= #if defined(INCLUDE_TEMPLATES) && !defined(ACG_CONSTRAINEDSOLVER_C) #define ACG_CONSTRAINEDSOLVER_TEMPLATES #include "ConstrainedSolverT.cc" #endif //============================================================================= #endif // ACG_CONSTRAINEDSOLVER_HH defined //=============================================================================