MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
design_history.hpp
Go to the documentation of this file.
1 /*
2 * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3 * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #ifndef __mast_optimization_design_history_h__
21 #define __mast_optimization_design_history_h__
22 
23 // C++ includes
24 #include <sys/stat.h>
25 #include <string>
26 #include <fstream>
27 #include <iomanip>
28 #include <boost/algorithm/string.hpp>
29 
30 
31 // MAST includes
33 #include <mast/base/exceptions.hpp>
34 
35 namespace MAST {
36 namespace Optimization {
37 namespace Utility {
38 
56 template <typename FuncEvalType>
57 inline void
58 write_dhistory_to_screen(const FuncEvalType &feval,
59  std::ostream &out,
60  const uint_t iter,
61  const std::vector<real_t> &x,
62  const real_t &obj,
63  const std::vector<real_t> &fval) {
64 
65  out
66  << " *************************** " << std::endl
67  << " *** Optimization Output *** " << std::endl
68  << " *************************** " << std::endl
69  << 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
74  << std::endl
75  << "Obj = " << std::setw(20) << obj << std::endl
76  << std::endl
77  << "Vars: " << std::endl;
78 
79  for (unsigned int i=0; i<feval.n_vars(); i++)
80  out
81  << "x [ " << std::setw(10) << i << " ] = "
82  << std::setw(20) << x[i] << std::endl;
83 
84  if (feval.n_eq()) {
85 
86  out << std::endl
87  << "Equality Constraints: " << std::endl;
88 
89  for (unsigned int i=0; i<feval.n_eq(); i++)
90  out
91  << "feq [ " << std::setw(10) << i << " ] = "
92  << std::setw(20) << fval[i] << std::endl;
93  }
94 
95  if (feval.n_ineq()) {
96 
97  out << std::endl
98  << "Inequality Constraints: " << std::endl;
99  unsigned int
100  n_active = 0,
101  n_violated = 0,
102  max_constr_id = 0;
103  real_t
104  max_constr = -1.e20;
105 
106  for (unsigned int i=0; i<feval.n_ineq(); i++) {
107  out
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()) {
111  n_active++;
112  out << " ***";
113  }
114  else if (fval[i+feval.n_eq()] > feval.tol()) {
115  n_violated++;
116  out << " +++";
117  }
118  out << std::endl;
119 
120  if (max_constr < fval[i+feval.n_eq()]) {
121  max_constr_id = i;
122  max_constr = fval[i+feval.n_eq()];
123  }
124  }
125 
126  out << std::endl
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;
133  }
134 
135  out << std::endl
136  << " *************************** " << std::endl;
137 
138 }
139 
140 
157 template <typename FuncEvalType>
158 inline void
159 write_obj_constr_history_to_file(const FuncEvalType &feval,
160  std::ostream &output,
161  const uint_t iter,
162  const real_t &obj,
163  const std::vector<real_t> &fval) {
164 
165  // write header for the first iteration
166  if (iter == 0) {
167 
168  // number of desing variables
169  output
170  << std::setw(10) << "n_dv" << std::setw(10) << feval.n_vars() << std::endl;
171  output
172  << std::setw(10) << "n_eq" << std::setw(10) << feval.n_eq() << std::endl;
173  output
174  << std::setw(10) << "n_ineq" << std::setw(10) << feval.n_ineq() << std::endl;
175 
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();
181  }
182  output << std::endl;
183  }
184 
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];
189  output << std::endl;
190 }
191 
192 
193 
216 template <typename FuncEvalType>
217 inline void
218 write_dhistory_to_file(const FuncEvalType &feval,
219  std::ostream &output,
220  const uint_t iter,
221  const std::vector<real_t> &x,
222  const real_t &obj,
223  const std::vector<real_t> &fval) {
224 
225  // write header for the first iteration
226  if (iter == 0) {
227 
228  // number of desing variables
229  output
230  << std::setw(10) << "n_dv" << std::setw(10) << feval.n_vars() << std::endl;
231  output
232  << std::setw(10) << "n_eq" << std::setw(10) << feval.n_eq() << std::endl;
233  output
234  << std::setw(10) << "n_ineq" << std::setw(10) << feval.n_ineq() << std::endl;
235 
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();
240  }
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();
245  }
246  output << std::endl;
247  }
248 
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];
255  output << std::endl;
256 }
257 
258 
272 template <typename FuncEvalType>
273 inline void initialize_dv_from_output_file(const FuncEvalType &f_eval,
274  const std::string &file,
275  const uint_t iter,
276  std::vector<real_t> &dv) {
277 
278 
279  struct stat stat_info;
280  int stat_result = stat(file.c_str(), &stat_info);
281 
282  if (stat_result != 0)
283  Error(false, "File does not exist: " + file);
284 
285  if (!std::ifstream(file))
286  Error(false, "File missing: " + file);
287 
288  std::ifstream input;
289  input.open(file, std::ofstream::in);
290 
291 
292  std::string
293  line;
294  uint_t
295  ndv = 0,
296  nineq = 0,
297  neq = 0,
298  it_num = 0;
299 
300  std::vector<std::string> results;
301 
302  // number of desing variables
303  std::getline(input, line);
304  boost::trim(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");
311 
312 
313  // number of equality constraint
314  std::getline(input, line);
315  boost::trim(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");
322 
323 
324  // number of inequality constriants
325  std::getline(input, line);
326  boost::trim(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");
333 
334 
335  // skip all lines before iter.
336  while (!input.eof() && it_num < iter+1) {
337  std::getline(input, line);
338  it_num++;
339  }
340 
341  // make sure that the iteration number is what we are looking for
342  std::getline(input, line);
343  boost::trim(line);
344  boost::split(results, line, boost::is_any_of(" \t"), boost::token_compress_on);
345 
346  Assert2(results.size() > ndv+1,
347  results.size(), ndv+1,
348  "Invalid file format or incomplete data in in put file.");
349 
350  it_num = stoi(results[0]);
351  Assert2(it_num == iter, it_num, iter,
352  "Requested iteration number not found");
353 
354  // make sure that the file has data
355  for (unsigned int i=0; i<ndv; i++)
356  dv[i] = real_t(stod(results[i+1]));
357 }
358 
359 
360 
361 template <typename FuncEvalType>
362 inline void write_dv_to_file(const FuncEvalType &feval,
363  const std::string &file,
364  const uint_t iter,
365  const std::vector<complex_t> &x,
366  const complex_t &obj,
367  const std::vector<complex_t> &fval) {
368 
369  Error(false, "Not implemented for complex type");
370 }
371 
372 
373 template <typename FuncEvalType>
374 inline void initialize_dv_from_output_file(const FuncEvalType &f_eval,
375  const std::string &file,
376  const uint_t iter,
377  std::vector<complex_t> &dv) {
378 
379  Error(false, "Not implemented for complex type");
380 }
381 
382 
383 #if MAST_ENABLE_ADOLC == 1
384 template <typename FuncEvalType>
385 inline void write_dv_to_file(const FuncEvalType &feval,
386  const std::string &file,
387  const uint_t iter,
388  const std::vector<adouble_tl_t> &x,
389  const adouble_tl_t &obj,
390  const std::vector<adouble_tl_t> &fval) {
391 
392  Error(false, "Not implemented for adouble type");
393 }
394 
395 
396 template <typename FuncEvalType>
397 inline void initialize_dv_from_output_file(const FuncEvalType &f_eval,
398  const std::string &file,
399  const uint_t iter,
400  std::vector<adouble_tl_t> &dv) {
401 
402  Error(false, "Not implemented for adouble type");
403 }
404 #endif
405 
406 } // namespace Utility
407 } // namespace Optimization
408 } // namespace MAST
409 
410 
411 #endif // __mast_optimization_design_history_h__
#define Error(cond, msg)
Definition: exceptions.hpp:166
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)
Definition: exceptions.hpp:134
unsigned int uint_t
#define Assert2(cond, v1, v2, msg)
Definition: exceptions.hpp:152
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)
double real_t
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...