algorithms.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 <string>
21 #include <stdexcept>
22 #include "mango.hpp"
23 #include "Solver.hpp"
24 
26  if (algorithm_in < 0) throw std::runtime_error("Error in mango::Problem::set_algorithm. Algorithm cannot be negative.");
27  if (algorithm_in >= NUM_ALGORITHMS) throw std::runtime_error("Error in mango::Problem::set_algorithm. Algorithm is too large.");
28 
29  solver->algorithm = algorithm_in;
30 
31  if (solver->verbose > 0) std::cout << "Algorithm set (by integer) to " << algorithm_in << ", a.k.a. " << algorithms[algorithm_in].name << std::endl;
32 }
33 
34 ///////////////////////////////////////////////////////////////////////////
35 
36 void mango::Problem::set_algorithm(std::string str) {
37  algorithm_type algorithm;
38  bool found_match = get_algorithm(str, &algorithm);
39  if (!found_match) {
40  std::cerr << "Error in mango::Problem::set_algorithm. The following algorithm name was requested but not found: " << str << std::endl;
41  throw std::runtime_error("Error in mango::Problem::set_algorithm: The requested algorithm name was not found.");
42  }
43  solver->algorithm = algorithm;
44  if (solver->verbose > 0) std::cout << "Algorithm set (by string) to " << algorithm << ", a.k.a. " << algorithms[algorithm].name << std::endl;
45 }
46 
47 ///////////////////////////////////////////////////////////////////////////
48 
49 bool mango::get_algorithm(std::string str, algorithm_type* code) {
50  // The return value of this function indicates whether an algorithm with the requested name str was found.
51  // If a match is found, the corresponding index into the algorithms[] database is returned in the parameter code.
52 
53  bool found_match = false;
54  int j;
55  for (j = 0; j < NUM_ALGORITHMS; j++) {
56  if (algorithms[j].name.compare(str) == 0) {
57  found_match = true;
58  break;
59  }
60  }
61 
62  if (found_match) {
63  *code = (algorithm_type) j;
64  return(true);
65  } else {
66  return(false);
67  }
68 }
69 
70 ///////////////////////////////////////////////////////////////////////////
71 
72 bool mango::does_algorithm_exist(std::string str) {
73  algorithm_type algorithm_code;
74  return(get_algorithm(str,&algorithm_code));
75 }
mango::Problem::solver
Solver * solver
Definition: mango.hpp:446
mango::Solver::verbose
int verbose
Definition: Solver.hpp:63
mango::get_algorithm
bool get_algorithm(std::string name, algorithm_type *algorithm_int)
Returns the integer (enum) for an optimization algorithm associated with its string name.
Definition: algorithms.cpp:49
mango::does_algorithm_exist
bool does_algorithm_exist(std::string algorithm_name)
Checks whether or not a string corresponds to the name of one of the optimization algorithms known by...
Definition: algorithms.cpp:72
mango::algorithm_type
algorithm_type
A list of the algorithms that MANGO can potentially use.
Definition: mango.hpp:75
Solver.hpp
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::Problem::set_algorithm
void set_algorithm(algorithm_type algorithm)
Sets the optimization algorithm.
Definition: algorithms.cpp:25
mango.hpp
mango::Solver::algorithm
algorithm_type algorithm
Definition: Solver.hpp:42
mango::algorithm_properties::name
std::string name
A short string with the algorithm's name, e.g. 'petsc_pounders' or 'hopspack'.
Definition: mango.hpp:59
mango::NUM_ALGORITHMS
@ NUM_ALGORITHMS
Not an actual algorithm, just counting.
Definition: mango.hpp:117