GetFEM  5.4.2
gmm_solver_constrained_cg.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2002-2020 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program; if not, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 
32 /**@file gmm_solver_constrained_cg.h
33  @author Yves Renard <Yves.Renard@insa-lyon.fr>
34  @date October 13, 2002.
35  @brief Constrained conjugate gradient. */
36 // preconditionning does not work
37 
38 #ifndef GMM_SOLVER_CCG_H__
39 #define GMM_SOLVER_CCG_H__
40 
41 #include "gmm_kernel.h"
42 #include "gmm_iter.h"
43 
44 namespace gmm {
45 
46  template <typename CMatrix, typename CINVMatrix, typename Matps,
47  typename VectorX>
48  void pseudo_inverse(const CMatrix &C, CINVMatrix &CINV,
49  const Matps& /* PS */, VectorX&) {
50  // compute the pseudo inverse of the non-square matrix C such
51  // CINV = inv(C * trans(C)) * C.
52  // based on a conjugate gradient method.
53 
54  // optimisable : copie de la ligne, precalcul de C * trans(C).
55 
56  typedef VectorX TmpVec;
57  typedef typename linalg_traits<VectorX>::value_type value_type;
58 
59  size_type nr = mat_nrows(C), nc = mat_ncols(C);
60 
61  TmpVec d(nr), e(nr), l(nc), p(nr), q(nr), r(nr);
62  value_type rho, rho_1, alpha;
63  clear(d);
64  clear(CINV);
65 
66  for (size_type i = 0; i < nr; ++i) {
67  d[i] = 1.0; rho = 1.0;
68  clear(e);
69  copy(d, r);
70  copy(d, p);
71 
72  while (rho >= 1E-38) { /* conjugate gradient to compute e */
73  /* which is the i nd row of inv(C * trans(C)) */
74  mult(gmm::transposed(C), p, l);
75  mult(C, l, q);
76  alpha = rho / vect_sp(p, q);
77  add(scaled(p, alpha), e);
78  add(scaled(q, -alpha), r);
79  rho_1 = rho;
80  rho = vect_sp(r, r);
81  add(r, scaled(p, rho / rho_1), p);
82  }
83 
84  mult(transposed(C), e, l); /* l is the i nd row of CINV */
85  // cout << "l = " << l << endl;
86  clean(l, 1E-15);
87  copy(l, mat_row(CINV, i));
88 
89  d[i] = 0.0;
90  }
91  }
92 
93  /** Compute the minimum of @f$ 1/2((Ax).x) - bx @f$ under the contraint @f$ Cx <= f @f$ */
94  template < typename Matrix, typename CMatrix, typename Matps,
95  typename VectorX, typename VectorB, typename VectorF,
96  typename Preconditioner >
97  void constrained_cg(const Matrix& A, const CMatrix& C, VectorX& x,
98  const VectorB& b, const VectorF& f,const Matps& PS,
99  const Preconditioner& M, iteration &iter) {
100  typedef typename temporary_dense_vector<VectorX>::vector_type TmpVec;
101  typedef typename temporary_vector<CMatrix>::vector_type TmpCVec;
102  typedef row_matrix<TmpCVec> TmpCmat;
103 
104  typedef typename linalg_traits<VectorX>::value_type value_type;
105  value_type rho = 1.0, rho_1, lambda, gamma;
106  TmpVec p(vect_size(x)), q(vect_size(x)), q2(vect_size(x)),
107  r(vect_size(x)), old_z(vect_size(x)), z(vect_size(x)),
108  memox(vect_size(x));
109  std::vector<bool> satured(mat_nrows(C));
110  clear(p);
111  iter.set_rhsnorm(sqrt(vect_sp(PS, b, b)));
112  if (iter.get_rhsnorm() == 0.0) iter.set_rhsnorm(1.0);
113 
114  TmpCmat CINV(mat_nrows(C), mat_ncols(C));
115  pseudo_inverse(C, CINV, PS, x);
116 
117  while(true) {
118  // computation of residu
119  copy(z, old_z);
120  copy(x, memox);
121  mult(A, scaled(x, -1.0), b, r);
122  mult(M, r, z); // preconditionner not coherent
123  bool transition = false;
124  for (size_type i = 0; i < mat_nrows(C); ++i) {
125  value_type al = vect_sp(mat_row(C, i), x) - f[i];
126  if (al >= -1.0E-15) {
127  if (!satured[i]) { satured[i] = true; transition = true; }
128  value_type bb = vect_sp(mat_row(CINV, i), z);
129  if (bb > 0.0) add(scaled(mat_row(C, i), -bb), z);
130  }
131  else
132  satured[i] = false;
133  }
134 
135  // descent direction
136  rho_1 = rho; rho = vect_sp(PS, r, z); // ...
137 
138  if (iter.finished(rho)) break;
139 
140  if (iter.get_noisy() > 0 && transition) std::cout << "transition\n";
141  if (transition || iter.first()) gamma = 0.0;
142  else gamma = std::max(0.0, (rho - vect_sp(PS, old_z, z) ) / rho_1);
143  // std::cout << "gamma = " << gamma << endl;
144  // itl::add(r, itl::scaled(p, gamma), p);
145  add(z, scaled(p, gamma), p); // ...
146 
147  ++iter;
148  // one dimensional optimization
149  mult(A, p, q);
150  lambda = rho / vect_sp(PS, q, p);
151  for (size_type i = 0; i < mat_nrows(C); ++i)
152  if (!satured[i]) {
153  value_type bb = vect_sp(mat_row(C, i), p) - f[i];
154  if (bb > 0.0)
155  lambda = std::min(lambda, (f[i]-vect_sp(mat_row(C, i), x)) / bb);
156  }
157  add(x, scaled(p, lambda), x);
158  add(memox, scaled(x, -1.0), memox);
159 
160  }
161  }
162 
163 }
164 
165 #endif // GMM_SOLVER_CCG_H__
bgeot::size_type
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:49
gmm::clear
void clear(L &l)
clear (fill with zeros) a vector or matrix.
Definition: gmm_blas.h:59
gmm::constrained_cg
void constrained_cg(const Matrix &A, const CMatrix &C, VectorX &x, const VectorB &b, const VectorF &f, const Matps &PS, const Preconditioner &M, iteration &iter)
Compute the minimum of under the contraint .
Definition: gmm_solver_constrained_cg.h:97
gmm::vect_sp
strongest_value_type< V1, V2 >::value_type vect_sp(const V1 &v1, const V2 &v2)
*‍/
Definition: gmm_blas.h:263
gmm::clean
void clean(L &l, double threshold)
Clean a vector or matrix (replace near-zero entries with zeroes).
gmm::iteration
The Iteration object calculates whether the solution has reached the desired accuracy,...
Definition: gmm_iter.h:53
bgeot::alpha
size_type alpha(short_type n, short_type d)
Return the value of which is the number of monomials of a polynomial of variables and degree .
Definition: bgeot_poly.cc:47
gmm_kernel.h
Include the base gmm files.
gmm::copy
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:977
gmm_iter.h
Iteration object.
gmm::add
void add(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:1268