20 #ifndef __mast_optimization_design_history_h__ 21 #define __mast_optimization_design_history_h__ 28 #include <boost/algorithm/string.hpp> 36 namespace Optimization {
56 template <
typename FuncEvalType>
61 const std::vector<real_t> &x,
63 const std::vector<real_t> &fval) {
66 <<
" *************************** " << std::endl
67 <<
" *** Optimization Output *** " << std::endl
68 <<
" *************************** " << std::endl
70 <<
"Iter: " << std::setw(10) << iter << std::endl
71 <<
"Nvars: " << std::setw(10) << x.size() << std::endl
72 <<
"Ncons-Equality: " << std::setw(10) << feval.n_eq() << std::endl
73 <<
"Ncons-Inquality: " << std::setw(10) << feval.n_ineq() << std::endl
75 <<
"Obj = " << std::setw(20) << obj << std::endl
77 <<
"Vars: " << std::endl;
79 for (
unsigned int i=0; i<feval.n_vars(); i++)
81 <<
"x [ " << std::setw(10) << i <<
" ] = " 82 << std::setw(20) << x[i] << std::endl;
87 <<
"Equality Constraints: " << std::endl;
89 for (
unsigned int i=0; i<feval.n_eq(); i++)
91 <<
"feq [ " << std::setw(10) << i <<
" ] = " 92 << std::setw(20) << fval[i] << std::endl;
98 <<
"Inequality Constraints: " << std::endl;
106 for (
unsigned int i=0; i<feval.n_ineq(); i++) {
108 <<
"fineq [ " << std::setw(10) << i <<
" ] = " 109 << std::setw(20) << fval[i+feval.n_eq()];
110 if (fabs(fval[i+feval.n_eq()]) <= feval.tol()) {
114 else if (fval[i+feval.n_eq()] > feval.tol()) {
120 if (max_constr < fval[i+feval.n_eq()]) {
122 max_constr = fval[i+feval.n_eq()];
127 << std::setw(35) <<
" N Active Constraints: " 128 << std::setw(20) << n_active << std::endl
129 << std::setw(35) <<
" N Violated Constraints: " 130 << std::setw(20) << n_violated << std::endl
131 << std::setw(35) <<
" Most critical constraint: " 132 << std::setw(20) << max_constr << std::endl;
136 <<
" *************************** " << std::endl;
157 template <
typename FuncEvalType>
160 std::ostream &output,
163 const std::vector<real_t> &fval) {
170 << std::setw(10) <<
"n_dv" << std::setw(10) << feval.n_vars() << std::endl;
172 << std::setw(10) <<
"n_eq" << std::setw(10) << feval.n_eq() << std::endl;
174 << std::setw(10) <<
"n_ineq" << std::setw(10) << feval.n_ineq() << std::endl;
176 output << std::setw(10) <<
"Iter";
177 output << std::setw(20) <<
"Obj";
178 for (
unsigned int i=0; i<fval.size(); i++) {
179 std::stringstream f; f <<
"f_" << i;
180 output << std::setw(20) << f.str();
185 output << std::setw(10) << iter;
186 output << std::setw(20) << obj;
187 for (
unsigned int i=0; i < fval.size(); i++)
188 output << std::setw(20) << fval[i];
216 template <
typename FuncEvalType>
219 std::ostream &output,
221 const std::vector<real_t> &x,
223 const std::vector<real_t> &fval) {
230 << std::setw(10) <<
"n_dv" << std::setw(10) << feval.n_vars() << std::endl;
232 << std::setw(10) <<
"n_eq" << std::setw(10) << feval.n_eq() << std::endl;
234 << std::setw(10) <<
"n_ineq" << std::setw(10) << feval.n_ineq() << std::endl;
236 output << std::setw(10) <<
"Iter";
237 for (
unsigned int i=0; i < x.size(); i++) {
238 std::stringstream x; x <<
"x_" << i;
239 output << std::setw(20) << x.str();
241 output << std::setw(20) <<
"Obj";
242 for (
unsigned int i=0; i<fval.size(); i++) {
243 std::stringstream f; f <<
"f_" << i;
244 output << std::setw(20) << f.str();
249 output << std::setw(10) << iter;
250 for (
unsigned int i=0; i < x.size(); i++)
251 output << std::setw(20) << x[i];
252 output << std::setw(20) << obj;
253 for (
unsigned int i=0; i < fval.size(); i++)
254 output << std::setw(20) << fval[i];
272 template <
typename FuncEvalType>
274 const std::string &file,
276 std::vector<real_t> &dv) {
279 struct stat stat_info;
280 int stat_result = stat(file.c_str(), &stat_info);
282 if (stat_result != 0)
283 Error(
false,
"File does not exist: " + file);
285 if (!std::ifstream(file))
286 Error(
false,
"File missing: " + file);
289 input.open(file, std::ofstream::in);
300 std::vector<std::string> results;
303 std::getline(input, line);
305 boost::split(results, line, boost::is_any_of(
" \t"), boost::token_compress_on);
306 Assert0(results[0].compare(
"n_dv") != 0,
307 "Invalid file format: Expected n_dv, found: " + results[0]);
308 ndv = stod(results[1]);
309 Assert2(ndv == dv.size(), ndv, dv.size(),
310 "Design variable vector size incompatible with number of DVs in file");
314 std::getline(input, line);
316 boost::split(results, line, boost::is_any_of(
" \t"), boost::token_compress_on);
317 Assert0(results[0].compare(
"n_eq") != 0,
318 "Invalid file format: Expected n_eq, found: " + results[0]);
319 neq = stod(results[1]);
320 Assert2(neq == f_eval.n_eq(), neq, f_eval.n_eq(),
321 "Incompatible number of equality constraints");
325 std::getline(input, line);
327 boost::split(results, line, boost::is_any_of(
" \t"), boost::token_compress_on);
328 Assert0(results[0].compare(
"n_ineq") != 0,
329 "Invalid file format: Expected n_ineq, found: " + results[0]);
330 nineq = stod(results[1]);
331 Assert2(nineq == f_eval.n_ineq(), nineq, f_eval.n_ineq(),
332 "Incompatible number of inequality constraints");
336 while (!input.eof() && it_num < iter+1) {
337 std::getline(input, line);
342 std::getline(input, line);
344 boost::split(results, line, boost::is_any_of(
" \t"), boost::token_compress_on);
346 Assert2(results.size() > ndv+1,
347 results.size(), ndv+1,
348 "Invalid file format or incomplete data in in put file.");
350 it_num = stoi(results[0]);
351 Assert2(it_num == iter, it_num, iter,
352 "Requested iteration number not found");
355 for (
unsigned int i=0; i<ndv; i++)
356 dv[i] =
real_t(stod(results[i+1]));
361 template <
typename FuncEvalType>
363 const std::string &file,
365 const std::vector<complex_t> &x,
367 const std::vector<complex_t> &fval) {
369 Error(
false,
"Not implemented for complex type");
373 template <
typename FuncEvalType>
375 const std::string &file,
377 std::vector<complex_t> &dv) {
379 Error(
false,
"Not implemented for complex type");
383 #if MAST_ENABLE_ADOLC == 1 384 template <
typename FuncEvalType>
386 const std::string &file,
388 const std::vector<adouble_tl_t> &x,
389 const adouble_tl_t &obj,
390 const std::vector<adouble_tl_t> &fval) {
392 Error(
false,
"Not implemented for adouble type");
396 template <
typename FuncEvalType>
398 const std::string &file,
400 std::vector<adouble_tl_t> &dv) {
402 Error(
false,
"Not implemented for adouble type");
411 #endif // __mast_optimization_design_history_h__
void initialize_dv_from_output_file(const FuncEvalType &f_eval, const std::string &file, const uint_t iter, std::vector< real_t > &dv)
Initializes the design variable vector dv from iter iteration stored in file .
std::complex< real_t > complex_t
void write_dhistory_to_file(const FuncEvalType &feval, std::ostream &output, const uint_t iter, const std::vector< real_t > &x, const real_t &obj, const std::vector< real_t > &fval)
Prints the design iteration data to stream output in a format that can be read back to initialize a p...
#define Assert0(cond, msg)
#define Assert2(cond, v1, v2, msg)
void write_dhistory_to_screen(const FuncEvalType &feval, std::ostream &out, const uint_t iter, const std::vector< real_t > &x, const real_t &obj, const std::vector< real_t > &fval)
Prints the design iteration data to stream out.
void write_dv_to_file(const FuncEvalType &feval, const std::string &file, const uint_t iter, const std::vector< complex_t > &x, const complex_t &obj, const std::vector< complex_t > &fval)
void write_obj_constr_history_to_file(const FuncEvalType &feval, std::ostream &output, const uint_t iter, const real_t &obj, const std::vector< real_t > &fval)
Prints the objective function and constraint values from design iterations stream output...