OpenMS
GridSearch.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Julianus Pfeuffer $
32 // $Authors: Julianus Pfeuffer $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <array>
38 #include <vector>
39 #include <cmath>
40 #include <tuple>
41 
42 namespace OpenMS
43 {
44  namespace Internal
45  {
46  // The general class template
47  template <size_t param_index, size_t grid_size, typename EvalResult, typename Tuple, typename... TupleTypes>
48  struct Looper
49  {
50  };
51 
52  // Specialization for the base case
53  // - shape_index == shape_size
54  // - TupleTypes is empty here
55  // - All indices in Functor are bound (i.e. can be called with empty arguments)
56  template <size_t grid_size, typename EvalResult, typename Tuple, typename... TupleTypes>
57  struct Looper<grid_size, grid_size, EvalResult, Tuple, TupleTypes...>
58  {
59  template <typename Functor>
60  double operator()(const Tuple&, Functor functor, EvalResult /*bestValue*/, std::array<size_t, grid_size>& /*bestIndices*/)
61  {
62  return functor();
63  }
64  };
65 
66  // Specialization for the loop case
67  // - increment shape_index
68  // - create new Functor with one argument less and the first being bound to it
69  // - loop over all values in the current vector and update best score and best indices
70  template <size_t param_index, size_t grid_size, typename EvalResult, typename Tuple, typename FirstTupleType, typename... TupleTypes>
71  struct Looper<param_index, grid_size, EvalResult, Tuple, FirstTupleType, TupleTypes...>
72  {
73  template <typename Functor>
74  EvalResult operator()(const Tuple& grid, Functor functor, EvalResult bestValue, std::array<size_t, grid_size>& bestIndices)
75  {
76  for (size_t index = 0; index < std::get<param_index>(grid).size(); ++index)
77  {
78  double currVal = Looper<param_index + 1, grid_size, EvalResult, Tuple, TupleTypes...>()
79  (
80  grid,
81  [&grid, index, &functor](TupleTypes... rest){ return functor(std::get<param_index>(grid)[index], rest...);},
82  bestValue,
83  bestIndices
84  );
85 
86  if ( currVal > bestValue )
87  {
88  bestValue = currVal;
89  bestIndices[param_index] = index;
90  }
91  }
92  return bestValue;
93  }
94  };
95  } // namespace Internal
96 
97  template <typename... TupleTypes>
98  class GridSearch
99  {
100  public:
101  explicit GridSearch(std::vector<TupleTypes>... gridValues):
102  grid_(std::make_tuple<std::vector<TupleTypes>...>(std::move(gridValues)...))
103  {}
104 
105  //Specific implementation for function objects
106  template <typename Functor>
107  typename std::result_of<Functor(TupleTypes...)>::type evaluate(Functor evaluator,
108  typename std::result_of<Functor(TupleTypes...)>::type startValue,
109  std::array<size_t,std::tuple_size<std::tuple<std::vector<TupleTypes>...>>::value>& resultIndices)
110  {
111  return Internal::Looper<0,
112  std::tuple_size<std::tuple<std::vector<TupleTypes>...>>::value,
113  typename std::result_of<Functor(TupleTypes...)>::type,
114  std::tuple<std::vector<TupleTypes>...>,
115  TupleTypes...> ()
116  (grid_, evaluator, startValue, resultIndices);
117  }
118 
119 
120  //Specific implementation for function pointers
121  template <typename EvalResult>
122  EvalResult evaluate(EvalResult evaluator(TupleTypes...),
123  EvalResult startValue,
124  std::array<size_t,std::tuple_size<std::tuple<std::vector<TupleTypes>...>>::value>& resultIndices)
125  {
126  return Internal::Looper<0,
127  std::tuple_size<std::tuple<std::vector<TupleTypes>...>>::value,
128  EvalResult,
129  std::tuple<std::vector<TupleTypes>...>,
130  TupleTypes...>()
131  (grid_, evaluator, startValue, resultIndices);
132  }
133 
134 
135  unsigned int getNrCombos()
136  {
137  if (combos_ready_)
138  {
139  return combos_;
140  }
141  else
142  {
143  return nrCombos();
144  }
145  }
146 
147  private:
148  std::tuple<std::vector<TupleTypes>...> grid_;
149  unsigned int combos_ = 1;
150  bool combos_ready_ = false;
151 
152  template<std::size_t I = 0>
153  typename std::enable_if<I == sizeof...(TupleTypes), unsigned int>::type
155  {
156  combos_ready_ = true;
157  return combos_;
158  }
159 
160  template<std::size_t I = 0>
161  typename std::enable_if<I < sizeof...(TupleTypes), unsigned int>::type
162  nrCombos()
163  {
164  combos_ *= std::get<I>(grid_).size();
165  return nrCombos<I + 1>();
166  }
167  };
168 } // namespace OpenMS
169 
Definition: GridSearch.h:99
std::result_of< Functor(TupleTypes...)>::type evaluate(Functor evaluator, typename std::result_of< Functor(TupleTypes...)>::type startValue, std::array< size_t, std::tuple_size< std::tuple< std::vector< TupleTypes >... >>::value > &resultIndices)
Definition: GridSearch.h:107
unsigned int getNrCombos()
Definition: GridSearch.h:135
std::tuple< std::vector< TupleTypes >... > grid_
Definition: GridSearch.h:148
unsigned int combos_
Definition: GridSearch.h:149
bool combos_ready_
Definition: GridSearch.h:150
EvalResult evaluate(EvalResult evaluator(TupleTypes...), EvalResult startValue, std::array< size_t, std::tuple_size< std::tuple< std::vector< TupleTypes >... >>::value > &resultIndices)
Definition: GridSearch.h:122
std::enable_if< I==sizeof...(TupleTypes), unsigned int >::type nrCombos()
Definition: GridSearch.h:154
GridSearch(std::vector< TupleTypes >... gridValues)
Definition: GridSearch.h:101
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48