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/Portable.h>
27 #include <yarp/os/Bottle.h>
28 #include <yarp/os/Value.h>
29 
31 
32 namespace iCub {
33 namespace learningmachine {
34 
88 class IMachineLearner : public yarp::os::Portable {
89 protected:
93  std::string name;
94 
102  virtual void writeBottle(yarp::os::Bottle& bot) const = 0;
103 
111  virtual void readBottle(yarp::os::Bottle& bot) = 0;
112 
113 public:
117  IMachineLearner() : name("") { }
118 
122  virtual ~IMachineLearner() { }
123 
124 
128  virtual bool open(yarp::os::Searchable& config) {return true;}
129 
133  virtual bool close() {return true;}
134 
138  virtual bool configure(yarp::os::Searchable& config) {return false;}
139 
146  virtual void feedSample(const yarp::sig::Vector& input, const yarp::sig::Vector& output) = 0;
147 
154  virtual void train() { }
155 
162  virtual Prediction predict(const yarp::sig::Vector& input) = 0;
163 
169  virtual IMachineLearner* clone() = 0;
170 
174  virtual void reset() = 0;
175 
176  /*
177  * Inherited from Portable.
178  */
179  bool write(yarp::os::ConnectionWriter& connection) const {
180  yarp::os::Bottle model;
181  this->writeBottle(model);
182  return model.write(connection);
183  }
184 
185  /*
186  * Inherited from Portable.
187  */
188  bool read(yarp::os::ConnectionReader& connection) {
189  yarp::os::Bottle model;
190  model.read(connection);
191  this->readBottle(model);
192  return true;
193  }
194 
201  virtual std::string getInfo() {
202  return std::string("Type: ") + this->getName() + std::string("\n");
203  }
204 
211  virtual std::string getConfigHelp() {
212  return std::string("Machine configuration options for '") +
213  this->getName() + "'\n";
214  }
215 
221  virtual std::string toString() {
222  yarp::os::Bottle model;
223  this->writeBottle(model);
224  return model.toString().c_str();
225  }
226 
232  virtual bool fromString(const std::string& str) {
233  yarp::os::Bottle model(str.c_str());
234  this->readBottle(model);
235  return true;
236  }
237 
243  std::string getName() const {
244  return this->name;
245  }
246 
252  void setName(const std::string& name) {
253  this->name = name;
254  }
255 };
256 
257 } // learningmachine
258 } // iCub
259 
260 #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 bool configure(yarp::os::Searchable &config)
Change parameters.
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 bool close()
Shut the object down.
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.
virtual bool open(yarp::os::Searchable &config)
Initialize the object.
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.