MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
OptimizationArray.hpp
Go to the documentation of this file.
1 
25 #ifndef OPTIMIZATIONARRAY_HPP_
26 #define OPTIMIZATIONARRAY_HPP_
27 
28 #include <string>
29 #include <map>
30 #include <stdio.h>
31 
32 
33 // Forward declaration of class boost::serialization::access
34 namespace boost {
35 namespace serialization {
36 class access;
37 }//: serialization
38 }//: access
39 
40 namespace mic {
41 namespace neural_nets {
42 namespace optimization {
43 
44 
49 template<typename T>
51 public:
52 
56  OptimizationArray() = default;
57 
65  template <typename opT>
66  void add( std::string name_, size_t rows_, size_t cols_){
67  keys_map[name_] = functions.size();
68  functions.push_back ( std::make_shared< opT > ( opT ( rows_, cols_ ) ) );
69 
70  }
71 
72  template <typename opT>
73  void add( std::string name_, std::shared_ptr<opT> opt_){
74  keys_map[name_] = functions.size();
75  functions.push_back ( opt_ );
76  }
77 
78  void clear(){
79  keys_map.clear();
80  functions.clear();
81  }
82 
83 
89  std::shared_ptr<mic::neural_nets::optimization::OptimizationFunction<T> > & operator[] ( size_t number_ ) {
90  // TODO: throw exception when out of the scope?
91 
92  return functions[number_];
93 
94  }
95 
101  std::shared_ptr<mic::neural_nets::optimization::OptimizationFunction<T> > & operator[] ( char key_ ) {
102  // TODO: throw exception when out of the scope?
103 
104  return ( *this ) [std::string ( 1, key_ )];
105 
106  }
107 
113  std::shared_ptr<mic::neural_nets::optimization::OptimizationFunction<T> > & operator[] ( std::string key ) {
114 
115  if ( keys_map.find ( key ) == keys_map.end() )
116  std::cout << "Warning !!! " <<
117  "::[] - key not found:" << key << std::endl;
118 
119  // TODO: throw exception when out of the scope?
120 
121  return functions[keys_map[key]];
122 
123  }
124 
130  friend std::ostream& operator<<(std::ostream& os_, const mic::neural_nets::optimization::OptimizationArray<T> & obj_) {
131  // Display name
132  for (auto& i: obj_.keys_map) {
133  // Display elements.
134  os_ << "[" << i.first << "] \n";
135 // os_ << (*obj_.functions[i.second]) << std::endl;
136  }
137  return os_;
138  }
139 
143  std::map<std::string, size_t> keys() {
144  return keys_map;
145  }
146 
150  std::string size() {
151  return functions.size();
152  }
153 
154 protected:
156  std::vector<std::shared_ptr< mic::neural_nets::optimization::OptimizationFunction<T> > > functions;
157 
158 
160  std::map<std::string, size_t> keys_map;
161 
162 private:
163  // Friend class - required for using boost serialization.
164  friend class boost::serialization::access;
165 
166 };
167 
168 
169 } /* namespace optimzation */
170 } /* namespace neural_nets */
171 } /* namespace mic */
172 
173 
174 #endif /* OPTIMIZATIONARRAY_HPP_ */
std::shared_ptr< mic::neural_nets::optimization::OptimizationFunction< T > > & operator[](size_t number_)
void add(std::string name_, std::shared_ptr< opT > opt_)
void add(std::string name_, size_t rows_, size_t cols_)
std::vector< std::shared_ptr< mic::neural_nets::optimization::OptimizationFunction< T > > > functions
Vector of pointers of optimization functions.
A dynamic array of optimization functions (a hash-table).
std::map< std::string, size_t > keys_map
Vector of keys of consecutive functions in the array.