12 #include "../config/config.hpp" 18 #include "hiopAlgFilterIPM.hpp" 25 bool HiopOptimizationProblem::get_prob_sizes(size_type &n, size_type &m)
28 m =
problem.GetNumConstraints();
33 bool HiopOptimizationProblem::get_starting_point(
const size_type &n,
double *x0)
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));
43 bool HiopOptimizationProblem::get_vars_info(
const size_type &n,
44 double *xlow,
double *xupp,
45 NonlinearityType *type)
47 MFEM_ASSERT(n == ntdofs_glob,
"Global input mismatch.");
48 MFEM_ASSERT(
problem.GetBoundsVec_Lo() &&
problem.GetBoundsVec_Hi(),
49 "Solution bounds are not set!");
51 const int s = ntdofs_loc *
sizeof(double);
52 std::memcpy(xlow,
problem.GetBoundsVec_Lo()->GetData(),
s);
53 std::memcpy(xupp,
problem.GetBoundsVec_Hi()->GetData(),
s);
58 bool HiopOptimizationProblem::get_cons_info(
const size_type &m,
59 double *clow,
double *cupp,
60 NonlinearityType *type)
62 MFEM_ASSERT(m == m_total,
"Global constraint size mismatch.");
67 csize =
problem.GetEqualityVec()->Size();
68 const int s = csize *
sizeof(double);
69 std::memcpy(clow,
problem.GetEqualityVec()->GetData(),
s);
70 std::memcpy(cupp,
problem.GetEqualityVec()->GetData(),
s);
74 const int s =
problem.GetInequalityVec_Lo()->Size() *
sizeof(double);
75 std::memcpy(clow + csize,
problem.GetInequalityVec_Lo()->GetData(),
s);
76 std::memcpy(cupp + csize,
problem.GetInequalityVec_Hi()->GetData(),
s);
82 bool HiopOptimizationProblem::eval_f(
const size_type &n,
const double *x,
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; }
92 obj_value =
problem.CalcObjective(x_vec);
97 bool HiopOptimizationProblem::eval_grad_f(
const size_type &n,
const double *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);
107 problem.CalcObjectiveGrad(x_vec, gradf_vec);
108 std::memcpy(gradf, gradf_vec.
GetData(), ntdofs_loc *
sizeof(double));
113 bool HiopOptimizationProblem::eval_cons(
const size_type &n,
const size_type &m,
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; }
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]);
140 bool HiopOptimizationProblem::eval_Jac_cons(
const size_type &n,
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; }
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);
172 bool HiopOptimizationProblem::get_vecdistrib_info(size_type global_n,
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];
196 void HiopOptimizationProblem::solution_callback(hiop::hiopSolveStatus status,
203 const double *lambda,
211 bool HiopOptimizationProblem::iterate_callback(
int iter,
213 double logbar_obj_value,
222 const double *lambda,
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);
238 void HiopOptimizationProblem::UpdateConstrValsGrads(
const Vector x)
240 if (constr_info_is_current) {
return; }
248 cheight =
problem.GetC()->Height();
251 Vector vals_C(constr_vals.GetData(), cheight);
252 problem.GetC()->Mult(x, vals_C);
255 const Operator &oper_C =
problem.GetC()->GetGradient(x);
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);
271 const int dheight =
problem.GetD()->Height();
274 Vector vals_D(constr_vals.GetData() + cheight, dheight);
275 problem.GetD()->Mult(x, vals_D);
278 const Operator &oper_D =
problem.GetD()->GetGradient(x);
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());
374 #endif // MFEM_USE_HIOP
virtual void SetOptimizationProblem(const OptimizationProblem &prob)
void setStartingPoint(const Vector &x0)
Users can inherit this class to access to HiOp-specific functionality.
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.
int print_level
(DEPRECATED) Legacy print level definition, which is left for compatibility with custom iterative sol...
virtual ~HiopNlpOptimizer()
double * GetData() const
Return a pointer to the beginning of the Vector data.
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(...).
int max_iter
Limit for the number of iterations the solver is allowed to do.
int height
Dimension of the output / number of rows in the matrix.
Abstract solver for OptimizationProblems.
double rel_tol
Relative tolerance.
const OptimizationProblem * problem
HiopOptimizationProblem * hiop_problem
int width
Dimension of the input / number of columns in the matrix.
double abs_tol
Absolute tolerance.
Internal class - adapts the OptimizationProblem class to HiOp's interface.