residual_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 "mango.hpp"
24 #include "Least_squares_solver.hpp"
25 
26 void mango::Least_squares_solver::residual_function_wrapper(const double* x, double* f, bool* failed) {
27  // This subroutine is used to call the residual function when N_worker_groups = 1 (i.e. parallelization only within the objective function evaluation),
28  // or for algorithms like hopspack that allow concurrent evaluations of the objective function but not using finite difference Jacobians.
29  // This subroutine is not used for finite difference Jacobians.
30 
31  // I chose to make this subroutine take a parameter f[] to store the residuals rather than always storing them
32  // in the "residuals" array of the mango::problem class because PETSc uses its own storage for the residuals.
33 
34  // For non-least-squares algorithms,
35 
36  int failed_int;
37  residual_function(&(N_parameters), x, &N_terms, f, &failed_int, problem, user_data);
38  *failed = (failed_int != 0);
39 
40  if (verbose > 0) {
41  std::cout << "Hello from residual_function_wrapper. Here comes x:" << std::endl;
42  int j;
43  for (j=0; j < N_parameters; j++) {
44  std::cout << std::setw(24) << std::setprecision(15) << x[j];
45  }
46  std::cout << std::endl;
47  }
48 
50 
51 }
52 
53 void mango::Least_squares_solver::record_function_evaluation_pointer(const double* x, double *f, bool failed) {
54  // This method overrides mango::Solver::record_function_evaluation_pointer().
55  if (verbose>0) std::cout << "Hello from Least_squares_solver::record_function_evaluation, the non-override." << std::endl;
56  double objective_value = residuals_to_single_objective(f);
57  current_residuals = f;
58  record_function_evaluation(x, objective_value, failed);
59 }
60 
61 bool mango::Least_squares_solver::record_function_evaluation(const double* x, double f, bool failed) {
62  // This method overrides mango::Solver::record_function_evaluation()
63 
64  if (verbose>0) std::cout << "Hello from Least_squares_solver::record_function_evaluation, the override." << std::endl;
65  // Call the overridden function from the base class:
66  bool new_optimum = mango::Solver::record_function_evaluation(x,f,failed);
67  if (new_optimum) {
68  memcpy(best_residual_function, current_residuals, N_terms * sizeof(double));
69  }
70 
71  return new_optimum;
72  /*
73 
74  // For non-least-squares algorithms, the function evaluations are recorded in objective_function_wrapper.
75  // Otherwise, do the recording here.
76  bool new_optimum;
77  if (algorithms[algorithm].least_squares) {
78  double objective_value = residuals_to_single_objective(f);
79  current_residuals = f;
80  new_optimum = record_function_evaluation(x, objective_value, failed);
81  if (new_optimum) {
82  memcpy(best_residual_function, f, N_terms * sizeof(double));
83  }
84  }
85 
86  */
87 }
88 
89 
90 void mango::Least_squares_solver::objective_function_wrapper(const double* x, double* f, bool* failed) {
91  // This method overrides mango::Solver::objective_function_wrapper().
92  // The difference from that method is that here we do not call record_function_evaluation,
93  // since it was done already in residual_function_wrapper().
94  if (verbose > 0) std::cout << "Hello from Least_squares_solver::objective_function_wrapper" << std::endl;
95 
96  int failed_int;
97  objective_function(&N_parameters, x, f, &failed_int, problem, user_data);
98  *failed = (failed_int != 0);
99 }
mango::Least_squares_solver::record_function_evaluation_pointer
void record_function_evaluation_pointer(const double *, double *, bool)
Definition: residual_function_wrapper.cpp:53
Least_squares_solver.hpp
mango::Least_squares_solver::N_terms
int N_terms
Definition: Least_squares_solver.hpp:38
mango::Solver::problem
Problem * problem
Definition: Solver.hpp:66
mango::Solver::verbose
int verbose
Definition: Solver.hpp:63
mango::Solver::user_data
void * user_data
Definition: Solver.hpp:64
mango.hpp
mango::Least_squares_solver::residual_function
vector_function_type residual_function
Definition: Least_squares_solver.hpp:41
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::Least_squares_solver::objective_function_wrapper
void objective_function_wrapper(const double *, double *, bool *)
Definition: residual_function_wrapper.cpp:90
mango::Least_squares_solver::record_function_evaluation
bool record_function_evaluation(const double *, double, bool)
Definition: residual_function_wrapper.cpp:61
mango::Least_squares_solver::residual_function_wrapper
void residual_function_wrapper(const double *, double *, bool *)
Definition: residual_function_wrapper.cpp:26