18#include "hiopAlgFilterIPM.hpp"
35 MFEM_ASSERT(x_start != NULL && ntdofs_loc == x_start->
Size(),
36 "Starting point is not set properly.");
38 memcpy(x0, x_start->
GetData(), ntdofs_loc *
sizeof(
double));
44 double *xlow,
double *xupp,
45 NonlinearityType *type)
47 MFEM_ASSERT(n == ntdofs_glob,
"Global input mismatch.");
49 "Solution bounds are not set!");
51 const int s = ntdofs_loc *
sizeof(double);
59 double *clow,
double *cupp,
60 NonlinearityType *type)
62 MFEM_ASSERT(m == m_total,
"Global constraint size mismatch.");
68 const int s = csize *
sizeof(double);
83 bool new_x,
double &obj_value)
85 MFEM_ASSERT(n == ntdofs_glob,
"Global input mismatch.");
87 if (new_x) { constr_info_is_current =
false; }
91 problem.new_x = new_x;
98 bool new_x,
double *gradf)
100 MFEM_ASSERT(n == ntdofs_glob,
"Global input mismatch.");
102 if (new_x) { constr_info_is_current =
false; }
104 Vector x_vec(ntdofs_loc), gradf_vec(ntdofs_loc);
106 problem.new_x = new_x;
108 std::memcpy(gradf, gradf_vec.
GetData(), ntdofs_loc *
sizeof(
double));
114 const size_type &num_cons,
115 const index_type *idx_cons,
116 const double *x,
bool new_x,
119 MFEM_ASSERT(n == ntdofs_glob,
"Global input mismatch.");
120 MFEM_ASSERT(m == m_total,
"Constraint size mismatch.");
121 MFEM_ASSERT(num_cons <= m,
"num_cons should be at most m = " << m);
123 if (num_cons == 0) {
return true; }
125 if (new_x) { constr_info_is_current =
false; }
128 problem.new_x = new_x;
129 UpdateConstrValsGrads(x_vec);
131 for (
int c = 0; c < num_cons; c++)
133 MFEM_ASSERT(idx_cons[c] < m_total,
"Constraint index is out of bounds.");
134 cons[c] = constr_vals(idx_cons[c]);
142 const size_type &num_cons,
143 const index_type *idx_cons,
144 const double *x,
bool new_x,
147 MFEM_ASSERT(n == ntdofs_glob,
"Global input mismatch.");
148 MFEM_ASSERT(m == m_total,
"Constraint size mismatch.");
149 MFEM_ASSERT(num_cons <= m,
"num_cons should be at most m = " << m);
151 if (num_cons == 0) {
return true; }
153 if (new_x) { constr_info_is_current =
false; }
156 problem.new_x = new_x;
157 UpdateConstrValsGrads(x_vec);
159 for (
int c = 0; c < num_cons; c++)
161 MFEM_ASSERT(idx_cons[c] < m_total,
"Constraint index is out of bounds.");
162 for (
int j = 0; j < ntdofs_loc; j++)
165 Jac[c * ntdofs_loc + j] = constr_grads(idx_cons[c], j);
177 MPI_Comm_size(comm, &nranks);
179 size_type *sizes =
new size_type[nranks];
180 MPI_Allgather(&ntdofs_loc, 1, MPI_HIOP_SIZE_TYPE, sizes, 1,
181 MPI_HIOP_SIZE_TYPE, comm);
183 for (
int r = 1; r <= nranks; r++)
185 cols[r] = sizes[r-1] + cols[r-1];
203 const double *lambda,
206 auto hp =
dynamic_cast<const HiOpProblem *
>(&problem);
213 double logbar_obj_value,
222 const double *lambda,
231 auto hp =
dynamic_cast<const HiOpProblem *
>(&problem);
232 if (!hp) {
return true; }
233 return hp->
IterateCallback(iter, obj_value, logbar_obj_value, n, x, z_L, z_U,
234 m_ineq,
s, m, g, lambda, inf_pr, inf_du,
235 onenorm_pr_,
mu, alpha_du, alpha_pr, ls_trials);
238void HiopOptimizationProblem::UpdateConstrValsGrads(
const Vector x)
240 if (constr_info_is_current) {
return; }
251 Vector vals_C(constr_vals.
GetData(), cheight);
256 const DenseMatrix *grad_C =
dynamic_cast<const DenseMatrix *
>(&oper_C);
257 MFEM_VERIFY(grad_C,
"Hiop expects DenseMatrices as operator gradients.");
258 MFEM_ASSERT(grad_C->Height() == cheight && grad_C->Width() == ntdofs_loc,
259 "Incorrect dimensions of the C constraint gradient.");
260 for (
int i = 0; i < cheight; i++)
262 for (
int j = 0; j < ntdofs_loc; j++)
264 constr_grads(i, j) = (*grad_C)(i, j);
274 Vector vals_D(constr_vals.
GetData() + cheight, dheight);
279 const DenseMatrix *grad_D =
dynamic_cast<const DenseMatrix *
>(&oper_D);
280 MFEM_VERIFY(grad_D,
"Hiop expects DenseMatrices as operator gradients.");
281 MFEM_ASSERT(grad_D->Height() == dheight && grad_D->Width() == ntdofs_loc,
282 "Incorrect dimensions of the D constraint gradient.");
283 for (
int i = 0; i < dheight; i++)
285 for (
int j = 0; j < ntdofs_loc; j++)
287 constr_grads(i + cheight, j) = (*grad_D)(i, j);
292 constr_info_is_current =
true;
299 comm = MPI_COMM_WORLD;
300 int initialized, nret = MPI_Initialized(&initialized);
301 MFEM_ASSERT(MPI_SUCCESS == nret,
"Failure in calling MPI_Initialized!");
304 nret = MPI_Init(NULL, NULL);
305 MFEM_ASSERT(MPI_SUCCESS == nret,
"Failure in calling MPI_Init!");
337 "Unspecified OptimizationProblem that must be solved.");
341 hiop::hiopNlpDenseConstraints hiopInstance(*
hiop_problem);
343 hiopInstance.options->SetNumericValue(
"rel_tolerance",
rel_tol);
344 hiopInstance.options->SetNumericValue(
"tolerance",
abs_tol);
345 hiopInstance.options->SetIntegerValue(
"max_iter",
max_iter);
347 hiopInstance.options->SetStringValue(
"fixed_var",
"relax");
348 hiopInstance.options->SetNumericValue(
"fixed_var_tolerance", 1e-20);
349 hiopInstance.options->SetNumericValue(
"fixed_var_perturb", 1e-9);
351 hiopInstance.options->SetNumericValue(
"mu0", 1e-1);
354 hiopInstance.options->SetIntegerValue(
"verbosity_level",
print_level);
357 hiop::hiopAlgFilterIPM solver(&hiopInstance);
358 const hiop::hiopSolveStatus status = solver.run();
362 if (status != hiop::Solve_Success && status != hiop::Solve_Success_RelTol)
365 MFEM_WARNING(
"HIOP returned with a non-success status: " << status);
370 solver.getSolution(x.
GetData());
Users can inherit this class to access to HiOp-specific functionality.
virtual bool IterateCallback(int iter, double obj_value, double logbar_obj_value, int n, const double *x, const double *z_L, const double *z_U, int m_ineq, const double *s, int m, const double *g, const double *lambda, double inf_pr, double inf_du, double onenorm_pr_, double mu, double alpha_du, double alpha_pr, int ls_trials) const
See hiopInterfaceBase::iterate_callback(...).
virtual void SolutionCallback(hiop::hiopSolveStatus status, hiop::size_type n, const double *x, const double *z_L, const double *z_U, hiop::size_type m, const double *g, const double *lambda, double obj_value) const
See hiopInterfaceBase::solution_callback(...).
virtual void Mult(const Vector &xt, Vector &x) const
Solves the optimization problem with xt as initial guess.
virtual ~HiopNlpOptimizer()
virtual void SetOptimizationProblem(const OptimizationProblem &prob)
HiopOptimizationProblem * hiop_problem
Internal class - adapts the OptimizationProblem class to HiOp's interface.
virtual bool get_cons_info(const hiop::size_type &m, double *clow, double *cupp, NonlinearityType *type)
virtual bool eval_grad_f(const hiop::size_type &n, const double *x, bool new_x, double *gradf)
void setStartingPoint(const Vector &x0)
virtual bool iterate_callback(int iter, double obj_value, double logbar_obj_value, int n, const double *x, const double *z_L, const double *z_U, int m_ineq, const double *s, int m, const double *g, const double *lambda, double inf_pr, double inf_du, double onenorm_pr_, double mu, double alpha_du, double alpha_pr, int ls_trials)
virtual bool get_prob_sizes(hiop::size_type &n, hiop::size_type &m)
virtual bool eval_f(const hiop::size_type &n, const double *x, bool new_x, double &obj_value)
virtual bool get_vecdistrib_info(hiop::size_type global_n, hiop::index_type *cols)
virtual bool get_starting_point(const hiop::size_type &n, double *x0)
virtual void solution_callback(hiop::hiopSolveStatus status, hiop::size_type n, const double *x, const double *z_L, const double *z_U, hiop::size_type m, const double *g, const double *lambda, double obj_value)
virtual bool eval_cons(const hiop::size_type &n, const hiop::size_type &m, const hiop::size_type &num_cons, const hiop::index_type *idx_cons, const double *x, bool new_x, double *cons)
virtual bool eval_Jac_cons(const hiop::size_type &n, const hiop::size_type &m, const hiop::size_type &num_cons, const hiop::index_type *idx_cons, const double *x, bool new_x, double *Jac)
virtual bool get_vars_info(const hiop::size_type &n, double *xlow, double *xupp, NonlinearityType *type)
real_t abs_tol
Absolute tolerance.
real_t rel_tol
Relative tolerance.
int print_level
(DEPRECATED) Legacy print level definition, which is left for compatibility with custom iterative sol...
int max_iter
Limit for the number of iterations the solver is allowed to do.
int width
Dimension of the input / number of columns in the matrix.
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows().
int height
Dimension of the output / number of rows in the matrix.
virtual void Mult(const Vector &x, Vector &y) const =0
Operator application: y=A(x).
virtual Operator & GetGradient(const Vector &x) const
Evaluate the gradient operator at the point x. The default behavior in class Operator is to generate ...
virtual void CalcObjectiveGrad(const Vector &x, Vector &grad) const
The result grad is expected to enter with the correct size.
const Vector * GetEqualityVec() const
int GetNumConstraints() const
virtual real_t CalcObjective(const Vector &x) const =0
Objective F(x). In parallel, the result should be reduced over tasks.
const Vector * GetInequalityVec_Hi() const
const Operator * GetC() const
const Vector * GetBoundsVec_Hi() const
const Operator * GetD() const
const Vector * GetInequalityVec_Lo() const
const Vector * GetBoundsVec_Lo() const
Abstract solver for OptimizationProblems.
const OptimizationProblem * problem
int Size() const
Returns the size of the vector.
real_t * GetData() const
Return a pointer to the beginning of the Vector data.