iCub-main
IMachineLearner.h
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 #ifndef LM_IMACHINELEARNER__
20 #define LM_IMACHINELEARNER__
21 
22 #include <string>
23 #include <sstream>
24 
25 #include <yarp/sig/Vector.h>
26 #include <yarp/os/IConfig.h>
27 #include <yarp/os/Portable.h>
28 #include <yarp/os/Bottle.h>
29 #include <yarp/os/Value.h>
30 
32 
33 namespace iCub {
34 namespace learningmachine {
35 
89 class IMachineLearner : public yarp::os::IConfig, public yarp::os::Portable {
90 protected:
94  std::string name;
95 
103  virtual void writeBottle(yarp::os::Bottle& bot) const = 0;
104 
112  virtual void readBottle(yarp::os::Bottle& bot) = 0;
113 
114 public:
118  IMachineLearner() : name("") { }
119 
123  virtual ~IMachineLearner() { }
124 
131  virtual void feedSample(const yarp::sig::Vector& input, const yarp::sig::Vector& output) = 0;
132 
139  virtual void train() { }
140 
147  virtual Prediction predict(const yarp::sig::Vector& input) = 0;
148 
154  virtual IMachineLearner* clone() = 0;
155 
159  virtual void reset() = 0;
160 
161  /*
162  * Inherited from Portable.
163  */
164  bool write(yarp::os::ConnectionWriter& connection) const {
165  yarp::os::Bottle model;
166  this->writeBottle(model);
167  return model.write(connection);
168  }
169 
170  /*
171  * Inherited from Portable.
172  */
173  bool read(yarp::os::ConnectionReader& connection) {
174  yarp::os::Bottle model;
175  model.read(connection);
176  this->readBottle(model);
177  return true;
178  }
179 
186  virtual std::string getInfo() {
187  return std::string("Type: ") + this->getName() + std::string("\n");
188  }
189 
196  virtual std::string getConfigHelp() {
197  return std::string("Machine configuration options for '") +
198  this->getName() + "'\n";
199  }
200 
206  virtual std::string toString() {
207  yarp::os::Bottle model;
208  this->writeBottle(model);
209  return model.toString().c_str();
210  }
211 
217  virtual bool fromString(const std::string& str) {
218  yarp::os::Bottle model(str.c_str());
219  this->readBottle(model);
220  return true;
221  }
222 
228  std::string getName() const {
229  return this->name;
230  }
231 
237  void setName(const std::string& name) {
238  this->name = name;
239  }
240 };
241 
242 } // learningmachine
243 } // iCub
244 
245 #endif
A generalized interface for a learning machine for offline and online learning machines (e....
virtual bool fromString(const std::string &str)
Asks the learning machine to initialize from a string serialization.
virtual void readBottle(yarp::os::Bottle &bot)=0
Unserializes a machine from a bottle.
std::string getName() const
Retrieve the name of this machine learning technique.
virtual ~IMachineLearner()
Destructor (empty).
std::string name
The name of this type of machine learner.
virtual IMachineLearner * clone()=0
Asks the learning machine to return a clone of its type.
virtual void writeBottle(yarp::os::Bottle &bot) const =0
Writes a serialization of the machine into a bottle.
bool read(yarp::os::ConnectionReader &connection)
virtual void reset()=0
Forget everything and start over.
virtual void train()
Train the learning machine on the examples that have been supplied so far.
virtual Prediction predict(const yarp::sig::Vector &input)=0
Ask the learning machine to predict the output for a given input.
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.
virtual void feedSample(const yarp::sig::Vector &input, const yarp::sig::Vector &output)=0
Provide the learning machine with an example of the desired mapping.
bool write(yarp::os::ConnectionWriter &connection) const
virtual std::string toString()
Asks the learning machine to return a string serialization.
void setName(const std::string &name)
Set the name of this machine learning technique.
A class that represents a prediction result.
Definition: Prediction.h:44
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.