objective_function_wrapper.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 <iomanip>
21 #include <cstring>
22 #include <ctime>
23 #include <sstream>
24 #include "mango.hpp"
25 #include "Solver.hpp"
26 
27 void mango::Solver::objective_function_wrapper(const double* x, double* f, bool* failed) {
28  if (verbose > 0) std::cout << "Hello from objective_function_wrapper" << std::endl;
29 
30  int failed_int = 123;
31  objective_function(&N_parameters, x, f, &failed_int, problem, user_data);
32  *failed = (failed_int != 0);
33 
34  if (verbose > 0) std::cout << " objective_function_wrapper: *failed=" << *failed << " at_least_one_success=" << at_least_one_success
35  << ", *f < best_objective_function=" << (*f < best_objective_function) << std::endl;
36 
37  record_function_evaluation(x, *f, *failed);
38 }
39 
40 
41 bool mango::Solver::record_function_evaluation(const double* x, double f, bool failed) {
42  if (verbose > 0) std::cout << "Hello from Solver::record_function_evaluation" << std::endl;
43 
44  function_evaluations++;
45 
46  clock_t now = clock();
47 
48  bool new_optimum = false;
49  if (!failed && (!at_least_one_success || f < best_objective_function)) {
50  new_optimum = true;
51  at_least_one_success = true;
52  best_objective_function = f;
53  best_function_evaluation = function_evaluations;
54  memcpy(best_state_vector, x, N_parameters * sizeof(double));
55  best_time = now;
56  }
57 
58  if (mpi_partition->get_proc0_world()) recorder->record_function_evaluation(function_evaluations, now, x, f);
59 
60  return new_optimum;
61 }
62 
63 
mango::Solver::problem
Problem * problem
Definition: Solver.hpp:66
mango::Solver::objective_function
objective_function_type objective_function
Definition: Solver.hpp:44
mango::Solver::verbose
int verbose
Definition: Solver.hpp:63
mango::Solver::objective_function_wrapper
virtual void objective_function_wrapper(const double *, double *, bool *)
Definition: objective_function_wrapper.cpp:27
mango::Solver::user_data
void * user_data
Definition: Solver.hpp:64
Solver.hpp
mango::Solver::best_objective_function
double best_objective_function
Definition: Solver.hpp:51
mango.hpp
mango::Solver::N_parameters
int N_parameters
Definition: Solver.hpp:43
mango::Solver::record_function_evaluation
virtual bool record_function_evaluation(const double *, double, bool)
Definition: objective_function_wrapper.cpp:41
mango::Solver::at_least_one_success
bool at_least_one_success
Definition: Solver.hpp:49