| 1 | // Tony : lambda=0.25 ou lambda=0.025 ???cxx . . $ `<file>/src/Algo/CubicLS.hpp`/ |
| 2 | // cf. CG.hpp |
| 3 | // |
| 4 | // The minimizer along the search direction is returned by the function. |
| 5 | // |
| 6 | /* |
| 7 | CubicLineSearch() class implements the efficient line search procedure |
| 8 | described in Dennis and Schbabel's book entitled "Numerical |
| 9 | Methods for Unconstrained and Nonlinear Equations. The |
| 10 | objective is to perform a unidimensional search for a minimum |
| 11 | point along a specified direction in a multidimensional |
| 12 | space. |
| 13 | */ |
| 14 | // This procedure seems to be fairly robust. It has worked for a fairly broad class of |
| 15 | // problems from optimization of standard test functons in optimization theory and |
| 16 | // to hard geophysical problems as stacking power optimization and amplitude |
| 17 | // seismogram inversion |
| 18 | |
| 19 | #ifndef CUBIC_LINE_SEARCH_HH |
| 20 | #define CUBIC_LINE_SEARCH_HHcxx . . $ `src/Algo/CubicLS.hpp:20:9`! |
| 21 | |
| 22 | #include "LineSearch.hpp"cxx . . $ `<file>/src/Algo/LineSearch.hpp`/ |
| 23 | #include <limits.h> |
| 24 | #include <float.h> |
| 25 | |
| 26 | template< class LS > |
| 27 | class CubicLineSearchcxx . . $ CubicLineSearch# : public LS { |
| 28 | typedef typename LS::Real Realcxx . . $ CubicLineSearch#Real#; |
| 29 | typedef typename LS::Param Paramcxx . . $ CubicLineSearch#Param#; |
| 30 | typedef typename LS::Vect Vectcxx . . $ CubicLineSearch#Vect#; |
| 31 | typedef typename LS::VMat VMatcxx . . $ CubicLineSearch#VMat#; |
| 32 | typedef LS LineSearchcxx . . $ CubicLineSearch#LineSearch#; |
| 33 | typedef tNRJcxx . . $ tNRJ#< Paramcxx . . $ CubicLineSearch#Param#, Vectcxx . . $ CubicLineSearch#Vect#, VMatcxx . . $ CubicLineSearch#VMat#, Realcxx . . $ CubicLineSearch#Real# > NRJcxx . . $ CubicLineSearch#NRJ#; |
| 34 | |
| 35 | public: |
| 36 | // a constructor with the default delta |
| 37 | CubicLineSearchcxx . . $ CubicLineSearch#`CubicLineSearch<LS>`(b0d51172318c85d6).(NRJcxx . . $ CubicLineSearch#NRJ#* f, int iter); |
| 38 | // a constructor with the specified delta |
| 39 | CubicLineSearchcxx . . $ CubicLineSearch#`CubicLineSearch<LS>`(5d34a9eae5819e2f).(NRJcxx . . $ CubicLineSearch#NRJ#* f, int iter, Vectcxx . . $ CubicLineSearch#Vect#* delta); |
| 40 | /* The parameter $delta$ is not used by the line search |
| 41 | itself. Rather it is used in the numerical computation |
| 42 | of the derivatives using centered differences. For |
| 43 | example the derivative of f(x) at the point x0 would be |
| 44 | given by |
| 45 | \[f(x0 - delta) - f(x0 + delta) / 2 * delta\] |
| 46 | */ |
| 47 | ~cxx . . $ CubicLineSearch#`~CubicLineSearch<LS>`(49f6e7a06ebc5aa8).CubicLineSearch( ); |
| 48 | |
| 49 | // search for the minimum model along a 1-D direction |
| 50 | Paramcxx . . $ CubicLineSearch#Param# searchcxx . . $ CubicLineSearch#search(aff2eb3ce65c7e24).( /// initial model for the line search |
| 51 | const Paramcxx . . $ CubicLineSearch#Param#& m0, |
| 52 | /// search direction |
| 53 | Vectcxx . . $ CubicLineSearch#Vect#& direction, |
| 54 | /// product of search direction and gradient |
| 55 | Realcxx . . $ CubicLineSearch#Real# descent, |
| 56 | /// a parameter |
| 57 | double lambda); |
| 58 | /* The parameter $lambda$ controls the accuraccy of the |
| 59 | line search. $lambda = .25$ is a good choice. |
| 60 | The minimizer along the search direction is returned |
| 61 | by the function. */ |
| 62 | |
| 63 | // lambda=0.25 ou lambda=0.025 ??? |
| 64 | // cf. CG.hpp |
| 65 | }; |
| 66 | |
| 67 | template< class LS > |
| 68 | CubicLineSearchcxx . . $ CubicLineSearch#< LS >::CubicLineSearchcxx . . $ CubicLineSearch#`CubicLineSearch<LS>`(b0d51172318c85d6).(NRJcxx . . $ CubicLineSearch#NRJ#* f, int it) : LS(f) { |
| 69 | this->iterMax = it; |
| 70 | } |
| 71 | |
| 72 | template< class LS > |
| 73 | CubicLineSearchcxx . . $ CubicLineSearch#< LS >::CubicLineSearchcxx . . $ CubicLineSearch#`CubicLineSearch<LS>`(5d34a9eae5819e2f).(NRJcxx . . $ CubicLineSearch#NRJ#* f, int it, Vectcxx . . $ CubicLineSearch#Vect#* interval) : LS(f, interval) { |
| 74 | this->iterMax = it; |
| 75 | } |
| 76 | |
| 77 | template< class LS > |
| 78 | CubicLineSearchcxx . . $ CubicLineSearch#< LS >::~cxx . . $ CubicLineSearch#`~CubicLineSearch<LS>`(49f6e7a06ebc5aa8).CubicLineSearch( ) {} |
| 79 | |
| 80 | // Code for the Cubic Line Search |
| 81 | template< class LS > |
| 82 | typename CubicLineSearchcxx . . $ CubicLineSearch#< LS >::Paramcxx . . $ CubicLineSearch#Param# CubicLineSearchcxx . . $ CubicLineSearch#< LS >::searchcxx . . $ CubicLineSearch#search(aff2eb3ce65c7e24).(const Paramcxx . . $ CubicLineSearch#Param#& current_solution, |
| 83 | Vectcxx . . $ CubicLineSearch#Vect#& p, Realcxx . . $ CubicLineSearch#Real# slope, |
| 84 | double lambda) { |
| 85 | |
| 86 | int tst = 0; // useful vars |
| 87 | Realcxx . . $ CubicLineSearch#Real# alpha = 0., alpha_prev = 0; |
| 88 | Realcxx . . $ CubicLineSearch#Real# fprev = 0; |
| 89 | Realcxx . . $ CubicLineSearch#Real# new_m = 0, old_m = 0; |
| 90 | Paramcxx . . $ CubicLineSearch#Param# new_solution(current_solution); |
| 91 | coutcxx . . $ cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " search " <<cxx . . $ `operator<<`(2587130c0179f881). p.max( ) <<cxx . . $ `operator<<`(2587130c0179f881). endlcxx . . $ endl(345bf97a19cc83a9).; |
| 92 | assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(p.max( ) < 1e100); |
| 93 | old_m = this->nrj->getVal(current_solution); // Evaluation at the current solution |
| 94 | this->iterNum = 0; |
| 95 | this->iterNum++; // iteration counter |
| 96 | alpha = 1.; // updating step |
| 97 | |
| 98 | new_solution = this->update(current_solution, 1, alpha, p); // updating |
| 99 | new_m = this->nrj->getVal(new_solution); // Evaluation at the |
| 100 | // new solution |
| 101 | this->iterNum++; |
| 102 | |
| 103 | // Implementing Goldstein's test for alpha too small |
| 104 | while (new_m <cxx . . $ std/`operator<`(c2ad8cf295ccec45). old_m +cxx . . $ operator+(1f62e43363bf5779). (1. - lambda) *cxx . . $ `operator*`(ca0d58f853b0940). alpha *cxx . . $ `operator*`(ca0d58f853b0940). slope && this->iterNum <cxx . . $ std/`operator<`(c2ad8cf295ccec45). this->iterMax) { |
| 105 | alpha *= 3; |
| 106 | new_solution = this->update(current_solution, 1, alpha, p); |
| 107 | new_m = this->nrj->getVal(new_solution); |
| 108 | this->iterNum++; |
| 109 | } |
| 110 | if (this->iterNum ==cxx . . $ std/`operator==`(bf4c77cd91c4b201). this->iterMax) cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Alpha overflowed! \n"; |
| 111 | |
| 112 | // Armijo's test for alpha too large |
| 113 | alpha_prev = alpha; // H.L. Deng, 6/13/95 |
| 114 | while (new_m >cxx . . $ std/`operator>`(c2ad8cf295ccec45). old_m +cxx . . $ operator+(1f62e43363bf5779). lambda *cxx . . $ `operator*`(ca0d58f853b0940). alpha *cxx . . $ `operator*`(ca0d58f853b0940). slope && this->iterNum <cxx . . $ std/`operator<`(c2ad8cf295ccec45). this->iterMax) { |
| 115 | Realcxx . . $ CubicLineSearch#Real# alpha2 = alpha *cxx . . $ `operator*`(ca0d58f853b0940). alpha, alpha_tmp = 0.; |
| 116 | Realcxx . . $ CubicLineSearch#Real# f1 = new_m -cxx . . $ operator-(4b6fd085471a9fb1). old_m -cxx . . $ operator-(4b6fd085471a9fb1). slope *cxx . . $ `operator*`(ca0d58f853b0940). alpha; |
| 117 | Realcxx . . $ CubicLineSearch#Real# a = 0, b = 0; |
| 118 | Realcxx . . $ CubicLineSearch#Real# c = 0, cm11 = 0, cm12 = 0, cm21 = 0, cm22 = 0; |
| 119 | Realcxx . . $ CubicLineSearch#Real# disc = 0; |
| 120 | |
| 121 | if (tst == 0) { |
| 122 | alpha_tmp = -cxx . . $ operator-(4b6fd085471a9fb1).slope *cxx . . $ `operator*`(ca0d58f853b0940). alpha2 /cxx . . $ `operator/`(42306a5d7abcb141). (f1 *cxx . . $ `operator*`(ca0d58f853b0940). 2.); |
| 123 | // tentative alpha |
| 124 | |
| 125 | tst = 1; |
| 126 | } else { |
| 127 | Realcxx . . $ CubicLineSearch#Real# alpha_prev2 = alpha_prev *cxx . . $ `operator*`(ca0d58f853b0940). alpha_prev; |
| 128 | Realcxx . . $ CubicLineSearch#Real# f2 = fprev -cxx . . $ operator-(4b6fd085471a9fb1). old_m -cxx . . $ operator-(4b6fd085471a9fb1). alpha_prev *cxx . . $ `operator*`(ca0d58f853b0940). slope; |
| 129 | |
| 130 | c = 1. /cxx . . $ `operator/`(42306a5d7abcb141). (alpha -cxx . . $ operator-(4b6fd085471a9fb1). alpha_prev); |
| 131 | cm11 = 1. /cxx . . $ `operator/`(42306a5d7abcb141). alpha2; |
| 132 | cm12 = -1. /cxx . . $ `operator/`(42306a5d7abcb141). alpha_prev2; |
| 133 | cm21 = -cxx . . $ operator-(4b6fd085471a9fb1).alpha_prev /cxx . . $ `operator/`(42306a5d7abcb141). alpha2; |
| 134 | cm22 = alpha /cxx . . $ `operator/`(42306a5d7abcb141). alpha_prev2; |
| 135 | |
| 136 | a = c *cxx . . $ `operator*`(ca0d58f853b0940). (cm11 *cxx . . $ `operator*`(ca0d58f853b0940). f1 +cxx . . $ operator+(1f62e43363bf5779). cm12 *cxx . . $ `operator*`(ca0d58f853b0940). f2); |
| 137 | b = c *cxx . . $ `operator*`(ca0d58f853b0940). (cm21 *cxx . . $ `operator*`(ca0d58f853b0940). f1 +cxx . . $ operator+(1f62e43363bf5779). cm22 *cxx . . $ `operator*`(ca0d58f853b0940). f2); |
| 138 | disc = b *cxx . . $ `operator*`(ca0d58f853b0940). b -cxx . . $ operator-(4b6fd085471a9fb1). 3. *cxx . . $ `operator*`(ca0d58f853b0940). a *cxx . . $ `operator*`(ca0d58f853b0940). slope; |
| 139 | |
| 140 | if ((Abscxx . . $ `src/Algo/defs.hpp:66:9`!(a) >cxx . . $ std/`operator>`(c2ad8cf295ccec45). FLT_MINcxx . . $ `<external>/8223903785965736313/float.h:121:9`!) && (disc >cxx . . $ std/`operator>`(c2ad8cf295ccec45). FLT_MINcxx . . $ `<external>/8223903785965736313/float.h:121:9`!)) |
| 141 | alpha_tmp = (-cxx . . $ operator-(4b6fd085471a9fb1).b +cxx . . $ operator+(1f62e43363bf5779). sqrt(disc)) /cxx . . $ `operator/`(42306a5d7abcb141). (3. *cxx . . $ `operator*`(ca0d58f853b0940). a); |
| 142 | else |
| 143 | alpha_tmp = slope *cxx . . $ `operator*`(ca0d58f853b0940). alpha2 /cxx . . $ `operator/`(42306a5d7abcb141). (2. *cxx . . $ `operator*`(ca0d58f853b0940). f1); |
| 144 | |
| 145 | if (alpha_tmp >=cxx . . $ std/`operator>=`(c2ad8cf295ccec45). .5 *cxx . . $ `operator*`(ca0d58f853b0940). alpha) alpha_tmp = .5 *cxx . . $ `operator*`(ca0d58f853b0940). alpha; |
| 146 | } |
| 147 | alpha_prev = alpha; |
| 148 | fprev = new_m; |
| 149 | |
| 150 | if (alpha_tmp <cxx . . $ std/`operator<`(c2ad8cf295ccec45). .1 *cxx . . $ `operator*`(ca0d58f853b0940). alpha) |
| 151 | alpha *= .1; |
| 152 | else |
| 153 | alpha = alpha_tmp; |
| 154 | |
| 155 | new_solution = this->update(current_solution, 1, alpha, p); |
| 156 | new_m = this->nrj->getVal(new_solution); |
| 157 | this->iterNum++; |
| 158 | } |
| 159 | if (this->iterNum ==cxx . . $ std/`operator==`(bf4c77cd91c4b201). this->iterMax) { |
| 160 | cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Alpha underflowed! \n"; |
| 161 | cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ `operator<<`(2587130c0179f881). this->iterMax; |
| 162 | } |
| 163 | |
| 164 | this->value = new_m; |
| 165 | this->appendSearchNumber( ); |
| 166 | return (new_solution); // # of iterations |
| 167 | } |
| 168 | |
| 169 | #endif |