Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidating qn solver #697

Merged
merged 8 commits into from
Nov 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
renaming and removed unnecessary member variables
cnpetra committed Oct 9, 2024
commit e281882578ba6dbd2e1e94e393540e04f211d358
56 changes: 29 additions & 27 deletions src/Optimization/KktLinSysLowRank.cpp
Original file line number Diff line number Diff line change
@@ -60,42 +60,41 @@ namespace hiop
KktLinSysLowRank::KktLinSysLowRank(hiopNlpFormulation* nlp)
: hiopKKTLinSysCompressedXYcYd(nlp)
{
nlpD = dynamic_cast<hiopNlpDenseConstraints*>(nlp_);
auto* nlpd = dynamic_cast<hiopNlpDenseConstraints*>(nlp_);

kxn_mat_ = nlpD->alloc_multivector_primal(nlpD->m());
assert("DEFAULT" == toupper(nlpD->options->GetString("mem_space")));
N = LinearAlgebraFactory::create_matrix_dense(nlpD->options->GetString("mem_space"),
nlpD->m(),
nlpD->m());
kxn_mat_ = nlpd->alloc_multivector_primal(nlpd->m());
assert("DEFAULT" == toupper(nlpd->options->GetString("mem_space")));
N_ = LinearAlgebraFactory::create_matrix_dense(nlpd->options->GetString("mem_space"),
nlpd->m(),
nlpd->m());
#ifdef HIOP_DEEPCHECKS
Nmat=N->alloc_clone();
Nmat_ = N_->alloc_clone();
#endif
k_vec1_ = dynamic_cast<hiopVector*>(nlpD->alloc_dual_vec());
k_vec1_ = nlpd->alloc_dual_vec();
}

KktLinSysLowRank::~KktLinSysLowRank()
{
if(N) delete N;
delete N_;
#ifdef HIOP_DEEPCHECKS
if(Nmat) delete Nmat;
delete Nmat_;
#endif
if(kxn_mat_) delete kxn_mat_;
if(k_vec1_) delete k_vec1_;
delete kxn_mat_;
delete k_vec1_;
}

