20 #include <yarp/math/Math.h>
21 #include <yarp/math/SVD.h>
32 using namespace yarp::os;
33 using namespace yarp::sig;
34 using namespace yarp::math;
43 int m=(int)spatialCompetence.A.rows();
44 int n=(int)spatialCompetence.A.cols();
45 Matrix U(m,
n),V(
n,
n); Vector S(
n);
46 SVD(spatialCompetence.A,U,S,V);
48 spatialCompetence.radii.resize(3);
49 spatialCompetence.radii[0]=(S[0]!=0.0)?(1.0/sqrt(fabs(S[0]))):0.0;
50 spatialCompetence.radii[1]=(S[1]!=0.0)?(1.0/sqrt(fabs(S[1]))):0.0;
51 spatialCompetence.radii[2]=(S[2]!=0.0)?(1.0/sqrt(fabs(S[2]))):0.0;
60 Matrix R(4,4); R.zero(); R(3,3)=1.0;
61 R.setSubmatrix(V,0,0);
62 spatialCompetence.R=SE3inv(R);
72 if (computeSpatialTransformation())
95 Vector
x=(point.subVector(0,2)-spatialCompetence.c)/spatialCompetence.scale;
96 if (
dot(
x,spatialCompetence.A*
x)<=1.0)
98 else if (spatialCompetence.extrapolation)
102 x=spatialCompetence.R*
x;
111 double cos_theta=
x[2]/
norm(
x);
112 double theta=acos(cos_theta);
113 double phi=atan2(
x[1],
x[0]);
116 double cos_phi=cos(phi);
117 xp[0]=spatialCompetence.radii[0]*cos_theta*cos_phi;
118 xp[1]=spatialCompetence.radii[1]*sin(theta)*cos_phi;
119 xp[2]=spatialCompetence.radii[2]*sin(phi);
122 return exp(-40.0*d*d);
133 Bottle &values=
data.addList();
134 values.addString(spatialCompetence.extrapolation?
"true":
"false");
135 values.addFloat64(spatialCompetence.scale);
136 values.addFloat64(spatialCompetence.c[0]);
137 values.addFloat64(spatialCompetence.c[1]);
138 values.addFloat64(spatialCompetence.c[2]);
139 for (
int r=0; r<spatialCompetence.A.rows(); r++)
140 for (
int c=0; c<spatialCompetence.A.cols(); c++)
141 values.addFloat64(spatialCompetence.A(r,c));
143 info.put(
"spatial_competence",
data.get(0));
151 if (!
info.check(
"spatial_competence"))
154 spatialCompetence.A=eye(3,3);
155 spatialCompetence.c=Vector(3,0.0);
156 spatialCompetence.scale=1.0;
157 spatialCompetence.extrapolation=
true;
158 if (Bottle *values=
info.find(
"spatial_competence").asList())
160 spatialCompetence.extrapolation=(values->get(0).asString()==
"true");
161 spatialCompetence.scale=values->get(1).asFloat64();
162 spatialCompetence.c[0]=values->get(2).asFloat64();
163 spatialCompetence.c[1]=values->get(3).asFloat64();
164 spatialCompetence.c[2]=values->get(4).asFloat64();
166 for (
int i=5; i<values->size(); i++)
168 spatialCompetence.A(r,c)=values->get(i).asFloat64();
169 if (++c>=spatialCompetence.A.cols())
172 if (++r>=spatialCompetence.A.rows())
178 computeSpatialTransformation();
197 if ((this->type!=
"se3") && (this->type!=
"se3+scale"))
206 copySuperClassData(calibrator);
208 scale=calibrator.
scale;
226 if ((in.length()>=3) && (
out.length()>=3))
228 Vector _in=in.subVector(0,2);
229 Vector _out=
out.subVector(0,2);
231 this->in.push_back(_in);
232 this->out.push_back(_out);
233 return impl->addPoints(_in,_out);
265 if (computeSpatialCompetence(in))
267 spatialCompetence.scale=1.2;
280 Vector _in=in.subVector(0,2);
296 Bottle &values=
data.addList();
297 values.addFloat64(scale);
298 for (
int r=0; r<
H.rows(); r++)
299 for (
int c=0; c<
H.cols(); c++)
300 values.addFloat64(
H(r,c));
303 info.put(
"type",type);
304 info.put(
"calibration_data",
data.get(0));
312 if (!
info.check(
"type"))
315 string type=
info.find(
"type").asString();
316 if ((type==
"se3") || (type==
"se3+scale"))
321 else if (type==
"affine")
331 if (Bottle *values=
info.find(
"calibration_data").asList())
333 scale=values->get(0).asFloat64();
336 for (
int i=1; i<values->size(); i++)
338 H(r,c)=values->get(i).asFloat64();
369 copySuperClassData(calibrator);
377 if ((in.length()>=3) && (
out.length()>=3))
379 Vector _in=in.subVector(0,2);
380 Vector _out=
out.subVector(0,2);
381 impl->feedSample(in,
out);
383 this->in.push_back(_in);
384 this->out.push_back(_out);
414 if (getNumPoints()==0)
420 for (
size_t i=0; i<getNumPoints(); i++)
426 error/=getNumPoints();
428 if (computeSpatialCompetence(in))
430 spatialCompetence.scale=1.0;
443 Prediction prediction=impl->predict(in.subVector(0,2));
456 Bottle &values=
data.addList();
457 values.addString(impl->toString());
460 info.put(
"type",type);
461 info.put(
"calibration_data",
data.get(0));
469 if (!
info.check(
"type"))
472 string type=
info.find(
"type").asString();
479 if (Bottle *values=
info.find(
"calibration_data").asList())
480 if (values->size()>=1)
481 ret=impl->fromString(values->toString());
516 models.push_back(calibrator);
527 Vector _in=in.subVector(0,2);
528 Vector outExperts(_in.length(),0.0);
529 Vector outExtrapolators(_in.length(),0.0);
530 double sumExperts=0.0;
531 double sumExtrapolators=0.0;
534 for (
size_t i=0; i<models.size(); i++)
536 double competence=models[i]->getSpatialCompetence(_in);
540 models[i]->retrieve(_in,pred);
542 outExtrapolators+=competence*pred;
543 sumExtrapolators+=competence;
546 outExperts+=competence*pred;
547 sumExperts+=competence;
555 out=outExperts/sumExperts;
559 else if (sumExtrapolators!=0.0)
561 out=outExtrapolators/sumExtrapolators;
573 for (
size_t i=0; i<models.size(); i++)
std::deque< yarp::sig::Vector > out
virtual bool toProperty(yarp::os::Property &info) const
virtual bool computeSpatialCompetence(const std::deque< yarp::sig::Vector > &points)
virtual bool fromProperty(const yarp::os::Property &info)
void copySuperClassData(const Calibrator &src)
virtual bool computeSpatialTransformation()
std::deque< yarp::sig::Vector > in
struct Calibrator::SpatialCompetence spatialCompetence
virtual std::string getType() const
virtual double getSpatialCompetence(const yarp::sig::Vector &point)
virtual ~LSSVMCalibrator()
virtual bool addPoints(const yarp::sig::Vector &in, const yarp::sig::Vector &out)
virtual bool getPoints(std::deque< yarp::sig::Vector > &in, std::deque< yarp::sig::Vector > &out) const
LSSVMCalibrator(const std::string &type="lssvm")
virtual bool clearPoints()
iCub::learningmachine::IMachineLearner * impl
virtual bool toProperty(yarp::os::Property &info) const
virtual bool calibrate(double &error)
virtual bool fromProperty(const yarp::os::Property &info)
virtual bool retrieve(const yarp::sig::Vector &in, yarp::sig::Vector &out)
virtual LocallyWeightedExperts & operator<<(Calibrator &c)
virtual Calibrator * operator[](const size_t i)
virtual bool retrieve(const yarp::sig::Vector &in, yarp::sig::Vector &out)
virtual ~LocallyWeightedExperts()
virtual bool clearPoints()
virtual bool fromProperty(const yarp::os::Property &info)
virtual bool calibrate(double &error)
MatrixCalibrator(const std::string &type="se3")
virtual bool retrieve(const yarp::sig::Vector &in, yarp::sig::Vector &out)
virtual bool addPoints(const yarp::sig::Vector &in, const yarp::sig::Vector &out)
virtual bool toProperty(yarp::os::Property &info) const
virtual bool getPoints(std::deque< yarp::sig::Vector > &in, std::deque< yarp::sig::Vector > &out) const
iCub::optimization::MatrixTransformationWithMatchedPoints * impl
virtual ~MatrixCalibrator()
This is basic implementation of the LSSVM algorithms.
void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
void setDomainSize(unsigned int size)
Mutator for the domain size.
virtual void setC(double C)
Mutator for the regularization parameter C.
virtual RBFKernel * getKernel()
Accessor for the kernel.
A class that represents a prediction result.
yarp::sig::Vector getPrediction()
Accessor for the expected value of the prediction.
virtual void setGamma(double g)
A class that deals with the problem of determining the affine transformation matrix A between two set...
A class that deals with the problem of determining the roto-translation matrix H and scaling factors ...
virtual bool calibrate(yarp::sig::Matrix &H, double &error)
Perform reference calibration to determine the matrix H.
bool minVolumeEllipsoid(const std::deque< yarp::sig::Vector > &points, const double tol, yarp::sig::Matrix &A, yarp::sig::Vector &c)
Find the minimum volume ellipsoide (MVEE) of a set of N d-dimensional data points.
double dot(const yarp::sig::Matrix &A, int colA, const yarp::sig::Matrix &B, int colB)
Returns the dot product between two vectors given in the form: matrix(:,col).
double norm(const yarp::sig::Matrix &M, int col)
Returns the norm of the vector given in the form: matrix(:,col).
yarp::sig::Vector cross(const yarp::sig::Matrix &A, int colA, const yarp::sig::Matrix &B, int colB)
Returns the cross product between two vectors given in the form: matrix(:,col).