MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
MAST::Optimization::Utility Namespace Reference

Functions

template<typename FuncEvalType >
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 . More...
 
template<typename FuncEvalType >
void initialize_dv_from_output_file (const FuncEvalType &f_eval, const std::string &file, const uint_t iter, std::vector< complex_t > &dv)
 
template<typename FuncEvalType >
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 problem from a design history. More...
 
template<typename FuncEvalType >
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. More...
 
template<typename FuncEvalType >
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)
 
template<typename FuncEvalType >
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. More...
 

Function Documentation

◆ initialize_dv_from_output_file() [1/2]

template<typename FuncEvalType >
void MAST::Optimization::Utility::initialize_dv_from_output_file ( const FuncEvalType &  f_eval,
const std::string &  file,
const uint_t  iter,
std::vector< real_t > &  dv 
)
inline

Initializes the design variable vector dv from iter iteration stored in file .

This assumes that the file was written by the function write_dhistory_to_file(). The template parameter FuncEvalType is the class that provides the method

  • FuncEvalType::n_vars() : returns number of design variables
  • FuncEvalType::n_eq() : returns number of equality constraints
  • FuncEvalType::n_ineq() : returns number of inequality constraints

function arguments are

  • feval : object of type FuncEvalType that provides the relevant functions noted above
  • file : Name of file from which the data will be read
  • iter : iteration number that will be read from file
  • dv : vector into which the design variables will be initialized

Definition at line 273 of file design_history.hpp.

276  {
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 }
#define Error(cond, msg)
Definition: exceptions.hpp:166
#define Assert0(cond, msg)
Definition: exceptions.hpp:134
unsigned int uint_t
#define Assert2(cond, v1, v2, msg)
Definition: exceptions.hpp:152
double real_t

◆ initialize_dv_from_output_file() [2/2]

template<typename FuncEvalType >
void MAST::Optimization::Utility::initialize_dv_from_output_file ( const FuncEvalType &  f_eval,
const std::string &  file,
const uint_t  iter,
std::vector< complex_t > &  dv 
)
inline

Definition at line 374 of file design_history.hpp.

377  {
378 
379  Error(false, "Not implemented for complex type");
380 }
#define Error(cond, msg)
Definition: exceptions.hpp:166

◆ write_dhistory_to_file()

template<typename FuncEvalType >
void MAST::Optimization::Utility::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 
)
inline

Prints the design iteration data to stream output in a format that can be read back to initialize a problem from a design history.

The template parameter FuncEvalType is the class that provides the method

  • FuncEvalType::n_vars() : returns number of design variables
  • FuncEvalType::n_eq() : returns number of equality constraints
  • FuncEvalType::n_ineq() : returns number of inequality constraints function arguments are
  • feval : object of type FuncEvalType that provides the relevant functions noted above
  • output : Stream to which the output will be written
  • iter : current iteration number
  • x : current design variable vector
  • obj : current objective function values.
  • fval : current constraint function values. It is assumed that the equality constraint values are included before the inequality constraint values.

Following the initial header information, the data is written in a tabular format where each row corresponds to a design iteration and the columns are arranged with:

  • all design variables,
  • all equality constraints,
  • objective function,
  • all inequality constraints.

Definition at line 218 of file design_history.hpp.

223  {
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 }

◆ write_dhistory_to_screen()

template<typename FuncEvalType >
void MAST::Optimization::Utility::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 
)
inline

Prints the design iteration data to stream out.

The template parameter FuncEvalType is the class that provides the method

  • FuncEvalType::n_vars() : returns number of design variables
  • FuncEvalType::n_eq() : returns number of equality constraints
  • FuncEvalType::n_ineq() : returns number of inequality constraints
  • FuncEvalType::tol() : returns tolerance for identifying constraint as active

function arguments are

  • feval : object of type FuncEvalType that provides the relevant functions noted above
  • out : Stream to which the output will be written
  • iter : current iteration number
  • x : current design variable vector
  • obj : current objective function values.
  • fval : current constraint function values. It is assumed that the equality constraint values are included before the inequality constraint values.

Definition at line 58 of file design_history.hpp.

63  {
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 }
double real_t

◆ write_dv_to_file()

template<typename FuncEvalType >
void MAST::Optimization::Utility::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 
)
inline

Definition at line 362 of file design_history.hpp.

367  {
368 
369  Error(false, "Not implemented for complex type");
370 }
#define Error(cond, msg)
Definition: exceptions.hpp:166

◆ write_obj_constr_history_to_file()

template<typename FuncEvalType >
void MAST::Optimization::Utility::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 
)
inline

Prints the objective function and constraint values from design iterations stream output.

Note that design variables are not included. If the design variables are needed then write_dhistory_to_file() must be used instead. The template parameter FuncEvalType is the class that provides the method

  • FuncEvalType::n_vars() : returns number of design variables
  • FuncEvalType::n_eq() : returns number of equality constraints
  • FuncEvalType::n_ineq() : returns number of inequality constraints

function arguments are

  • feval : object of type FuncEvalType that provides the relevant functions noted above
  • output : Stream to which the output will be written
  • iter : current iteration number
  • obj : current objective function values.
  • fval : current constraint function values. It is assumed that the equality constraint values are included before the inequality constraint values.

Definition at line 159 of file design_history.hpp.

163  {
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 }