/***************************************************************************
 *   Copyright (C) 2005 by Robot Group Leipzig                             *
 *    martius@informatik.uni-leipzig.de                                    *
 *    fhesse@informatik.uni-leipzig.de                                     *
 *    der@informatik.uni-leipzig.de                                        *
 *                                                                         *
 *   ANY COMMERCIAL USE FORBIDDEN!                                         *
 *   LICENSE:                                                              *
 *   This work is licensed under the Creative Commons                      *
 *   Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of   *
 *   this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ *
 *   or send a letter to Creative Commons, 543 Howard Street, 5th Floor,   *
 *   San Francisco, California, 94105, USA.                                *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *
 *                                                                         *
 *   $Log: invertmotorspace.h,v $
 *   Revision 1.22  2008/05/30 11:58:27  martius
 *   use cmath instead of math.h
 *
 *   Revision 1.21  2008/04/17 14:54:45  martius
 *   randomGen added, which is a random generator with long period and an
 *    internal state. Each Agent has an instance and passed it to the controller
 *    and the wiring. This is good for
 *   a) repeatability on agent basis,
 *   b) parallel execution as done in ode_robots
 *
 *   Revision 1.20  2006/11/29 16:22:43  martius
 *   name is a variable of configurable and is used as such
 *
 *   Revision 1.19  2006/07/20 17:14:34  martius
 *   removed std namespace from matrix.h
 *   storable interface
 *   abstract model and invertablemodel as superclasses for networks
 *
 *   Revision 1.18  2006/07/18 15:17:16  martius
 *   buffer operations like delayed values and smoothed values moved to invertmotorcontroller
 *
 *   Revision 1.17  2006/07/14 12:23:58  martius
 *   selforg becomes HEAD
 *
 *   Revision 1.15.6.4  2006/07/10 13:05:16  martius
 *   NON-COMMERICAL LICENSE added to controllers
 *
 *   Revision 1.15.6.3  2006/07/10 11:59:24  martius
 *   Matrixlib now in selforg
 *   no namespace std in header files
 *
 *   Revision 1.15.6.2  2006/01/18 16:48:35  martius
 *   stored and restore
 *
 *   Revision 1.15.6.1  2005/11/14 14:54:30  martius
 *   cpp file is separate now
 *
 *   Revision 1.15  2005/10/27 15:46:38  martius
 *   inspectable interface is expanded to structural information for network visualiser
 *
 *   Revision 1.14  2005/10/06 17:08:40  martius
 *   switched to stl lists
 *   Bias noise
 *   correct error_factor for controller learning
 *
 *   Revision 1.13  2005/08/22 20:32:09  martius
 *   C- initialisation variable
 *
 *   Revision 1.12  2005/08/03 20:31:14  martius
 *   destructor added
 *
 *   Revision 1.11  2005/07/21 11:33:01  fhesse
 *   R as internal parameter instead of C
 *
 *   Revision 1.10  2005/07/21 09:40:48  martius
 *   moved squashing and g to superclass
 *
 *   Revision 1.9  2005/07/05 14:48:18  martius
 *   desens removed
 *   zeta update is continuous
 *
 *   Revision 1.8  2005/07/04 15:23:27  martius
 *   x_smooth used for controller learning
 *
 *   Revision 1.7  2005/06/29 08:30:28  martius
 *   inherited from invertmotorcontroller
 *
 *   Revision 1.6  2005/06/21 15:31:11  martius
 *   getSensorNumber and getMotorMumber added controller interface
 *
 *   Revision 1.5  2005/06/20 09:04:16  martius
 *   init function added to controller-interface
 *
 *   Revision 1.4  2005/06/16 14:23:14  martius
 *   GPL and name member added
 *
 ***************************************************************************/
#ifndef __INVERTMOTORSPACE_H
#define __INVERTMOTORSPACE_H

#include "invertmotorcontroller.h" 
#include <assert.h>
#include <cmath>

#include <selforg/matrix.h>
#include <selforg/noisegenerator.h>

/**
 * class for robot controller that uses the georg's matrixlib for 
 *  direct matrix inversion for n channels 
 * (simple one layer networks)
 * 
 * Implements standart parameters: eps, rho, mu, stepnumber4avg, stepnumber4delay
 */
class InvertMotorSpace : public InvertMotorController {

public:
  InvertMotorSpace(int buffersize, double cInit = 0.1 , bool someInternalParams = true);
  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);

  virtual ~InvertMotorSpace();

  /// returns the number of sensors the controller was initialised with or 0 if not initialised
  virtual int getSensorNumber() const { return number_sensors; }
  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
  virtual int getMotorNumber() const  { return number_motors; }

  /// performs one step (includes learning). 
  /// Calulates motor commands from sensor inputs.
  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);

  /// performs one step without learning. Calulates motor commands from sensor inputs.
  virtual void stepNoLearning(const sensor* , int number_sensors, 
			      motor* , int number_motors);

  /**** STOREABLE ****/
  /** stores the controller values to a given file (binary).  */
  virtual bool store(FILE* f) const;
  /** loads the controller values from a given file (binary). */
  virtual bool restore(FILE* f);  

  // inspectable interface
  virtual std::list<iparamkey> getInternalParamNames() const;
  virtual std::list<iparamval> getInternalParams() const;
  virtual std::list<ILayer> getStructuralLayers() const;
  virtual std::list<IConnection> getStructuralConnections() const;
  

protected:
  unsigned short number_sensors;
  unsigned short number_motors;
  
  matrix::Matrix A; // Model Matrix
  matrix::Matrix C; // Controller Matrix
  matrix::Matrix R; // C*A
  matrix::Matrix H; // Controller Bias
  matrix::Matrix B; // Model Bias
  NoiseGenerator* BNoiseGen; // Noisegenerator for noisy bias
  matrix::Matrix* x_buffer;
  matrix::Matrix* y_buffer;
  matrix::Matrix x_smooth;

  bool someInternalParams;
  double cInit;
  
  /// puts the sensors in the ringbuffer, generate controller values and put them in the 
  //  ringbuffer as well
  void fillBuffersAndControl(const sensor* x_, int number_sensors, 
			     motor* y_, int number_motors);

  /// learn h,C, delayed motors y and corresponding sensors x
  virtual void learnController(const matrix::Matrix& x, const matrix::Matrix& x_smooth, int delay);

  /// learn A, using motors y and corresponding sensors x
  virtual void learnModel( const matrix::Matrix& x, const matrix::Matrix& y);

  /// returns controller output for given sensor values
  virtual matrix::Matrix calculateControllerValues(const matrix::Matrix& x_smooth);
    
};

#endif