bool KktLinSysLowRank::update(const hiopIterate* iter,
const hiopVector* grad_f,
const hiopMatrixDense* Jac_c,
const hiopMatrixDense* Jac_d,
hiopHessianLowRank* Hess)
hiopHessianLowRank* hess_low_rank)
{
nlp_->runStats.tmSolverInternal.start();

iter_=iter;
iter_ = iter;
grad_f_ = dynamic_cast<const hiopVector*>(grad_f);
Jac_c_ = Jac_c; Jac_d_ = Jac_d;
//Hess = dynamic_cast<hiopHessianInvLowRank*>(Hess_);
Hess_=HessLowRank=Hess;
Hess_ = hess_low_rank;

//compute the diagonals
//Dx=(Sxl)^{-1}Zl + (Sxu)^{-1}Zu
@@ -104,7 +103,7 @@ bool KktLinSysLowRank::update(const hiopIterate* iter,
Dx_->axdzpy_w_pattern(1.0, *iter_->zu, *iter_->sxu, nlp_->get_ixu());
nlp_->log->write("Dx in KKT", *Dx_, hovMatrices);

HessLowRank->updateLogBarrierDiagonal(*Dx_);
hess_low_rank->update_logbar_diag(*Dx_);

//Dd=(Sdl)^{-1}Vu + (Sdu)^{-1}Vu
Dd_inv_->setToZero();
@@ -161,23 +160,25 @@ bool KktLinSysLowRank::solveCompressed(hiopVector& rx,
J.copyRowsFrom(*Jac_c_de, nlp_->m_eq(), 0); //!opt
J.copyRowsFrom(*Jac_d_de, nlp_->m_ineq(), nlp_->m_eq());//!opt

auto* hess_low_rank = dynamic_cast<hiopHessianLowRank*>(Hess_);

//N = J*(Hess\J')
//Hess->symmetricTimesMat(0.0, *N, 1.0, J);
HessLowRank->sym_mat_times_inverse_times_mattrans(0.0, *N, 1.0, J);
hess_low_rank->sym_mat_times_inverse_times_mattrans(0.0, *N_, 1.0, J);

//subdiag of N += 1., Dd_inv
N->addSubDiagonal(1., nlp_->m_eq(), *Dd_inv_);
N_->addSubDiagonal(1., nlp_->m_eq(), *Dd_inv_);
#ifdef HIOP_DEEPCHECKS
assert(J.isfinite());
nlp_->log->write("solveCompressed: N is", *N, hovMatrices);
nlp_->log->write("solveCompressed: N is", *N_, hovMatrices);
nlp_->log->write("solveCompressed: rx is", rx, hovMatrices);
nlp_->log->printf(hovLinAlgScalars, "inf norm of Dd_inv is %g\n", Dd_inv_->infnorm());
N->assertSymmetry(1e-10);
N_->assertSymmetry(1e-10);
#endif

//compute the rhs of the lin sys involving N
// 1. first compute (H+Dx)^{-1} rx_tilde and store it temporarily in dx
HessLowRank->solve(rx, dx);
hess_low_rank->solve(rx, dx);
#ifdef HIOP_DEEPCHECKS
assert(rx.isfinite_local() && "Something bad happened: nan or inf value");
assert(dx.isfinite_local() && "Something bad happened: nan or inf value");
@@ -193,14 +194,14 @@ bool KktLinSysLowRank::solveCompressed(hiopVector& rx,
#ifdef HIOP_DEEPCHECKS
nlp_->log->write("solveCompressed: dx sol is", dx, hovMatrices);
nlp_->log->write("solveCompressed: rhs for N is", rhs, hovMatrices);
Nmat->copyFrom(*N);
Nmat_->copyFrom(*N_);
hiopVector* r=rhs.new_copy(); //save the rhs to check the norm of the residual
#endif

//
//solve N * dyc_dyd = rhs
//
int ierr = solveWithRefin(*N,rhs);
int ierr = solveWithRefin(*N_,rhs);
//int ierr = solve(*N,rhs);

hiopVector& dyc_dyd= rhs;
@@ -211,7 +212,7 @@ bool KktLinSysLowRank::solveCompressed(hiopVector& rx,
//first rx = -(Jc^T*dyc+Jd^T*dyd - rx)
J.transTimesVec(1.0, rx, -1.0, dyc_dyd);
//then dx = (H+Dx)^{-1} rx
HessLowRank->solve(rx, dx);
hess_low_rank->solve(rx, dx);

#ifdef HIOP_DEEPCHECKS
//some outputing
@@ -459,12 +460,13 @@ double KktLinSysLowRank::errorCompressedLinsys(const hiopVector& rx,
const hiopVector& dyd)
{
nlp_->log->printf(hovLinAlgScalars, "KktLinSysLowRank::errorCompressedLinsys residuals norm:\n");

auto* hess_low_rank = dynamic_cast<hiopHessianLowRank*>(Hess_);

double derr = -1.;
double aux;
hiopVector* RX = rx.new_copy();
//RX=rx-H*dx-J'c*dyc-J'*dyd
HessLowRank->timesVec(1.0, *RX, -1.0, dx);
hess_low_rank->timesVec(1.0, *RX, -1.0, dx);
//RX->axzpy(-1.0,*Dx,dx);
Jac_c_->transTimesVec(1.0, *RX, -1.0, dyc);
Jac_d_->transTimesVec(1.0, *RX, -1.0, dyd);
15 changes: 5 additions & 10 deletions src/Optimization/KktLinSysLowRank.hpp
Original file line number Diff line number Diff line change
@@ -148,23 +148,18 @@ class KktLinSysLowRank : public hiopKKTLinSysCompressedXYcYd
/// @brief perform y=beta*y+alpha*H*x without the log barrier term from H
void HessianTimesVec_noLogBarrierTerm(double beta, hiopVector& y, double alpha, const hiopVector& x)
{
hiopHessianLowRank* HessLowR = dynamic_cast<hiopHessianLowRank*>(Hess_);
assert(nullptr != HessLowR);
if(HessLowR) {
HessLowR->times_vec_no_logbar_term(beta, y, alpha, x);
}
hiopHessianLowRank* hesslowrank = dynamic_cast<hiopHessianLowRank*>(Hess_);
assert(nullptr != hesslowrank);
hesslowrank->times_vec_no_logbar_term(beta, y, alpha, x);
}
#endif

private:
hiopNlpDenseConstraints* nlpD;
hiopHessianLowRank* HessLowRank;

/// The kxk reduced matrix
hiopMatrixDense* N;
hiopMatrixDense* N_;
#ifdef HIOP_DEEPCHECKS
/// A copy of the above to compute the residual
hiopMatrixDense* Nmat;
hiopMatrixDense* Nmat_;
#endif
//internal buffers: k is usually 2 x quasi-Newton memory; n is the size of primal variable vector
hiopMatrixDense* kxn_mat_;
2 changes: 1 addition & 1 deletion src/Optimization/hiopHessianLowRank.cpp
Original file line number Diff line number Diff line change
@@ -234,7 +234,7 @@ void hiopHessianLowRank::alloc_for_limited_mem(const size_type& mem_length)
D_ = LinearAlgebraFactory::create_vector("DEFAULT", mem_length);
}

bool hiopHessianLowRank::updateLogBarrierDiagonal(const hiopVector& Dx)
bool hiopHessianLowRank::update_logbar_diag(const hiopVector& Dx)
{
DhInv_->setToConstant(sigma_);
DhInv_->axpy(1.0,Dx);
2 changes: 1 addition & 1 deletion src/Optimization/hiopHessianLowRank.hpp
Original file line number Diff line number Diff line change
@@ -101,7 +101,7 @@ class hiopHessianLowRank : public hiopMatrix
const hiopMatrix& Jac_d_curr);

/* updates the logBar diagonal term from the representation */
virtual bool updateLogBarrierDiagonal(const hiopVector& Dx);
virtual bool update_logbar_diag(const hiopVector& Dx);

/* solves this*x=res */
virtual void solve(const hiopVector& rhs, hiopVector& x);