Robot Simulator of the Robotics Group for Self-Organization of Control: inspectable.h Source File

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages
- Examples

- Search for |

- File List
- File Members

# inspectable.h

Go to the documentation of this file.

    00001 /***************************************************************************
    00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
    00003  *    martius@informatik.uni-leipzig.de                                    *
    00004  *    fhesse@informatik.uni-leipzig.de                                     *
    00005  *    der@informatik.uni-leipzig.de                                        *
    00006  *    guettler@informatik.uni-leipzig.de                                   *
    00007  *    joergweide84@aol.com (robot12)                                       *
    00008  *                                                                         *
    00009  *   This program is free software; you can redistribute it and/or modify  *
    00010  *   it under the terms of the GNU General Public License as published by  *
    00011  *   the Free Software Foundation; either version 2 of the License, or     *
    00012  *   (at your option) any later version.                                   *
    00013  *                                                                         *
    00014  *   This program is distributed in the hope that it will be useful,       *
    00015  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
    00016  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
    00017  *   GNU General Public License for more details.                          *
    00018  *                                                                         *
    00019  *   You should have received a copy of the GNU General Public License     *
    00020  *   along with this program; if not, write to the                         *
    00021  *   Free Software Foundation, Inc.,                                       *
    00022  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
    00023  *                                                                         *
    00024  *   $Log: inspectable.h,v $
    00025  *   Revision 1.8  2009/10/14 09:58:29  martius
    00026  *   added support for description strings that are exported using the infolines
    00027  *
    00028  *   Revision 1.7  2009/08/10 07:37:48  guettler
    00029  *   -Inspectable interface now supports to add infoLines itself.
    00030  *    These lines are then outprinted line by line to the PlotOption once,
    00031  *    preceded by a #I.
    00032  *   -Restart functionality of PlotOptionEngine added (e.g. closePipes(), reInit()).
    00033  *
    00034  *   Revision 1.6  2009/08/05 08:19:23  martius
    00035  *   addInspectableMatrix allows to specify optionally whether all or only 4x4+Diagonal is used
    00036  *
    00037  *   Revision 1.5  2009/07/15 13:01:15  robot12
    00038  *   one bugfixe
    00039  *
    00040  *   Revision 1.4  2009/06/29 13:24:13  robot12
    00041  *   add function getInternalParamsPtr for new class inspectableproxy
    00042  *
    00043  *   Revision 1.3  2008/08/01 14:42:03  guettler
    00044  *   we try the trip to hell! make selforg AVR compatible...good luck (first changes)
    00045  *
    00046  *   Revision 1.2  2008/04/29 09:55:30  guettler
    00047  *   -class uses now a list of pairs instead of a map
    00048  *   -debug printouts removed
    00049  *
    00050  *   Revision 1.1  2008/04/29 07:39:24  guettler
    00051  *   -interfaces moved to selforg/utils
    00052  *   -added addInspectableValue and addInspectableMatrix
    00053  *   -methods getInternalParamNames and getInternalParams do not need to be
    00054  *   overloaded anymore, use addInspectableValue and addInspectableMatrix
    00055  *   instead (preferred)
    00056  *
    00057  *   Revision 1.9  2006/12/21 11:44:17  martius
    00058  *   commenting style for doxygen //< -> ///<
    00059  *   FOREACH and FOREACHC are macros for collection iteration
    00060  *
    00061  *   Revision 1.8  2006/07/20 17:14:34  martius
    00062  *   removed std namespace from matrix.h
    00063  *   storable interface
    00064  *   abstract model and invertablemodel as superclasses for networks
    00065  *
    00066  *   Revision 1.7  2006/07/14 12:23:58  martius
    00067  *   selforg becomes HEAD
    00068  *
    00069  *   Revision 1.5.6.1  2006/06/25 16:51:35  martius
    00070  *   configureable has name and revision
    00071  *   a robot is configureable by default
    00072  *
    00073  *   Revision 1.5  2005/10/27 15:46:38  martius
    00074  *   inspectable interface is expanded to structural information for network visualiser
    00075  *
    00076  *   Revision 1.4  2005/10/06 17:06:57  martius
    00077  *   switched to stl lists
    00078  *
    00079  *   Revision 1.3  2005/09/11 11:20:01  martius
    00080  *   virtual destructor
    00081  *
    00082  *   Revision 1.2  2005/08/06 20:47:54  martius
    00083  *   Commented
    00084  *
    00085  *   Revision 1.1  2005/08/03 20:28:57  martius
    00086  *   inspectable interface
    00087  *
    00088  *                                                                 *
    00089  ***************************************************************************/
    00090 #ifndef __INSPECTABLE_H
    00091 #define __INSPECTABLE_H
    00092
    00093
    00094 #include <list>
    00095 #include <map>
    00096 #include <utility>
    00097 #include <string>
    00098 #include "stl_adds.h"
    00099
    00100 #include <iostream>
    00101
    00102 namespace matrix {
    00103   class Matrix;
    00104 }
    00105
    00106 /**
    00107  * Interface for inspectable objects.
    00108  * That means that one can read out some internal parameters indentified by string keys
    00109 */
    00110 class Inspectable {
    00111 public:
    00112
    00113   // TYPEDEFS BEGIN
    00114
    00115   typedef std::string iparamkey;
    00116   typedef double iparamval;
    00117   typedef std::pair<iparamkey,iparamval*> iparampair;
    00118
    00119   // the bool says whether  only 4x4AndDiag is used
    00120   typedef std::pair<iparamkey,std::pair< matrix::Matrix*, bool > > imatrixpair;
    00121   typedef std::list<iparamkey> iparamkeylist;
    00122   typedef std::list<iparamval> iparamvallist;
    00123   typedef std::list<iparamval*> iparamvalptrlist;
    00124   typedef std::list<iparampair> iparampairlist;
    00125   typedef std::list<imatrixpair> imatrixpairlist;
    00126
    00127   // the network stucture export will be discontinued soon
    00128   typedef struct ILayer{
    00129     ILayer(const std::string& vectorname, const std::string& biasname,
    00130            int dimension, int rank, const std::string& layername)
    00131       : vectorname(vectorname), biasname(biasname),
    00132          dimension(dimension), rank(rank), layername(layername) {}
    00133     std::string vectorname;  //< prefix of the internal parameter vector e.g. "v"
    00134     std::string biasname;    ///< prefix of the internal parameter vector used as bias for the neurons e.g. "h"
    00135     int dimension;      ///< length of the vector (number of units)
    00136     int rank;           ///< rank of the layer (0 are input layers)
    00137     std::string layername;   ///< name of the layer as displayed by the visualiser
    00138   }ILayer;
    00139
    00140   typedef struct IConnection{
    00141     IConnection(const std::string& matrixname, const std::string& vector1, const std::string& vector2)
    00142       : matrixname(matrixname), vector1(vector1), vector2(vector2) {}
    00143     std::string matrixname; ///< matrix name is the prefix of the internal parameter matrix e.g. "A"
    00144     std::string vector1;    ///< vectorname of input layer
    00145     std::string vector2;    ///< vectorname of output layer
    00146   }IConnection;
    00147
    00148   typedef std::list<ILayer> ilayerlist;
    00149   typedef std::list<IConnection> iconnectionlist;
    00150
    00151   /// nice predicate function for finding a Layer with its vectorname
    00152   struct matchName : public std::unary_function<ILayer, bool> {
    00153     matchName(std::string name) : name(name) {}
    00154     std::string name;
    00155     bool operator()(ILayer l) { return l.vectorname == name; }
    00156   };
    00157
    00158
    00159
    00160   /// TYPEDEFS END
    00161
    00162
    00163   Inspectable();
    00164
    00165
    00166   virtual ~Inspectable();
    00167
    00168
    00169   /** The list of the names of all internal parameters given by getInternalParams().
    00170       The naming convention is "v[i]" for vectors
    00171        and "A[i][j]" for matrices, where i, j start at 0.
    00172       @return: list of keys
    00173    */
    00174   virtual iparamkeylist getInternalParamNames() const;
    00175
    00176
    00177 /** @return: list of values
    00178    */
    00179   virtual iparamvallist getInternalParams() const;
    00180
    00181   // should be protected, but it does not work
    00182   /**
    00183    * be careful: matrices will be ignored
    00184    * @return list of values (pointer)
    00185    */
    00186   virtual iparamvalptrlist getInternalParamsPtr() const;
    00187
    00188   /** Specifies which parameter vector forms a structural layer (in terms of a neural network)
    00189       The ordering is important. The first entry is the input layer and so on.
    00190       @return: list of layer names with dimension
    00191
    00192    */
    00193   virtual ilayerlist getStructuralLayers() const;
    00194
    00195
    00196   /** Specifies which parameter matrix forms a connection between layers (in terms of a neural network)
    00197       The orderning is not important.
    00198       @return: list of layer names with dimension
    00199    */
    00200   virtual iconnectionlist getStructuralConnections() const;
    00201
    00202
    00203   /**
    00204    * This is the new style for adding inspectable values. Just call this
    00205    * function for each parameter and you are done.
    00206    * registers a single value
    00207    * @param key the name of the inspectable, shown e.g. in guilogger (no spaces allowed)
    00208    * @param val the address of the value to inspect
    00209    * @param descr description string to be exported (using infolines)
    00210    */
    00211   virtual void addInspectableValue(const iparamkey& key, iparamval* val,
    00212                                    const std::string& descr = std::string());
    00213
    00214
    00215   /**
    00216    * This is the new style for adding inspectable values. Just call this
    00217    * function for each parameter and you are done.
    00218    * Inspects elements of the given matrix or vector (automatic detection)
    00219    * For Matrixes Either all or only the values given by
    00220    * store4x4AndDiagonalFieldNames(Matrix& m,string& matrixName);
    00221    * are used.
    00222    * @param key the name of the matrix, shown e.g. in guilogger (no spaces allowed)
    00223    * @param m the address of the matrix to inspect
    00224    * @param only4x4AndDiag of true only 4x4 matrix plus the diagonal is used
    00225    * @param descr description string to be exported
    00226    *              (for the entire matrix with key_) (using infolines)
    00227    * @note that you can change the structure of the matrix while
    00228    * being inspected, but no after the getInternalParameterNames is called!
    00229    */
    00230   virtual void addInspectableMatrix(const iparamkey& key, matrix::Matrix* m,
    00231                                     bool only4x4AndDiag=true,
    00232                                     const std::string& descr = std::string());
    00233
    00234   /**
    00235    * adds a description for the given parameter using info-lines
    00236    * The line will start (appart from the #I) with a D for description
    00237    * followed by the key end then followed by the string.
    00238    * @param key parameter name (no spaces allowed)
    00239    * @param descr descriptive string (no newlines allowed)
    00240    */
    00241   virtual void addInspectableDescription(const iparamkey& key, const std::string& descr);
    00242
    00243
    00244   /**
    00245    * Adds an info line to this inspectable instance. All infolines are plotted
    00246    * by the PlotOptionEngine and therefore appear e.g. in the logfile.
    00247    * They are leaded by a #I. If you have your own identifiers, just begin with
    00248    * your one (e.g. N for number of
    00249    * NOTE: You must not add e.g. carriage return, all this is handled by the
    00250    * PlotOptionsEngine itself.
    00251    * If you like to add multiple lines, call this function a lot more or
    00252    * use instead addInfoLines.
    00253    * @param infoLine the line (as string) to be added
    00254    */
    00255   virtual void addInfoLine(std::string infoLine);
    00256
    00257   /**
    00258    * Adds a bunch of infolines with addInfoLine to this inspectable instance.
    00259    * Every infoline is separated by an carriage return automatically, done
    00260    * by the PlotOptionsEngine.
    00261    * @param infoLineList the infoLines to be added as a string list.
    00262    */
    00263   virtual void addInfoLines(std::list<std::string> infoLineList);
    00264
    00265   /**
    00266    * Removes all infolines from this inspectable instance.
    00267    * This is useful if you have new infoLines and would like to restart
    00268    * the PlotOptionEngine.
    00269    */
    00270   virtual void removeInfoLines();
    00271
    00272   /**
    00273    * Returns all infolines added to this inspectable instance.
    00274    * This function is called by the PlotOptionEngine.
    00275    * @return all added infolines.
    00276    */
    00277   virtual const std::list<std::string>& getInfoLines() const;
    00278
    00279 protected:
    00280   iparampairlist mapOfValues;
    00281   imatrixpairlist mapOfMatrices;
    00282
    00283   std::list<std::string> infoLineStringList;
    00284
    00285
    00286 };
    00287
    00288 #endif