ConstrainedSolver.hh 18.1 KB
Newer Older
Henrik Zimmer's avatar
Henrik Zimmer committed
1 2
/*===========================================================================*\
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
3
 *                               CoMISo                                      *
Henrik Zimmer's avatar
Henrik Zimmer committed
4
 *      Copyright (C) 2008-2009 by Computer Graphics Group, RWTH Aachen      *
Henrik Zimmer's avatar
Henrik Zimmer committed
5
 *                           www.rwth-graphics.de                            *
Henrik Zimmer's avatar
Henrik Zimmer committed
6
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
7 8
 *---------------------------------------------------------------------------* 
 *  This file is part of CoMISo.                                             *
Henrik Zimmer's avatar
Henrik Zimmer committed
9
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
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's avatar
Henrik Zimmer committed
14
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
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's avatar
Henrik Zimmer committed
18
 *  GNU General Public License for more details.                             *
Henrik Zimmer's avatar
Henrik Zimmer committed
19
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
20 21
 *  You should have received a copy of the GNU General Public License        *
 *  along with CoMISo.  If not, see <http://www.gnu.org/licenses/>.          *
Henrik Zimmer's avatar
Henrik Zimmer committed
22 23
 *                                                                           *
\*===========================================================================*/ 
Henrik Zimmer's avatar
Henrik Zimmer committed
24 25


26 27 28 29 30 31 32
//=============================================================================
//
//  CLASS ConstrainedSolver
//
//=============================================================================


33 34
#ifndef COMISO_CONSTRAINEDSOLVER_HH
#define COMISO_CONSTRAINEDSOLVER_HH
35 36 37


//== INCLUDES =================================================================
Henrik Zimmer's avatar
Henrik Zimmer committed
38
#include <CoMISo/Config/CoMISoDefines.hh>
39 40 41 42 43 44 45 46 47 48 49

#include "GMM_Tools.hh"
#include "MISolver.hh"
#include <vector>

//== FORWARDDECLARATIONS ======================================================

//== DEFINES ==================================================================
#define ROUND(x) ((x)<0?int((x)-0.5):int((x)+0.5))
//== NAMESPACES ===============================================================

50
namespace COMISO {
51 52 53 54 55 56 57 58 59 60
//== CLASS DEFINITION =========================================================

/** \class ConstrainedSolver ConstrainedSolver.hh <ACG/.../ConstrainedSolver.hh>

  Takes a linear (symmetric) system of equations and a set of linear constraints and solves it.
 */

class COMISODLLEXPORT ConstrainedSolver
{
public:
61 62
  typedef gmm::csc_matrix<double>                    CSCMatrix;
  typedef gmm::row_matrix< gmm::wsvector< double > > RowMatrix;
63 64 65


