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 * * 00007 * ANY COMMERCIAL USE FORBIDDEN! * 00008 * LICENSE: * 00009 * This work is licensed under the Creative Commons * 00010 * Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of * 00011 * this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ * 00012 * or send a letter to Creative Commons, 543 Howard Street, 5th Floor, * 00013 * San Francisco, California, 94105, USA. * 00014 * * 00015 * This program is distributed in the hope that it will be useful, * 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 00018 * * 00019 * $Log: ffnncontroller.h,v $ 00020 * Revision 1.6 2009/03/31 15:53:50 martius 00021 * added constructor for a given network 00022 * 00023 * Revision 1.5 2008/05/30 11:58:27 martius 00024 * use cmath instead of math.h 00025 * 00026 * Revision 1.4 2008/04/17 14:54:44 martius 00027 * randomGen added, which is a random generator with long period and an 00028 * internal state. Each Agent has an instance and passed it to the controller 00029 * and the wiring. This is good for 00030 * a) repeatability on agent basis, 00031 * b) parallel execution as done in ode_robots 00032 * 00033 * Revision 1.3 2007/06/14 13:57:08 martius 00034 * initial waiting time (no control before that) 00035 * 00036 * Revision 1.2 2007/06/08 15:46:54 martius 00037 * support different static networks (e.g. satellite nets) 00038 * 00039 * Revision 1.1 2006/11/29 16:21:35 martius 00040 * controller that uses a fixed feed forward neural network with some history 00041 * 00042 * 00043 ***************************************************************************/ 00044 #ifndef __FFNNCONTROLLER_H 00045 #define __FFNNCONTROLLER_H 00046 00047 #include "abstractcontroller.h" 00048 #include <assert.h> 00049 #include <cmath> 00050 00051 #include <selforg/matrix.h> 00052 #include <selforg/multilayerffnn.h> 00053 00054 /** 00055 * class for robot controller with a fixed neural network 00056 */ 00057 class FFNNController : public AbstractController { 00058 00059 public: 00060 /** @param networkfilename file to load the network 00061 @param history number of time steps the network gets input (in sense of dimension of input) 00062 @param input_only_x if true then the input vector is \f[ (x_{t},x_{t-1},...,x_{t-history})^T \f] 00063 if false then also the y values are used: \f[ (x_{t}, y_{t-1}, x_{t-1},y_{t-2},...,x_{t-history})^T \f] 00064 @param init_wait number of timesteps to wait before controlling 00065 */ 00066 FFNNController(const std::string& networkfilename, int history, bool input_only_x, unsigned int init_wait=0); 00067 00068 /** @param net pointer to network (it must have the right dimensions) 00069 @param history number of time steps the network gets input (in sense of dimension of input) 00070 @param input_only_x if true then the input vector is \f[ (x_{t},x_{t-1},...,x_{t-history})^T \f] 00071 if false then also the y values are used: \f[ (x_{t}, y_{t-1}, x_{t-1},y_{t-2},...,x_{t-history})^T \f] 00072 @param init_wait number of timesteps to wait before controlling 00073 */ 00074 FFNNController(MultiLayerFFNN* net, int history, bool input_only_x, unsigned int init_wait=0); 00075 00076 virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0); 00077 00078 virtual ~FFNNController(); 00079 00080 /// returns the number of sensors the controller was initialised with or 0 if not initialised 00081 virtual int getSensorNumber() const { return number_sensors; } 00082 /// returns the mumber of motors the controller was initialised with or 0 if not initialised 00083 virtual int getMotorNumber() const { return number_motors; } 00084 00085 virtual void step(const sensor* , int number_sensors, motor* , int number_motors); 00086 virtual void stepNoLearning(const sensor* , int number_sensors, 00087 motor* , int number_motors); 00088 00089 /**** STOREABLE ****/ 00090 /** stores the controller values to a given file (binary). */ 00091 virtual bool store(FILE* f) const; 00092 /** loads the controller values from a given file (binary). */ 00093 virtual bool restore(FILE* f); 00094 00095 // inspectable interface 00096 virtual std::list<iparamkey> getInternalParamNames()const { return std::list<iparamkey>(); } 00097 virtual std::list<iparamval> getInternalParams() const { return std::list<iparamval>(); } 00098 00099 // configureable interface 00100 virtual paramval getParam(const paramkey& key) const; 00101 virtual bool setParam(const paramkey& key, paramval val); 00102 virtual paramlist getParamList() const ; 00103 00104 protected: 00105 void putInBuffer(matrix::Matrix* buffer, const matrix::Matrix& vec, int delay = 0); 00106 00107 matrix::Matrix calculateSmoothValues(const matrix::Matrix* buffer, int number_steps_for_averaging_) const; 00108 00109 virtual matrix::Matrix assembleNetworkInputXY(matrix::Matrix* xbuffer, matrix::Matrix* ybuffer) const; 00110 00111 virtual matrix::Matrix assembleNetworkInputX(matrix::Matrix* xbuffer, matrix::Matrix* ybuffer) const; 00112 00113 virtual matrix::Matrix assembleNetworkOutput(const matrix::Matrix& output) const; 00114 00115 00116 protected: 00117 unsigned short number_motors; 00118 unsigned short number_sensors; 00119 unsigned short history; 00120 unsigned short buffersize; 00121 bool input_only_x; 00122 unsigned short s4avg; 00123 unsigned int t; 00124 unsigned int init_wait; 00125 00126 matrix::Matrix* x_buffer; 00127 matrix::Matrix* y_buffer; 00128 matrix::Matrix x_smooth; 00129 00130 MultiLayerFFNN* net; 00131 bool initialised; 00132 00133 }; 00134 00135 #endif