iCub-main
SparseSpectrumFeature.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 <cassert>
20 #include <stdexcept>
21 #include <sstream>
22 #include <algorithm>
23 #include <cmath>
24 
25 #include <yarp/math/Math.h>
26 #include <yarp/math/Rand.h>
27 
31 
32 //#define TWOPI 6.2831853071795862
33 
34 using namespace yarp::math;
35 using namespace iCub::learningmachine::math;
37 
38 namespace iCub {
39 namespace learningmachine {
40 
41 SparseSpectrumFeature::SparseSpectrumFeature(unsigned int dom, unsigned int cod, double sigma,
42  yarp::sig::Vector ell) {
43  this->setName("SparseSpectrumFeature");
44  // ell has to be initialized *prior* to anything that could reset this instance
45  this->setEll(ell);
46  this->setDomainSize(dom);
47  this->setCoDomainSize(cod);
48  // will initiate reset automatically
49  this->setSigma(sigma);
50 }
51 
52 yarp::sig::Vector SparseSpectrumFeature::transform(const yarp::sig::Vector& input) {
53  yarp::sig::Vector output = this->IFixedSizeTransformer::transform(input);
54 
55  yarp::sig::Vector inputW = (this->W * input);
56  int nproj = this->getCoDomainSize() >> 1;
57  double factor = this->sigma / sqrt((double)nproj);
58  for(int i = 0; i < nproj; i++) {
59  output(i) = cos(inputW(i)) * factor;
60  output(i+nproj) = sin(inputW(i)) * factor;
61  }
62  return output;
63 }
64 
65 void SparseSpectrumFeature::setDomainSize(unsigned int size) {
66  // call method in base class
67  this->IFixedSizeTransformer::setDomainSize(size);
68  // rebuild projection matrix
69  this->reset();
70 }
71 
72 void SparseSpectrumFeature::setCoDomainSize(unsigned int size) {
73  if((size & 0x1) == 0) {
74  // call method in base class
75  this->IFixedSizeTransformer::setCoDomainSize(size);
76  // rebuild projection matrix
77  this->reset();
78  } else {
79  throw std::runtime_error("Size of codomain of Sparse Spectrum Features has to be even");
80  }
81 }
82 
83 void SparseSpectrumFeature::reset() {
84  this->IFixedSizeTransformer::reset();
85 
86  // create pseudo random number generators
87  yarp::math::RandnScalar prng_normal;
88 
89  // create new projection matrix
90  this->W = yarp::sig::Matrix((int) this->getCoDomainSize() / 2, (int) this->getDomainSize());
91 
92  // fill projection matrix with random and scaled data
93  for(int r = 0; r < this->W.rows(); r++) {
94  for(int c = 0; c < this->W.cols(); c++) {
95  this->W(r, c) = prng_normal.get() / this->ell(c);
96  }
97  }
98 }
99 
100 void SparseSpectrumFeature::writeBottle(yarp::os::Bottle& bot) {
101  bot << this->getSigma() << this->ell << this->W;
102 
103  // make sure to call the superclass's method
104  this->IFixedSizeTransformer::writeBottle(bot);
105 }
106 
107 void SparseSpectrumFeature::readBottle(yarp::os::Bottle& bot) {
108  // make sure to call the superclass's method
109  this->IFixedSizeTransformer::readBottle(bot);
110 
111  // directly write to sigma to prevent resetting W
112  bot >> this->W >> this->ell >> this->sigma;
113  this->setSigma(sigma);
114 }
115 
116 void SparseSpectrumFeature::setEll(yarp::sig::Vector& ell) {
117  yarp::sig::Vector ls = yarp::sig::Vector(this->getDomainSize());
118  ls = 1.;
119  for(unsigned int i = 0; (int)i < std::min(ell.size(), ls.size()); i++) {
120  ls(i) = ell(i);
121  }
122  this->ell = ls;
123  // rebuild projection matrix
124  this->reset();
125 }
126 
127 std::string SparseSpectrumFeature::getInfo() {
128  std::ostringstream buffer;
129  buffer << this->IFixedSizeTransformer::getInfo();
130  buffer << " sigma: " << this->sigma << " | ";
131  buffer << " ell: " << this->ell.toString();
132  return buffer.str();
133 }
134 
135 std::string SparseSpectrumFeature::getConfigHelp() {
136  std::ostringstream buffer;
137  buffer << this->IFixedSizeTransformer::getConfigHelp();
138  buffer << " sigma val Set sigma parameter" << std::endl;
139  buffer << " ell (list) Set lambda parameter" << std::endl;
140  return buffer.str();
141 }
142 
143 bool SparseSpectrumFeature::configure(yarp::os::Searchable &config) {
144  bool success = this->IFixedSizeTransformer::configure(config);
145 
146  // format: set sigma val
147  if(config.find("sigma").isFloat64() || config.find("sigma").isInt32()) {
148  this->setSigma(config.find("sigma").asFloat64());
149  success = true;
150  }
151 
152  // format: set ell val | set ell (val ... val)
153  if(config.find("ell").isFloat64() || config.find("ell").isInt32()) {
154  yarp::sig::Vector ls = yarp::sig::Vector(this->getDomainSize());
155  ls = config.find("ell").asFloat64();
156  this->setEll(ls);
157  success = true;
158  } else if(config.find("ell").isList()) {
159  yarp::os::Bottle* b = config.find("ell").asList();
160  assert(b != (yarp::os::Bottle*) 0x0);
161  yarp::sig::Vector ls(0);
162  for(int i = 0; i < b->size(); i++) {
163  if(b->get(i).isFloat64() || b->get(i).isInt32()) {
164  ls.push_back(b->get(i).asFloat64());
165  }
166  }
167  this->setEll(ls);
168  success = true;
169  }
170  return success;
171 }
172 
173 
174 } // learningmachine
175 } // iCub
176 
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.
const FSC min
Definition: strain.h:49