Example #1
0
inline void pdxxxresid1(ClpPdco *model, const int nlow, const int nupp, const int nfix,
                        int *low, int *upp, int *fix,
                        CoinDenseVector <double> &b, double *bl, double *bu, double d1, double d2,
                        CoinDenseVector <double> &grad, CoinDenseVector <double> &rL,
                        CoinDenseVector <double> &rU, CoinDenseVector <double> &x,
                        CoinDenseVector <double> &x1, CoinDenseVector <double> &x2,
                        CoinDenseVector <double> &y,  CoinDenseVector <double> &z1,
                        CoinDenseVector <double> &z2, CoinDenseVector <double> &r1,
                        CoinDenseVector <double> &r2, double *Pinf, double *Dinf)
{

// Form residuals for the primal and dual equations.
// rL, rU are output, but we input them as full vectors
// initialized (permanently) with any relevant zeros.

// Get some element pointers for efficiency
     double *x_elts  = x.getElements();
     double *r2_elts = r2.getElements();

     for (int k = 0; k < nfix; k++)
          x_elts[fix[k]]  = 0;

     r1.clear();
     r2.clear();
     model->matVecMult( 1, r1, x );
     model->matVecMult( 2, r2, y );
     for (int k = 0; k < nfix; k++)
          r2_elts[fix[k]]  = 0;


     r1      = b    - r1 - d2 * d2 * y;
     r2      = grad - r2 - z1;              // grad includes d1*d1*x
     if (nupp > 0)
          r2    = r2 + z2;

     for (int k = 0; k < nlow; k++)
          rL[low[k]] = bl[low[k]] - x[low[k]] + x1[low[k]];
     for (int k = 0; k < nupp; k++)
          rU[upp[k]] = - bu[upp[k]] + x[upp[k]] + x2[upp[k]];

     double normL = 0.0;
     double normU = 0.0;
     for (int k = 0; k < nlow; k++)
          if (rL[low[k]] > normL) normL = rL[low[k]];
     for (int k = 0; k < nupp; k++)
          if (rU[upp[k]] > normU) normU = rU[upp[k]];

     *Pinf    = CoinMax(normL, normU);
     *Pinf    = CoinMax( r1.infNorm() , *Pinf );
     *Dinf    = r2.infNorm();
     *Pinf    = CoinMax( *Pinf, 1e-99 );
     *Dinf    = CoinMax( *Dinf, 1e-99 );
}
Example #2
0
void ClpLsqr::do_lsqr(CoinDenseVector< double > &b,
  double damp, double atol, double btol, double conlim, int itnlim,
  bool show, Info info, CoinDenseVector< double > &x, int *istop,
  int *itn, Outfo *outfo, bool precon, CoinDenseVector< double > &Pr)
{

  /**
     Special version of LSQR for use with pdco.m.
     It continues with a reduced atol if a pdco-specific test isn't
     satisfied with the input atol.
     */

  //     Initialize.
  static char term_msg[8][80] = {
    "The exact solution is x = 0",
    "The residual Ax - b is small enough, given ATOL and BTOL",
    "The least squares error is small enough, given ATOL",
    "The estimated condition number has exceeded CONLIM",
    "The residual Ax - b is small enough, given machine precision",
    "The least squares error is small enough, given machine precision",
    "The estimated condition number has exceeded machine precision",
    "The iteration limit has been reached"
  };

  //  printf("***************** Entering LSQR *************\n");
  assert(model_);

  char str1[100], str2[100], str3[100], str4[100], head1[100], head2[100];

  int n = ncols_; // set  m,n from lsqr object

  *itn = 0;
  *istop = 0;
  double ctol = 0;
  if (conlim > 0)
    ctol = 1 / conlim;

  double anorm = 0;
  double acond = 0;
  double ddnorm = 0;
  double xnorm = 0;
  double xxnorm = 0;
  double z = 0;
  double cs2 = -1;
  double sn2 = 0;

  // Set up the first vectors u and v for the bidiagonalization.
  // These satisfy  beta*u = b,  alfa*v = A'u.

  CoinDenseVector< double > u(b);
  CoinDenseVector< double > v(n, 0.0);
  x.clear();
  double alfa = 0;
  double beta = u.twoNorm();
  if (beta > 0) {
    u = (1 / beta) * u;
    matVecMult(2, v, u);
    if (precon)
      v = v * Pr;
    alfa = v.twoNorm();
  }
  if (alfa > 0) {
    v.scale(1 / alfa);
  }
  CoinDenseVector< double > w(v);

  double arnorm = alfa * beta;
  if (arnorm == 0) {
    printf("  %s\n\n", term_msg[0]);
    return;
  }

  double rhobar = alfa;
  double phibar = beta;
  double bnorm = beta;
  double rnorm = beta;
  sprintf(head1, "   Itn      x(1)      Function");
  sprintf(head2, " Compatible   LS      Norm A   Cond A");

  if (show) {
    printf(" %s%s\n", head1, head2);
    double test1 = 1;
    double test2 = alfa / beta;
    sprintf(str1, "%6d %12.5e %10.3e", *itn, x[0], rnorm);
    sprintf(str2, "  %8.1e  %8.1e", test1, test2);
    printf("%s%s\n", str1, str2);
  }

  //----------------------------------------------------------------
  // Main iteration loop.
  //----------------------------------------------------------------
  while (*itn < itnlim) {
    *itn += 1;
    // Perform the next step of the bidiagonalization to obtain the
    // next beta, u, alfa, v.  These satisfy the relations
    // beta*u  =  a*v   -  alfa*u,
    // alfa*v  =  A'*u  -  beta*v.

    u.scale((-alfa));
    if (precon) {
      CoinDenseVector< double > pv(v * Pr);
      matVecMult(1, u, pv);
    } else {
      matVecMult(1, u, v);
    }
    beta = u.twoNorm();
    if (beta > 0) {
      u.scale((1 / beta));
      anorm = sqrt(anorm * anorm + alfa * alfa + beta * beta + damp * damp);
      v.scale((-beta));
      CoinDenseVector< double > vv(n);
      vv.clear();
      matVecMult(2, vv, u);
      if (precon)
        vv = vv * Pr;
      v = v + vv;
      alfa = v.twoNorm();
      if (alfa > 0)
        v.scale((1 / alfa));
    }

    // Use a plane rotation to eliminate the damping parameter.
    // This alters the diagonal (rhobar) of the lower-bidiagonal matrix.

    double rhobar1 = sqrt(rhobar * rhobar + damp * damp);
    double cs1 = rhobar / rhobar1;
    double sn1 = damp / rhobar1;
    double psi = sn1 * phibar;
    phibar *= cs1;

    // Use a plane rotation to eliminate the subdiagonal element (beta)
    // of the lower-bidiagonal matrix, giving an upper-bidiagonal matrix.

    double rho = sqrt(rhobar1 * rhobar1 + beta * beta);
    double cs = rhobar1 / rho;
    double sn = beta / rho;
    double theta = sn * alfa;
    rhobar = -cs * alfa;
    double phi = cs * phibar;
    phibar = sn * phibar;
    double tau = sn * phi;

    // Update x and w.

    double t1 = phi / rho;
    double t2 = -theta / rho;
    //    dk           =   ((1/rho)*w);

    double w_norm = w.twoNorm();
    x = x + t1 * w;
    w = v + t2 * w;
    ddnorm = ddnorm + (w_norm / rho) * (w_norm / rho);
    // if wantvar, var = var  +  dk.*dk; end

    // Use a plane rotation on the right to eliminate the
    // super-diagonal element (theta) of the upper-bidiagonal matrix.
    // Then use the result to estimate  norm(x).

    double delta = sn2 * rho;
    double gambar = -cs2 * rho;
    double rhs = phi - delta * z;
    double zbar = rhs / gambar;
    xnorm = sqrt(xxnorm + zbar * zbar);
    double gamma = sqrt(gambar * gambar + theta * theta);
    cs2 = gambar / gamma;
    sn2 = theta / gamma;
    z = rhs / gamma;
    xxnorm = xxnorm + z * z;

    // Test for convergence.
    // First, estimate the condition of the matrix  Abar,
    // and the norms of  rbar  and  Abar'rbar.

    acond = anorm * sqrt(ddnorm);
    double res1 = phibar * phibar;
    double res2 = res1 + psi * psi;
    rnorm = sqrt(res1 + res2);
    arnorm = alfa * fabs(tau);

    // Now use these norms to estimate certain other quantities,
    // some of which will be small near a solution.

    double test1 = rnorm / bnorm;
    double test2 = arnorm / (anorm * rnorm);
    double test3 = 1 / acond;
    t1 = test1 / (1 + anorm * xnorm / bnorm);
    double rtol = btol + atol * anorm * xnorm / bnorm;

    // The following tests guard against extremely small values of
    // atol, btol  or  ctol.  (The user may have set any or all of
    // the parameters  atol, btol, conlim  to 0.)
    // The effect is equivalent to the normal tests using
    // atol = eps,  btol = eps,  conlim = 1/eps.

    if (*itn >= itnlim)
      *istop = 7;
    if (1 + test3 <= 1)
      *istop = 6;
    if (1 + test2 <= 1)
      *istop = 5;
    if (1 + t1 <= 1)
      *istop = 4;

    // Allow for tolerances set by the user.

    if (test3 <= ctol)
      *istop = 3;
    if (test2 <= atol)
      *istop = 2;
    if (test1 <= rtol)
      *istop = 1;

    //-------------------------------------------------------------------
    // SPECIAL TEST THAT DEPENDS ON pdco.m.
    // Aname in pdco   is  iw in lsqr.
    // dy              is  x
    // Other stuff     is in info.

    // We allow for diagonal preconditioning in pdDDD3.
    //-------------------------------------------------------------------
    if (*istop > 0) {
      double r3new = arnorm;
      double r3ratio = r3new / info.r3norm;
      double atolold = atol;
      double atolnew = atol;

      if (atol > info.atolmin) {
        if (r3ratio <= 0.1) { // dy seems good
          // Relax
        } else if (r3ratio <= 0.5) { // Accept dy but make next one more accurate.
          atolnew = atolnew * 0.1;
        } else { // Recompute dy more accurately
          if (show) {
            printf("\n                                ");
            printf("                                \n");
            printf(" %5.1f%7d%7.3f", log10(atolold), *itn, r3ratio);
          }
          atol = atol * 0.1;
          atolnew = atol;
          *istop = 0;
        }

        outfo->atolold = atolold;
        outfo->atolnew = atolnew;
        outfo->r3ratio = r3ratio;
      }

      //-------------------------------------------------------------------
      // See if it is time to print something.
      //-------------------------------------------------------------------
      int prnt = 0;
      if (n <= 40)
        prnt = 1;
      if (*itn <= 10)
        prnt = 1;
      if (*itn >= itnlim - 10)
        prnt = 1;
      if (*itn % 10 == 0)
        prnt = 1;
      if (test3 <= 2 * ctol)
        prnt = 1;
      if (test2 <= 10 * atol)
        prnt = 1;
      if (test1 <= 10 * rtol)
        prnt = 1;
      if (*istop != 0)
        prnt = 1;

      if (prnt == 1) {
        if (show) {
          sprintf(str1, "   %6d %12.5e %10.3e", *itn, x[0], rnorm);
          sprintf(str2, "  %8.1e %8.1e", test1, test2);
          sprintf(str3, " %8.1e %8.1e", anorm, acond);
          printf("%s%s%s\n", str1, str2, str3);
        }
      }
      if (*istop > 0)
        break;
    }
  }
  // End of iteration loop.
  // Print the stopping condition.

  if (show) {
    printf("\n LSQR finished\n");
    //    disp(msg(istop+1,:))
    //    disp(' ')
    printf("%s\n", term_msg[*istop]);
    sprintf(str1, "istop  =%8d     itn    =%8d", *istop, *itn);
    sprintf(str2, "anorm  =%8.1e   acond  =%8.1e", anorm, acond);
    sprintf(str3, "rnorm  =%8.1e   arnorm =%8.1e", rnorm, arnorm);
    sprintf(str4, "bnorm  =%8.1e   xnorm  =%8.1e", bnorm, xnorm);
    printf("%s %s\n", str1, str2);
    printf("%s %s\n", str3, str4);
  }
}