IterationController.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 /* NOTE The class IterationController has been adapted from the iteration
00026  *      class of the GMM++ and ITL libraries.
00027  */
00028 
00029 //=======================================================================
00030 // Copyright (C) 1997-2001
00031 // Authors: Andrew Lumsdaine <lums@osl.iu.edu> 
00032 //          Lie-Quan Lee     <llee@osl.iu.edu>
00033 //
00034 // This file is part of the Iterative Template Library
00035 //
00036 // You should have received a copy of the License Agreement for the
00037 // Iterative Template Library along with the software;  see the
00038 // file LICENSE.  
00039 //
00040 // Permission to modify the code and to distribute modified code is
00041 // granted, provided the text of this NOTICE is retained, a notice that
00042 // the code was modified is included with the above COPYRIGHT NOTICE and
00043 // with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE
00044 // file is distributed with the modified code.
00045 //
00046 // LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.
00047 // By way of example, but not limitation, Licensor MAKES NO
00048 // REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
00049 // PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS
00050 // OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS
00051 // OR OTHER RIGHTS.
00052 //=======================================================================
00053 
00054 //========================================================================
00055 //
00056 // Copyright (C) 2002-2007 Yves Renard
00057 //
00058 // This file is a part of GETFEM++
00059 //
00060 // Getfem++ is free software; you can redistribute it and/or modify
00061 // it under the terms of the GNU Lesser General Public License as
00062 // published by the Free Software Foundation; version 2.1 of the License.
00063 //
00064 // This program is distributed in the hope that it will be useful,
00065 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00066 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00067 // GNU Lesser General Public License for more details.
00068 // You should have received a copy of the GNU Lesser General Public
00069 // License along with this program; if not, write to the Free Software
00070 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301,
00071 // USA.
00072 //
00073 //========================================================================
00074 
00075 #ifndef EIGEN_ITERATION_CONTROLLER_H
00076 #define EIGEN_ITERATION_CONTROLLER_H
00077 
00078 namespace Eigen { 
00079 
00088 class IterationController
00089 {
00090   protected :
00091     double m_rhsn;        
00092     size_t m_maxiter;     
00093     int m_noise;          
00094     double m_resmax;      
00095     double m_resminreach, m_resadd;
00096     size_t m_nit;         
00097     double m_res;         
00098     bool m_written;
00099     void (*m_callback)(const IterationController&);
00100   public :
00101 
00102     void init()
00103     {
00104       m_nit = 0; m_res = 0.0; m_written = false;
00105       m_resminreach = 1E50; m_resadd = 0.0;
00106       m_callback = 0;
00107     }
00108 
00109     IterationController(double r = 1.0E-8, int noi = 0, size_t mit = size_t(-1))
00110       : m_rhsn(1.0), m_maxiter(mit), m_noise(noi), m_resmax(r) { init(); }
00111 
00112     void operator ++(int) { m_nit++; m_written = false; m_resadd += m_res; }
00113     void operator ++() { (*this)++; }
00114 
00115     bool first() { return m_nit == 0; }
00116 
00117     /* get/set the "noisyness" (verbosity) of the solvers */
00118     int noiseLevel() const { return m_noise; }
00119     void setNoiseLevel(int n) { m_noise = n; }
00120     void reduceNoiseLevel() { if (m_noise > 0) m_noise--; }
00121 
00122     double maxResidual() const { return m_resmax; }
00123     void setMaxResidual(double r) { m_resmax = r; }
00124 
00125     double residual() const { return m_res; }
00126 
00127     /* change the user-definable callback, called after each iteration */
00128     void setCallback(void (*t)(const IterationController&))
00129     {
00130       m_callback = t;
00131     }
00132 
00133     size_t iteration() const { return m_nit; }
00134     void setIteration(size_t i) { m_nit = i; }
00135 
00136     size_t maxIterarions() const { return m_maxiter; }
00137     void setMaxIterations(size_t i) { m_maxiter = i; }
00138 
00139     double rhsNorm() const { return m_rhsn; }
00140     void setRhsNorm(double r) { m_rhsn = r; }
00141 
00142     bool converged() const { return m_res <= m_rhsn * m_resmax; }
00143     bool converged(double nr)
00144     {
00145       m_res = internal::abs(nr); 
00146       m_resminreach = (std::min)(m_resminreach, m_res);
00147       return converged();
00148     }
00149     template<typename VectorType> bool converged(const VectorType &v)
00150     { return converged(v.squaredNorm()); }
00151 
00152     bool finished(double nr)
00153     {
00154       if (m_callback) m_callback(*this);
00155       if (m_noise > 0 && !m_written)
00156       {
00157         converged(nr);
00158         m_written = true;
00159       }
00160       return (m_nit >= m_maxiter || converged(nr));
00161     }
00162     template <typename VectorType>
00163     bool finished(const MatrixBase<VectorType> &v)
00164     { return finished(double(v.squaredNorm())); }
00165 
00166 };
00167 
00168 } // end namespace Eigen
00169 
00170 #endif // EIGEN_ITERATION_CONTROLLER_H