Recorder_standard.cpp
Go to the documentation of this file.
1 // Copyright 2019, University of Maryland and the MANGO development team.
2 //
3 // This file is part of MANGO.
4 //
5 // MANGO is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License as
7 // published by the Free Software Foundation, either version 3 of the
8 // License, or (at your option) any later version.
9 //
10 // MANGO is distributed in the hope that it will be useful, but
11 // 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 MANGO. If not, see
17 // <https://www.gnu.org/licenses/>.
18 
19 #include <iostream>
20 #include <fstream>
21 #include <iomanip>
22 #include <stdexcept>
23 #include <ctime>
24 #include "Recorder.hpp"
25 #include "Recorder_standard.hpp"
26 
28  solver = solver_in;
29 }
30 
32  if (!solver->mpi_partition->get_proc0_world()) return; // Proceed only on proc0_world.
33 
34  // Open output file
35  output_file.open(solver->output_filename.c_str());
36  if (!output_file.is_open()) {
37  std::cerr << "output file: " << solver->output_filename << std::endl;
38  throw std::runtime_error("Error! Unable to open output file.");
39  }
40  // Write header lines of output file
41  output_file << "Recorder type:" << std::endl << "standard" << std::endl << "N_parameters:" << std::endl << solver->N_parameters << std::endl << "function_evaluation,seconds";
42  for (int j=0; j<solver->N_parameters; j++) {
43  output_file << ",x(" << j+1 << ")";
44  }
45  output_file << ",objective_function" << std::endl;
46 }
47 
48 
49 void mango::Recorder_standard::write_file_line(int function_evaluations, clock_t print_time, const double* x, double f) {
50  // This subroutine writes a line in the output file for non-least-squares problems.
51 
52  /*
53  write_function_evaluation_and_time(print_time);
54  std::string file_string;
55  compose_x_f_string(file_string, x, f);
56  output_file << file_string << std::endl << std::flush;
57  */
58  double elapsed_time = ((float)(print_time - solver->start_time)) / CLOCKS_PER_SEC;
59  output_file << std::setw(6) << std::right << function_evaluations << "," << std::setw(12) << std::setprecision(4) << std::scientific << elapsed_time;
60  for (int j=0; j<solver->N_parameters; j++) {
61  output_file << "," << std::setw(24) << std::setprecision(16) << std::scientific << x[j];
62  }
63  output_file << "," << std::setw(24) << f << std::endl << std::flush;
64 }
65 
66 
67 void mango::Recorder_standard::record_function_evaluation(int function_evaluations, clock_t print_time, const double* x, double f) {
68  if (!solver->mpi_partition->get_proc0_world()) return; // Proceed only on proc0_world.
69  write_file_line(function_evaluations, print_time, x, f);
70 }
71 
72 
74  // Copy the line corresponding to the optimum to the bottom of the output file.
75 
76  if (!solver->mpi_partition->get_proc0_world()) return; // Proceed only on proc0_world.
77 
78  write_file_line(solver->best_function_evaluation, solver->best_time, solver->state_vector, solver->best_objective_function);
79 
80  output_file.close();
81 }
Recorder_standard.hpp
mango::Recorder_standard::finalize
void finalize()
Definition: Recorder_standard.cpp:73
mango::Recorder_standard::Recorder_standard
Recorder_standard(Solver *)
Definition: Recorder_standard.cpp:27
mango::Recorder_standard::record_function_evaluation
void record_function_evaluation(int function_evaluations, clock_t print_time, const double *x, double f)
Definition: Recorder_standard.cpp:67
mango::Recorder_standard::init
void init()
Definition: Recorder_standard.cpp:31
mango::Solver
Definition: Solver.hpp:31
Recorder.hpp