iCub-main
LSSVMLearner.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_LSSVMLEARNER__
20 #define LM_LSSVMLEARNER__
21 
22 #include <vector>
23 #include <sstream>
24 
25 #include <yarp/os/IConfig.h>
26 #include <yarp/sig/Matrix.h>
27 
29 
30 
31 namespace iCub {
32 namespace learningmachine {
33 
34 class Kernel : public yarp::os::IConfig {
35 private:
36  std::string name;
37 public:
38  Kernel(std::string n = "") : name(n) { }
39 
40  virtual double evaluate(const yarp::sig::Vector& v1, const yarp::sig::Vector& v2) = 0;
41 
43  return std::string("Kernel configuration options for '") +
44  this->getName() + "'\n";
45  }
46 
47  virtual std::string getName() {
48  return this->name;
49  }
50 
51  virtual void setName(std::string n) {
52  this->name = n;
53  }
54 
55  virtual std::string getInfo() {
56  return this->getName();
57  }
58 };
59 
60 class RBFKernel : public Kernel {
61 private:
62  double gamma;
63 public:
64  RBFKernel(double g = 1.0) : Kernel("RBF"), gamma(g) {
65  }
66 
67  virtual ~RBFKernel() {}
68 
69  virtual double evaluate(const yarp::sig::Vector& v1, const yarp::sig::Vector& v2);
70 
71  virtual void setGamma(double g) {
72  this->gamma = g;
73  }
74 
75  virtual double getGamma() {
76  return this->gamma;
77  }
78 
79  /*
80  * Inherited from IConfig.
81  */
82  virtual bool configure(yarp::os::Searchable& config) {
83  bool success = false;
84  // format: set c dbl
85  if(config.find("gamma").isFloat64() || config.find("gamma").isInt32()) {
86  this->setGamma(config.find("gamma").asFloat64());
87  success = true;
88  }
89  return success;
90  }
91 
93  std::ostringstream buffer;
94  buffer << this->Kernel::getConfigHelp();
95  buffer << " gamma val RBF parameter gamma" << std::endl;
96  return buffer.str();
97  }
98 
99  virtual std::string getInfo() {
100  std::ostringstream buffer;
101  buffer << this->Kernel::getInfo();
102  buffer << " gamma: " << this->getGamma();
103  return buffer.str();
104  }
105 
106 };
107 
122 private:
126  std::vector<yarp::sig::Vector> inputs;
127 
131  std::vector<yarp::sig::Vector> outputs;
132 
136  yarp::sig::Matrix alphas;
137 
141  yarp::sig::Vector bias;
142 
146  yarp::sig::Vector LOO;
147 
151  double C;
152 
156  RBFKernel* kernel;
157 
158 
159 public:
167  LSSVMLearner(unsigned int dom = 1, unsigned int cod = 1, double c = 1.0);
168 
172  LSSVMLearner(const LSSVMLearner& other);
173 
177  virtual ~LSSVMLearner();
178 
182  virtual LSSVMLearner& operator=(const LSSVMLearner& other);
183 
184  /*
185  * Inherited from IMachineLearner.
186  */
187  virtual void feedSample(const yarp::sig::Vector& input, const yarp::sig::Vector& output);
188 
189  /*
190  * Inherited from IMachineLearner.
191  */
192  virtual void train();
193 
194  /*
195  * Inherited from IMachineLearner.
196  */
197  Prediction predict(const yarp::sig::Vector& input);
198 
199  /*
200  * Inherited from IMachineLearner.
201  */
202  void reset();
203 
204  /*
205  * Inherited from IMachineLearner.
206  */
207  LSSVMLearner* clone();
208 
209  /*
210  * Inherited from IMachineLearner.
211  */
212  virtual std::string getInfo();
213 
214  /*
215  * Inherited from IMachineLearner.
216  */
217  virtual std::string getConfigHelp();
218 
219  /*
220  * Inherited from IMachineLearner.
221  */
222  virtual void writeBottle(yarp::os::Bottle& bot);
223 
224  /*
225  * Inherited from IMachineLearner.
226  */
227  virtual void readBottle(yarp::os::Bottle& bot);
228 
229  /*
230  * Inherited from IFixedSizeLearner.
231  */
232  void setDomainSize(unsigned int size);
233 
234  /*
235  * Inherited from IFixedSizeLearner.
236  */
237  void setCoDomainSize(unsigned int size);
238 
239  /*
240  * Inherited from IConfig.
241  */
242  virtual bool configure(yarp::os::Searchable& config);
243 
249  virtual void setC(double C) {
250  this->C = C;
251  }
252 
258  virtual double getC() {
259  return this->C;
260  }
261 
267  virtual RBFKernel* getKernel() {
268  return this->kernel;
269  }
270 };
271 
272 } // learningmachine
273 } // iCub
274 #endif
iCub::learningmachine::RBFKernel::configure
virtual bool configure(yarp::os::Searchable &config)
Definition: LSSVMLearner.h:82
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::Kernel::setName
virtual void setName(std::string n)
Definition: LSSVMLearner.h:51
iCub::learningmachine::LSSVMLearner::~LSSVMLearner
virtual ~LSSVMLearner()
Destructor.
Definition: LSSVMLearner.cpp:66
iCub::learningmachine::LSSVMLearner::operator=
virtual LSSVMLearner & operator=(const LSSVMLearner &other)
Assignment operator.
Definition: LSSVMLearner.cpp:70
iCub::learningmachine::LSSVMLearner::getC
virtual double getC()
Accessor for the regularization parameter C.
Definition: LSSVMLearner.h:258
iCub::learningmachine::LSSVMLearner::feedSample
virtual void feedSample(const yarp::sig::Vector &input, const yarp::sig::Vector &output)
Provide the learning machine with an example of the desired mapping.
Definition: LSSVMLearner.cpp:86
iCub::learningmachine::Kernel::Kernel
Kernel(std::string n="")
Definition: LSSVMLearner.h:38
iCub::learningmachine::Kernel::getName
virtual std::string getName()
Definition: LSSVMLearner.h:47
iCub::learningmachine::LSSVMLearner::predict
Prediction predict(const yarp::sig::Vector &input)
Ask the learning machine to predict the output for a given input.
Definition: LSSVMLearner.cpp:145
iCub::learningmachine::LSSVMLearner::getConfigHelp
virtual std::string getConfigHelp()
Asks the learning machine to return a string containing the list of configuration options that it sup...
Definition: LSSVMLearner.cpp:184
iCub::learningmachine::LSSVMLearner::setCoDomainSize
void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
Definition: LSSVMLearner.cpp:251
iCub::learningmachine::LSSVMLearner::train
virtual void train()
Train the learning machine on the examples that have been supplied so far.
Definition: LSSVMLearner.cpp:94
iCub::learningmachine::LSSVMLearner::reset
void reset()
Forget everything and start over.
Definition: LSSVMLearner.cpp:161
iCub::learningmachine::LSSVMLearner::writeBottle
virtual void writeBottle(yarp::os::Bottle &bot)
Definition: LSSVMLearner.cpp:193
iCub::learningmachine::RBFKernel::evaluate
virtual double evaluate(const yarp::sig::Vector &v1, const yarp::sig::Vector &v2)
Definition: LSSVMLearner.cpp:35
IFixedSizeLearner.h
iCub::learningmachine::LSSVMLearner::readBottle
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a machine from a bottle.
Definition: LSSVMLearner.cpp:218
iCub::learningmachine::RBFKernel::setGamma
virtual void setGamma(double g)
Definition: LSSVMLearner.h:71
iCub::learningmachine::LSSVMLearner::setC
virtual void setC(double C)
Mutator for the regularization parameter C.
Definition: LSSVMLearner.h:249
iCub::learningmachine::RBFKernel
Definition: LSSVMLearner.h:60
n
int n
Definition: debugFunctions.cpp:58
iCub::learningmachine::LSSVMLearner::LSSVMLearner
LSSVMLearner(unsigned int dom=1, unsigned int cod=1, double c=1.0)
Constructor.
Definition: LSSVMLearner.cpp:49
string
string(REPLACE "-rdynamic" "" CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS}") include_directories($
Definition: CMakeLists.txt:9
iCub::learningmachine::Prediction
Definition: Prediction.h:44
iCub::learningmachine::RBFKernel::getInfo
virtual std::string getInfo()
Definition: LSSVMLearner.h:99
iCub::learningmachine::Kernel::getConfigHelp
virtual std::string getConfigHelp()
Definition: LSSVMLearner.h:42
iCub::learningmachine::RBFKernel::getConfigHelp
virtual std::string getConfigHelp()
Definition: LSSVMLearner.h:92
iCub::learningmachine::IFixedSizeLearner
Definition: IFixedSizeLearner.h:44
iCub::learningmachine::LSSVMLearner
Definition: LSSVMLearner.h:121
iCub::learningmachine::RBFKernel::RBFKernel
RBFKernel(double g=1.0)
Definition: LSSVMLearner.h:64
iCub::learningmachine::Kernel
Definition: LSSVMLearner.h:34
iCub::learningmachine::LSSVMLearner::configure
virtual bool configure(yarp::os::Searchable &config)
Definition: LSSVMLearner.cpp:256
iCub::learningmachine::RBFKernel::~RBFKernel
virtual ~RBFKernel()
Definition: LSSVMLearner.h:67
iCub::learningmachine::LSSVMLearner::getKernel
virtual RBFKernel * getKernel()
Accessor for the kernel.
Definition: LSSVMLearner.h:267
iCub::learningmachine::LSSVMLearner::getInfo
virtual std::string getInfo()
Asks the learning machine to return a string containing information on its operation so far.
Definition: LSSVMLearner.cpp:173
iCub::learningmachine::LSSVMLearner::setDomainSize
void setDomainSize(unsigned int size)
Mutator for the domain size.
Definition: LSSVMLearner.cpp:247
iCub::learningmachine::LSSVMLearner::clone
LSSVMLearner * clone()
Asks the learning machine to return a clone of its type.
Definition: LSSVMLearner.cpp:169
iCub::learningmachine::Kernel::getInfo
virtual std::string getInfo()
Definition: LSSVMLearner.h:55
iCub::learningmachine::Kernel::evaluate
virtual double evaluate(const yarp::sig::Vector &v1, const yarp::sig::Vector &v2)=0
iCub::learningmachine::RBFKernel::getGamma
virtual double getGamma()
Definition: LSSVMLearner.h:75