/***************************************************************************
 *   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: invertmotorcontroller.h,v $
 *   Revision 1.31  2007/12/11 14:44:28  martius
 *   possible segfault bug in buffer function
 *
 *   Revision 1.30  2007/04/20 12:26:35  martius
 *   *** empty log message ***
 *
 *   Revision 1.29  2007/02/23 09:40:45  der
 *   regularisation used from regularisation.h
 *
 *   Revision 1.28  2007/01/24 14:39:10  martius
 *   use new configurable addParameter feature
 *
 *   Revision 1.27  2006/12/21 11:44:17  martius
 *   commenting style for doxygen //< -> ///<
 *   FOREACH and FOREACHC are macros for collection iteration
 *
 *   Revision 1.26  2006/12/11 18:14:14  martius
 *   delete for BNoise
 *   delete of buffers
 *
 *   Revision 1.25  2006/11/29 16:22:43  martius
 *   name is a variable of configurable and is used as such
 *
 *   Revision 1.24  2006/10/20 13:32:35  der
 *   fantasy
 *
 *   Revision 1.23  2006/08/02 09:30:16  martius
 *   virtualized new calc functions
 *   calcErrorFactor added
 *
 *   Revision 1.22  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.21  2006/07/18 15:17:16  martius
 *   buffer operations like delayed values and smoothed values moved to invertmotorcontroller
 *
 *   Revision 1.20  2006/07/18 14:47:15  martius
 *   new regularisation of g' and g''/g'
 *   g''/g' uses the same clipped z which used for g'
 *   sqashing function with variable squash size
 *
 *   Revision 1.19  2006/07/14 12:23:58  martius
 *   selforg becomes HEAD
 *
 *   Revision 1.17.6.10  2006/07/10 13:05:16  martius
 *   NON-COMMERICAL LICENSE added to controllers
 *
 *                                                                         *
 ***************************************************************************/

#ifndef __INVERTMOTORCONTROLLER_H
#define __INVERTMOTORCONTROLLER_H

#include "abstractcontroller.h"
#include "controller_misc.h"
#include <stdlib.h>
#include <string.h>


/**
 * Abstract class (interface) for robot controller that use direct matrix inversion and
 * a time loop in motor domain
 * 
 * Implements standart configureable interface for some useful parameters like  epsC, epsA, s4avg ...
 */
class InvertMotorController : public AbstractController {
public:
  InvertMotorController( unsigned short buffersize ,
			 const std::string& name, const std::string& revision)
    : AbstractController(name, revision){
    this->buffersize = buffersize;
    addParameterDef("epsC", &epsC, 0.1); 
    addParameterDef("epsA",&epsA,  0.1); 
    addParameterDef("dampA",&dampA,0 ); 
    addParameterDef("adaptrate",&adaptRate,0.000);
    addParameterDef("nomupdate",&nomUpdate,0.005);
    addParameterDef("desens",&desens,0);          
    addParameterDef("s4delay",&s4delay,1); 
    addParameterDef("s4avg",&s4avg,1); 
    addParameterDef("steps",&steps,1); 
    addParameterDef("zetaupdate",&zetaupdate,0); 
    addParameterDef("squashsize",&squashSize,0.01);
    addParameterDef("factorB",&factorB,0.2); 
    addParameterDef("noiseB",&noiseB,0.001);
    addParameterDef("noiseY",&noiseY,0);
    addParameterDef("teacher",&teacher,5);
    rootE=0;
    logaE=0;
    relativeE=0;
    t=0;
    initialised = false;
  }

  /***** CONFIGURABLE ******/

  virtual paramval getParam(const paramkey& key) const{
    if(key == "logaE") return logaE; 
    else if(key == "rootE") return rootE; 
    else if(key == "relativeE") return relativeE; 
    else  return AbstractController::getParam(key);
  }

