OpenMS  2.7.0
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-2021.
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 <iostream>
39 #include <vector>
40 #include <cmath>
41 #include <tuple>
42 
43 namespace OpenMS
44 {
45  namespace Internal
46  {
47  // The general class template
48  template <size_t param_index, size_t grid_size, typename EvalResult, typename Tuple, typename... TupleTypes>
49  struct Looper
50  {
51  };
52 
53  // Specialization for the base case
54  // - shape_index == shape_size
55  // - TupleTypes is empty here
56  // - All indices in Functor are bound (i.e. can be called with empty arguments)
57  template <size_t grid_size, typename EvalResult, typename Tuple, typename... TupleTypes>
58  struct Looper<grid_size, grid_size, EvalResult, Tuple, TupleTypes...>
59  {
60  template <typename Functor>
61  double operator()(const Tuple&, Functor functor, EvalResult /*bestValue*/, std::array<size_t, grid_size>& /*bestIndices*/)
62  {
63  return functor();
64  }
65  };
66 
67  // Specialization for the loop case
68  // - increment shape_index
69  // - create new Functor with one argument less and the first being bound to it
70  // - loop over all values in the current vector and update best score and best indices
71  template <size_t param_index, size_t grid_size, typename EvalResult, typename Tuple, typename FirstTupleType, typename... TupleTypes>
72  struct Looper<param_index, grid_size, EvalResult, Tuple, FirstTupleType, TupleTypes...>
73  {
74  template <typename Functor>
75  EvalResult operator()(const Tuple& grid, Functor functor, EvalResult bestValue, std::array<size_t, grid_size>& bestIndices)
76  {
77  for (size_t index = 0; index < std::get<param_index>(grid).size(); ++index)
78  {
79  double currVal = Looper<param_index + 1, grid_size, EvalResult, Tuple, TupleTypes...>()
80  (
81  grid,
82  [&grid, index, &functor](TupleTypes... rest){ return functor(std::get<param_index>(grid)[index], rest...);},
83  bestValue,
84  bestIndices
85  );
86 
87  if ( currVal > bestValue )
88  {
89  bestValue = currVal;
90  bestIndices[param_index] = index;
91  }
92  }
93  return bestValue;
94  }
95  };
96  } // namespace Internal
97 
98  template <typename... TupleTypes>
99  class GridSearch
100  {
101  public:
102  explicit GridSearch(std::vector<TupleTypes>... gridValues):
103  grid_(std::make_tuple<std::vector<TupleTypes>...>(std::move(gridValues)...))
104  {}
105 
106  //Specific implementation for function objects
107  template <typename Functor>
108  typename std::result_of<Functor(TupleTypes...)>::type evaluate(Functor evaluator,
109  typename std::result_of<Functor(TupleTypes...)>::type startValue,
110  std::array<size_t,std::tuple_size<std::tuple<std::vector<TupleTypes>...>>::value>& resultIndices)
111  {
112  return Internal::Looper<0,
113  std::tuple_size<std::tuple<std::vector<TupleTypes>...>>::value,
114  typename std::result_of<Functor(TupleTypes...)>::type,
115  std::tuple<std::vector<TupleTypes>...>,
116  TupleTypes...> ()
117  (grid_, evaluator, startValue, resultIndices);
118  }
119 
120 
121  //Specific implementation for function pointers
122  template <typename EvalResult>
123  EvalResult evaluate(EvalResult evaluator(TupleTypes...),
124  EvalResult startValue,
125  std::array<size_t,std::tuple_size<std::tuple<std::vector<TupleTypes>...>>::value>& resultIndices)
126  {
127  return Internal::Looper<0,
128  std::tuple_size<std::tuple<std::vector<TupleTypes>...>>::value,
129  EvalResult,
130  std::tuple<std::vector<TupleTypes>...>,
131  TupleTypes...>()
132  (grid_, evaluator, startValue, resultIndices);
133  }
134 
135 
136  unsigned int getNrCombos()
137  {
138  if (combos_ready_)
139  {
140  return combos_;
141  }
142  else
143  {
144  return nrCombos();
145  }
146  }
147 
148  private:
149  std::tuple<std::vector<TupleTypes>...> grid_;
150  unsigned int combos_ = 1;
151  bool combos_ready_ = false;
152 
153  template<std::size_t I = 0>
154  typename std::enable_if<I == sizeof...(TupleTypes), unsigned int>::type
156  {
157  combos_ready_ = true;
158  return combos_;
159  }
160 
161  template<std::size_t I = 0>
162  typename std::enable_if<I < sizeof...(TupleTypes), unsigned int>::type
163  nrCombos()
164  {
165  combos_ *= std::get<I>(grid_).size();
166  return nrCombos<I + 1>();
167  }
168  };
169 } // namespace OpenMS
170 
Definition: GridSearch.h:100
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:108
unsigned int getNrCombos()
Definition: GridSearch.h:136
std::tuple< std::vector< TupleTypes >... > grid_
Definition: GridSearch.h:149
unsigned int combos_
Definition: GridSearch.h:150
bool combos_ready_
Definition: GridSearch.h:151
EvalResult evaluate(EvalResult evaluator(TupleTypes...), EvalResult startValue, std::array< size_t, std::tuple_size< std::tuple< std::vector< TupleTypes >... >>::value > &resultIndices)
Definition: GridSearch.h:123
std::enable_if< I==sizeof...(TupleTypes), unsigned int >::type nrCombos()
Definition: GridSearch.h:155
GridSearch(std::vector< TupleTypes >... gridValues)
Definition: GridSearch.h:102
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47