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/sig/Matrix.h>
26 
28 
29 
30 namespace iCub {
31 namespace learningmachine {
32 
33 class Kernel {
34 private:
35  std::string name;
36 public:
37  Kernel(std::string n = "") : name(n) { }
38 
39  virtual double evaluate(const yarp::sig::Vector& v1, const yarp::sig::Vector& v2) = 0;
40 
41  virtual std::string getConfigHelp() {
42  return std::string("Kernel configuration options for '") +
43  this->getName() + "'\n";
44  }
45 
46  virtual std::string getName() {
47  return this->name;
48  }
49 
50  virtual void setName(std::string n) {
51  this->name = n;
52  }
53 
54  virtual std::string getInfo() {
55  return this->getName();
56  }
57 };
58 
59 class RBFKernel : public Kernel {
60 private:
61  double gamma;
62 public:
63  RBFKernel(double g = 1.0) : Kernel("RBF"), gamma(g) {
64  }
65 
66  virtual ~RBFKernel() {}
67 
68  virtual double evaluate(const yarp::sig::Vector& v1, const yarp::sig::Vector& v2);
69 
70  virtual void setGamma(double g) {
71  this->gamma = g;
72  }
73 
74  virtual double getGamma() {
75  return this->gamma;
76  }
77 
78  /*
79  * Inherited from Kernel.
80  */
81  virtual bool configure(yarp::os::Searchable& config) {
82  bool success = false;
83  // format: set c dbl
84  if(config.find("gamma").isFloat64() || config.find("gamma").isInt32()) {
85  this->setGamma(config.find("gamma").asFloat64());
86  success = true;
87  }
88  return success;
89  }
90 
91  virtual std::string getConfigHelp() {
92  std::ostringstream buffer;
93  buffer << this->Kernel::getConfigHelp();
94  buffer << " gamma val RBF parameter gamma" << std::endl;
95  return buffer.str();
96  }
97 
98  virtual std::string getInfo() {
99  std::ostringstream buffer;
100  buffer << this->Kernel::getInfo();
101  buffer << " gamma: " << this->getGamma();
102  return buffer.str();
103  }
104 
105 };
106 
121 private:
125  std::vector<yarp::sig::Vector> inputs;
126 
130  std::vector<yarp::sig::Vector> outputs;
131 
135  yarp::sig::Matrix alphas;
136 
140  yarp::sig::Vector bias;
141 
145  yarp::sig::Vector LOO;
146 
150  double C;
151 
155  RBFKernel* kernel;
156 
157 
158 public:
166  LSSVMLearner(unsigned int dom = 1, unsigned int cod = 1, double c = 1.0);
167 
171  LSSVMLearner(const LSSVMLearner& other);
172 
176  virtual ~LSSVMLearner();
177 
181  virtual LSSVMLearner& operator=(const LSSVMLearner& other);
182 
183  /*
184  * Inherited from IMachineLearner.
185  */
186  virtual void feedSample(const yarp::sig::Vector& input, const yarp::sig::Vector& output);
187 
188  /*
189  * Inherited from IMachineLearner.
190  */
191  virtual void train();
192 
193  /*
194  * Inherited from IMachineLearner.
195  */
196  Prediction predict(const yarp::sig::Vector& input);
197 
198  /*
199  * Inherited from IMachineLearner.
200  */
201  void reset();
202 
203  /*
204  * Inherited from IMachineLearner.
205  */
206  LSSVMLearner* clone();
207 
208  /*
209  * Inherited from IMachineLearner.
210  */
211  virtual std::string getInfo();
212 
213  /*
214  * Inherited from IMachineLearner.
215  */
216  virtual std::string getConfigHelp();
217 
218  /*
219  * Inherited from IMachineLearner.
220  */
221  virtual void writeBottle(yarp::os::Bottle& bot);
222 
223  /*
224  * Inherited from IMachineLearner.
225  */
226  virtual void readBottle(yarp::os::Bottle& bot);
227 
228  /*
229  * Inherited from IFixedSizeLearner.
230  */
231  void setDomainSize(unsigned int size);
232 
233  /*
234  * Inherited from IFixedSizeLearner.
235  */
236  void setCoDomainSize(unsigned int size);
237 
238  /*
239  * Inherited from IMachineLearner.
240  */
241  virtual bool configure(yarp::os::Searchable& config);
242 
248  virtual void setC(double C) {
249  this->C = C;
250  }
251 
257  virtual double getC() {
258  return this->C;
259  }
260 
266  virtual RBFKernel* getKernel() {
267  return this->kernel;
268  }
269 };
270 
271 } // learningmachine
272 } // iCub
273 #endif
An generalized interface for a learning machine with a fixed domain and codomain size.
virtual std::string getInfo()
Definition: LSSVMLearner.h:54
Kernel(std::string n="")
Definition: LSSVMLearner.h:37
virtual std::string getConfigHelp()
Definition: LSSVMLearner.h:41
virtual double evaluate(const yarp::sig::Vector &v1, const yarp::sig::Vector &v2)=0
virtual void setName(std::string n)
Definition: LSSVMLearner.h:50
virtual std::string getName()
Definition: LSSVMLearner.h:46
This is basic implementation of the LSSVM algorithms.
Definition: LSSVMLearner.h:120
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 writeBottle(yarp::os::Bottle &bot)
virtual double getC()
Accessor for the regularization parameter C.
Definition: LSSVMLearner.h:257
void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
void reset()
Forget everything and start over.
virtual ~LSSVMLearner()
Destructor.
void setDomainSize(unsigned int size)
Mutator for the domain size.
virtual void setC(double C)
Mutator for the regularization parameter C.
Definition: LSSVMLearner.h:248
LSSVMLearner * clone()
Asks the learning machine to return a clone of its type.
Prediction predict(const yarp::sig::Vector &input)
Ask the learning machine to predict the output for a given input.
virtual void train()
Train the learning machine on the examples that have been supplied so far.
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a machine from a bottle.
LSSVMLearner(unsigned int dom=1, unsigned int cod=1, double c=1.0)
Constructor.
virtual std::string getInfo()
Asks the learning machine to return a string containing information on its operation so far.
virtual RBFKernel * getKernel()
Accessor for the kernel.
Definition: LSSVMLearner.h:266
virtual LSSVMLearner & operator=(const LSSVMLearner &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.
A class that represents a prediction result.
Definition: Prediction.h:44
virtual std::string getConfigHelp()
Definition: LSSVMLearner.h:91
virtual std::string getInfo()
Definition: LSSVMLearner.h:98
virtual void setGamma(double g)
Definition: LSSVMLearner.h:70
virtual double evaluate(const yarp::sig::Vector &v1, const yarp::sig::Vector &v2)
virtual bool configure(yarp::os::Searchable &config)
Definition: LSSVMLearner.h:81
int n
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.