Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
inspectable.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __INSPECTABLE_H
25 #define __INSPECTABLE_H
26 
27 
28 #include <list>
29 #include <map>
30 #include <utility>
31 #include <string>
32 #include "stl_adds.h"
33 
34 #include <iostream>
35 
36 namespace matrix {
37  class Matrix;
38 }
39 
40 /**
41  * Interface for inspectable objects.
42  * That means that one can read out some internal parameters indentified by string keys
43  *
44  * TODO: support for lead through of params (e.g. getInternalParams())
45  * for all children inspectables. For this use a instance-wide switch (useChildren)
46  * as a member variable to enable this feature when desired.
47 */
48 class Inspectable {
49 public:
50 
51  // TYPEDEFS BEGIN
52 
53  typedef std::string iparamkey;
54  typedef double iparamval;
55  typedef std::pair<iparamkey,iparamval const*> iparampair;
56 
57  // the bool says whether only 4x4AndDiag is used
58  typedef std::pair<iparamkey,std::pair< const matrix::Matrix*, bool > > imatrixpair;
59  typedef std::list<iparamkey> iparamkeylist;
60  typedef std::list<std::string> infoLinesList;
61  typedef std::list<iparamval> iparamvallist;
62  typedef std::list<iparamval const *> iparamvalptrlist;
63  typedef std::list<iparampair> iparampairlist;
64  typedef std::list<imatrixpair> imatrixpairlist;
65 
66  // the network stucture export will be discontinued soon
67  typedef struct ILayer{
68  ILayer(const std::string& vectorname, const std::string& biasname,
69  int dimension, int rank, const std::string& layername)
70  : vectorname(vectorname), biasname(biasname),
71  dimension(dimension), rank(rank), layername(layername) {}
72  std::string vectorname; //< prefix of the internal parameter vector e.g. "v"
73  std::string biasname; ///< prefix of the internal parameter vector used as bias for the neurons e.g. "h"
74  int dimension; ///< length of the vector (number of units)
75  int rank; ///< rank of the layer (0 are input layers)
76  std::string layername; ///< name of the layer as displayed by the visualiser
77  }ILayer;
78 
79  typedef struct IConnection{
80  IConnection(const std::string& matrixname, const std::string& vector1, const std::string& vector2)
81  : matrixname(matrixname), vector1(vector1), vector2(vector2) {}
82  std::string matrixname; ///< matrix name is the prefix of the internal parameter matrix e.g. "A"
83  std::string vector1; ///< vectorname of input layer
84  std::string vector2; ///< vectorname of output layer
85  }IConnection;
86 
87  typedef std::list<ILayer> ilayerlist;
88  typedef std::list<IConnection> iconnectionlist;
89 
90  /// nice predicate function for finding a Layer with its vectorname
91  struct matchName : public std::unary_function<ILayer, bool> {
92  matchName(std::string name) : name(name) {}
93  std::string name;
94  bool operator()(ILayer l) { return l.vectorname == name; }
95  };
96 
97  typedef std::list<const Inspectable*> inspectableList;
98 
99 
100 
101  /// TYPEDEFS END
102 
103 
104  Inspectable(const iparamkey& name = "");
105 
106 
107  virtual ~Inspectable();
108 
109 
110  /** The list of the names of all internal parameters given by getInternalParams().
111  The naming convention is "v[i]" for vectors
112  and "A[i][j]" for matrices, where i, j start at 0.
113  @return: list of keys
114  */
115  virtual iparamkeylist getInternalParamNames() const;
116 
117 
118 /** @return: list of values
119  */
120  virtual iparamvallist getInternalParams() const;
121 
122  // should be protected, but it does not work
123  /**
124  * be careful: matrices will be ignored
125  * @return list of values (pointer)
126  */
127  virtual iparamvalptrlist getInternalParamsPtr() const;
128 
129  /** Specifies which parameter vector forms a structural layer (in terms of a neural network)
130  The ordering is important. The first entry is the input layer and so on.
131  @return: list of layer names with dimension
132 
133  */
134  virtual ilayerlist getStructuralLayers() const;
135 
136 
137  /** Specifies which parameter matrix forms a connection between layers (in terms of a neural network)
138  The orderning is not important.
139  @return: list of layer names with dimension
140  */
142 
143 
144  /**
145  * This is the new style for adding inspectable values. Just call this
146  * function for each parameter and you are done.
147  * registers a single value
148  * @param key the name of the inspectable, shown e.g. in guilogger (no spaces allowed)
149  * @param val the address of the value to inspect
150  * @param descr description string to be exported (using infolines)
151  */
152  virtual void addInspectableValue(const iparamkey& key, iparamval const * val,
153  const std::string& descr = std::string());
154 
155 
156  /**
157  * This is the new style for adding inspectable values. Just call this
158  * function for each parameter and you are done.
159  * Inspects elements of the given matrix or vector (automatic detection)
160  * For Matrixes Either all or only the values given by
161  * store4x4AndDiagonalFieldNames(Matrix& m,string& matrixName);
162  * are used.
163  * @param key the name of the matrix, shown e.g. in guilogger (no spaces allowed)
164  * @param m the address of the matrix to inspect
165  * @param only4x4AndDiag of true only 4x4 matrix plus the diagonal is used
166  * @param descr description string to be exported
167  * (for the entire matrix with key_) (using infolines)
168  * @note that you can change the structure of the matrix while
169  * being inspected, but no after the getInternalParameterNames is called!
170  */
171  virtual void addInspectableMatrix(const iparamkey& key, const matrix::Matrix* m,
172  bool only4x4AndDiag=true,
173  const std::string& descr = std::string());
174 
175  /**
176  * adds a description for the given parameter using info-lines
177  * The line will start (appart from the #I) with a D for description
178  * followed by the key end then followed by the string.
179  * @param key parameter name (no spaces allowed)
180  * @param descr descriptive string (no newlines allowed)
181  */
182  virtual void addInspectableDescription(const iparamkey& key, const std::string& descr);
183 
184 
185  /**
186  * Adds an info line to this inspectable instance. All infolines are plotted
187  * by the PlotOptionEngine and therefore appear e.g. in the logfile.
188  * They are leaded by a #I. If you have your own identifiers, just begin with
189  * your one (e.g. N for number of
190  * NOTE: You must not add e.g. carriage return, all this is handled by the
191  * PlotOptionsEngine itself.
192  * If you like to add multiple lines, call this function a lot more or
193  * use instead addInfoLines.
194  * @param infoLine the line (as string) to be added
195  */
196  virtual void addInfoLine(std::string infoLine);
197 
198  /**
199  * Adds a bunch of infolines with addInfoLine to this inspectable instance.
200  * Every infoline is separated by an carriage return automatically, done
201  * by the PlotOptionsEngine.
202  * @param infoLineList the infoLines to be added as a string list.
203  */
204  virtual void addInfoLines(std::list<std::string> infoLineList);
205 
206  /**
207  * Removes all infolines from this inspectable instance.
208  * This is useful if you have new infoLines and would like to restart
209  * the PlotOptionEngine.
210  */
211  virtual void removeInfoLines();
212 
213  /**
214  * Returns all infolines added to this inspectable instance.
215  * This function is called by the PlotOptionEngine.
216  * @return all added infolines.
217  */
218  virtual const infoLinesList& getInfoLines() const;
219 
220  /**
221  * Adds an inspectable as a child object.
222  * @param insp the instance to add
223  */
224  virtual void addInspectable(Inspectable* insp);
225 
226  /**
227  * Removes an inspectable as a child object.
228  * @param insp the instance to remove
229  */
230  virtual void removeInspectable(Inspectable* insp);
231 
232 
233  /// set the name of the inspectable
234  virtual void setNameOfInspectable(const iparamkey& name);
235 
236  /// return the name of the inspectable, getName() would conflict with Configurable::getName() too often
237  virtual const iparamkey getNameOfInspectable() const;
238 
239  /**
240  * Returns the list containing all inspectable children.
241  */
242  virtual const inspectableList& getInspectables() const;
243 
244  /* added when needed
245  virtual void removeInspectable(const Inspectable* insp);
246 
247  virtual void removeAllInspectables();
248  */
249 
250 protected:
252 
255 
257 
258 private:
259  inspectableList listOfInspectableChildren;
260  bool printParentName;
261  Inspectable* parent;
262 
263 
264 };
265 
266 #endif
Matrix type.
Definition: matrix.h:65
std::list< IConnection > iconnectionlist
Definition: inspectable.h:88
Inspectable(const iparamkey &name="")
TYPEDEFS END.
Definition: inspectable.cpp:30
std::string vector1
vectorname of input layer
Definition: inspectable.h:83
std::pair< iparamkey, iparamval const * > iparampair
Definition: inspectable.h:55
std::list< imatrixpair > imatrixpairlist
Definition: inspectable.h:64
std::list< std::string > infoLinesList
Definition: inspectable.h:60
Matrixd Matrix
Definition: osgforwarddecl.h:47
std::string vectorname
Definition: inspectable.h:72
iparamkey name
Definition: inspectable.h:251
virtual ~Inspectable()
Definition: inspectable.cpp:28
struct Inspectable::IConnection IConnection
virtual const inspectableList & getInspectables() const
Returns the list containing all inspectable children.
Definition: inspectable.cpp:148
virtual void addInspectableDescription(const iparamkey &key, const std::string &descr)
adds a description for the given parameter using info-lines The line will start (appart from the #I) ...
Definition: inspectable.cpp:99
std::list< iparamval const * > iparamvalptrlist
Definition: inspectable.h:62
std::string iparamkey
Definition: inspectable.h:53
virtual void removeInfoLines()
Removes all infolines from this inspectable instance.
Definition: inspectable.cpp:123
std::string biasname
prefix of the internal parameter vector used as bias for the neurons e.g. "h"
Definition: inspectable.h:73
IConnection(const std::string &matrixname, const std::string &vector1, const std::string &vector2)
Definition: inspectable.h:80
Definition: inspectable.h:79
std::string matrixname
matrix name is the prefix of the internal parameter matrix e.g. "A"
Definition: inspectable.h:82
std::list< const Inspectable * > inspectableList
Definition: inspectable.h:97
virtual iparamvallist getInternalParams() const
Definition: inspectable.cpp:52
Definition: inspectable.h:67
virtual void addInspectableMatrix(const iparamkey &key, const matrix::Matrix *m, bool only4x4AndDiag=true, const std::string &descr=std::string())
This is the new style for adding inspectable values.
Definition: inspectable.cpp:92
virtual void addInspectable(Inspectable *insp)
Adds an inspectable as a child object.
Definition: inspectable.cpp:128
matchName(std::string name)
Definition: inspectable.h:92
iparampairlist mapOfValues
Definition: inspectable.h:253
ILayer(const std::string &vectorname, const std::string &biasname, int dimension, int rank, const std::string &layername)
Definition: inspectable.h:68
virtual void addInfoLine(std::string infoLine)
Adds an info line to this inspectable instance.
Definition: inspectable.cpp:104
std::string layername
name of the layer as displayed by the visualiser
Definition: inspectable.h:76
infoLinesList infoLineStringList
Definition: inspectable.h:256
Interface for inspectable objects.
Definition: inspectable.h:48
std::list< iparampair > iparampairlist
Definition: inspectable.h:63
std::string vector2
vectorname of output layer
Definition: inspectable.h:84
std::string name
Definition: inspectable.h:93
virtual const infoLinesList & getInfoLines() const
Returns all infolines added to this inspectable instance.
Definition: inspectable.cpp:118
double iparamval
Definition: inspectable.h:54
virtual ilayerlist getStructuralLayers() const
Specifies which parameter vector forms a structural layer (in terms of a neural network) The ordering...
Definition: inspectable.cpp:77
virtual iparamvalptrlist getInternalParamsPtr() const
be careful: matrices will be ignored
Definition: inspectable.cpp:67
int rank
rank of the layer (0 are input layers)
Definition: inspectable.h:75
struct Inspectable::ILayer ILayer
virtual const iparamkey getNameOfInspectable() const
return the name of the inspectable, getName() would conflict with Configurable::getName() too often ...
Definition: inspectable.cpp:143
virtual iconnectionlist getStructuralConnections() const
Specifies which parameter matrix forms a connection between layers (in terms of a neural network) The...
Definition: inspectable.cpp:81
int dimension
length of the vector (number of units)
Definition: inspectable.h:74
virtual iparamkeylist getInternalParamNames() const
The list of the names of all internal parameters given by getInternalParams().
Definition: inspectable.cpp:33
imatrixpairlist mapOfMatrices
Definition: inspectable.h:254
std::list< ILayer > ilayerlist
Definition: inspectable.h:87
virtual void setNameOfInspectable(const iparamkey &name)
set the name of the inspectable
Definition: inspectable.cpp:138
std::list< iparamkey > iparamkeylist
Definition: inspectable.h:59
std::list< iparamval > iparamvallist
Definition: inspectable.h:61
std::pair< iparamkey, std::pair< const matrix::Matrix *, bool > > imatrixpair
Definition: inspectable.h:58
bool operator()(ILayer l)
Definition: inspectable.h:94
nice predicate function for finding a Layer with its vectorname
Definition: inspectable.h:91
virtual void addInspectableValue(const iparamkey &key, iparamval const *val, const std::string &descr=std::string())
This is the new style for adding inspectable values.
Definition: inspectable.cpp:85
virtual void addInfoLines(std::list< std::string > infoLineList)
Adds a bunch of infolines with addInfoLine to this inspectable instance.
Definition: inspectable.cpp:110
virtual void removeInspectable(Inspectable *insp)
Removes an inspectable as a child object.
Definition: inspectable.cpp:133