optimize.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 
28 double mango::Solver::optimize(MPI_Partition* mpi_partition_in) {
29  // This subroutine is run only for non-least-squares optimizaiton.
30  // It carries out the part of the optimization that is not specific to any one package.
31 
32  mpi_partition = mpi_partition_in;
33  bool proc0_world = mpi_partition->get_proc0_world();
34  if (proc0_world && verbose > 0) std::cout << "Hello world from optimize()" << std::endl;
35 
37 
38  if (algorithms[algorithm].uses_derivatives && !proc0_world) {
39  // All group leaders that are not proc0_world do group_leaders_loop(), then return.
41  return(std::numeric_limits<double>::quiet_NaN());
42  }
43 
44  // proc0_world always continues past this point.
45  // For finite-difference-derivative algorithms, the other procs do not go past this point.
46  // For parallel algorithms that do not use finite-difference derivatives, such as HOPSPACK, the other group leader procs DO continue past this point.
47 
48  if (algorithms[algorithm].least_squares)
49  throw std::runtime_error("Error! An algorithm for least-squares problems was chosen, but the problem specified is not least-squares.");
50 
51  // Hand control over to one of the concrete Packages to carry out the main work of the optimization.
52  package->optimize(this);
53 
54  if (!proc0_world) return(std::numeric_limits<double>::quiet_NaN());
55  // Only proc0_world continues past this point.
56 
57  // Tell the other group leaders to exit.
58  int data = -1;
59  MPI_Bcast(&data,1,MPI_INT,0,mpi_partition->get_comm_group_leaders());
60 
61  memcpy(state_vector, best_state_vector, N_parameters * sizeof(double)); // Make sure we leave state_vector equal to the best state vector seen.
62 
63  recorder->finalize();
64 
65  if (verbose > 0) {
66  std::cout << "Here comes the optimal state_vector from optimize.cpp: " << state_vector[0];
67  for (int j=1; j<N_parameters; j++) {
68  std::cout << ", " << state_vector[j];
69  }
70  std::cout << std::endl;
71  }
72 
74 }
mango::Solver::package
Package * package
Definition: Solver.hpp:57
mango::Solver::verbose
int verbose
Definition: Solver.hpp:63
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
mango::Package::optimize
virtual void optimize(Solver *)=0
Solver.hpp
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::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::mpi_partition
MPI_Partition * mpi_partition
Definition: Solver.hpp:65
mango.hpp
mango::Solver::N_parameters
int N_parameters
Definition: Solver.hpp:43
mango::Solver::optimize
virtual double optimize(MPI_Partition *)
Definition: optimize.cpp:28
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::MPI_Partition
A class for dividing the set of MPI processes into worker groups.
Definition: mango.hpp:195
mango::Solver::group_leaders_loop
virtual void group_leaders_loop()
Definition: group_leaders_loop.cpp:23
mango::Recorder::finalize
virtual void finalize()
Definition: Recorder.hpp:31