23 #include <boost/algorithm/string.hpp> 29 #include "libmesh/parallel_implementation.h" 43 const std::vector<Real> &x,
45 const std::vector<Real> &fval,
46 bool if_write_to_optim_file) {
48 libmesh_assert_equal_to(x.size(),
_n_vars);
53 <<
" *************************** " << std::endl
54 <<
" *** Optimization Output *** " << std::endl
55 <<
" *************************** " << std::endl
57 <<
"Iter: " << std::setw(10) << iter << std::endl
58 <<
"Nvars: " << std::setw(10) << x.size() << std::endl
59 <<
"Ncons-Equality: " << std::setw(10) <<
_n_eq << std::endl
60 <<
"Ncons-Inquality: " << std::setw(10) <<
_n_ineq << std::endl
62 <<
"Obj = " << std::setw(20) << obj << std::endl
64 <<
"Vars: " << std::endl;
66 for (
unsigned int i=0; i<
_n_vars; i++)
68 <<
"x [ " << std::setw(10) << i <<
" ] = " 69 << std::setw(20) << x[i] << std::endl;
72 libMesh::out << std::endl
73 <<
"Equality Constraints: " << std::endl;
75 for (
unsigned int i=0; i<
_n_eq; i++)
77 <<
"feq [ " << std::setw(10) << i <<
" ] = " 78 << std::setw(20) << fval[i] << std::endl;
82 libMesh::out << std::endl
83 <<
"Inequality Constraints: " << std::endl;
91 for (
unsigned int i=0; i<
_n_ineq; i++) {
93 <<
"fineq [ " << std::setw(10) << i <<
" ] = " 94 << std::setw(20) << fval[i+
_n_eq];
97 libMesh::out <<
" ***";
101 libMesh::out <<
" +++";
103 libMesh::out << std::endl;
105 if (max_constr < fval[i+
_n_eq]) {
107 max_constr = fval[i+
_n_eq];
111 libMesh::out << std::endl
112 << std::setw(35) <<
" N Active Constraints: " 113 << std::setw(20) << n_active << std::endl
114 << std::setw(35) <<
" N Violated Constraints: " 115 << std::setw(20) << n_violated << std::endl
116 << std::setw(35) <<
" Most critical constraint: " 117 << std::setw(20) << max_constr << std::endl;
120 libMesh::out << std::endl
121 <<
" *************************** " << std::endl;
125 if (!if_write_to_optim_file ||
135 << std::setw(10) <<
"n_dv" << std::setw(10) << _n_vars << std::endl;
137 << std::setw(10) <<
"n_eq" << std::setw(10) <<
_n_eq << std::endl;
139 << std::setw(10) <<
"n_ineq" << std::setw(10) <<
_n_ineq << std::endl;
141 *
_output << std::setw(10) <<
"Iter";
142 for (
unsigned int i=0; i < x.size(); i++) {
143 std::stringstream x; x <<
"x_" << i;
144 *
_output << std::setw(20) << x.str();
146 *
_output << std::setw(20) <<
"Obj";
147 for (
unsigned int i=0; i<fval.size(); i++) {
148 std::stringstream f; f <<
"f_" << i;
149 *
_output << std::setw(20) << f.str();
154 *
_output << std::setw(10) << iter;
155 for (
unsigned int i=0; i < x.size(); i++)
156 *
_output << std::setw(20) << x[i];
157 *
_output << std::setw(20) << obj;
158 for (
unsigned int i=0; i < fval.size(); i++)
159 *
_output << std::setw(20) << fval[i];
168 const unsigned int iter,
169 std::vector<Real> &x) {
171 struct stat stat_info;
172 int stat_result = stat(nm.c_str(), &stat_info);
174 if (stat_result != 0)
175 libmesh_error_msg(
"File does not exist: " + nm);
177 if (!std::ifstream(nm))
178 libmesh_error_msg(
"File missing: " + nm);
181 input.open(nm, std::ofstream::in);
192 std::vector<std::string> results;
195 std::getline(input, line);
197 boost::split(results, line, boost::is_any_of(
" \t"), boost::token_compress_on);
198 libmesh_assert_equal_to(results[0],
"n_dv");
199 ndv = stod(results[1]);
200 libmesh_assert_equal_to( ndv, x.size());
204 std::getline(input, line);
206 boost::split(results, line, boost::is_any_of(
" \t"), boost::token_compress_on);
207 libmesh_assert_equal_to(results[0],
"n_eq");
208 neq = stod(results[1]);
209 libmesh_assert_equal_to( neq,
_n_eq);
213 std::getline(input, line);
215 boost::split(results, line, boost::is_any_of(
" \t"), boost::token_compress_on);
216 libmesh_assert_equal_to(results[0],
"n_ineq");
217 nineq = stod(results[1]);
222 while (!input.eof() && it_num < iter+1) {
223 std::getline(input, line);
228 std::getline(input, line);
230 boost::split(results, line, boost::is_any_of(
" \t"), boost::token_compress_on);
232 libmesh_assert_greater(results.size(), ndv+1);
234 it_num = stoi(results[0]);
235 libmesh_assert_equal_to(it_num, iter);
238 for (
unsigned int i=0; i<ndv; i++)
239 x[i] = stod(results[i+1]);
256 eval_obj_grad =
true;
274 std::fill( dvars_fd.begin(), dvars_fd.end(), 0.);
275 std::fill( obj_grad.begin(), obj_grad.end(), 0.);
276 std::fill( obj_grad_fd.begin(), obj_grad_fd.end(), 0.);
277 std::fill( fvals.begin(), fvals.end(), 0.);
278 std::fill( fvals_fd_p.begin(), fvals_fd_p.end(), 0.);
279 std::fill( fvals_fd_m.begin(), fvals_fd_m.end(), 0.);
280 std::fill( grads.begin(), grads.end(), 0.);
281 std::fill( grads_fd.begin(), grads_fd.end(), 0.);
282 std::fill( eval_grads.begin(), eval_grads.end(),
true);
296 eval_obj_grad =
false;
297 std::fill( eval_grads.begin(), eval_grads.end(),
false);
302 for (
unsigned int i=0; i<
_n_vars; i++) {
313 dvars_fd[i] += delta;
318 std::fill( fvals_fd_p.begin(), fvals_fd_p.end(), 0.);
319 std::fill( fvals_fd_m.begin(), fvals_fd_m.end(), 0.);
329 dvars_fd[i] -= 2*delta;
340 obj_grad_fd[i] = (obj_fd_p-obj_fd_m)/2./delta;
344 grads_fd[i*(
_n_eq+_n_ineq)+j] = (fvals_fd_p[j]-fvals_fd_m[j])/2./delta;
350 <<
" *** Objective function gradients: analytical vs numerical" 353 bool accurate_sens =
true;
356 << std::setw(10) <<
"DV" 357 << std::setw(30) <<
"Analytical" 358 << std::setw(30) <<
"Numerical" << std::endl;
360 for (
unsigned int i=0; i<
_n_vars; i++) {
362 << std::setw(10) << i
363 << std::setw(30) << obj_grad[i]
364 << std::setw(30) << obj_grad_fd[i];
365 if (fabs((obj_grad[i] - obj_grad_fd[i])/obj_grad[i]) > tol) {
366 libMesh::out <<
" : Mismatched sensitivity";
367 accurate_sens =
false;
369 libMesh::out << std::endl;
375 <<
" *** Constraint function gradients: analytical vs numerical" 379 << std::setw(10) <<
"DV" 380 << std::setw(30) <<
"Analytical" 381 << std::setw(30) <<
"Numerical" << std::endl;
385 libMesh::out <<
" Constraint: " << j << std::endl;
386 for (
unsigned int i=0; i<
_n_vars; i++) {
388 << std::setw(10) << i
391 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) {
392 libMesh::out <<
" : Mismatched sensitivity";
393 accurate_sens =
false;
395 libMesh::out << std::endl;
401 <<
"Verify gradients: all gradients satisfied relative tol: " << tol
402 <<
" with delta: " << delta
405 return accurate_sens;
411 const unsigned int iter1,
412 const unsigned int iter2,
428 for (
unsigned int i=0; i<=divs; i++) {
430 f = (1.*i)/(1.*divs);
431 for (
unsigned int j=0; j<
_n_vars; j++) {
432 dv[j] = (1.-f) * dv1[j] + f * dv2[j];
451 libmesh_assert(this->comm().verify(N));
452 libmesh_assert(this->comm().verify(N_EQ));
453 libmesh_assert(this->comm().verify(N_INEQ));
454 libmesh_assert(this->comm().verify(n_rel_change_iters));
461 std::vector<Real>& xmin,
462 std::vector<Real>& xmax) {
466 libmesh_assert(this->comm().verify(x));
467 libmesh_assert(this->comm().verify(xmin));
468 libmesh_assert(this->comm().verify(xmax));
477 std::vector<Real>& obj_grad,
478 std::vector<Real>& fvals,
479 std::vector<bool>& eval_grads,
480 std::vector<Real>& grads) {
484 libmesh_assert(this->comm().verify(dvars));
485 libmesh_assert(this->comm().verify(eval_obj_grad));
497 libmesh_assert(this->comm().verify(obj));
498 libmesh_assert(this->comm().verify(obj_grad));
499 libmesh_assert(this->comm().verify(fvals));
500 libmesh_assert(this->comm().verify(grads));
507 const std::vector<Real>& x,
509 const std::vector<Real>& fval,
510 bool if_write_to_optim_file) {
514 libmesh_assert(this->comm().verify(iter));
515 libmesh_assert(this->comm().verify(x));
517 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...
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
MAST::OptimizationInterface * _optimization_interface
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.
unsigned int n_ineq() const
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.
unsigned int n_vars() const
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...
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...
unsigned int n_eq() const
unsigned int n_iters_relative_change() const
virtual bool verify_gradients(const std::vector< Real > &dvars)
verifies the gradients at the specified design point