Solver.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 <stdexcept>
21 #include <cassert>
22 #include "mango.hpp"
23 #include "Solver.hpp"
24 #include "Recorder_standard.hpp"
25 
26 // Constructor
27 mango::Solver::Solver(Problem* problem_in, int N_parameters_in) {
28  if (N_parameters_in < 1) throw std::runtime_error("Error in mango::Solver::Solver(). N_parameters must be at least 1.");
29  N_parameters = N_parameters_in;
30  best_state_vector = new double[N_parameters_in];
31 
32  // Defaults are set in the following lines:
33  verbose = 0;
34  objective_function = NULL;
36  argc = 1;
37  argv = NULL;
38  state_vector = NULL;
40  centered_differences = false;
42  output_filename = "mango_out";
45  bound_constraints_set = false;
46  package = NULL;
47  user_data = NULL;
48  problem = problem_in;
49  recorder = new Recorder_standard(this);
50  N_line_search = 0;
51 }
52 
53 // Constructor with no arguments, used only for unit tests
55  // N_parameters = 1;
56  //best_state_vector = new double[1];
57  recorder = new Recorder();
58 
59  // We need a Problem to exist that is connected to this Solver, so create one.
60  problem = new Problem(1,NULL,NULL,1,NULL);
61  delete problem->solver;
62  problem->solver = this;
63 }
64 
65 
66 // Destructor
68  delete[] best_state_vector;
69 }
70 
71 void mango::Solver::objective_to_vector_function(int* N_parameters_arg, const double* state_vector_arg, int* N_terms, double* results, int* failed, mango::Problem* problem_arg, void* user_data_arg) {
72  // Note that this method is static, so there is no "this".
73  //assert(N_parameters_arg == N_parameters);
74  //assert(problem_arg == problem);
75  assert(*N_terms == 1);
76  //std::cout << "problem_arg->get_N_parameters(): " << problem_arg->get_N_parameters() << std::endl;
77  //std::cout << "problem_arg->get_solver()->finite_difference_step_size: " << problem_arg->get_solver()->finite_difference_step_size << std::endl;
78  problem_arg->get_solver()->objective_function(N_parameters_arg, state_vector_arg, results, failed, problem_arg, user_data_arg);
79  //objective_function(N_parameters_arg, state_vector_arg, results, failed, problem_arg, user_data_arg);
80 }
81 
82 void mango::Solver::finite_difference_gradient(const double* state_vector, double* base_case_objective_function, double* gradient) {
83  finite_difference_Jacobian(objective_to_vector_function, 1, state_vector, base_case_objective_function, gradient);
84 }
85 
86 void mango::Solver::record_function_evaluation_pointer(const double* state_vector_arg, double* objective_function_arg, bool failed) {
87  // This method is called from evaluate_set_in_parallel.
88  // Call the method with the same name but a different signature
89  if (verbose>0) std::cout << "Hello from void mango::Solver::record_function_evaluation_pointer(const double*, double*, bool)" << std::endl;
90  record_function_evaluation(state_vector_arg, *objective_function_arg, failed);
91 }
Recorder_standard.hpp
mango::Solver::finite_difference_gradient
virtual void finite_difference_gradient(const double *, double *, double *)
Definition: Solver.cpp:82
mango::Solver::Solver
Solver()
Definition: Solver.cpp:54
mango::Problem::solver
Solver * solver
Definition: mango.hpp:446
mango::Solver::record_function_evaluation_pointer
virtual void record_function_evaluation_pointer(const double *, double *, bool)
Definition: Solver.cpp:86
mango::Solver::problem
Problem * problem
Definition: Solver.hpp:66
mango::Solver::objective_function
objective_function_type objective_function
Definition: Solver.hpp:44
mango::Solver::centered_differences
bool centered_differences
Definition: Solver.hpp:59
mango::Solver::verbose
int verbose
Definition: Solver.hpp:63
mango::Solver::argc
int argc
Definition: Solver.hpp:46
mango::Recorder
Definition: Recorder.hpp:27
mango::Solver::~Solver
~Solver()
Definition: Solver.cpp:67
mango::Solver::user_data
void * user_data
Definition: Solver.hpp:64
mango::algorithm_type
algorithm_type
A list of the algorithms that MANGO can potentially use.
Definition: mango.hpp:75
Solver.hpp
mango::Solver::finite_difference_step_size
double finite_difference_step_size
Definition: Solver.hpp:60
mango::Solver::state_vector
double * state_vector
Definition: Solver.hpp:58
mango::Solver::best_state_vector
double * best_state_vector
Definition: Solver.hpp:50
mango::Solver::finite_difference_Jacobian
void finite_difference_Jacobian(vector_function_type, int, const double *, double *, double *)
Definition: finite_difference_Jacobian.cpp:28
mango::Solver::recorder
Recorder * recorder
Definition: Solver.hpp:67
mango::Solver::N_line_search
int N_line_search
Definition: Solver.hpp:68
mango::Solver::bound_constraints_set
bool bound_constraints_set
Definition: Solver.hpp:53
mango::Solver::argv
char ** argv
Definition: Solver.hpp:47
mango.hpp
mango::Solver::max_function_evaluations
int max_function_evaluations
Definition: Solver.hpp:62
mango::Solver::output_filename
std::string output_filename
Definition: Solver.hpp:61
mango::Problem::get_solver
Solver * get_solver()
Get the Solver object associated with the optimization problem.
Definition: Problem.cpp:180
mango::Solver::N_parameters
int N_parameters
Definition: Solver.hpp:43
mango::Solver::objective_to_vector_function
static void objective_to_vector_function(int *, const double *, int *, double *, int *, mango::Problem *, void *)
Definition: Solver.cpp:71
mango::Solver::algorithm
algorithm_type algorithm
Definition: Solver.hpp:42
mango::Solver::record_function_evaluation
virtual bool record_function_evaluation(const double *, double, bool)
Definition: objective_function_wrapper.cpp:41
mango::Recorder_standard
Definition: Recorder_standard.hpp:28
mango::Solver::function_evaluations
int function_evaluations
Definition: Solver.hpp:45
mango::Solver::best_function_evaluation
int best_function_evaluation
Definition: Solver.hpp:52
mango::Problem
Definition: mango.hpp:442