CMAC.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief -
5  *
6  * \author O. Krause
7  * \date 2010-01-01
8  *
9  *
10  * \par Copyright 1995-2015 Shark Development Team
11  *
12  * <BR><HR>
13  * This file is part of Shark.
14  * <http://image.diku.dk/shark/>
15  *
16  * Shark is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Lesser General Public License as published
18  * by the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * Shark is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public License
27  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 
31 #ifndef SHARK_ML_MODEL_CMAC_H
32 #define SHARK_ML_MODEL_CMAC_H
33 
34 #include <shark/Core/DLLSupport.h>
36 #include <shark/Rng/GlobalRng.h>
37 #include <vector>
38 
39 namespace shark{
40 
41 //!
42 //! \brief The CMACMap class represents a linear combination of piecewise constant functions
43 //!
44 //! when a point is fed into the CMAC, it is first mapped into a vector of binary features.
45 //! For this purpose the inputspace is divided into several tilings. Every tiling produces a bitstring where an element
46 //! is 1 if the point lies inside the tile, 0 otherwise. The concatenation of all tilings forms the feature vector which is then fed
47 //! into a linear function.
48 //! Usually the CMAC is only good for low dimensional input data since the size of the featurevector grows exponentially with the
49 //! number of dimensions.
50 //!
51 class CMACMap :public AbstractModel<RealVector,RealVector>{
52 protected:
53  ///offset of the position of every tiling
54  RealMatrix m_offset;
55 
56  ///coordinate offset for every dimension in the Array
57  std::vector<std::size_t> m_dimOffset;
58 
59  ///lower bound and tileWidth for every Dimension
60  RealMatrix m_tileBounds;
61 
62  ///number of tilings
63  std::size_t m_tilings;
64  std::size_t m_parametersPerTiling;
65 
66  std::size_t m_inputSize;
67  std::size_t m_outputSize;
68 
69  ///The parameters of the model
70  RealVector m_parameters;
71 
72  ///calculates the index in the parameter vector for the activated feature in the tiling
73  SHARK_EXPORT_SYMBOL std::size_t getArrayIndexForTiling(std::size_t indexOfTiling,RealVector const& point)const;
74  ///returns an index in the parameter array for each activated feature
75  SHARK_EXPORT_SYMBOL std::vector<std::size_t> getIndizes(ConstRealMatrixRow const& point)const;
76 public:
77  ///\brief construct the CMAC
79 
80  /// \brief From INameable: return the class name.
81  std::string name() const
82  { return "CMACMap"; }
83 
84  ///\brief initializes the structure of the cmac. it uses the same lower and upper bound for every input dimension. default is [0,1]
85  ///
86  ///\param inputs number of input dimensions
87  ///\param outputs number of output dimensions
88  ///\param numberOfTilings number of Tilings to be created
89  ///\param numberOfTiles amount of tiles per dimension
90  ///\param lower lower bound of input values
91  ///\param upper upper bound of input values
92  ///\param randomTiles flag specifying whether distance between tiles is regular or randomized
93  SHARK_EXPORT_SYMBOL void setStructure(std::size_t inputs, std::size_t outputs, std::size_t numberOfTilings, std::size_t numberOfTiles, double lower = 0., double upper = 1.,bool randomTiles = false);
94 
95  ///\brief initializes the structure of the cmac
96  ///
97  ///\param inputs number of input dimensions
98  ///\param outputs number of output dimensions
99  ///\param numberOfTilings number of Tilings to be created
100  ///\param numberOfTiles amount of tiles per dimension
101  ///\param bounds lower and upper bounts for every input dimension. every row consists of (lower,upper)
102  ///\param randomTiles flag specifying whether distance between tiles is regular or randomized
103  SHARK_EXPORT_SYMBOL void setStructure(std::size_t inputs, std::size_t outputs, std::size_t numberOfTilings, std::size_t numberOfTiles, RealMatrix const& bounds,bool randomTiles = false);
104 
105  virtual std::size_t inputSize()const
106  {
107  return m_inputSize;
108  }
109  virtual std::size_t outputSize()const
110  {
111  return m_outputSize;
112  }
113 
114  virtual RealVector parameterVector()const
115  {
116  return m_parameters;
117  }
118  virtual void setParameterVector(RealVector const& newParameters)
119  {
120  SIZE_CHECK(numberOfParameters() == newParameters.size());
121  m_parameters=newParameters;
122  }
123  virtual std::size_t numberOfParameters()const
124  {
125  return m_parameters.size();
126  }
127 
128  boost::shared_ptr<State> createState()const{
129  return boost::shared_ptr<State>(new EmptyState());
130  }
131 
133  SHARK_EXPORT_SYMBOL void eval(const RealMatrix& patterns,RealMatrix& outputs)const;
134  void eval(const RealMatrix& patterns,RealMatrix& outputs, State& state)const{
135  eval(patterns,outputs);
136  }
138  RealMatrix const& pattern,
139  RealMatrix const& coefficients,
140  State const& state,
141  RealVector& gradient)const;
142 
143  /// From ISerializable, reads a model from an archive
144  SHARK_EXPORT_SYMBOL void read( InArchive & archive );
145 
146  /// From ISerializable, writes a model to an archive
147  SHARK_EXPORT_SYMBOL void write( OutArchive & archive ) const;
148 };
149 
150 
151 }
152 #endif