  /// default Constructor
66 67
  /** _do_gcd specifies if a greatest common devisor correction should be used when no (+-)1-coefficient is found*/
  ConstrainedSolver( bool _do_gcd = true): do_gcd_(_do_gcd)
David Bommes's avatar
David Bommes committed
68
  { epsilon_ = 1e-8; noisy_ = 1; }
69 70 71 72 73 74 75 76 77 78 79

/** @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$
80
  *  @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$.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  *  @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<class RMatrixT, class CMatrixT, class VectorT, class VectorIT >
  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 );

David Bommes's avatar
David Bommes committed
100 101 102 103 104 105 106 107 108 109 110 111
  // const version of above function
  template<class RMatrixT, class CMatrixT, class VectorT, class VectorIT >
  void solve_const(
      const RMatrixT& _constraints,
      const CMatrixT& _A, 
            VectorT&  _x,
      const VectorT&  _rhs,
      const VectorIT& _idx_to_round,
            double    _reg_factor = 0.0,
            bool      _show_miso_settings = true,
            bool      _show_timings = true );

112 113 114
  // efficent re-solve with modified _constraint_rhs and/or _rhs (if not modified use 0 pointer)
  // by keeping previous _constraints and _A fixed
  // _constraint_rhs and _rhs are constant, i.e. not changed
David Bommes's avatar
David Bommes committed
115
  template<class VectorT >
116 117
  void resolve(
       VectorT&  _x,
118 119
       VectorT*  _constraint_rhs = 0,
       VectorT*  _rhs            = 0,
120 121
       bool      _show_timings = true );

122 123 124

/// Non-Quadratic matrix constrained solver
/**  
125 126
  *  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, \cdots, 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.
127
  *  \note Internally the \f$ A=B^\top B \f$ matrix is formed.
128
  *  @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$.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  *  @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<class RMatrixT, class VectorT, class VectorIT >
  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 );
David Bommes's avatar
David Bommes committed
145 146 147 148

  // const version of above function
  template<class RMatrixT, class VectorT, class VectorIT >
  void solve_const(
149 150 151 152 153 154 155 156 157
      const RMatrixT& _constraints,
      const RMatrixT& _B,
      VectorT&  _x,
      const VectorIT& _idx_to_round,
      double    _reg_factor = 0.0,
      bool      _show_miso_settings = true,
      bool      _show_timings = true );

  // efficent re-solve with modified _rhs by keeping previous _constraints and _A fixed
158
  // ATTENTION: only the rhs resulting from B^TB can be changed!!! otherwise use solve
159 160 161 162
  template<class RMatrixT, class VectorT >
    void resolve(
      const RMatrixT& _B,
      VectorT&  _x,
163
      VectorT*  _constraint_rhs = 0,
David Bommes's avatar
David Bommes committed
164 165
      bool      _show_timings = true );

166 167
/*@}*/

168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/** @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<class RMatrixT, class VectorIT >
  void make_constraints_independent(
      RMatrixT&         _constraints,
			VectorIT&         _idx_to_round,
			std::vector<int>& _c_elim );

David Bommes's avatar
David Bommes committed
190 191 192 193 194 195
  template<class RMatrixT, class VectorIT >
  void make_constraints_independent_reordering(
      RMatrixT&         _constraints,
			VectorIT&         _idx_to_round,
			std::vector<int>& _c_elim );

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
/// 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<class SVector1T, class SVector2T, class VectorIT, class SVector3T>
  void eliminate_constraints(
      gmm::row_matrix<SVector1T>& _constraints,
			gmm::row_matrix<SVector2T>& _B, 
			VectorIT&                   _idx_to_round,
			std::vector<int>&           _c_elim,
			std::vector<int>&           _new_idx,
			gmm::col_matrix<SVector3T>& _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<class SVector1T, class SVector2T, class VectorIT, class CSCMatrixT>
  void eliminate_constraints(
      gmm::row_matrix<SVector1T>& _constraints,
      gmm::col_matrix<SVector2T>& _A, 
      std::vector<double>&        _x, 
      std::vector<double>&        _rhs, 
      VectorIT&                   _idx_to_round,
      std::vector<int>&           _c_elim,
      std::vector<int>&           _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<class RMatrixT, class VectorT >
  void restore_eliminated_vars( RMatrixT&         _constraints,
				VectorT&          _x,
				std::vector<int>& _c_elim,
				std::vector<int>& _new_idx);


/*@}*/

257
  /// Set numerical epsilon for valid constraint coefficient
David Bommes's avatar
David Bommes committed
258
  void set_epsilon( double _epsilon) { epsilon_ = _epsilon; }
259

260 261 262
  /// Set noise-level (how much std output is given) 0 basically none, 1 important stuff (warning/timing, is default), 2+ not so important
  void set_noisy( int _noisy) { noisy_ = _noisy;}

263 264 265
  // Get/Set whether the constraint reordering is used (default true)
  bool& use_constraint_reordering() { return miso_.use_constraint_reordering(); }

266 267 268 269 270 271 272 273 274 275 276
/** @name Verify the result.
 * Functions to verify the result of the constrained solver. Are the constraints met, are the correct variables correctly rounded ...
 */
/*@{*/


  template<class RMatrixT, class CMatrixT, class VectorT>
  double verify_constrained_system( 
				   const RMatrixT& _conditions,
				   const CMatrixT& _A,
				   const VectorT&  _x,
David Bommes's avatar
David Bommes committed
277 278
				   const VectorT&  _rhs,
				   double          _eps = 1e-9);
279 280 281 282 283 284 285

  template<class RMatrixT, class CMatrixT, class VectorT, class VectorIT>
  double verify_constrained_system_round( 
					 const RMatrixT& _conditions,
					 const CMatrixT& _A,
					 const VectorT&  _x,
					 const VectorT&  _rhs,
David Bommes's avatar
David Bommes committed
286 287
					 const VectorIT& _idx_to_round,
					 double          _eps = 1e-9);
288 289 290 291 292 293 294 295

  template<class RMatrixT, class VectorT, class VectorIT>
  void verify_mi_factored( const RMatrixT& _conditions,
			   const RMatrixT& _B, 
			   const VectorT&  _x,
			   const VectorIT& _idx_to_round );
/*@}*/

David Bommes's avatar
David Bommes committed
296 297 298 299

  /// Access the MISolver (e.g. to change settings)
  COMISO::MISolver& misolver() { return miso_;}

300 301 302
private:

  template<class RowT, class MatrixT>
Andreas Fabri's avatar
Andreas Fabri committed
303 304 305 306
    void add_row( gmm::size_type _row_i,
		double         _coeff,
		RowT           _row, 
		MatrixT&       _mat );
307 308

  template<class RowT, class RMatrixT, class CMatrixT>
Andreas Fabri's avatar
Andreas Fabri committed
309
    void add_row_simultaneously( gmm::size_type       _row_i,
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
			       double    _coeff,
			       RowT      _row, 
			       RMatrixT& _rmat,
			       CMatrixT& _cmat );


  template<class CMatrixT, class VectorT, class VectorIT>
  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<class CMatrixT>
  void eliminate_columns( CMatrixT& _M,
			  const std::vector< int >& _columns);

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  inline int gcd( int _a, int _b)
  {
    while( _b != 0)
    {
      int t(_b);
      _b = _a%_b;
      _a = t;
    }
    return _a;
  }