  virtual bool setParam(const paramkey& key, paramval val){
    if(key == "logaE") logaE=(short)val;
    else if(key == "rootE") rootE=(short)val;
    else if(key == "relativeE") relativeE=(short)val;
    else return AbstractController::setParam(key, val);
    //    else fprintf(stderr, "parameter %s unknown\n", key);
    return true;
  }

  virtual paramlist getParamList() const{
    paramlist list;
    list.push_back(std::pair<paramkey, paramval> ("logaE", logaE));
    list.push_back(std::pair<paramkey, paramval> ("rootE", rootE));
    list.push_back(std::pair<paramkey, paramval> ("relativeE", relativeE));
    return list;
  }

protected:
  paramval epsC; ///< learning rate factor for controller learning
  paramval desens;
  paramval s4delay; ///< number of timesteps of delay in the SML 
  paramval s4avg; ///< number of timesteps used for smoothing the controller output values 
  paramval steps; ///< number of timesteps is used for controller learning
  paramval epsA; ///< learning rate factor for model learning
  paramval factorB; ///< additional learning rate factor for model bias 
  paramval zetaupdate;
  paramval dampA; ///< damping term for model (0: no damping, 0.1 high damping)
  short logaE;  ///< logarithmic error is used for learning 1: controller 2: model 3: both
  short rootE;  ///< root error is used for learning 1: controller 2: model 3: both
  short relativeE; ///< if not 0: a relative error signal is used (xsi is normalised in respect to |y|)

  paramval squashSize; ///< size of the box, where the parameter updates are clipped to
  paramval adaptRate;  ///< adaptation rate for learning rate adaptation
  paramval nomUpdate;  ///< nominal update of controller in respect to matrix norm
  paramval noiseB;     ///< size of the additional noise for model bias B
  paramval noiseY;     ///< size of the additional noise for motor values
  paramval teacher;    ///< factor for teaching signal

  int t;
  unsigned short buffersize;
  bool initialised;

protected:
  // put new value in ring buffer
  void putInBuffer(matrix::Matrix* buffer, const matrix::Matrix& vec, int delay=0){
    buffer[(t-delay)%buffersize] = vec;
  }

  /// calculate delayed values
  virtual matrix::Matrix calculateDelayedValues(const matrix::Matrix* buffer, 
						  int number_steps_of_delay_){
    // number_steps_of_delay must not be smaller than buffersize
    assert ((unsigned)number_steps_of_delay_ < buffersize);  
    return buffer[(t - number_steps_of_delay_) % buffersize];
  };
  
  /// calculate time-smoothed values 
  virtual matrix::Matrix calculateSmoothValues(const matrix::Matrix* buffer, 
						 int number_steps_for_averaging_){
    // number_steps_for_averaging_ must not be larger than buffersize
    assert ((int)number_steps_for_averaging_ <= buffersize);
    
    matrix::Matrix result(buffer[t % buffersize]);
    for (int k = 1; k < number_steps_for_averaging_; k++) {
      result += buffer[(t - k + buffersize) % buffersize];
    }
    result *= 1/((double) (number_steps_for_averaging_)); // scalar multiplication
    return result;
  };

  /// calculates the error_factor for either logarithmic (E=ln(e^T*e)) or square (E=sqrt(e^t*e)) error
  virtual double calcErrorFactor(const matrix::Matrix& e, bool loga, bool root) {
    double error_factor = 1;
    if (loga){   // using logarithmic error E=ln(v^T*v)
      error_factor= 1/(e.multTM().val(0,0)+0.000001)*0.01; // factor 1/100 for normalising (empirically)
    }
    if (root){  // using root error E=(v^T*v)^(1/2) 
      error_factor= 1/sqrt(e.multTM().val(0,0)+0.000001)*0.1; // factor 1/10 for normalising (empirically)
    }  
    return error_factor;
  }
  
  
  /// neuron transfer function
  static double g(double z)
  {
    return tanh(z);
  };


 
};

#endif
