iCub-main
FixedRangeScaler.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 <cfloat>
20 #include <sstream>
21 
22 #include <yarp/os/Value.h>
23 
26 
28 
29 namespace iCub {
30 namespace learningmachine {
31 
32 FixedRangeScaler::FixedRangeScaler(double li, double ui, double lo, double uo)
33  : lowerBoundIn(li), upperBoundIn(ui), lowerBoundOut(lo), upperBoundOut(uo) {
34  this->setName("Fixed");
35  this->updateScales();
36 }
37 
39  this->scale = (this->getUpperBoundIn() - this->getLowerBoundIn()) /
40  (this->getUpperBoundOut() - this->getLowerBoundOut());
41  this->offset = this->getLowerBoundIn() - (this->getLowerBoundOut() * this->scale);
42 }
43 
45  std::ostringstream buffer;
46  buffer << this->IScaler::getInfo() << ", ";
47  buffer << "In Bounds: [" << this->getLowerBoundIn() << ","
48  << this->getUpperBoundIn() << "], ";
49  buffer << "Out Bounds: [" << this->getLowerBoundOut() << ","
50  << this->getUpperBoundOut() << "]";
51  return buffer.str();
52 }
53 
54 void FixedRangeScaler::writeBottle(yarp::os::Bottle& bot) {
55  bot << this->lowerBoundOut << this->upperBoundOut
56  << this->lowerBoundIn << this->upperBoundIn;
57  // make sure to call the superclass's method
58  this->IScaler::writeBottle(bot);
59 }
60 
61 void FixedRangeScaler::readBottle(yarp::os::Bottle& bot) {
62  // make sure to call the superclass's method (will reset transformer)
63  this->IScaler::readBottle(bot);
64 
65  bot >> this->upperBoundIn >> this->lowerBoundIn
66  >> this->upperBoundOut >> this->lowerBoundOut;
67 }
68 
69 
70 bool FixedRangeScaler::configure(yarp::os::Searchable& config) {
71  bool success = this->IScaler::configure(config);
72 
73  // set the expected incoming lower bound (double)
74  if(config.find("lowerin").isFloat64() || config.find("lowerin").isInt32()) {
75  this->setLowerBoundIn(config.find("lowerin").asFloat64());
76  success = true;
77  }
78  // set the expected incoming upper bound (double)
79  if(config.find("upperin").isFloat64() || config.find("upperin").isInt32()) {
80  this->setUpperBoundIn(config.find("upperin").asFloat64());
81  success = true;
82  }
83 
84  // set the desired outgoing lower bound (double)
85  if(config.find("lowerout").isFloat64() || config.find("lowerout").isInt32()) {
86  this->setLowerBoundOut(config.find("lowerout").asFloat64());
87  success = true;
88  }
89  // set the desired outgoing bound (double)
90  if(config.find("upperout").isFloat64() || config.find("upperout").isInt32()) {
91  this->setUpperBoundOut(config.find("upperout").asFloat64());
92  success = true;
93  }
94 
95  if(!config.findGroup("in").isNull()) {
96  yarp::os::Bottle& bot = config.findGroup("in");
97  if(bot.size() == 3 && (bot.get(1).isInt32() || bot.get(1).isFloat64()) &&
98  (bot.get(2).isInt32() || bot.get(2).isFloat64())) {
99 
100  this->setLowerBoundIn(bot.get(1).asFloat64());
101  this->setUpperBoundIn(bot.get(2).asFloat64());
102  success = true;
103  }
104  }
105 
106  if(!config.findGroup("out").isNull()) {
107  yarp::os::Bottle& bot = config.findGroup("out");
108  if(bot.size() == 3 && (bot.get(1).isInt32() || bot.get(1).isFloat64()) &&
109  (bot.get(2).isInt32() || bot.get(2).isFloat64())) {
110 
111  this->setLowerBoundOut(bot.get(1).asFloat64());
112  this->setUpperBoundOut(bot.get(2).asFloat64());
113  success = true;
114  }
115  }
116 
117  return success;
118 }
119 
120 } // learningmachine
121 } // iCub
122 
iCub::learningmachine::IScaler::configure
virtual bool configure(yarp::os::Searchable &config)
Definition: IScaler.cpp:73
iCub::learningmachine::serialization
Definition: Serialization.h:28
iCub
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.
Definition: emotionInterface.h:17
iCub::learningmachine::IScaler::setName
void setName(std::string name)
Set the name of this machine learning technique.
Definition: IScaler.h:140
iCub::learningmachine::FixedRangeScaler::setLowerBoundOut
virtual void setLowerBoundOut(double lo)
Mutator for the desired lower bound.
Definition: FixedRangeScaler.h:115
iCub::learningmachine::IScaler::offset
double offset
The offset for the linear transformation.
Definition: IScaler.h:46
iCub::learningmachine::FixedRangeScaler::upperBoundOut
double upperBoundOut
The desired upper bound for the output range.
Definition: FixedRangeScaler.h:59
iCub::learningmachine::FixedRangeScaler::FixedRangeScaler
FixedRangeScaler(double li=-1., double ui=1., double lo=-1., double uo=1.)
Constructor.
Definition: FixedRangeScaler.cpp:32
iCub::learningmachine::IScaler::getInfo
virtual std::string getInfo()
Asks the learning machine to return a string containing statistics on its operation so far.
Definition: IScaler.cpp:40
iCub::learningmachine::FixedRangeScaler::upperBoundIn
double upperBoundIn
The expected upper bound in the sample values.
Definition: FixedRangeScaler.h:49
iCub::learningmachine::FixedRangeScaler::lowerBoundIn
double lowerBoundIn
The expected lower bound in the sample values.
Definition: FixedRangeScaler.h:44
iCub::learningmachine::FixedRangeScaler::setLowerBoundIn
virtual void setLowerBoundIn(double li)
Mutator for the expected lower bound.
Definition: FixedRangeScaler.h:145
FixedRangeScaler.h
iCub::learningmachine::FixedRangeScaler::updateScales
void updateScales()
Updates the scale and offset according to the specified expected and desired ranges.
Definition: FixedRangeScaler.cpp:38
iCub::learningmachine::FixedRangeScaler::readBottle
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a scaler from a bottle.
Definition: FixedRangeScaler.cpp:61
iCub::learningmachine::IScaler::readBottle
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a scaler from a bottle.
Definition: IScaler.cpp:67
iCub::learningmachine::IScaler::scale
double scale
The scale for the linear transformation.
Definition: IScaler.h:51
Serialization.h
string
string(REPLACE "-rdynamic" "" CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS}") include_directories($
Definition: CMakeLists.txt:9
iCub::learningmachine::FixedRangeScaler::configure
virtual bool configure(yarp::os::Searchable &config)
Definition: FixedRangeScaler.cpp:70
iCub::learningmachine::FixedRangeScaler::getUpperBoundOut
virtual double getUpperBoundOut()
Accessor for the desired upper bound.
Definition: FixedRangeScaler.h:123
iCub::learningmachine::FixedRangeScaler::getInfo
virtual std::string getInfo()
Asks the learning machine to return a string containing statistics on its operation so far.
Definition: FixedRangeScaler.cpp:44
iCub::learningmachine::FixedRangeScaler::getLowerBoundOut
virtual double getLowerBoundOut()
Accessor for the desired lower bound.
Definition: FixedRangeScaler.h:108
iCub::learningmachine::FixedRangeScaler::lowerBoundOut
double lowerBoundOut
The desired lower bound for the output range.
Definition: FixedRangeScaler.h:54
iCub::learningmachine::FixedRangeScaler::getUpperBoundIn
virtual double getUpperBoundIn()
Accessor for the expected upper bound.
Definition: FixedRangeScaler.h:153
iCub::learningmachine::IScaler::writeBottle
virtual void writeBottle(yarp::os::Bottle &bot)
Writes a serialization of the scaler into a bottle.
Definition: IScaler.cpp:61
iCub::learningmachine::FixedRangeScaler::getLowerBoundIn
virtual double getLowerBoundIn()
Accessor for the expected lower bound.
Definition: FixedRangeScaler.h:138
iCub::learningmachine::FixedRangeScaler::writeBottle
virtual void writeBottle(yarp::os::Bottle &bot)
Writes a serialization of the scaler into a bottle.
Definition: FixedRangeScaler.cpp:54
iCub::learningmachine::FixedRangeScaler::setUpperBoundOut
virtual void setUpperBoundOut(double uo)
Mutator for the desired upper bound.
Definition: FixedRangeScaler.h:130
iCub::learningmachine::FixedRangeScaler::setUpperBoundIn
virtual void setUpperBoundIn(double ui)
Mutator for the expected upper bound.
Definition: FixedRangeScaler.h:160