23 #include <boost/algorithm/string.hpp> 29 #include "libmesh/parallel.h" 42 const std::vector<Real> &x,
44 const std::vector<Real> &fval,
45 bool if_write_to_optim_file) {
47 libmesh_assert_equal_to(x.size(),
_n_vars);
52 <<
" *************************** " << std::endl
53 <<
" *** Optimization Output *** " << std::endl
54 <<
" *************************** " << std::endl
56 <<
"Iter: " << std::setw(10) << iter << std::endl
57 <<
"Nvars: " << std::setw(10) << x.size() << std::endl
58 <<
"Ncons-Equality: " << std::setw(10) <<
_n_eq << std::endl
59 <<
"Ncons-Inquality: " << std::setw(10) <<
_n_ineq << std::endl
61 <<
"Obj = " << std::setw(20) << obj << std::endl
63 <<
"Vars: " << std::endl;
65 for (
unsigned int i=0; i<
_n_vars; i++)
67 <<
"x [ " << std::setw(10) << i <<
" ] = " 68 << std::setw(20) << x[i] << std::endl;
71 libMesh::out << std::endl
72 <<
"Equality Constraints: " << std::endl;
74 for (
unsigned int i=0; i<
_n_eq; i++)
76 <<
"feq [ " << std::setw(10) << i <<
" ] = " 77 << std::setw(20) << fval[i] << std::endl;
81 libMesh::out << std::endl
82 <<
"Inequality Constraints: " << std::endl;
90 for (
unsigned int i=0; i<
_n_ineq; i++) {
92 <<
"fineq [ " << std::setw(10) << i <<
" ] = " 93 << std::setw(20) << fval[i+
_n_eq];
96 libMesh::out <<
" ***";
100 libMesh::out <<
" +++";
102 libMesh::out << std::endl;
104 if (max_constr < fval[i+
_n_eq]) {
106 max_constr = fval[i+
_n_eq];
110 libMesh::out << std::endl
111 << std::setw(35) <<
" N Active Constraints: " 112 << std::setw(20) << n_active << std::endl
113 << std::setw(35) <<
" N Violated Constraints: " 114 << std::setw(20) << n_violated << std::endl
115 << std::setw(35) <<
" Most critical constraint: " 116 << std::setw(20) << max_constr << std::endl;
119 libMesh::out << std::endl
120 <<
" *************************** " << std::endl;
124 if (!if_write_to_optim_file ||
134 << std::setw(10) <<
"n_dv" << std::setw(10) << _n_vars << std::endl;
136 << std::setw(10) <<
"n_eq" << std::setw(10) <<
_n_eq << std::endl;
138 << std::setw(10) <<
"n_ineq" << std::setw(10) <<
_n_ineq << std::endl;
140 *
_output << std::setw(10) <<
"Iter";
141 for (
unsigned int i=0; i < x.size(); i++) {
142 std::stringstream x; x <<
"x_" << i;
143 *
_output << std::setw(20) << x.str();
145 *
_output << std::setw(20) <<
"Obj";
146 for (
unsigned int i=0; i<fval.size(); i++) {
147 std::stringstream f; f <<
"f_" << i;
148 *
_output << std::setw(20) << f.str();
153 *
_output << std::setw(10) << iter;
154 for (
unsigned int i=0; i < x.size(); i++)
155 *
_output << std::setw(20) << x[i];
156 *
_output << std::setw(20) << obj;
157 for (
unsigned int i=0; i < fval.size(); i++)
158 *
_output << std::setw(20) << fval[i];
167 const unsigned int iter,
168 std::vector<Real> &x) {
170 struct stat stat_info;
171 int stat_result = stat(nm.c_str(), &stat_info);
173 if (stat_result != 0)
174 libmesh_error_msg(
"File does not exist: " + nm);
176 if (!std::ifstream(nm))
177 libmesh_error_msg(
"File missing: " + nm);
180 input.open(nm, std::ofstream::in);
191 std::vector<std::string> results;
194 std::getline(input, line);
196 boost::split(results, line, boost::is_any_of(
" \t"), boost::token_compress_on);
197 libmesh_assert_equal_to(results[0],
"n_dv");
198 ndv = stod(results[1]);
199 libmesh_assert_equal_to( ndv, x.size());
203 std::getline(input, line);
205 boost::split(results, line, boost::is_any_of(
" \t"), boost::token_compress_on);
206 libmesh_assert_equal_to(results[0],
"n_eq");
207 neq = stod(results[1]);
208 libmesh_assert_equal_to( neq,
_n_eq);
212 std::getline(input, line);
214 boost::split(results, line, boost::is_any_of(
" \t"), boost::token_compress_on);
215 libmesh_assert_equal_to(results[0],
"n_ineq");
216 nineq = stod(results[1]);
221 while (!input.eof() && it_num < iter+1) {
222 std::getline(input, line);
227 std::getline(input, line);
229 boost::split(results, line, boost::is_any_of(
" \t"), boost::token_compress_on);
231 libmesh_assert_greater(results.size(), ndv+1);
233 it_num = stoi(results[0]);
234 libmesh_assert_equal_to(it_num, iter);
237 for (
unsigned int i=0; i<ndv; i++)
238 x[i] = stod(results[i+1]);
255 eval_obj_grad =
true;
273 std::fill( dvars_fd.begin(), dvars_fd.end(), 0.);
274 std::fill( obj_grad.begin(), obj_grad.end(), 0.);
275 std::fill( obj_grad_fd.begin(), obj_grad_fd.end(), 0.);
276 std::fill( fvals.begin(), fvals.end(), 0.);
277 std::fill( fvals_fd_p.begin(), fvals_fd_p.end(), 0.);
278 std::fill( fvals_fd_m.begin(), fvals_fd_m.end(), 0.);
279 std::fill( grads.begin(), grads.end(), 0.);
280 std::fill( grads_fd.begin(), grads_fd.end(), 0.);
281 std::fill( eval_grads.begin(), eval_grads.end(),
true);
295 eval_obj_grad =
false;
296 std::fill( eval_grads.begin(), eval_grads.end(),
false);
301 for (
unsigned int i=0; i<
_n_vars; i++) {
312 dvars_fd[i] += delta;
317 std::fill( fvals_fd_p.begin(), fvals_fd_p.end(), 0.);
318 std::fill( fvals_fd_m.begin(), fvals_fd_m.end(), 0.);
328 dvars_fd[i] -= 2*delta;
339 obj_grad_fd[i] = (obj_fd_p-obj_fd_m)/2./delta;
343 grads_fd[i*(
_n_eq+_n_ineq)+j] = (fvals_fd_p[j]-fvals_fd_m[j])/2./delta;
349 <<
" *** Objective function gradients: analytical vs numerical" 352 bool accurate_sens =
true;
355 << std::setw(10) <<
"DV" 356 << std::setw(30) <<
"Analytical" 357 << std::setw(30) <<
"Numerical" << std::endl;
359 for (
unsigned int i=0; i<
_n_vars; i++) {
361 << std::setw(10) << i
362 << std::setw(30) << obj_grad[i]
363 << std::setw(30) << obj_grad_fd[i];
364 if (fabs((obj_grad[i] - obj_grad_fd[i])/obj_grad[i]) > tol) {
365 libMesh::out <<
" : Mismatched sensitivity";
366 accurate_sens =
false;
368 libMesh::out << std::endl;
374 <<
" *** Constraint function gradients: analytical vs numerical" 378 << std::setw(10) <<
"DV" 379 << std::setw(30) <<
"Analytical" 380 << std::setw(30) <<
"Numerical" << std::endl;
384 libMesh::out <<
" Constraint: " << j << std::endl;
385 for (
unsigned int i=0; i<
_n_vars; i++) {
387 << std::setw(10) << i
390 if (fabs((grads[i*(
_n_eq+_n_ineq)+j] - grads_fd[i*(
_n_eq+_n_ineq)+j])/grads[i*(
_n_eq+
_n_ineq)+j]) > tol) {
391 libMesh::out <<
" : Mismatched sensitivity";
392 accurate_sens =
false;
394 libMesh::out << std::endl;
400 <<
"Verify gradients: all gradients satisfied relative tol: " << tol
401 <<
" with delta: " << delta
404 return accurate_sens;
410 const unsigned int iter1,
411 const unsigned int iter2,
427 for (
unsigned int i=0; i<=divs; i++) {
429 f = (1.*i)/(1.*divs);
430 for (
unsigned int j=0; j<
_n_vars; j++) {
431 dv[j] = (1.-f) * dv1[j] + f * dv2[j];
450 libmesh_assert(this->comm().verify(N));
451 libmesh_assert(this->comm().verify(N_EQ));
452 libmesh_assert(this->comm().verify(N_INEQ));
453 libmesh_assert(this->comm().verify(n_rel_change_iters));
460 std::vector<Real>& xmin,
461 std::vector<Real>& xmax) {
465 libmesh_assert(this->comm().verify(x));
466 libmesh_assert(this->comm().verify(xmin));
467 libmesh_assert(this->comm().verify(xmax));
476 std::vector<Real>& obj_grad,
477 std::vector<Real>& fvals,
478 std::vector<bool>& eval_grads,
479 std::vector<Real>& grads) {
483 libmesh_assert(this->comm().verify(dvars));
484 libmesh_assert(this->comm().verify(eval_obj_grad));
496 libmesh_assert(this->comm().verify(obj));
497 libmesh_assert(this->comm().verify(obj_grad));
498 libmesh_assert(this->comm().verify(fvals));
499 libmesh_assert(this->comm().verify(grads));
506 const std::vector<Real>& x,
508 const std::vector<Real>& fval,
509 bool if_write_to_optim_file) {
513 libmesh_assert(this->comm().verify(iter));
514 libmesh_assert(this->comm().verify(x));
516 this->
output(
_iter, x, obj, fval, if_write_to_optim_file);
virtual void output(unsigned int iter, const std::vector< Real > &x, Real obj, const std::vector< Real > &fval, bool if_write_to_optim_file)
outputs the the current iterate to libMesh::out, and to the output file if it was set for this rank...
unsigned int n_eq() const
virtual void parametric_line_study(const std::string &nm, const unsigned int iter1, const unsigned int iter2, unsigned int divs)
computes a parametric evaluation along a line from iter1 to iter2 in file nm with divs runs between t...
void sanitize_parallel()
make sure that the analysis is setup consistently across all parallel processes
virtual void init_dvar(std::vector< Real > &x, std::vector< Real > &xmin, std::vector< Real > &xmax)=0
unsigned int n_vars() const
MAST::OptimizationInterface * _optimization_interface
unsigned int n_ineq() const
virtual void evaluate(const std::vector< Real > &dvars, Real &obj, bool eval_obj_grad, std::vector< Real > &obj_grad, std::vector< Real > &fvals, std::vector< bool > &eval_grads, std::vector< Real > &grads)=0
grads(k): Derivative of f_i(x) with respect to x_j, where k = (j-1)*M + i.
virtual void _evaluate_wrapper(const std::vector< Real > &dvars, Real &obj, bool eval_obj_grad, std::vector< Real > &obj_grad, std::vector< Real > &fvals, std::vector< bool > &eval_grads, std::vector< Real > &grads)
This serves as a wrapper around evaluate() and makes sure that the derived class's implementation is ...
void attach_optimization_interface(MAST::OptimizationInterface &opt)
Provides the basic interface API for classes the provide implement optimization problems.
void initialize_dv_from_output_file(const std::string &nm, const unsigned int iter, std::vector< Real > &x)
This reads and initializes the DV vector from a previous optimization history output file...
unsigned int n_iters_relative_change() const
virtual void _output_wrapper(unsigned int iter, const std::vector< Real > &x, Real obj, const std::vector< Real > &fval, bool if_write_to_optim_file)
This serves as a wrapper around evaluate() and makes sure that the derived class's implementation is ...
virtual void _init_dvar_wrapper(std::vector< Real > &x, std::vector< Real > &xmin, std::vector< Real > &xmax)
This serves as a wrapper around init_dvar() and makes sure that the derived class's implementation pr...
virtual bool verify_gradients(const std::vector< Real > &dvars)
verifies the gradients at the specified design point