iCub-main
Prediction.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_PREDICTION__
20 #define LM_PREDICTION__
21 
22 #include <string>
23 #include <cassert>
24 
25 #include <yarp/os/ConnectionReader.h>
26 #include <yarp/os/ConnectionWriter.h>
27 #include <yarp/os/Portable.h>
28 #include <yarp/sig/Vector.h>
29 
30 namespace iCub {
31 namespace learningmachine {
32 
44 class Prediction : public yarp::os::Portable {
45 protected:
49  yarp::sig::Vector prediction;
50 
54  yarp::sig::Vector variance;
55 
60 
61 public:
66  this->setPrediction(yarp::sig::Vector(0));
67  this->setVariance(yarp::sig::Vector(0));
68  }
69 
77  Prediction(const yarp::sig::Vector& prediction) {
78  this->setPrediction(prediction);
79  this->setVariance(yarp::sig::Vector(0));
80  }
81 
87  Prediction(const yarp::sig::Vector& prediction, const yarp::sig::Vector& variance) {
88  this->setPrediction(prediction);
89  this->setVariance(variance);
90  }
91 
97  unsigned int size() {
98  return this->prediction.size();
99  }
100 
106  yarp::sig::Vector getPrediction() {
107  return this->prediction;
108  }
109 
115  void setPrediction(const yarp::sig::Vector& prediction) {
116  this->prediction = prediction;
117  }
118 
125  yarp::sig::Vector getVariance() {
126  return this->variance;
127  }
128 
135  void setVariance(const yarp::sig::Vector& variance) {
136  assert(prediction.size() == variance.size() || variance.size() == 0);
137  this->variance = variance;
138  this->varianceSet = (variance.size() > 0);
139  }
140 
146  bool hasVariance() {
147  return this->varianceSet;
148  }
149 
155  std::string toString() {
156  std::string out = this->prediction.toString().c_str();
157  if(this->hasVariance()) {
158  out += std::string(" +/- ") + this->variance.toString().c_str();
159  }
160  return out;
161  }
162 
163  /*
164  * Inherited from Portable.
165  */
166  bool write(yarp::os::ConnectionWriter& connection) const {
167  // follows PortablePair implementation
168  connection.appendInt32(BOTTLE_TAG_LIST);
169  connection.appendInt32(2);
170 
171  bool ok = this->prediction.write(connection);
172  if (ok) {
173  ok = this->variance.write(connection);
174  }
175 
176  if (ok) {
177  connection.convertTextMode();
178  }
179 
180  return ok;
181  }
182 
183  /*
184  * Inherited from Portable.
185  */
186  bool read(yarp::os::ConnectionReader& connection) {
187  // follows PortablePair implementation
188  connection.convertTextMode();
189 
190  int header = connection.expectInt32();
191  if(header != BOTTLE_TAG_LIST) {
192  return false;
193  }
194 
195  int len = connection.expectInt32();
196  if(len != 2) {
197  return false;
198  }
199 
200  bool ok = this->prediction.read(connection);
201  if (ok) {
202  ok = this->variance.read(connection);
203  }
204  this->varianceSet = (this->variance.size() > 0);
205  return ok;
206  }
207 
208  /*
209  * Inherited from Portable.
210  */
211  virtual void onCompletion() {
212  this->prediction.onCompletion();
213  this->variance.onCompletion();
214  }
215 
216 };
217 
218 
219 } // learningmachine
220 } // iCub
221 
222 #endif
A class that represents a prediction result.
Definition: Prediction.h:44
Prediction(const yarp::sig::Vector &prediction, const yarp::sig::Vector &variance)
Constructor for expected value prediction with predictive variance.
Definition: Prediction.h:87
std::string toString()
Returns a string represenation of the prediction.
Definition: Prediction.h:155
unsigned int size()
Returns the size of the prediction.
Definition: Prediction.h:97
yarp::sig::Vector getVariance()
Accessor for the variance of the prediction.
Definition: Prediction.h:125
yarp::sig::Vector prediction
Expected value of the prediction.
Definition: Prediction.h:49
Prediction(const yarp::sig::Vector &prediction)
Constructor for expected value prediction without predictive variance.
Definition: Prediction.h:77
bool varianceSet
Indicator whether prediction variance is available.
Definition: Prediction.h:59
bool write(yarp::os::ConnectionWriter &connection) const
Definition: Prediction.h:166
yarp::sig::Vector variance
Optional variance of the prediction, measured as a unit standard deviation.
Definition: Prediction.h:54
void setPrediction(const yarp::sig::Vector &prediction)
Mutator for the expected value of the prediction.
Definition: Prediction.h:115
void setVariance(const yarp::sig::Vector &variance)
Mutator for the variance of the prediction.
Definition: Prediction.h:135
Prediction()
Empty constructor.
Definition: Prediction.h:65
bool hasVariance()
Indicator whether the prediction contains a predicted variance.
Definition: Prediction.h:146
bool read(yarp::os::ConnectionReader &connection)
Definition: Prediction.h:186
yarp::sig::Vector getPrediction()
Accessor for the expected value of the prediction.
Definition: Prediction.h:106
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.
out
Definition: sine.m:8