📄 mcsrch.java
字号:
mcstep ( stx , fxm , dgxm , sty , fym , dgym , stp , fm , dgm , brackt , stmin , stmax , infoc ); // Reset the function and gradient values for f. fx[0] = fxm[0] + stx[0]*dgtest; fy[0] = fym[0] + sty[0]*dgtest; dgx[0] = dgxm[0] + dgtest; dgy[0] = dgym[0] + dgtest; } else { // Call mcstep to update the interval of uncertainty // and to compute the new step. mcstep ( stx , fx , dgx , sty , fy , dgy , stp , f , dg , brackt , stmin , stmax , infoc ); } // Force a sufficient decrease in the size of the // interval of uncertainty. if ( brackt[0] ) { if ( Math.abs ( sty[0] - stx[0] ) >= p66 * width1 ) stp[0] = stx[0] + p5 * ( sty[0] - stx[0] ); width1 = width; width = Math.abs ( sty[0] - stx[0] ); } } } /** The purpose of this function is to compute a safeguarded step for * a linesearch and to update an interval of uncertainty for * a minimizer of the function.<p> * * The parameter <code>stx</code> contains the step with the least function * value. The parameter <code>stp</code> contains the current step. It is * assumed that the derivative at <code>stx</code> is negative in the * direction of the step. If <code>brackt[0]</code> is <code>true</code> * when <code>mcstep</code> returns then a * minimizer has been bracketed in an interval of uncertainty * with endpoints <code>stx</code> and <code>sty</code>.<p> * * Variables that must be modified by <code>mcstep</code> are * implemented as 1-element arrays. * * @param stx Step at the best step obtained so far. * This variable is modified by <code>mcstep</code>. * @param fx Function value at the best step obtained so far. * This variable is modified by <code>mcstep</code>. * @param dx Derivative at the best step obtained so far. The derivative * must be negative in the direction of the step, that is, <code>dx</code> * and <code>stp-stx</code> must have opposite signs. * This variable is modified by <code>mcstep</code>. * * @param sty Step at the other endpoint of the interval of uncertainty. * This variable is modified by <code>mcstep</code>. * @param fy Function value at the other endpoint of the interval of uncertainty. * This variable is modified by <code>mcstep</code>. * @param dy Derivative at the other endpoint of the interval of * uncertainty. This variable is modified by <code>mcstep</code>. * * @param stp Step at the current step. If <code>brackt</code> is set * then on input <code>stp</code> must be between <code>stx</code> * and <code>sty</code>. On output <code>stp</code> is set to the * new step. * @param fp Function value at the current step. * @param dp Derivative at the current step. * * @param brackt Tells whether a minimizer has been bracketed. * If the minimizer has not been bracketed, then on input this * variable must be set <code>false</code>. If the minimizer has * been bracketed, then on output this variable is <code>true</code>. * * @param stpmin Lower bound for the step. * @param stpmax Upper bound for the step. * * @param info On return from <code>mcstep</code>, this is set as follows: * If <code>info</code> is 1, 2, 3, or 4, then the step has been * computed successfully. Otherwise <code>info</code> = 0, and this * indicates improper input parameters. * * Jorge J. More, David J. Thuente: original Fortran version, * as part of Minpack project. Argonne Nat'l Laboratory, June 1983. * Robert Dodier: Java translation, August 1997. */ public static void mcstep ( double[] stx , double[] fx , double[] dx , double[] sty , double[] fy , double[] dy , double[] stp , double fp , double dp , boolean[] brackt , double stpmin , double stpmax , int[] info ) { boolean bound; double gamma, p, q, r, s, sgnd, stpc, stpf, stpq, theta; info[0] = 0; if ( ( brackt[0] && ( stp[0] <= Math.min ( stx[0] , sty[0] ) || stp[0] >= Math.max ( stx[0] , sty[0] ) ) ) || dx[0] * ( stp[0] - stx[0] ) >= 0.0 || stpmax < stpmin ) return; // Determine if the derivatives have opposite sign. sgnd = dp * ( dx[0] / Math.abs ( dx[0] ) ); if ( fp > fx[0] ) { // First case. A higher function value. // The minimum is bracketed. If the cubic step is closer // to stx than the quadratic step, the cubic step is taken, // else the average of the cubic and quadratic steps is taken. info[0] = 1; bound = true; theta = 3 * ( fx[0] - fp ) / ( stp[0] - stx[0] ) + dx[0] + dp; s = max3 ( Math.abs ( theta ) , Math.abs ( dx[0] ) , Math.abs ( dp ) ); gamma = s * Math.sqrt ( sqr( theta / s ) - ( dx[0] / s ) * ( dp / s ) ); if ( stp[0] < stx[0] ) gamma = - gamma; p = ( gamma - dx[0] ) + theta; q = ( ( gamma - dx[0] ) + gamma ) + dp; r = p/q; stpc = stx[0] + r * ( stp[0] - stx[0] ); stpq = stx[0] + ( ( dx[0] / ( ( fx[0] - fp ) / ( stp[0] - stx[0] ) + dx[0] ) ) / 2 ) * ( stp[0] - stx[0] ); if ( Math.abs ( stpc - stx[0] ) < Math.abs ( stpq - stx[0] ) ) { stpf = stpc; } else { stpf = stpc + ( stpq - stpc ) / 2; } brackt[0] = true; } else if ( sgnd < 0.0 ) { // Second case. A lower function value and derivatives of // opposite sign. The minimum is bracketed. If the cubic // step is closer to stx than the quadratic (secant) step, // the cubic step is taken, else the quadratic step is taken. info[0] = 2; bound = false; theta = 3 * ( fx[0] - fp ) / ( stp[0] - stx[0] ) + dx[0] + dp; s = max3 ( Math.abs ( theta ) , Math.abs ( dx[0] ) , Math.abs ( dp ) ); gamma = s * Math.sqrt ( sqr( theta / s ) - ( dx[0] / s ) * ( dp / s ) ); if ( stp[0] > stx[0] ) gamma = - gamma; p = ( gamma - dp ) + theta; q = ( ( gamma - dp ) + gamma ) + dx[0]; r = p/q; stpc = stp[0] + r * ( stx[0] - stp[0] ); stpq = stp[0] + ( dp / ( dp - dx[0] ) ) * ( stx[0] - stp[0] ); if ( Math.abs ( stpc - stp[0] ) > Math.abs ( stpq - stp[0] ) ) { stpf = stpc; } else { stpf = stpq; } brackt[0] = true; } else if ( Math.abs ( dp ) < Math.abs ( dx[0] ) ) { // Third case. A lower function value, derivatives of the // same sign, and the magnitude of the derivative decreases. // The cubic step is only used if the cubic tends to infinity // in the direction of the step or if the minimum of the cubic // is beyond stp. Otherwise the cubic step is defined to be // either stpmin or stpmax. The quadratic (secant) step is also // computed and if the minimum is bracketed then the the step // closest to stx is taken, else the step farthest away is taken. info[0] = 3; bound = true; theta = 3 * ( fx[0] - fp ) / ( stp[0] - stx[0] ) + dx[0] + dp; s = max3 ( Math.abs ( theta ) , Math.abs ( dx[0] ) , Math.abs ( dp ) ); gamma = s * Math.sqrt ( Math.max ( 0, sqr( theta / s ) - ( dx[0] / s ) * ( dp / s ) ) ); if ( stp[0] > stx[0] ) gamma = - gamma; p = ( gamma - dp ) + theta; q = ( gamma + ( dx[0] - dp ) ) + gamma; r = p/q; if ( r < 0.0 && gamma != 0.0 ) { stpc = stp[0] + r * ( stx[0] - stp[0] ); } else if ( stp[0] > stx[0] ) { stpc = stpmax; } else { stpc = stpmin; } stpq = stp[0] + ( dp / ( dp - dx[0] ) ) * ( stx[0] - stp[0] ); if ( brackt[0] ) { if ( Math.abs ( stp[0] - stpc ) < Math.abs ( stp[0] - stpq ) ) { stpf = stpc; } else { stpf = stpq; } } else { if ( Math.abs ( stp[0] - stpc ) > Math.abs ( stp[0] - stpq ) ) { stpf = stpc; } else { stpf = stpq; } } } else { // Fourth case. A lower function value, derivatives of the // same sign, and the magnitude of the derivative does // not decrease. If the minimum is not bracketed, the step // is either stpmin or stpmax, else the cubic step is taken. info[0] = 4; bound = false; if ( brackt[0] ) { theta = 3 * ( fp - fy[0] ) / ( sty[0] - stp[0] ) + dy[0] + dp; s = max3 ( Math.abs ( theta ) , Math.abs ( dy[0] ) , Math.abs ( dp ) ); gamma = s * Math.sqrt ( sqr( theta / s ) - ( dy[0] / s ) * ( dp / s ) ); if ( stp[0] > sty[0] ) gamma = - gamma; p = ( gamma - dp ) + theta; q = ( ( gamma - dp ) + gamma ) + dy[0]; r = p/q; stpc = stp[0] + r * ( sty[0] - stp[0] ); stpf = stpc; } else if ( stp[0] > stx[0] ) { stpf = stpmax; } else { stpf = stpmin; } } // Update the interval of uncertainty. This update does not // depend on the new step or the case analysis above. if ( fp > fx[0] ) { sty[0] = stp[0]; fy[0] = fp; dy[0] = dp; } else { if ( sgnd < 0.0 ) { sty[0] = stx[0]; fy[0] = fx[0]; dy[0] = dx[0]; } stx[0] = stp[0]; fx[0] = fp; dx[0] = dp; } // Compute the new step and safeguard it. stpf = Math.min ( stpmax , stpf ); stpf = Math.max ( stpmin , stpf ); stp[0] = stpf; if ( brackt[0] && bound ) { if ( sty[0] > stx[0] ) { stp[0] = Math.min ( stx[0] + 0.66 * ( sty[0] - stx[0] ) , stp[0] ); } else { stp[0] = Math.max ( stx[0] + 0.66 * ( sty[0] - stx[0] ) , stp[0] ); } } return; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -