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

improved log output for NLP scaling #709

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions src/Optimization/hiopNlpFormulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ bool hiopNlpFormulation::apply_scaling(hiopVector& c, hiopVector& d, hiopVector&
{
// check if we need to do scaling
if("none" == options->GetString("scaling_type")) {
log->printf(hovScalars, "NLP scaling is disabled.\n");
return false;
}

Expand All @@ -686,8 +687,13 @@ bool hiopNlpFormulation::apply_scaling(hiopVector& c, hiopVector& d, hiopVector&
con_grad_target = max_con_grad;
}

if(gradf.infnorm() < obj_grad_target && Jac_c.max_abs_value() < con_grad_target &&
Jac_d.max_abs_value() < con_grad_target) {
const auto gmax = gradf.infnorm();
const auto Jcmax = Jac_c.max_abs_value();
const auto Jdmax = Jac_d.max_abs_value();
if(gmax < obj_grad_target && Jcmax < con_grad_target && Jdmax < con_grad_target) {
log->printf(hovScalars, "No NLP scaling is performed:\n");
log->printf(hovScalars, "\tgrad target %12.5e NLP inf grad %12.5e\n", obj_grad_target, gmax);
log->printf(hovScalars, "\tJac target %12.5e NLP inf/max Jc %12.5e Jd %12.5e\n/", con_grad_target, Jcmax, Jdmax);
return false;
}

Expand Down
38 changes: 33 additions & 5 deletions src/Optimization/hiopNlpTransforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,21 +448,35 @@ hiopNLPObjGradScaling::hiopNLPObjGradScaling(hiopNlpFormulation* nlp,
const double max_obj_grad = nlp_->options->GetNumeric("scaling_max_obj_grad");
const double max_con_grad = nlp_->options->GetNumeric("scaling_max_con_grad");

std::stringstream ss_obj;

const double gradf_infnorm = gradf.infnorm();
scale_factor_obj = 1.;
if(max_obj_grad == 0.) {
if(gradf_infnorm > max_grad) {
scale_factor_obj = max_grad / gradf.infnorm();
ss_obj << "NLPObjGradScaling: NLP objective scaling due to option scaling_max_grad="
<< max_grad << " ";
}
} else {
if(gradf_infnorm > 0.) {
ss_obj << "NLPObjGradScaling: NLP objective scaling due to option scaling_max_obj_grad="
<< max_obj_grad << " ";
scale_factor_obj = max_obj_grad / gradf.infnorm();
}
}
if(min_grad > 0.0 && scale_factor_obj < min_grad) {
scale_factor_obj = min_grad;
ss_obj << "NLPObjGradScaling: NLP objective scaling overwritten by option scaling_min_grad="
<< min_grad << std::endl;
}

if(ss_obj.str().size()>0) {
nlp->log->printf(hovScalars, "%s: scale factor obj %12.5e.\n", ss_obj.str().c_str(), scale_factor_obj);
} else {
nlp->log->printf(hovScalars,
"NLPObjGradScaling: No NLP objective scaling performed due to combination of options.\n");
}

scale_factor_c = c.new_copy();
scale_factor_d = d.new_copy();
scale_factor_cd = LinearAlgebraFactory::create_vector(nlp_->options->GetString("mem_space"), n_eq + n_ineq);
Expand All @@ -478,14 +492,16 @@ hiopNLPObjGradScaling::hiopNLPObjGradScaling(hiopNlpFormulation* nlp,
scale_factor_d->invert();

scale_factor_cd->copy_from_two_vec_w_pattern(*scale_factor_c, cons_eq_mapping, *scale_factor_d, cons_ineq_mapping);

std::stringstream ss_cons;
Jac_c.row_max_abs_value(*scale_factor_c);
Jac_d.row_max_abs_value(*scale_factor_d);
if(max_con_grad == 0.) {
if(scale_factor_c->infnorm() > max_grad) {
scale_factor_c->scale(1. / max_grad);
scale_factor_c->component_max(1.0);
scale_factor_c->invert();
ss_cons << "NLPObjGradScaling: NLP constraints (c) scaling due to option scaling_max_grad="
<< max_grad << std::endl;
} else {
scale_factor_c->setToConstant(1.0);
}
Expand All @@ -494,24 +510,36 @@ hiopNLPObjGradScaling::hiopNLPObjGradScaling(hiopNlpFormulation* nlp,
scale_factor_d->scale(1. / max_grad);
scale_factor_d->component_max(1.0);
scale_factor_d->invert();
ss_cons << "NLPObjGradScaling: NLP constraints (d) scaling due to option scaling_max_grad="
<< max_grad << std::endl;
} else {
scale_factor_d->setToConstant(1.0);
}
} else {
scale_factor_c->setToConstant(max_con_grad / scale_factor_c->infnorm());
scale_factor_d->setToConstant(max_con_grad / scale_factor_d->infnorm());
ss_cons << "NLPObjGradScaling: NLP constraints (c&d) scaling due to option scaling_max_con_grad="
<< "max_con_grad" << std::endl;
}
if(min_grad > 0.0) {
scale_factor_c->component_max(min_grad);
scale_factor_d->component_max(min_grad);
ss_cons << "NLPObjGradScaling: scaling for some constraints may have changed "
<< "due to option scaling_min_grad option=" << min_grad;
}
if(ss_cons.str().size()>0) {
nlp->log->printf(hovScalars, "%s\n", ss_cons.str().c_str());
} else {
nlp->log->printf(hovScalars,
"NLPObjGradScaling: No NLP constraints scaling performed due to combination of options.\n");
}
}

hiopNLPObjGradScaling::~hiopNLPObjGradScaling()
{
if(scale_factor_c) delete scale_factor_c;
if(scale_factor_d) delete scale_factor_d;
if(scale_factor_cd) delete scale_factor_cd;
delete scale_factor_c;
delete scale_factor_d;
delete scale_factor_cd;
}

} // namespace hiop
5 changes: 2 additions & 3 deletions src/Utils/hiopOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,8 @@ void hiopOptionsNLP::register_options()
1e-8,
0.0,
1e+20,
"a positive value for this option will be used as a lower bound for (and will overwrite) "
"the scaling factors computed as instructed by options scaling_max_grad, scaling_max_obj_grad and "
"scaling_max_con_grad.");
"Any scaling factors (computed due to any of the scaling_max_grad, scaling_max_obj_grad, "
"and scaling_max_con_grad options) smaller than this option will be set to it.");
}

// outer iterative refinement
Expand Down