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
classicreinforce.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 by *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Ralf Der <ralfder at mis dot mpg dot de> *
5  * *
6  * ANY COMMERCIAL USE FORBIDDEN! *
7  * LICENSE: *
8  * This work is licensed under the Creative Commons *
9  * Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of *
10  * this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ *
11  * or send a letter to Creative Commons, 543 Howard Street, 5th Floor, *
12  * San Francisco, California, 94105, USA. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
17  * *
18  ***************************************************************************/
19 #ifndef __CLASSICREINFORCE_H
20 #define __CLASSICREINFORCE_H
21 
22 #include <selforg/abstractcontroller.h>
23 
24 #include <assert.h>
25 #include <cmath>
26 
27 #include <selforg/matrix.h>
28 #include <selforg/noisegenerator.h>
29 #include <selforg/qlearning.h>
30 
31 typedef struct ClassicReinforceConf {
32  unsigned short buffersize; ///< size of the ringbuffers for sensors, motors,...
33  int numContext; ///< number of context sensors (ignored)
34  int reinforce_interval; ///< time between consecutive reinforcement selections
35 
36  QLearning* qlearning; ///< QLearning instance
38 
39 /**
40  * class for robot controller
41  * using Q-learning algorithm. Needs to be inherited from to overwrite calcReinforcement()
42  */
44 
45 public:
47  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
48 
49  virtual ~ClassicReinforce();
50 
51  /// returns the number of sensors the controller was initialised with or 0 if not initialised
52  virtual int getSensorNumber() const { return number_sensors; }
53  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
54  virtual int getMotorNumber() const { return number_motors; }
55 
56  /// performs one step (includes learning).
57  /// Calulates motor commands from sensor inputs.
58  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
59 
60  /// performs one step without learning. Calulates motor commands from sensor inputs.
61  virtual void stepNoLearning(const sensor* , int number_sensors,
62  motor* , int number_motors);
63 
64  // !!!!!!!!!!!!!!!!!!! MISC STUFF !!!!!!!!
65 
66  /** enables/disables manual control, action_ is the sat network number to be used
67  if mControl is false, action is ignored
68  */
69  void setManualControl(bool mControl, int action_ = 0);
70 
71 
72  /************** CONFIGURABLE ********************************/
73  virtual void notifyOnChange(const paramkey& key);
74 
75  /**** STOREABLE ****/
76  /** stores the controller values to a given file. */
77  virtual bool store(FILE* f) const;
78  /** loads the controller values from a given file. */
79  virtual bool restore(FILE* f);
80 
81  /**** INSPECTABLE ****/
82  virtual std::list<iparamkey> getInternalParamNames() const;
83  virtual std::list<iparamval> getInternalParams() const;
84  virtual std::list<ILayer> getStructuralLayers() const;
85  virtual std::list<IConnection> getStructuralConnections() const;
86 
89  c.buffersize=10;
90  c.numContext=0;
91  c.reinforce_interval=10;
92  c.qlearning=0;
93  return c;
94  }
95 
96 
97 protected:
98  unsigned short number_sensors;
99  unsigned short number_motors;
100 
101  // sensor, sensor-derivative and motor values storage
102  unsigned short buffersize;
106 
107  bool manualControl; ///< True if actions (sats) are selected manually
108 
109  int action; ///< action
110  int oldaction; ///< old action
111  int state; ///< current state
112  double reward; ///< current reward
113  double oldreward; ///< old reward (nicer for plotting)
114 
117  int t;
118  int managementInterval; ///< interval between subsequent management calls
119 
120  /// returns number of state, to be overwritten
121  virtual int getStateNumber() = 0;
122 
123  /// returns state, to be overwritten
124  virtual int calcState() = 0;
125 
126  /// returns number of actions, to be overwritten
127  virtual int getActionNumber() = 0;
128  /// returns action Matrix from discrete actions, to be overwritten
129  virtual matrix::Matrix calcMotor(int action) = 0;
130 
131  /// returns the reinforcement (reward), to be overwritten
132  virtual double calcReinforcement() = 0;
133 
134  // put new value in ring buffer
135  void putInBuffer(matrix::Matrix* buffer, const matrix::Matrix& vec, int delay = 0);
136 
137  /// puts the sensors in the ringbuffer
138  virtual void fillSensorBuffer(const sensor* x_, int number_sensors);
139  /// puts the motors in the ringbuffer
140  virtual void fillMotorBuffer(const motor* y_, int number_motors);
141 
142  /// handles inhibition damping etc.
143  virtual void management();
144 
145 };
146 
147 #endif
Matrix type.
Definition: matrix.h:65
static ClassicReinforceConf getDefaultConf()
Definition: classicreinforce.h:87
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: classicreinforce.h:54
Abstract class for robot controller (with some basic functionality).
Definition: abstractcontroller.h:46
virtual void fillSensorBuffer(const sensor *x_, int number_sensors)
puts the sensors in the ringbuffer
Definition: classicreinforce.cpp:139
virtual double calcReinforcement()=0
returns the reinforcement (reward), to be overwritten
int reinforce_interval
time between consecutive reinforcement selections
Definition: classicreinforce.h:34
implements QLearning
Definition: qlearning.h:33
void setManualControl(bool mControl, int action_=0)
enables/disables manual control, action_ is the sat network number to be used if mControl is false...
Definition: classicreinforce.cpp:157
unsigned short buffersize
size of the ringbuffers for sensors, motors,...
Definition: classicreinforce.h:32
charArray paramkey
Definition: avrtypes.h:36
Definition: classicreinforce.h:31
virtual void management()
handles inhibition damping etc.
Definition: classicreinforce.cpp:166
bool manualControl
True if actions (sats) are selected manually.
Definition: classicreinforce.h:107
virtual std::list< iparamval > getInternalParams() const
Definition: classicreinforce.cpp:224
QLearning * qlearning
QLearning instance.
Definition: classicreinforce.h:36
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: classicreinforce.cpp:183
double sensor
Definition: types.h:29
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: classicreinforce.cpp:192
virtual matrix::Matrix calcMotor(int action)=0
returns action Matrix from discrete actions, to be overwritten
int managementInterval
interval between subsequent management calls
Definition: classicreinforce.h:118
virtual std::list< IConnection > getStructuralConnections() const
Specifies which parameter matrix forms a connection between layers (in terms of a neural network) The...
Definition: classicreinforce.cpp:239
int numContext
number of context sensors (ignored)
Definition: classicreinforce.h:33
ClassicReinforceConf conf
Definition: classicreinforce.h:115
void putInBuffer(matrix::Matrix *buffer, const matrix::Matrix &vec, int delay=0)
Definition: classicreinforce.cpp:79
virtual void notifyOnChange(const paramkey &key)
Is called when a parameter was changes via setParam().
Definition: classicreinforce.cpp:170
virtual void fillMotorBuffer(const motor *y_, int number_motors)
puts the motors in the ringbuffer
Definition: classicreinforce.cpp:149
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: classicreinforce.cpp:129
double sensor
Definition: abstractcontroller.h:48
int t
Definition: classicreinforce.h:117
double oldreward
old reward (nicer for plotting)
Definition: classicreinforce.h:113
unsigned short number_motors
Definition: classicreinforce.h:99
unsigned short buffersize
Definition: classicreinforce.h:102
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: classicreinforce.cpp:85
virtual void init(int sensornumber, int motornumber, RandGen *randGen=0)
initialisation of the controller with the given sensor/ motornumber Must be called before use...
Definition: classicreinforce.cpp:52
struct ClassicReinforceConf ClassicReinforceConf
ClassicReinforce(const ClassicReinforceConf &conf=getDefaultConf())
Definition: classicreinforce.cpp:28
int oldaction
old action
Definition: classicreinforce.h:110
double reward
current reward
Definition: classicreinforce.h:112
matrix::Matrix * x_buffer
Definition: classicreinforce.h:103
double motor
Definition: types.h:30
int action
action
Definition: classicreinforce.h:109
class for robot controller using Q-learning algorithm.
Definition: classicreinforce.h:43
virtual ~ClassicReinforce()
Definition: classicreinforce.cpp:41
double motor
Definition: abstractcontroller.h:49
virtual std::list< iparamkey > getInternalParamNames() const
The list of the names of all internal parameters given by getInternalParams().
Definition: classicreinforce.cpp:213
unsigned short number_sensors
Definition: classicreinforce.h:98
virtual int getStateNumber()=0
returns number of state, to be overwritten
virtual int calcState()=0
returns state, to be overwritten
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: classicreinforce.h:52
virtual int getActionNumber()=0
returns number of actions, to be overwritten
matrix::Matrix * y_buffer
Definition: classicreinforce.h:104
int c
Definition: hexapod.cpp:56
int state
current state
Definition: classicreinforce.h:111
bool initialised
Definition: classicreinforce.h:116
virtual std::list< ILayer > getStructuralLayers() const
Specifies which parameter vector forms a structural layer (in terms of a neural network) The ordering...
Definition: classicreinforce.cpp:234
matrix::Matrix * x_context_buffer
Definition: classicreinforce.h:105