36 #define SWAP(x,y) tmp = a[x];a[x]=a[y];a[y] = tmp; 37 #define ROUND(x) rnd(x*10)/10.0) 46 boost::variate_generator<boost::mt19937&, boost::uniform_01<double> >
pRngUniform01(pRngMt19937, pRngDist);
60 void CVec::print()
const 62 printf(
"%f %f %f", x, y, z);
65 std::string CVec::toString()
68 sprintf(str,
"%f %f %f", x, y, z);
69 return std::string(str);
75 set( 1.0, 0.0, 0.0, 0.0,
81 void CMatrix::randomOrientation(
double max)
89 CMathLib::getMatrixFromRotation(*
this, tmp,
getUniform() * max);
94 set( 1.0, 0.0, 0.0, 0.0,
100 void CMatrix::printText()
108 CMathLib::getOrientation(*
this, tmp1, tmp2);
110 a = tmp1.
x * 180.0/M_PI;
111 b = tmp1.
y * 180.0/M_PI;
112 g = tmp1.
z * 180.0/M_PI;
114 printf(
"%g %g %g %g %g %g\n", x, y, z, a, b, g);
118 void CMatrix::transpose()
137 a[0] = a0; a[4] = a4; a[8] = a8; a[12] = a12;
138 a[1] = a1; a[5] = a5; a[9] = a9; a[13] = a13;
139 a[2] = a2; a[6] = a6; a[10] = a10; a[14] = a14;
140 a[3] = a3; a[7] = a7; a[11] = a11; a[15] = a15;
149 a[0] = a0; a[4] = a4; a[8] = a8; a[12] = a12;
150 a[1] = a1; a[5] = a5; a[9] = a9; a[13] = a13;
151 a[2] = a2; a[6] = a6; a[10] = a10; a[14] = a14;
152 a[3] = a3; a[7] = a7; a[11] = a11; a[15] = a15;
156 void CMatrix::setDh(
const CDh &dh)
162 CMathLib::getMatrixFromRotation(*
this, dh.
axis, dh.
rot_z + dh.
angle);
190 a[3] = 0.0; a[7] = 0.0; a[11] = 0.0; a[15] = 1.0;
196 a[3] = 0.0; a[7] = 0.0; a[11] = 0.0; a[15] = 1.0;
203 return a[0] + a[5] + a[10];
207 void CMatrix::print(
bool round)
const 211 #define RND(x) (round ? rnd(x*10)/10.0 : x) 213 "%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n",
221 std::string CMatrix::toString(
bool round)
224 #define RND(x) (round ? rnd(x*10)/10.0 : x) 226 "%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n",
231 return std::string(str);
237 return CMatrix( a[0]*f, a[4]*f, a[8]*f, a[12]*f,
238 a[1]*f, a[5]*f, a[9]*f, a[13]*f,
239 a[2]*f, a[6]*f, a[10]*f, a[14]*f,
246 return CMatrix( a[0]+b.
a[0], a[4]+b.
a[4], a[8]+b.
a[8], a[12]+b.
a[12],
247 a[1]+b.
a[1], a[5]+b.
a[5], a[9]+b.
a[9], a[13]+b.
a[13],
248 a[2]+b.
a[2], a[6]+b.
a[6], a[10]+b.
a[10], a[14]+b.
a[14],
255 return CMatrix( a[0]-b.
a[0], a[4]-b.
a[4], a[8]-b.
a[8], a[12]-b.
a[12],
256 a[1]-b.
a[1], a[5]-b.
a[5], a[9]-b.
a[9], a[13]-b.
a[13],
257 a[2]-b.
a[2], a[6]-b.
a[6], a[10]-b.
a[10], a[14]-b.
a[14],
282 x = a[0]*v.
x + a[4]*v.
y + a[8]*v.
z + a[12]*v.
w;
283 y = a[1]*v.
x + a[5]*v.
y + a[9]*v.
z + a[13]*v.
w;
284 z = a[2]*v.
x + a[6]*v.
y + a[10]*v.
z + a[14]*v.
w;
285 return CVec(x, y, z);
290 const CVec& CMatrix::operator [] (
int i )
const 292 return *(
const CVec*) &a[4*i];
295 CVec& CMatrix::operator [] (
int i )
297 return *(
CVec*) &a[4*i];
310 return (
int) (value < 0.0 ? ceil(value - 0.5) : floor(value +0.5));
357 double dist = sqrt(a[12]*a[12]+a[13]*a[13]+a[14]*a[14]);
360 CMathLib::getRotationFromMatrix(*
this, axis, angle);
362 if (2.0*M_PI - angle < angle)
363 angle = 2.0*M_PI-angle;
365 return dist + fabsf(angle) * 180.0/M_PI;
372 void CMathLib::matrixFromQuaternion(
CVec &quaternion,
CMatrix &matrix)
374 double xx,xy,xz,xw,yy,yz,yw,zz,zw;
375 double X = quaternion.
x;
376 double Y = quaternion.
y;
377 double Z = quaternion.
z;
378 double W = quaternion.
w;
391 matrix.
a[0] = 1.0 - 2.0 * ( yy + zz );
392 matrix.
a[4] = 2.0 * ( xy - zw );
393 matrix.
a[8] = 2.0 * ( xz + yw );
395 matrix.
a[1] = 2.0 * ( xy + zw );
396 matrix.
a[5] = 1.0 - 2.0 * ( xx + zz );
397 matrix.
a[9] = 2.0 * ( yz - xw );
399 matrix.
a[2] = 2.0 * ( xz - yw );
400 matrix.
a[6] = 2.0 * ( yz + xw );
401 matrix.
a[10] = 1.0 - 2.0 * ( xx + yy );
403 matrix.
a[3] = matrix.
a[7] = matrix.
a[11] = matrix.
a[12] = matrix.
a[13] = matrix.
a[14] = 0.0;
409 void CMathLib::getMatrixFromRotation(
CMatrix &mat,
const CVec &axis,
double angle)
412 double sin_a = sin( angle / 2.0 );
413 double cos_a = cos( angle / 2.0 );
415 tmp.
x = axis.
x * sin_a;
416 tmp.
y = axis.
y * sin_a;
417 tmp.
z = axis.
z * sin_a;
421 double tmpf = 1.0/sqrt(tmp.
x*tmp.
x+
428 quater.
x = tmp.
x * tmpf;
429 quater.
y = tmp.
y * tmpf;
430 quater.
z = tmp.
z * tmpf;
431 quater.
w = tmp.
w * tmpf;
433 matrixFromQuaternion(quater, mat);
436 void CMathLib::getQuaternionFromRotation(
CVec &quater,
CVec &axis,
double angle)
439 double sin_a = sin( angle / 2.0 );
440 double cos_a = cos( angle / 2.0 );
442 tmp.
x = axis.
x * sin_a;
443 tmp.
y = axis.
y * sin_a;
444 tmp.
z = axis.
z * sin_a;
448 double tmpf = 1.0/sqrt(tmp.
x*tmp.
x+
455 quater.
x = tmp.
x * tmpf;
456 quater.
y = tmp.
y * tmpf;
457 quater.
z = tmp.
z * tmpf;
458 quater.
w = tmp.
w * tmpf;
463 void CMathLib::getRotationFromMatrix(
const CMatrix &mat,
CVec &result,
double &angle)
466 double rotangle, tmpf, cos_a, sin_a;
467 quaternionFromMatrix(mat, tmp);
470 tmpf = 1.0/sqrt(tmp.
x*tmp.
x+
476 quat.
w = tmp.
w * tmpf;
479 rotangle = acos( cos_a ) * 2;
480 sin_a = sqrt( 1.0 - cos_a * cos_a );
481 if ( fabs( sin_a ) < 0.0005 ) sin_a = 1;
483 result.
x = quat.
x / sin_a;
484 result.
y = quat.
y / sin_a;
485 result.
z = quat.
z / sin_a;
499 void CMathLib::calcMatrixResult(
double a[],
double b[],
int n,
int m,
double result[])
506 result[i] += a[i*n+j]*b[j];
511 double CMathLib::calcDotProduct(
double a[],
double b[],
int n)
514 double tmp = a[0]*b[0];
525 void CMathLib::quaternionFromMatrix(
const CMatrix &mat,
CVec &quaternion)
527 double T = 1 + mat.
a[0] + mat.
a[5] + mat.
a[10];
533 X = (mat.
a[6] - mat.
a[9])/S;
534 Y = (mat.
a[8] - mat.
a[2])/S;
535 Z = (mat.
a[1] - mat.
a[4])/S;
538 if ( mat.
a[0] > mat.
a[5] && mat.
a[0] > mat.
a[10] ) {
539 S = sqrt( 1.0 + mat.
a[0] - mat.
a[5] - mat.
a[10] ) * 2;
541 Y = (mat.
a[4] + mat.
a[1] ) / S;
542 Z = (mat.
a[2] + mat.
a[8] ) / S;
543 W = (mat.
a[6] - mat.
a[9] ) / S;
544 }
else if ( mat.
a[5] > mat.
a[10] ) {
545 S = sqrt( 1.0 + mat.
a[5] - mat.
a[0] - mat.
a[10] ) * 2;
546 X = (mat.
a[4] + mat.
a[1] ) / S;
548 Z = (mat.
a[9] + mat.
a[6] ) / S;
549 W = (mat.
a[8] - mat.
a[2] ) / S;
551 S = sqrt( 1.0 + mat.
a[10] - mat.
a[0] - mat.
a[5] ) * 2;
552 X = (mat.
a[2] + mat.
a[8] ) / S;
553 Y = (mat.
a[9] + mat.
a[6] ) / S;
555 W = (mat.
a[1] - mat.
a[4] ) / S;
565 void CMathLib::transposeMatrix(
double *a,
double *result,
int n)
573 result[i*n+j] = a[j*n+i];
579 void CMathLib::multiplyMatrices(
double *a,
double *b,
int n,
int m,
int k,
double *res)
587 res[i*n + j] += a[i*n + l] * b[l*m + j];
624 first.
x = atan2(mat.
a[8], -mat.
a[9]);
625 first.
y = acos(mat.
a[10]);
626 first.
z = atan2(mat.
a[2], mat.
a[6]);
629 first.
x = -atan2(-mat.
a[4], mat.
a[0]);
636 first.
y = atan2(-mat.
a[4], mat.
a[0]);
646 b = atan2(-mat.
a[2], sqrt(mat.
a[0]*mat.
a[0]+mat.
a[1]*mat.
a[1]));
648 if (fabsf(b - M_PI/2.0) < 0.000001)
651 g = atan2(mat.
a[4], mat.
a[5]);
652 }
else if (fabsf(b + M_PI/2.0) < 0.000001)
655 g = -atan2(mat.
a[4], mat.
a[5]);
658 a = atan2(mat.
a[1]/cos(b), mat.
a[0]/cos(b));
659 g = atan2(mat.
a[6]/cos(b), mat.
a[10]/cos(b));
692 void CMathLib::getRotation(
CMatrix &mat,
double aX,
double aY,
double aZ,
bool old)
704 if (fabsf(b - M_PI/2.0) < 0.0001)
709 mat.
a[4] = sin(g - a);
710 mat.
a[5] = cos(g - a);
713 mat.
a[8] = cos(g - a);
714 mat.
a[9] = -sin(g - a);
717 if (fabsf(b + M_PI/2.0) < 0.0001)
722 mat.
a[4] = -sin(g + a);
723 mat.
a[5] = cos(g + a);
726 mat.
a[8] = -cos(g + a);
727 mat.
a[9] = -sin(g + a);
731 mat.
a[0] = cos(b)*cos(a);
732 mat.
a[1] = sin(a)*cos(b);
734 mat.
a[4] = cos(a)*sin(b)*sin(g) - sin(a)*cos(g);
735 mat.
a[5] = sin(a)*sin(b)*sin(g) + cos(a)*cos(g);
736 mat.
a[6] = cos(b)*sin(g);
738 mat.
a[8] = cos(a)*sin(b)*cos(g)+sin(a)*sin(g);
739 mat.
a[9] = sin(a)*sin(b)*cos(g)-cos(a)*sin(g);
740 mat.
a[10] = cos(b)*cos(g);
752 getRotation(mat, vec.
x, vec.
y, vec.
z, old);
756 void CMathLib::calcAngles(
double leg1,
double leg2,
double x,
double y,
double &first,
double &second)
761 const double factor = 0.9999;
762 if ((x*x +y*y) >= factor*(leg1+leg2)*(leg1+leg2))
764 x = factor*x*sqrt((leg1+leg2)*(leg1+leg2) /(x*x +y*y));
765 y = factor*y*sqrt((leg1+leg2)*(leg1+leg2) /(x*x +y*y));
767 second = atan2(-x,-y);
774 double cosb, sinb, sinab, cosab, cosa, sina;
775 lambda = sqrt(x*x+y*y);
778 cosb = (leg1*leg1 + lambda*lambda - leg2*leg2) / (2*leg1*lambda);
779 sinb = sqrt(1-cosb*cosb);
780 sinab = sina*cosb + cosa*sinb;
781 cosab = cosa*cosb-sina*sinb;
782 second = atan2(sinab, cosab)+0.5*M_PI;
784 cosa = (leg1*leg1 + leg2*leg2 - lambda*lambda)/(2*leg1*leg2);
785 sina = sqrt(1-cosa*cosa);
786 first = 0.5*M_PI-atan2(-cosa, -sina);
788 first = atan2(sina, cosa) - M_PI;
794 for (
unsigned int i=0; i<v.size(); i++)
801 #define MIN(a,b) ((a)<(b)?(a):(b)) 802 #define MAX(a,b) ((a)>(b)?(a):(b)) 805 double bigbnd,
int maxiter,
double eps,
int verbose,
806 void obj(
int,
double *,
double *,
void *),
void *extraparams)
808 double *best = (
double*)calloc(n,
sizeof(
double));
809 double besty, t0, beta, y, delta;;
814 memcpy(best,x,n*
sizeof(
double));
816 (*obj)(n,best,&besty,extraparams);
818 unsigned int steps, unchanged;
822 while ((
int)steps < maxiter && unchanged < 100)
824 for (
int i=0; i<n;i++)
830 if ((x[i] - eps) < bl[i])
835 if ((x[i] + eps) > bu[i])
846 x[i] = lo + (hi -lo) * rnd;
849 (*obj)(n,x,&y,extraparams);
855 if (delta <= 0 ||
getUniform() <= exp(-delta/(t0 * pow(beta, steps))))
857 memcpy(best,x,n*
sizeof(
double));
866 printf(
"steps: %d best: %g\n", steps, besty);
868 memcpy(x,best,n*
sizeof(
double));
872 double bigbnd,
int maxiter,
double eps,
int verbose,
873 void obj(
int,
double *,
double *,
void *),
void *extraparams)
875 double **xi=(
double**)calloc(n,
sizeof(
double*)),
876 *temp1=(
double*)calloc(n*n,
sizeof(
double)),
877 **A=(
double**)calloc(n,
sizeof(
double*)),
878 *temp2=(
double*)calloc(n*n,
sizeof(
double)),
879 *d=(
double*)calloc(n,
sizeof(
double)),
880 *lambda=(
double*)calloc(n,
sizeof(
double)),
881 *xk=(
double*)calloc(n,
sizeof(
double)),
882 *xcurrent=(
double*)calloc(n,
sizeof(
double)),
883 *t=(
double*)calloc(n,
sizeof(
double)),
886 yfirst,yfirstfirst,ybest,ycurrent,mini,div;
887 int i,k,j,restart,numfeval=0;
889 memset(temp1,0,n*n*
sizeof(
double));
899 memcpy(xk,x,n*
sizeof(
double));
902 memset(lambda,0,n*
sizeof(
double));
903 (*obj)(n,x,&yfirstfirst,extraparams);
916 xcurrent[j]=xk[j]+d[i]*xi[i][j];
917 if (xcurrent[j] < bl[j])
919 else if (xcurrent[j] > bu[j])
922 (*obj)(n,xcurrent,&ycurrent,extraparams);
930 memcpy(xk,xcurrent,n*
sizeof(
double));
936 if (numfeval > maxiter)
938 memcpy(x,xk,n*
sizeof(
double));
939 goto LABEL_ROSENBROCK_DONE;
942 }
while (ybest<yfirst);
946 mini=
MIN(mini,fabs(d[i]));
949 memcpy(x,xk,n*
sizeof(
double));
951 if (ybest<yfirstfirst)
955 mini=
MIN(mini,fabs(xk[i]-x[i]));
956 restart=restart||(mini>eps);
964 A[n-1][i]=lambda[n-1]*xi[n-1][i];
966 for (k=n-2; k>=0; k--)
968 A[k][i]=A[k+1][i]+lambda[k]*xi[k][i];
970 t[n-1]=lambda[n-1]*lambda[n-1];
971 for (i=n-2; i>=0; i--)
972 t[i]=t[i+1]+lambda[i]*lambda[i];
973 for (i=n-1; i>0; i--)
975 div=sqrt(t[i-1]*t[i]);
978 xi[i][j]=(lambda[i-1]*A[i][j]-xi[i-1][j]*t[i])/div;
982 xi[0][i]=A[0][i]/div;
984 memcpy(x,xk,n*
sizeof(
double));
985 memset(lambda,0,n*
sizeof(
double));
992 }
while ((restart)&&(numfeval<maxiter));
994 LABEL_ROSENBROCK_DONE:
999 printf(
"ROSENBROCK method for local optimization (minimization)\n" 1000 "number of evaluation of the objective function= %i\n",numfeval);
1022 y.
x = (plane.
y + plane.
z) / plane.
x;
1028 y.
y = (plane.
x + plane.
z) / plane.
y;
1034 y.
z = (plane.
y + plane.
x) / plane.
z;
1037 }
else printf(
"error\n");
1045 transformation.
a[0] = x.
x;
1046 transformation.
a[1] = x.
y;
1047 transformation.
a[2] = x.
z;
1049 transformation.
a[4] = y.
x;
1050 transformation.
a[5] = y.
y;
1051 transformation.
a[6] = y.
z;
1053 transformation.
a[8] = z.x;
1054 transformation.
a[9] = z.y;
1055 transformation.
a[10] = z.z;
1068 return 1.0/(1.0+exp(-factor*(value - offset)));
1075 return 0.063494 / std * exp(-0.5 * (value - mean) * (value - mean) / (std * std) );
1080 return exp(-0.5 * (value - mean) * (value - mean) / (std * std) );
1095 return alpha - beta * log(u/(1.0-u));
1121 R[0][0] = from.
a[0];
1122 R[0][1] = from.
a[4];
1123 R[0][2] = from.
a[8];
1125 R[1][0] = from.
a[1];
1126 R[1][1] = from.
a[5];
1127 R[1][2] = from.
a[9];
1129 R[2][0] = from.
a[2];
1130 R[2][1] = from.
a[6];
1131 R[2][2] = from.
a[10];
1141 if (values.size() == 0)
1145 mean.resize(values[0].size(), 0.0);
1146 unsigned int counter = 0;
1147 for (
unsigned int i=0; i<values.size(); i++)
1149 for (
unsigned int j=0; j<mean.size() && j<values[i].size(); j++)
1150 mean[j] += values[i][j];
1156 for (
unsigned int j=0; j<mean.size(); j++)
1157 mean[j] /= (
double) counter;
1165 CMathLib::getRotationFromMatrix(matrix, axis, angle);
1173 CMathLib::getRotationFromMatrix(matrix, axis, angle);
1175 axis2[0] = axis.
x * angle;
1176 axis2[1] = axis.
y * angle;
1177 axis2[2] = axis.
z * angle;
1183 double angle = axis2.
length();
1187 axis = axis2 / angle;
1189 CMathLib::getMatrixFromRotation(matrix, axis, angle);
1194 CVec axis(axis2[0], axis2[1], axis2[2]);
1195 double angle = axis.
length();
1198 CMathLib::getMatrixFromRotation(matrix, axis, angle);
double trans_x
Translation offset along rotated x-axis.
void calculateTransformationFromPlane(CVec &plane, CMatrix &transformation)
Denavit Hartenberg Link information.
boost::uniform_01< double > pRngDist
double getSigmoid(double value, double offset, double factor)
void mul(const CMatrix &first, const CMatrix &second)
Assigns product of two matrices to matrix.
void getMeanFromVectors(std::vector< std::vector< double > > &values, std::vector< double > &mean)
boost::variate_generator< boost::mt19937 &, boost::uniform_01< double > > pRngUniform01(pRngMt19937, pRngDist)
double getLogisticProb(double alpha, double beta)
double getGaussian(double value, double mean, double std)
boost::mt19937 pRngMt19937
double rot_x
Rotation offset around rotated x-axis.
double getGaussianProb(double mean, double std)
void setUniformSeed(unsigned int seed)
double rot_z
Rotation offset around z-axis.
void set(PRECISION x, PRECISION y, PRECISION z)
char rosenbrock_version[]
double angle
Current angle of rotation aroung z-axis (changes with the real servo position)
void simulatedannealing(int n, double *x, double *bl, double *bu, double bigbnd, int maxiter, double eps, int verbose, void obj(int, double *, double *, void *), void *extraparams)
double trans_z
Translation offset along z-axis.
double getGaussianWithoutNormalization(double value, double mean, double std)
int rnd(PRECISION value)
Rounds a value.
void rosenbrock(int n, double *x, double *bl, double *bu, double bigbnd, int maxiter, double eps, int verbose, void obj(int, double *, double *, void *), void *extraparams)
void orientation2scaledAxis(CMatrix &matrix, CVec &axis)
void matrixToArray(const CMatrix &matrix, std::vector< double > &values)
void scaledAxis2Orientation(CVec &axis, CMatrix &matrix)
double vectorlength(std::vector< double > &v)
void arrayToMatrix(CMatrix &matrix, std::vector< double > &values)
void convertMatrix(CMatrix &from, double(*R)[3], double *T)