Ejemplo n.º 1
0
LinearProgram::Result MinNormProblem_Sparse::Solve(Vector& x) 
{
  if(norm == 2) {
    Assert(IsValid());

    if(HasInequalities()) {  //must use quadratic program for inequalities
      FatalError("Sparse QP not done yet");
      return LinearProgram::Error;
    }
    else if(A.m != 0) {  //no inequality constraints, just equality
      FatalError("Sparse LS with equality constraints not done yet");
      return LinearProgram::Error;
    }
    else {
      LSQRInterface lsqr;
      if(!lsqr.Solve(C,d)) {
	cout<<"Error solving for least squares!!!"<<endl;
	return LinearProgram::Error;
      }
      x = lsqr.x;
      return LinearProgram::Feasible;
    }
  }
  else {
    RobustLPSolver lps;
    lps.verbose = verbose;
    LinearProgram::Result res= lps.Solve(lp);
    if(res==LinearProgram::Feasible) {
      x.resize(C.n);
      lps.xopt.getSubVectorCopy(0,x);
    }
    return res;
  }
}
Ejemplo n.º 2
0
  void TestLSQR(const SparseMatrix& A,const Vector& b)
  {
    Matrix mA;
    Vector x,temp;
    A.get(mA);
    x.resize(A.n);
    temp.resize(A.n);
    LSQRInterface lsqr;
    if(!lsqr.Solve(A,b)) {
      cout<<"Failed LSQR solve!"<<endl;
      cout<<"A="<<endl;
      cout<<MatrixPrinter(mA)<<endl;
      cout<<"b="<<VectorPrinter(b)<<endl;
      return;
    }
    x=lsqr.x;
    MatrixEquation eq(mA,b);
    if(!eq.LeastSquares_Cholesky(temp)) {
      cout<<"Failed cholesky solve!"<<endl;
      cout<<"A="<<endl;
      cout<<MatrixPrinter(mA)<<endl;
      cout<<"b="<<VectorPrinter(b)<<endl;
      return;
    }

    if(!x.isEqual(temp,1e-3)) {
      cout<<"Results differ!"<<endl;
      cout<<"LSQR: "<<VectorPrinter(x)<<endl;
      cout<<"Cholesky: "<<VectorPrinter(temp)<<endl;
    }
  }