init_optimization.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 <math.h>
21 #include <limits>
22 //#include <cstring>
23 #include <stdexcept>
24 #include <ctime>
25 #include "mango.hpp"
26 #include "Solver.hpp"
27 
29 
30  // This subroutine carries out a few steps that are shared by both least_squares and conventional optimization.
31 
33  throw std::runtime_error("Error! The mango::optimize() subroutine should only be called by group leaders, not by all workers.");
34 
36  at_least_one_success = false;
37  best_objective_function = std::numeric_limits<double>::quiet_NaN();
39  start_time = clock();
40 
41  // To simplify code a bit...
42  MPI_Comm mpi_comm_group_leaders = mpi_partition->get_comm_group_leaders();
43 
44  // Make sure that parameters used by the finite-difference gradient routine are the same for all group leaders:
45  MPI_Bcast(&N_parameters, 1, MPI_INT, 0, mpi_comm_group_leaders);
46  MPI_Bcast(&centered_differences, 1, MPI_C_BOOL, 0, mpi_comm_group_leaders);
47  MPI_Bcast(&finite_difference_step_size, 1, MPI_DOUBLE, 0, mpi_comm_group_leaders);
48  MPI_Bcast(&algorithm, 1, MPI_INT, 0, mpi_comm_group_leaders);
49  // 20200127 These next 2 lines should end up in Least_squares_data::optimize()?
50  // MPI_Bcast(&N_terms, 1, MPI_INT, 0, mpi_comm_group_leaders);
51  // MPI_Bcast(&least_squares, 1, MPI_C_BOOL, 0, mpi_comm_group_leaders);
52 
53  if (algorithms[algorithm].requires_bound_constraints && (!bound_constraints_set))
54  throw std::runtime_error("Error! A MANGO algorithm was chosen that requires bound constraints, but bound constraints were not set.");
55 
56  if (bound_constraints_set && (!algorithms[algorithm].allows_bound_constraints) && mpi_partition->get_proc0_world()) {
57 #define star_line "*******************************************************************************************************"
58  std::cerr << star_line << std::endl;
59  std::cerr << "WARNING! Bound constraints were set, but an algorithm was chosen that does not allow bound constraints." << std::endl;
60  std::cerr << "Therefore, the bound constraints will be ignored for this calculation." << std::endl;
61  std::cerr << star_line << std::endl;
62  }
63 
64  if (algorithms[algorithm].uses_derivatives) {
67  } else {
69  }
70  } else {
72  }
73 
74  set_package();
75 
76  if (verbose > 0) {
77  std::cout << "Proc " << mpi_partition->get_rank_world() << " is entering optimize(), and thinks proc0_world=" << mpi_partition->get_proc0_world() << std::endl;
78  std::cout << "max_function_evaluations = " << max_function_evaluations <<
79  ", max_function_and_gradient_evaluations = " << max_function_and_gradient_evaluations << std::endl;
80  }
81 
83 }
star_line
#define star_line
mango::Solver::start_time
clock_t start_time
Definition: Solver.hpp:56
mango::Recorder::init
virtual void init()
Definition: Recorder.hpp:29
mango::Solver::centered_differences
bool centered_differences
Definition: Solver.hpp:59
mango::Solver::verbose
int verbose
Definition: Solver.hpp:63
mango::MPI_Partition::get_proc0_worker_groups
bool get_proc0_worker_groups()
Determine whether this MPI processor has rank 0 in MANGO's "worker groups" communicator.
Definition: mpi_partition.cpp:61
mango::MPI_Partition::get_comm_group_leaders
MPI_Comm get_comm_group_leaders()
Get the MPI communicator for MANGO's "group leaders" communicator.
Definition: mpi_partition.cpp:51
Solver.hpp
mango::Solver::finite_difference_step_size
double finite_difference_step_size
Definition: Solver.hpp:60
mango::algorithms
const algorithm_properties algorithms[NUM_ALGORITHMS]
A database of the algorithms that MANGO is aware of, including various properties of each algorithm.
Definition: mango.hpp:124
mango::Solver::init_optimization
virtual void init_optimization()
Definition: init_optimization.cpp:28
mango::Solver::recorder
Recorder * recorder
Definition: Solver.hpp:67
mango::Solver::best_objective_function
double best_objective_function
Definition: Solver.hpp:51
mango::Solver::bound_constraints_set
bool bound_constraints_set
Definition: Solver.hpp:53
mango::Solver::mpi_partition
MPI_Partition * mpi_partition
Definition: Solver.hpp:65
mango.hpp
mango::Solver::max_function_evaluations
int max_function_evaluations
Definition: Solver.hpp:62
mango::MPI_Partition::get_rank_world
int get_rank_world()
Get the MPI rank of this processor in MANGO's world communicator.
Definition: mpi_partition.cpp:66
mango::Solver::N_parameters
int N_parameters
Definition: Solver.hpp:43
mango::Solver::algorithm
algorithm_type algorithm
Definition: Solver.hpp:42
mango::MPI_Partition::get_proc0_world
bool get_proc0_world()
Determine whether this MPI processor has rank 0 in MANGO's world communicator.
Definition: mpi_partition.cpp:56
mango::Solver::at_least_one_success
bool at_least_one_success
Definition: Solver.hpp:49
mango::Solver::max_function_and_gradient_evaluations
int max_function_and_gradient_evaluations
Definition: Solver.hpp:48
mango::Solver::function_evaluations
int function_evaluations
Definition: Solver.hpp:45
mango::Solver::set_package
virtual void set_package()
Definition: set_package.cpp:34
mango::Solver::best_function_evaluation
int best_function_evaluation
Definition: Solver.hpp:52