#include "ranksvmtn.h" #include #include #include"../tools/matrixIO.h" using namespace std; using namespace Eigen; const int maxiter = 10; const double prec=1e-4; const double C=1; int cg_solve(const MatrixXd &A, const VectorXd &b, VectorXd &x) { double alpha,beta,r_1,r_2; int step=0; VectorXd q; VectorXd res = b - A*x; // TODO Hessian product VectorXd p = res; while (1) { // Non preconditioned version r_1 = res.dot(res); if (r_1<1e-10) // Terminate condition break; if (step){ beta = r_1 / r_2; p = res + p * beta; } q = A*p; // TODO Hessian product alpha = r_1/p.dot(q); x=x+p*alpha; res=res-q*alpha; ++step; r_2=r_1; } return 0; } // Calculate objfunc gradient & support vectors int objfunc_linear(const VectorXd &w,const MatrixXd &D,const MatrixXd &A,const double C,VectorXd &pred,VectorXd &grad, double &obj) { for (int i=0;i0?pred(i):0; obj = (pred.cwiseProduct(pred)*C).sum()/2 + w.dot(w)/2; grad = w - (((pred*C).transpose()*A)*D).transpose(); return 0; } // line search using newton method int line_search(const VectorXd &w,const MatrixXd &D,const MatrixXd &A,const VectorXd &step,VectorXd &pred,double &t) { double wd=w.dot(step),dd=step.dot(step); double g,h; t = 0; VectorXd Xd=A*(D*step); VectorXd pred2; while (1) { pred2 = pred - t*Xd; g=wd+t*dd; h=dd; for (int i=0;i0) { g -= C*pred2(i)*Xd(i); h += C*Xd(i)*Xd(i); } g=g+1e-12; h=h+1e-12; t=t-g/h; cout< maxiter) { LOG(INFO)<< "Maxiter :"<0) { VectorXd v = A.row(i)*D; H = H + C * (v * v.transpose()); } // Solve //cout<0) ++sv; // When dec is small enough LOG(INFO)<<"Iter: "<feature(j); } int cnt=0; i=j=0; while (iqid!=D.getData()[i+1]->qid) { int high=j; while (D.getData()[high]->rank>0) ++high; cnt += (high-j)*(i-high+1); j = i+1; } ++i; } A.resize(cnt,D.getSize()); cnt=i=j=0; while (iqid!=D.getData()[i+1]->qid) { int v1=j,v2; for (v1=j;(D.getData()[v1]->rank)>0;++v1) for (v2=i;(D.getData()[v2]->rank)<0;--v2) { A(cnt,v1) = 1; A(cnt,v2) = -1; ++cnt; } j = i+1; } ++i; } train_orig(fsize,Data,A,model.weight); return 0; }; int RSVMTN::predict(DataList &D, vector &res){ //TODO define A res.clear(); for (int i=0;ifeature).dot(model.weight)); return 0; };