iCub-main
Loading...
Searching...
No Matches
DummyLearner.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 <iostream>
20
23
25
26namespace iCub {
27namespace learningmachine {
28
29// just here for debugging, since Vector.toString() cannot be applied to const Vector :(
30std::string printVector(const yarp::sig::Vector& v) {
31 std::ostringstream output;
32 output << "[";
33 for(size_t i = 0; i < v.size(); i++) {
34 if(i > 0) output << ",";
35 output << v[i];
36 }
37 output << "]";
38 return output.str();
39}
40
41void DummyLearner::feedSample(const yarp::sig::Vector& input, const yarp::sig::Vector& output) {
42 // call parent method to let it do some validation and processing with the transformers for us
43 this->IFixedSizeLearner::feedSample(input, output);
44
45 std::cout << "Received a sample: " << printVector(input) << " => " << printVector(output) << std::endl;
46
47 this->inputs.push_back(input);
48 this->outputs.push_back(input);
49}
50
52 this->sampleCount = this->inputs.size();
53 this->trainCount++;
54}
55
56Prediction DummyLearner::predict(const yarp::sig::Vector& input) {
57 this->checkDomainSize(input);
58 std::cout << "Received a prediction sample: " << printVector(input) << " => ";
59 yarp::sig::Vector output = input;
60 output.resize(this->getCoDomainSize());
61 for(size_t i = 0; i < output.size(); i++)
62 output[i] += this->sampleCount;
63 std::cout << "(" << printVector(output) << ")" << std::endl;
64 return Prediction(output);
65}
66
68 this->sampleCount = 0;
69 this->trainCount = 0;
70 this->inputs.clear();
71 this->outputs.clear();
72}
73
74std::string DummyLearner::getInfo() {
75 std::ostringstream buffer;
76 buffer << this->IFixedSizeLearner::getInfo();
77 buffer << "Training Samples: " << this->sampleCount << std::endl;
78 buffer << "Training Iterations: " << this->trainCount << std::endl;
79 buffer << "Collected Samples: " << this->inputs.size() << std::endl;
80 return buffer.str();
81}
82
83void DummyLearner::writeBottle(yarp::os::Bottle& bot) const {
84 bot << this->sampleCount << this->trainCount;
85 // make sure to call the superclass's method
87}
88
89void DummyLearner::readBottle(yarp::os::Bottle& bot) {
90 // make sure to call the superclass's method
92 bot >> this->trainCount >> this->sampleCount;
93}
94
95
96} // learningmachine
97} // iCub
virtual void writeBottle(yarp::os::Bottle &bot) const
Writes a serialization of the machine into a bottle.
virtual void train()
Train the learning machine on the examples that have been supplied so far.
void reset()
Forget everything and start over.
std::string getInfo()
Asks the learning machine to return a string containing information on its operation so far.
virtual void feedSample(const yarp::sig::Vector &input, const yarp::sig::Vector &output)
Provide the learning machine with an example of the desired mapping.
Prediction predict(const yarp::sig::Vector &input)
Ask the learning machine to predict the output for a given input.
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a machine from a bottle.
virtual void writeBottle(yarp::os::Bottle &bot) const
Writes a serialization of the machine into a bottle.
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.
virtual bool checkDomainSize(const yarp::sig::Vector &input)
Checks whether the input is of the desired dimensionality.
unsigned int getCoDomainSize() const
Returns the size (dimensionality) of the output domain (codomain).
virtual std::string getInfo()
Asks the learning machine to return a string containing information on its operation so far.
A class that represents a prediction result.
Definition Prediction.h:44
std::string printVector(const yarp::sig::Vector &v)
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.