So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
ADMM.hpp
1 /*
2  * This file is part of bogus, a C++ sparse block matrix library.
3  *
4  * Copyright 2016 Gilles Daviet <gdaviet@gmail.com>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
10 
11 
12 #ifndef BOGUS_ADMM_HPP
13 #define BOGUS_ADMM_HPP
14 
15 #include "ConstrainedSolverBase.hpp"
16 
17 #include <vector>
18 
19 namespace bogus
20 {
21 
23 namespace admm {
25  enum Variant {
30  } ;
31 }
32 
34 
43 template < typename BlockMatrixType >
44 class ADMM : public ConstrainedSolverBase< ADMM<BlockMatrixType >, BlockMatrixType >
45 {
46 public:
48 
50  typedef typename GlobalProblemTraits::Scalar Scalar ;
51 
53  ADMM( ) : Base() { init() ; }
55  explicit ADMM( const BlockObjectBase< BlockMatrixType > & matrix ) : Base()
56  { init() ; Base::setMatrix( matrix ) ; }
57 
68  template < admm::Variant variant, typename NSLaw, typename ProxOp, typename RhsT, typename ResT >
69  Scalar solve(
70  const NSLaw &law, const ProxOp& op,
71  const RhsT &w, ResT &v, ResT &r ) const ;
72 
74  template < typename NSLaw, typename ProxOp, typename RhsT, typename ResT >
75  Scalar solve(
76  const NSLaw &law, const ProxOp& op,
77  const RhsT &w, ResT &v, ResT &r ) const ;
78 
81  {
82  m_matrix = &matrix ;
83  Base::updateScalings() ;
84  return *this ;
85  }
86 
88  void setStepSize( const Scalar size )
89  { m_stepSize = size ; }
90 
93  { m_defaultVariant = variant ; }
94 
95  Scalar stepSize() const { return m_stepSize ; }
96 
97 protected:
98 
99  typedef typename Base::Index Index ;
100 
102  void init()
103  {
104  m_tol = 1.e-6 ;
105  m_maxIters = 300 ;
106 
107  m_stepSize = 1.e-2 ;
108 
109  m_defaultVariant = admm::Accelerated ;
110  }
111 
112  using Base::m_matrix ;
113  using Base::m_maxIters ;
114  using Base::m_tol ;
115 
116  Scalar m_stepSize ;
117 
118  admm::Variant m_defaultVariant ;
119 
120 } ;
121 
123 
126 template< typename ObjectType >
128 {
130  typedef typename BlockTraits::Scalar Scalar ;
132 
134  typedef typename GlobalProblemTraits::DynVector AffineVec ;
135 
137 
143  QuadraticProxOp( const LinearOp& linearExpr, const Scalar lambda,
145  const AffineVec& affinePart )
146  : m_coefficient( lambda ), m_linearOp( linearExpr ),
147  m_affinePart( affinePart )
148  {
149  }
150 
152 
153  template < typename RhsT, typename ResT >
154  void eval( RhsT & rhs, ResT & res ) const ;
155 
157  Scalar coefficient() const {
158  return m_coefficient ;
159  }
160 
161 private:
162  const Scalar m_coefficient ;
163  const LinearOp& m_linearOp ;
164  const AffineVec& m_affinePart ;
165 };
166 
167 
169 
190 template < typename BlockMatrixType >
191 class DualAMA : public ConstrainedSolverBase< DualAMA<BlockMatrixType>, BlockMatrixType >
192 {
193 public:
195 
197  typedef typename GlobalProblemTraits::Scalar Scalar ;
198 
200  DualAMA( ) : Base() { init() ; }
202  explicit DualAMA( const BlockObjectBase< BlockMatrixType > & matrix ) : Base()
203  { init() ; Base::setMatrix( matrix ) ; }
204 
206  template < typename NSLaw, typename MatrixT, typename RhsT, typename ResT >
207  Scalar solve(
208  const NSLaw &law, const BlockObjectBase< MatrixT >& A,
209  const RhsT &f, const RhsT &w,
210  ResT &v, ResT &r ) const ;
211 
213  template < admm::Variant variant, typename NSLaw, typename MatrixT,
214  typename RhsT, typename ResT >
215  Scalar solve(
216  const NSLaw &law, const BlockObjectBase< MatrixT >& A,
217  const RhsT &f, const RhsT &w,
218  ResT &v, ResT &r ) const ;
219 
221  template < admm::Variant variant, typename NSLaw,
222  typename AType, typename BType, typename HType, typename PrecondT,
223  typename RhsT, typename ORhsT, typename ResT, typename OResT >
225  const NSLaw &law,
226  const BlockObjectBase< AType >& A,
227  const BlockObjectBase< BType >& B,
228  const BlockObjectBase< HType >& H,
229  const PrecondT& preconditioner,
230  const RhsT &f, const ORhsT& k, const RhsT &w,
231  ResT &v, OResT &p, ResT &r, Scalar stepRatio = 1 ) const ;
232 
235  {
236  m_matrix = &matrix ;
237  Base::updateScalings() ;
238  return *this ;
239  }
240 
242  void setFpStepSize( const Scalar size )
243  { m_fpStepSize = size ; }
244 
245  void setProjStepSize( const Scalar size )
246  { m_projStepSize = size ; }
247 
250  { m_defaultVariant = variant ; }
251 
252  Scalar fpStepSize() const { return m_fpStepSize ; }
253  Scalar projStepSize() const { return m_projStepSize ; }
254 
255  // LS
256 
258  void setLineSearchIterations( const unsigned lsIterations )
259  { m_lsIters = lsIterations ; }
260 
263  void setLineSearchOptimisticFactor( const Scalar lsOptimisticFactor )
264  { m_lsOptimisticFactor = lsOptimisticFactor ; }
265 
268  void setLineSearchPessimisticFactor( const Scalar lsPessimisticFactor )
269  { m_lsPessimisticFactor = lsPessimisticFactor ; }
270 
271  unsigned lineSearchIterations() const { return m_lsIters ; }
272  Scalar lineSearchOptimisticFactor() const { return m_lsOptimisticFactor ; }
273  Scalar lineSearchPessimisticFactor() const { return m_lsPessimisticFactor ; }
274 
275 protected:
276 
277  typedef typename Base::Index Index ;
278 
280  void init()
281  {
282  m_tol = 1.e-6 ;
283  m_maxIters = 300 ;
284 
285  m_fpStepSize = 1.e-1 ;
286  m_projStepSize = 1.e-1 ;
287 
288  m_lsIters = 6 ;
289  m_lsOptimisticFactor = 1.25 ;
290  m_lsPessimisticFactor = .5 ;
291 
292  m_defaultVariant = admm::Accelerated ;
293  }
294 
295  using Base::m_matrix ;
296  using Base::m_maxIters ;
297  using Base::m_tol ;
298 
299  Scalar m_fpStepSize ;
300  Scalar m_projStepSize ;
301 
302  admm::Variant m_defaultVariant ;
303 
304  unsigned m_lsIters ;
305  Scalar m_lsOptimisticFactor ;
306  Scalar m_lsPessimisticFactor ;
307 
308 } ;
309 
310 } //namespace bogus
311 
312 
313 #endif
314 
DualAMA()
Default constructor – you will have to call setMatrix() before using the solve() function.
Definition: ADMM.hpp:200
ADMM()
Default constructor – you will have to call setMatrix() before using the solve() function.
Definition: ADMM.hpp:53
Evaluation of prox_{1/c} J with J(x) = 1/2 xM&#39;x + f&#39;x.
Definition: ADMM.hpp:127
Definition: Traits.hpp:19
Scalar m_tol
See setTol()
Definition: BlockSolverBase.hpp:70
void setDefaultVariant(admm::Variant variant)
Sets the variant that will be used when calling solve() without template arguments.
Definition: ADMM.hpp:92
void setLineSearchPessimisticFactor(const Scalar lsPessimisticFactor)
Definition: ADMM.hpp:268
Scalar solve(const NSLaw &law, const BlockObjectBase< MatrixT > &A, const RhsT &f, const RhsT &w, ResT &v, ResT &r) const
Solve function using default variant.
void init()
Sets up the default values for all parameters.
Definition: ADMM.hpp:280
Standard ADMM.
Definition: ADMM.hpp:27
Definition: BlockSolvers.fwd.hpp:33
Variant
Variants of ADMM algorithm.
Definition: ADMM.hpp:25
void setLineSearchOptimisticFactor(const Scalar lsOptimisticFactor)
Definition: ADMM.hpp:263
ADMM(const BlockObjectBase< BlockMatrixType > &matrix)
Constructor with the system matrix.
Definition: ADMM.hpp:55
ADMM (Alternating Direction Method of Multipliers ) iterative solver.
Definition: ADMM.hpp:44
Scalar coefficient() const
Definition: ADMM.hpp:157
DualAMA & setMatrix(const BlockObjectBase< BlockMatrixType > &matrix)
Sets the problem matrix – the one defining the constraints.
Definition: ADMM.hpp:234
Derived & setMatrix(const BlockObjectBase< BlockMatrixType > &matrix)
Sets the system matrix and initializes internal structures.
Definition: ConstrainedSolverBase.hpp:62
Scalar solve(const NSLaw &law, const ProxOp &op, const RhsT &w, ResT &v, ResT &r) const
Dual AMA iterative solver (Alternating Minimization Algorithm on dual formuation of quadratic optimiz...
Definition: ADMM.hpp:191
void setFpStepSize(const Scalar size)
Sets the step size for updating the dual variable (forces).
Definition: ADMM.hpp:242
const BlockObjectBase< BlockMatrixType > * m_matrix
Pointer to the matrix of the system.
Definition: BlockSolverBase.hpp:65
QuadraticProxOp(const LinearOp &linearExpr, const Scalar lambda, const AffineVec &affinePart)
Construct the proximal evaluator of a quadratic function.
Definition: ADMM.hpp:144
ADMM & setMatrix(const BlockObjectBase< BlockMatrixType > &matrix)
Sets the problem matrix – the one defining the constraints.
Definition: ADMM.hpp:80
void setStepSize(const Scalar size)
Sets the step size for updating the dual variable (forces).
Definition: ADMM.hpp:88
unsigned m_maxIters
See setMaxIters()
Definition: BlockSolverBase.hpp:68
void setLineSearchIterations(const unsigned lsIterations)
Sets the maximum number of line-search iterations.
Definition: ADMM.hpp:258
DualAMA(const BlockObjectBase< BlockMatrixType > &matrix)
Constructor with the system matrix.
Definition: ADMM.hpp:202
void setDefaultVariant(admm::Variant variant)
Sets the variant that will be used when calling solve() without template arguments.
Definition: ADMM.hpp:249
ADDM with acceleration (see Goldstein 2014)
Definition: ADMM.hpp:29
Definition: ConstrainedSolverBase.hpp:20
void eval(RhsT &rhs, ResT &res) const
Evaluates prox_{1/coeff} J ( rhs/coeff )
void init()
Sets up the default values for all parameters.
Definition: ADMM.hpp:102
Scalar solveWithLinearConstraints(const NSLaw &law, const BlockObjectBase< AType > &A, const BlockObjectBase< BType > &B, const BlockObjectBase< HType > &H, const PrecondT &preconditioner, const RhsT &f, const ORhsT &k, const RhsT &w, ResT &v, OResT &p, ResT &r, Scalar stepRatio=1) const
Idem with constraint ( B v + k = 0 )