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-2017 Shark Development Team
11 *
12 * <BR><HR>
13 * This file is part of Shark.
14 * <https://shark-ml.github.io/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
36#include <shark/Core/Random.h>
37#include <vector>
38
39namespace 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/// \ingroup models
52class CMACMap :public AbstractModel<RealVector,RealVector>{
53protected:
54 ///offset of the position of every tiling
55 RealMatrix m_offset;
56
57 ///coordinate offset for every dimension in the Array
58 std::vector<std::size_t> m_dimOffset;
59
60 ///lower bound and tileWidth for every Dimension
61 RealMatrix m_tileBounds;
62
63 ///number of tilings
64 std::size_t m_tilings;
66
68 std::size_t m_inputSize;
70
71 ///The parameters of the model
72 RealVector m_parameters;
73
74 ///calculates the index in the parameter vector for the activated feature in the tiling
75 SHARK_EXPORT_SYMBOL std::size_t getArrayIndexForTiling(std::size_t indexOfTiling,RealVector const& point)const;
76 ///returns an index in the parameter array for each activated feature
77 SHARK_EXPORT_SYMBOL std::vector<std::size_t> getIndizes(blas::dense_vector_adaptor<double const> const& point)const;
78public:
79 ///\brief construct the CMAC
81
82 /// \brief From INameable: return the class name.
83 std::string name() const
84 { return "CMACMap"; }
85
86 ///\brief initializes the structure of the cmac. it uses the same lower and upper bound for every input dimension. default is [0,1]
87 ///
88 ///\param inputs Shape of the input dimensions
89 ///\param outputs Shape of the input dimensions
90 ///\param numberOfTilings number of Tilings to be created
91 ///\param numberOfTiles amount of tiles per dimension
92 ///\param lower lower bound of input values
93 ///\param upper upper bound of input values
94 ///\param randomTiles flag specifying whether distance between tiles is regular or randomized
95 SHARK_EXPORT_SYMBOL void setStructure(Shape const& inputs, Shape const& outputs, std::size_t numberOfTilings, std::size_t numberOfTiles, double lower = 0., double upper = 1.,bool randomTiles = false);
96
97 ///\brief initializes the structure of the cmac
98 ///
99 ///\param inputs number of input dimensions
100 ///\param outputs number of output dimensions
101 ///\param numberOfTilings number of Tilings to be created
102 ///\param numberOfTiles amount of tiles per dimension
103 ///\param bounds lower and upper bounts for every input dimension. every row consists of (lower,upper)
104 ///\param randomTiles flag specifying whether distance between tiles is regular or randomized
105 SHARK_EXPORT_SYMBOL void setStructure(Shape const& inputs, Shape const& outputs, std::size_t numberOfTilings, std::size_t numberOfTiles, RealMatrix const& bounds,bool randomTiles = false);
106
107 ///\brief Returns the expected shape of the input
109 return m_inputShape;
110 }
111 ///\brief Returns the shape of the output
113 return m_outputShape;
114 }
115
116 virtual RealVector parameterVector()const{
117 return m_parameters;
118 }
119 virtual void setParameterVector(RealVector const& newParameters){
120 SIZE_CHECK(numberOfParameters() == newParameters.size());
121 m_parameters=newParameters;
122 }
123 virtual std::size_t numberOfParameters()const{
124 return m_parameters.size();
125 }
126
127 boost::shared_ptr<State> createState()const{
128 return boost::shared_ptr<State>(new EmptyState());
129 }
130
131 using AbstractModel<RealVector,RealVector>::eval;
132 SHARK_EXPORT_SYMBOL void eval(const RealMatrix& patterns,RealMatrix& outputs)const;
133 void eval(const RealMatrix& patterns,RealMatrix& outputs, State& state)const{
134 eval(patterns,outputs);
135 }
137 RealMatrix const& pattern,
138 BatchOutputType const& outputs,
139 RealMatrix const& coefficients,
140 State const& state,
141 RealVector& gradient)const;
142
143 /// From ISerializable, reads a model from an 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