iCub-main
DatasetRecorder.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2010 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 <iomanip>
20 #include <sstream>
21 
23 
24 namespace iCub {
25 namespace learningmachine {
26 
28  if(this == &other) return *this; // handle self initialization
29 
30  this->IMachineLearner::operator=(other);
31  this->filename = other.filename;
32  this->precision = other.precision;
33  this->sampleCount = other.sampleCount;
34 
35  return *this;
36 }
37 
38 
39 void DatasetRecorder::feedSample(const yarp::sig::Vector& input, const yarp::sig::Vector& output) {
40  // open stream if not opened yet
41  if(!this->stream.is_open()) {
42  // perhaps check if file already exists
43  this->stream.open(this->filename.c_str(), std::ios_base::out | std::ios_base::app);
44  // set precision
45  this->stream.precision(this->precision);
46  }
47 
48  // first write inputs
49  for(size_t i = 0; i < input.size(); i++) {
50  if(i > 0) this->stream << " ";
51  this->stream << std::setw(this->precision + 4) << input[i];
52  }
53  this->stream << " ";
54 
55  // then write outputs
56  for(size_t i = 0; i < output.size(); i++) {
57  if(i > 0) this->stream << " ";
58  this->stream << std::setw(this->precision + 4) << output[i] << " ";
59  }
60  this->sampleCount++;
61 
62  this->stream << std::endl;
63 
64  this->stream.flush();
65 }
66 
67 
68 std::string DatasetRecorder::getInfo() {
69  std::ostringstream buffer;
70  buffer << this->IMachineLearner::getInfo();
71  buffer << "Filename: " << this->filename << std::endl;
72  buffer << "Precision: " << this->precision << std::endl;
73  buffer << "Sample Count: " << this->sampleCount << std::endl;
74  return buffer.str();
75 }
76 
77 void DatasetRecorder::writeBottle(yarp::os::Bottle& bot) const {
78  bot.addString(this->filename.c_str());
79  bot.addInt32(this->precision);
80 }
81 
82 void DatasetRecorder::readBottle(yarp::os::Bottle& bot) {
83  this->precision = bot.pop().asInt32();
84  this->filename = bot.pop().asString().c_str();
85 }
86 
88  std::ostringstream buffer;
89  buffer << this->IMachineLearner::getConfigHelp();
90  buffer << " filename name Filename to write to" << std::endl;
91  buffer << " precision n Number of digits precision for doubles" << std::endl;
92  return buffer.str();
93 }
94 
95 bool DatasetRecorder::configure(yarp::os::Searchable& config) {
96  bool success = false;
97 
98  // set the filename
99  if(config.find("filename").isString()) {
100  this->reset();
101  this->filename = config.find("filename").asString().c_str();
102  success = true;
103  }
104 
105  // set the precision
106  if(config.find("precision").isInt32()) {
107  this->precision = config.find("precision").asInt32();
108  success = true;
109  }
110 
111  return success;
112 }
113 
114 } // learningmachine
115 } // iCub
This 'machine learner' demonstrates how the IMachineLearner interface can be used to easily record sa...
virtual void feedSample(const yarp::sig::Vector &input, const yarp::sig::Vector &output)
Provide the learning machine with an example of the desired mapping.
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a machine from a bottle.
void reset()
Forget everything and start over.
virtual void writeBottle(yarp::os::Bottle &bot) const
Writes a serialization of the machine into a bottle.
DatasetRecorder & operator=(const DatasetRecorder &other)
Assignment operator.
virtual std::string getConfigHelp()
Asks the learning machine to return a string containing the list of configuration options that it sup...
virtual bool configure(yarp::os::Searchable &config)
Change parameters.
std::string getInfo()
Asks the learning machine to return a string containing information on its operation so far.
virtual std::string getConfigHelp()
Asks the learning machine to return a string containing the list of configuration options that it sup...
virtual std::string getInfo()
Asks the learning machine to return a string containing information on its operation so far.
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.
out
Definition: sine.m:8