  int find_gcd(std::vector<int>& _v_gcd, int& _n_ints);
  // TODO if no gcd correction was possible, at least use a variable divisible by 2 as new elim_j (to avoid in-exactness e.g. 1/3)
  template<class RMatrixT>
  bool update_constraint_gcd( RMatrixT& _constraints, 
                              int _row_i,
                              int& _elim_j,
                              std::vector<int>& _v_gcd,
                              int& _n_ints);

349 350 351 352 353 354 355
private:

  /// Copy constructor (not used)
  ConstrainedSolver(const ConstrainedSolver& _rhs);

  /// Assignment operator (not used)
  ConstrainedSolver& operator=(const ConstrainedSolver& _rhs);
356

David Bommes's avatar
David Bommes committed
357 358 359
  // MISO solver
  COMISO::MISolver miso_;

360
  double epsilon_;
361
  int    noisy_;
362
  bool   do_gcd_;
363 364 365 366 367 368

  // --------------- Update by Marcel to enable efficient re-solve with changed rhs ----------------------
  // Store for symbolic elimination information for rhs
  class rhsUpdateTable {
  public:

369 370 371 372 373
    void append(int _i, double _f, int _j, bool _flag)
    {
//      std::cerr << "append " << _i << ", " << _j << ", " << _f << ", " << int(_flag) << std::endl;
      table_.push_back(rhsUpdateTableEntry(_i, _j, _f, _flag));
    }
374 375 376
    void add_elim_id(int _i) { elim_var_ids_.push_back(_i); }
    void clear() { table_.clear(); elim_var_ids_.clear(); }
    // apply stored transformations to _rhs
377
    void apply(std::vector<double>& _constraint_rhs, std::vector<double>& _rhs)
378 379 380 381 382 383 384
    {
      std::vector<rhsUpdateTableEntry>::const_iterator t_it, t_end;
      t_end = table_.end();
      int cur_j = -1;
      double cur_rhs = 0.0;
      for(t_it = table_.begin(); t_it != t_end; ++t_it)
      {
385 386 387
        if(t_it->rhs_flag)
            _rhs[t_it->i] += t_it->f*_constraint_rhs[t_it->j];
        else
David Bommes's avatar
David Bommes committed
388
        {
389 390 391 392 393 394 395 396 397 398 399 400 401
          if(t_it->j != cur_j) { cur_j = t_it->j; cur_rhs = _rhs[cur_j]; }
          _rhs[t_it->i] += t_it->f * cur_rhs;
        }
      }
    }
    // remove eliminated elements from _rhs
    void eliminate(std::vector<double>& _rhs)
    {
      std::vector<int> evar( elim_var_ids_ );
      std::sort( evar.begin(), evar.end() );
      evar.push_back( std::numeric_limits<int>::max() );

      int cur_evar_idx=0;
402 403
      size_t nc = _rhs.size();
      for (size_t i = 0; i < nc; ++i)
404
      {
405
        size_t next_i = evar[cur_evar_idx];
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

        if ( i != next_i ) _rhs[i-cur_evar_idx] = _rhs[i];
        else ++cur_evar_idx;
      }
      _rhs.resize( nc - cur_evar_idx );
    }
    // store transformed constraint matrix and index map to allow for later re-substitution
    template<class RMatrixT>
    void store(const RMatrixT& _constraints, const std::vector<int>& _c_elim, const std::vector<int>& _new_idx)
    {
      constraints_p_.resize( gmm::mat_nrows(_constraints), gmm::mat_ncols(_constraints));
      gmm::copy(_constraints, constraints_p_);
      c_elim_ = _c_elim;
      new_idx_ = _new_idx;
    }

  private:
    class rhsUpdateTableEntry {
    public:
425
      rhsUpdateTableEntry(int _i, int _j, double _f, bool _rhs_flag) : i(_i), j(_j), f(_f), rhs_flag(_rhs_flag) {}
426 427 428
      int i;
      int j;
      double f;
429
      bool rhs_flag;
430 431 432 433
    };

    std::vector<rhsUpdateTableEntry> table_;
    std::vector<int> elim_var_ids_;
434

435 436 437 438
  public:
    std::vector<int> c_elim_;
    std::vector<int> new_idx_;
    RowMatrix constraints_p_;
439 440 441 442 443 444

    // cache current rhs_ and constraint_rhs_ and linear transformation of constraint_rhs_ D_
    RowMatrix D_;
    std::vector<double> cur_rhs_;
    // constraint_rhs after Gaussian elimination update D*constraint_rhs_orig_
    std::vector<double> cur_constraint_rhs_;
445 446
  } rhs_update_table_;

447 448 449 450
};


//=============================================================================
451
} // namespace COMISO
452
//=============================================================================
453 454
#if defined(INCLUDE_TEMPLATES) && !defined(COMISO_CONSTRAINEDSOLVER_C)
#define COMISO_CONSTRAINEDSOLVER_TEMPLATES
455 456 457
#include "ConstrainedSolverT.cc"
#endif
//=============================================================================
458
#endif // COMISO_CONSTRAINEDSOLVER_HH defined
459 460
//=============================================================================