⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 levenbergmarquardtestimator.java

📁 Apache的common math数学软件包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      for (double ratio = 0; ratio < 1.0e-4;) {        // save the state        for (int j = 0; j < solvedCols; ++j) {          int pj = permutation[j];          oldX[pj] = parameters[pj].getEstimate();        }        double previousCost = cost;        double[] tmpVec = residuals;        residuals = oldRes;        oldRes    = tmpVec;                // determine the Levenberg-Marquardt parameter        determineLMParameter(oldRes, delta, diag, work1, work2, work3);        // compute the new point and the norm of the evolution direction        double lmNorm = 0;        for (int j = 0; j < solvedCols; ++j) {          int pj = permutation[j];          lmDir[pj] = -lmDir[pj];          parameters[pj].setEstimate(oldX[pj] + lmDir[pj]);          double s = diag[pj] * lmDir[pj];          lmNorm  += s * s;        }        lmNorm = Math.sqrt(lmNorm);        // on the first iteration, adjust the initial step bound.        if (firstIteration) {          delta = Math.min(delta, lmNorm);        }        // evaluate the function at x + p and calculate its norm        updateResidualsAndCost();        // compute the scaled actual reduction        double actRed = -1.0;        if (0.1 * cost < previousCost) {          double r = cost / previousCost;          actRed = 1.0 - r * r;        }        // compute the scaled predicted reduction        // and the scaled directional derivative        for (int j = 0; j < solvedCols; ++j) {          int pj = permutation[j];          double dirJ = lmDir[pj];          work1[j] = 0;          for (int i = 0, index = pj; i <= j; ++i, index += cols) {            work1[i] += jacobian[index] * dirJ;          }        }        double coeff1 = 0;        for (int j = 0; j < solvedCols; ++j) {         coeff1 += work1[j] * work1[j];        }        double pc2 = previousCost * previousCost;        coeff1 = coeff1 / pc2;        double coeff2 = lmPar * lmNorm * lmNorm / pc2;        double preRed = coeff1 + 2 * coeff2;        double dirDer = -(coeff1 + coeff2);        // ratio of the actual to the predicted reduction        ratio = (preRed == 0) ? 0 : (actRed / preRed);        // update the step bound        if (ratio <= 0.25) {          double tmp =            (actRed < 0) ? (0.5 * dirDer / (dirDer + 0.5 * actRed)) : 0.5;          if ((0.1 * cost >= previousCost) || (tmp < 0.1)) {            tmp = 0.1;          }          delta = tmp * Math.min(delta, 10.0 * lmNorm);          lmPar /= tmp;        } else if ((lmPar == 0) || (ratio >= 0.75)) {          delta = 2 * lmNorm;          lmPar *= 0.5;        }        // test for successful iteration.        if (ratio >= 1.0e-4) {          // successful iteration, update the norm          firstIteration = false;          xNorm = 0;          for (int k = 0; k < cols; ++k) {            double xK = diag[k] * parameters[k].getEstimate();            xNorm    += xK * xK;          }          xNorm = Math.sqrt(xNorm);        } else {          // failed iteration, reset the previous values          cost = previousCost;          for (int j = 0; j < solvedCols; ++j) {            int pj = permutation[j];            parameters[pj].setEstimate(oldX[pj]);          }          tmpVec    = residuals;          residuals = oldRes;          oldRes    = tmpVec;        }           // tests for convergence.        if (((Math.abs(actRed) <= costRelativeTolerance) &&             (preRed <= costRelativeTolerance) &&             (ratio <= 2.0)) ||             (delta <= parRelativeTolerance * xNorm)) {          return;        }        // tests for termination and stringent tolerances        // (2.2204e-16 is the machine epsilon for IEEE754)        if ((Math.abs(actRed) <= 2.2204e-16) && (preRed <= 2.2204e-16) && (ratio <= 2.0)) {          throw new EstimationException("cost relative tolerance is too small ({0})," +                                        " no further reduction in the" +                                        " sum of squares is possible",                                        new Object[] { new Double(costRelativeTolerance) });        } else if (delta <= 2.2204e-16 * xNorm) {          throw new EstimationException("parameters relative tolerance is too small" +                                        " ({0}), no further improvement in" +                                        " the approximate solution is possible",                                        new Object[] { new Double(parRelativeTolerance) });        } else if (maxCosine <= 2.2204e-16)  {          throw new EstimationException("orthogonality tolerance is too small ({0})," +                                        " solution is orthogonal to the jacobian",                                        new Object[] { new Double(orthoTolerance) });        }      }    }  }  /**    * Determine the Levenberg-Marquardt parameter.   * <p>This implementation is a translation in Java of the MINPACK   * <a href="http://www.netlib.org/minpack/lmpar.f">lmpar</a>   * routine.</p>   * <p>This method sets the lmPar and lmDir attributes.</p>   * <p>The authors of the original fortran function are:</p>   * <ul>   *   <li>Argonne National Laboratory. MINPACK project. March 1980</li>   *   <li>Burton  S. Garbow</li>   *   <li>Kenneth E. Hillstrom</li>   *   <li>Jorge   J. More</li>   * </ul>   * <p>Luc Maisonobe did the Java translation.</p>   *    * @param qy array containing qTy   * @param delta upper bound on the euclidean norm of diagR * lmDir   * @param diag diagonal matrix   * @param work1 work array   * @param work2 work array   * @param work3 work array   */  private void determineLMParameter(double[] qy, double delta, double[] diag,                                    double[] work1, double[] work2, double[] work3) {    // compute and store in x the gauss-newton direction, if the    // jacobian is rank-deficient, obtain a least squares solution    for (int j = 0; j < rank; ++j) {      lmDir[permutation[j]] = qy[j];    }    for (int j = rank; j < cols; ++j) {      lmDir[permutation[j]] = 0;    }    for (int k = rank - 1; k >= 0; --k) {      int pk = permutation[k];      double ypk = lmDir[pk] / diagR[pk];      for (int i = 0, index = pk; i < k; ++i, index += cols) {        lmDir[permutation[i]] -= ypk * jacobian[index];      }      lmDir[pk] = ypk;    }    // evaluate the function at the origin, and test    // for acceptance of the Gauss-Newton direction    double dxNorm = 0;    for (int j = 0; j < solvedCols; ++j) {      int pj = permutation[j];      double s = diag[pj] * lmDir[pj];      work1[pj] = s;      dxNorm += s * s;    }    dxNorm = Math.sqrt(dxNorm);    double fp = dxNorm - delta;    if (fp <= 0.1 * delta) {      lmPar = 0;      return;    }    // if the jacobian is not rank deficient, the Newton step provides    // a lower bound, parl, for the zero of the function,    // otherwise set this bound to zero    double sum2, parl = 0;    if (rank == solvedCols) {      for (int j = 0; j < solvedCols; ++j) {        int pj = permutation[j];        work1[pj] *= diag[pj] / dxNorm;       }      sum2 = 0;      for (int j = 0; j < solvedCols; ++j) {        int pj = permutation[j];        double sum = 0;        for (int i = 0, index = pj; i < j; ++i, index += cols) {          sum += jacobian[index] * work1[permutation[i]];        }        double s = (work1[pj] - sum) / diagR[pj];        work1[pj] = s;        sum2 += s * s;      }      parl = fp / (delta * sum2);    }    // calculate an upper bound, paru, for the zero of the function    sum2 = 0;    for (int j = 0; j < solvedCols; ++j) {      int pj = permutation[j];      double sum = 0;      for (int i = 0, index = pj; i <= j; ++i, index += cols) {        sum += jacobian[index] * qy[i];      }      sum /= diag[pj];      sum2 += sum * sum;    }    double gNorm = Math.sqrt(sum2);    double paru = gNorm / delta;    if (paru == 0) {      // 2.2251e-308 is the smallest positive real for IEE754      paru = 2.2251e-308 / Math.min(delta, 0.1);    }    // if the input par lies outside of the interval (parl,paru),    // set par to the closer endpoint    lmPar = Math.min(paru, Math.max(lmPar, parl));    if (lmPar == 0) {      lmPar = gNorm / dxNorm;    }    for (int countdown = 10; countdown >= 0; --countdown) {      // evaluate the function at the current value of lmPar      if (lmPar == 0) {        lmPar = Math.max(2.2251e-308, 0.001 * paru);      }      double sPar = Math.sqrt(lmPar);      for (int j = 0; j < solvedCols; ++j) {        int pj = permutation[j];        work1[pj] = sPar * diag[pj];      }      determineLMDirection(qy, work1, work2, work3);      dxNorm = 0;      for (int j = 0; j < solvedCols; ++j) {        int pj = permutation[j];        double s = diag[pj] * lmDir[pj];        work3[pj] = s;        dxNorm += s * s;      }      dxNorm = Math.sqrt(dxNorm);      double previousFP = fp;      fp = dxNorm - delta;      // if the function is small enough, accept the current value      // of lmPar, also test for the exceptional cases where parl is zero      if ((Math.abs(fp) <= 0.1 * delta) ||          ((parl == 0) && (fp <= previousFP) && (previousFP < 0))) {        return;      }       // compute the Newton correction      for (int j = 0; j < solvedCols; ++j) {       int pj = permutation[j];        work1[pj] = work3[pj] * diag[pj] / dxNorm;       }      for (int j = 0; j < solvedCols; ++j) {        int pj = permutation[j];        work1[pj] /= work2[j];        double tmp = work1[pj];        for (int i = j + 1; i < solvedCols; ++i) {          work1[permutation[i]] -= jacobian[i * cols + pj] * tmp;        }      }      sum2 = 0;      for (int j = 0; j < solvedCols; ++j) {        double s = work1[permutation[j]];        sum2 += s * s;      }      double correction = fp / (delta * sum2);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -