iCub-main
Standardizer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2011 RobotCub Consortium, European Commission FP6 Project IST-004370
3  * author: Arjan Gijsberts
4  * email: arjan.gijsberts@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 #include <sstream>
20 #include <cmath>
21 
22 //#include <yarp/os/Value.h>
23 
26 
28 
29 namespace iCub {
30 namespace learningmachine {
31 
32 Standardizer::Standardizer(double m, double s)
33  : noSamples(0), runningMean(0.), runningStd(0.), squaredErrors(0.) {
34  this->setName("Standardizer");
35  this->setDesiredMean(m);
36  this->setDesiredStd(s);
37 }
38 
39 void Standardizer::update(double val) {
40  /*
41  * Running variance calculation, taken from:
42  *
43  * Donald E. Knuth (1998). The Art of Computer Programming, volume 2:
44  * Seminumerical Algorithms, 3rd edn., p. 232. Boston: Addison-Wesley
45  */
46  this->noSamples++;
47  double delta = val - this->runningMean;
48  this->runningMean += (delta / this->noSamples);
49  this->squaredErrors += (delta * (val - this->runningMean)); // M2 in TAOCP
50  this->runningStd = sqrt(this->squaredErrors / (this->noSamples + 1));
51 
52  this->scale = this->runningStd / this->std;
53  this->offset = (this->runningMean - this->mean) * this->scale;
54 }
55 
56 std::string Standardizer::getInfo() {
57  std::ostringstream buffer;
58  buffer << this->IScaler::getInfo() << ", ";
59  buffer << "Input Stats: " << this->runningMean << " +/- " << this->runningStd << ", ";
60  buffer << "Desired: " << this->getDesiredMean() << " +/- " << this->getDesiredStd();
61  return buffer.str();
62 }
63 
64 void Standardizer::writeBottle(yarp::os::Bottle& bot) {
65  bot << this->noSamples << this->squaredErrors << this->mean << this->std
66  << this->runningMean << this->runningStd;
67 
68  // make sure to call the superclass's method
69  this->IScaler::writeBottle(bot);
70 }
71 
72 void Standardizer::readBottle(yarp::os::Bottle& bot) {
73  // make sure to call the superclass's method (will reset transformer)
74  this->IScaler::readBottle(bot);
75 
76  bot >> this->runningStd >> this->runningMean >> this->std >> this->mean
77  >> this->squaredErrors >> this->noSamples;
78 }
79 
80 bool Standardizer::configure(yarp::os::Searchable& config) {
81  bool success = this->IScaler::configure(config);
82 
83  // set the desired output mean (double)
84  if(config.find("mean").isFloat64() || config.find("mean").isInt32()) {
85  this->setDesiredMean(config.find("mean").asFloat64());
86  success = true;
87  }
88  // set the desired output standard deviation (double)
89  if(config.find("std").isFloat64() || config.find("std").isInt32()) {
90  this->setDesiredStd(config.find("std").asFloat64());
91  success = true;
92  }
93 
94  return success;
95 }
96 
97 
98 } // learningmachine
99 } // iCub
100 
virtual std::string getInfo()
Asks the learning machine to return a string containing statistics on its operation so far.
Definition: IScaler.cpp:40
virtual void writeBottle(yarp::os::Bottle &bot)
Writes a serialization of the scaler into a bottle.
Definition: IScaler.cpp:61
void setName(std::string name)
Set the name of this machine learning technique.
Definition: IScaler.h:140
double scale
The scale for the linear transformation.
Definition: IScaler.h:51
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a scaler from a bottle.
Definition: IScaler.cpp:67
double offset
The offset for the linear transformation.
Definition: IScaler.h:46
virtual bool configure(yarp::os::Searchable &config)
Definition: IScaler.cpp:73
virtual double getDesiredStd()
Accessor for the desired standard deviation.
Definition: Standardizer.h:131
double runningStd
Running standard deviation based on the samples seen so far.
Definition: Standardizer.h:68
Standardizer(double m=0., double s=1.)
Constructor.
double mean
Desired mean for the output distribution.
Definition: Standardizer.h:53
double squaredErrors
Temporary variable that counts the sum of the squared errors.
Definition: Standardizer.h:73
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a scaler from a bottle.
virtual std::string getInfo()
Asks the learning machine to return a string containing statistics on its operation so far.
virtual void writeBottle(yarp::os::Bottle &bot)
Writes a serialization of the scaler into a bottle.
virtual void setDesiredStd(double s)
Mutator for the desired standard deviation.
Definition: Standardizer.h:138
virtual bool configure(yarp::os::Searchable &config)
double std
Desired standard deviation for the output distribution.
Definition: Standardizer.h:58
int noSamples
The number of samples that have been received so far.
Definition: Standardizer.h:48
virtual void update(double val)
Feeds a single sample into the scaler, so that it can use this sample to update the offset and scale.
virtual void setDesiredMean(double m)
Mutator for the desired mean.
Definition: Standardizer.h:126
double runningMean
Running mean based on the samples seen so far.
Definition: Standardizer.h:63
virtual double getDesiredMean()
Accessor for the desired mean.
Definition: Standardizer.h:119
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.