Skip to content
Snippets Groups Projects
linear.cpp 66.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdarg.h>
    #include <locale.h>
    #include "linear.h"
    #include "tron.h"
    #include <omp.h>
    int liblinear_version = LIBLINEAR_VERSION;
    typedef signed char schar;
    template <class T> static inline void swap(T& x, T& y) { T t=x; x=y; y=t; }
    #ifndef min
    template <class T> static inline T min(T x,T y) { return (x<y)?x:y; }
    #endif
    #ifndef max
    template <class T> static inline T max(T x,T y) { return (x>y)?x:y; }
    #endif
    template <class S, class T> static inline void clone(T*& dst, S* src, int n)
    {
    	dst = new T[n];
    	memcpy((void *)dst,(void *)src,sizeof(T)*n);
    }
    #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
    #define INF HUGE_VAL
    
    static void print_string_stdout(const char *s)
    {
    	fputs(s,stdout);
    	fflush(stdout);
    }
    static void print_null(const char *s) {}
    
    static void (*liblinear_print_string) (const char *) = &print_string_stdout;
    
    #if 1
    static void info(const char *fmt,...)
    {
    	char buf[BUFSIZ];
    	va_list ap;
    	va_start(ap,fmt);
    	vsprintf(buf,fmt,ap);
    	va_end(ap);
    	(*liblinear_print_string)(buf);
    }
    #else
    static void info(const char *fmt,...) {}
    #endif
    
    static inline int rand_int(const int max)
    {
    	static int seed = omp_get_thread_num();
    #ifdef CV_OMP
    #pragma omp threadprivate(seed)
    #endif
    	seed = ((seed * 1103515245) + 12345) & 0x7fffffff;
    	return seed%max;
    }
    
    class sparse_operator
    {
    public:
    	static double nrm2_sq(const feature_node *x)
    	{
    		double ret = 0;
    		while(x->index != -1)
    		{
    			ret += x->value*x->value;
    			x++;
    		}
    		return (ret);
    	}
    
    	static double dot(const double *s, const feature_node *x)
    	{
    		double ret = 0;
    		while(x->index != -1)
    		{
    			ret += s[x->index-1]*x->value;
    			x++;
    		}
    		return (ret);
    	}
    
    	static void axpy(const double a, const feature_node *x, double *y)
    	{
    		while(x->index != -1)
    		{
    			y[x->index-1] += a*x->value;
    			x++;
    		}
    	}
    };
    
    class Reduce_Vectors
    {
    public:
    	Reduce_Vectors(int size);
    	~Reduce_Vectors();
    
    	void init(void);
    	void sum_scale_x(double scalar, feature_node *x);
    	void reduce_sum(double* v);
    
    private:
    	int nr_thread;
    	int size;
    	double **tmp_array;
    };
    
    Reduce_Vectors::Reduce_Vectors(int size)
    {
    	nr_thread = omp_get_max_threads();
    	this->size = size;
    	tmp_array = new double*[nr_thread];
    	for(int i = 0; i < nr_thread; i++)
    		tmp_array[i] = new double[size];
    }
    
    Reduce_Vectors::~Reduce_Vectors(void)
    {
    	for(int i = 0; i < nr_thread; i++)
    		delete[] tmp_array[i];
    	delete[] tmp_array;
    }
    
    void Reduce_Vectors::init(void)
    {
    #pragma omp parallel for schedule(static)
    	for(int i = 0; i < size; i++)
    		for(int j = 0; j < nr_thread; j++)
    			tmp_array[j][i] = 0.0;
    }
    
    void Reduce_Vectors::sum_scale_x(double scalar, feature_node *x)
    {
    	int thread_id = omp_get_thread_num();
    
    	sparse_operator::axpy(scalar, x, tmp_array[thread_id]);
    }
    
    void Reduce_Vectors::reduce_sum(double* v)
    {
    #pragma omp parallel for schedule(static)
    	for(int i = 0; i < size; i++)
    	{
    		v[i] = 0;
    		for(int j = 0; j < nr_thread; j++)
    			v[i] += tmp_array[j][i];
    	}
    }
    
    class l2r_lr_fun: public function
    {
    public:
    	l2r_lr_fun(const problem *prob, double *C);
    	~l2r_lr_fun();
    
    	double fun(double *w);
    	void grad(double *w, double *g);
    	void Hv(double *s, double *Hs);
    
    	int get_nr_variable(void);
    
    private:
    	void Xv(double *v, double *Xv);
    	void XTv(double *v, double *XTv);
    
    	double *C;
    	double *z;
    	double *D;
    	Reduce_Vectors *reduce_vectors;
    
    	const problem *prob;
    };
    
    l2r_lr_fun::l2r_lr_fun(const problem *prob, double *C)
    {
    	int l=prob->l;
    
    	this->prob = prob;
    
    	z = new double[l];
    	D = new double[l];
    
    	reduce_vectors = new Reduce_Vectors(get_nr_variable());
    
    	this->C = C;
    }
    
    l2r_lr_fun::~l2r_lr_fun()
    {
    	delete[] z;
    	delete[] D;
    	delete reduce_vectors;
    }
    
    
    double l2r_lr_fun::fun(double *w)
    {
    	int i;
    	double f=0;
    	double *y=prob->y;
    	int l=prob->l;
    	int w_size=get_nr_variable();
    
    	Xv(w, z);
    
    #pragma omp parallel for private(i) reduction(+:f) schedule(static)
    	for(i=0;i<w_size;i++)
    		f += w[i]*w[i];
    	f /= 2.0;
    #pragma omp parallel for private(i) reduction(+:f) schedule(static)
    	for(i=0;i<l;i++)
    	{
    		double yz = y[i]*z[i];
    		if (yz >= 0)
    			f += C[i]*log(1 + exp(-yz));
    		else
    			f += C[i]*(-yz+log(1 + exp(yz)));
    	}
    
    	return(f);
    }
    
    void l2r_lr_fun::grad(double *w, double *g)
    {
    	int i;
    	double *y=prob->y;
    	int l=prob->l;
    	int w_size=get_nr_variable();
    
    #pragma omp parallel for private(i) schedule(static)
    	for(i=0;i<l;i++)
    	{
    		z[i] = 1/(1 + exp(-y[i]*z[i]));
    		D[i] = z[i]*(1-z[i]);
    		z[i] = C[i]*(z[i]-1)*y[i];
    	}
    	XTv(z, g);
    
    #pragma omp parallel for private(i) schedule(static)
    	for(i=0;i<w_size;i++)
    		g[i] = w[i] + g[i];
    }
    
    int l2r_lr_fun::get_nr_variable(void)
    {
    	return prob->n;
    }
    
    void l2r_lr_fun::Hv(double *s, double *Hs)
    {
    	int i;
    	int l=prob->l;
    	int w_size=get_nr_variable();
    	feature_node **x=prob->x;
    
    	reduce_vectors->init();
    
    #pragma omp parallel for private(i) schedule(guided)
    	for(i=0;i<l;i++)
    	{
    		feature_node * const xi=x[i];
    		double xTs = sparse_operator::dot(s, xi);
    
    		xTs = C[i]*D[i]*xTs;
    
    		reduce_vectors->sum_scale_x(xTs, xi);
    	}
    
    	reduce_vectors->reduce_sum(Hs);
    #pragma omp parallel for private(i) schedule(static)
    	for(i=0;i<w_size;i++)
    		Hs[i] = s[i] + Hs[i];
    }
    
    void l2r_lr_fun::Xv(double *v, double *Xv)
    {
    	int i;
    	int l=prob->l;
    	feature_node **x=prob->x;
    
    #pragma omp parallel for private (i) schedule(guided)
    	for(i=0;i<l;i++)
    		Xv[i]=sparse_operator::dot(v, x[i]);
    }
    
    void l2r_lr_fun::XTv(double *v, double *XTv)
    {
    	int i;
    	int l=prob->l;
    	feature_node **x=prob->x;
    
    	reduce_vectors->init();
    
    #pragma omp parallel for private(i) schedule(guided)
    	for(i=0;i<l;i++)
    		reduce_vectors->sum_scale_x(v[i], x[i]);
    	
    	reduce_vectors->reduce_sum(XTv);
    }
    
    class l2r_l2_svc_fun: public function
    {
    public:
    	l2r_l2_svc_fun(const problem *prob, double *C);
    	~l2r_l2_svc_fun();
    
    	double fun(double *w);
    	void grad(double *w, double *g);
    	void Hv(double *s, double *Hs);
    
    	int get_nr_variable(void);
    
    protected:
    	void Xv(double *v, double *Xv);
    	void subXTv(double *v, double *XTv);
    
    	double *C;
    	double *z;
    	Reduce_Vectors *reduce_vectors;
    
    	int *I;
    	int sizeI;
    	const problem *prob;
    };
    
    l2r_l2_svc_fun::l2r_l2_svc_fun(const problem *prob, double *C)
    {
    	int l=prob->l;
    
    	this->prob = prob;
    
    	z = new double[l];
    
    	reduce_vectors = new Reduce_Vectors(get_nr_variable());
    
    	I = new int[l];
    	this->C = C;
    }
    
    l2r_l2_svc_fun::~l2r_l2_svc_fun()
    {
    	delete[] z;
    	delete[] I;
    	delete reduce_vectors;
    }
    
    double l2r_l2_svc_fun::fun(double *w)
    {
    	int i;
    	double f=0;
    	double *y=prob->y;
    	int l=prob->l;
    	int w_size=get_nr_variable();
    
    	Xv(w, z);
    
    #pragma omp parallel for private(i) reduction(+:f) schedule(static)
    	for(i=0;i<w_size;i++)
    		f += w[i]*w[i];
    	f /= 2.0;
    #pragma omp parallel for private(i) reduction(+:f) schedule(static)
    	for(i=0;i<l;i++)
    	{
    		z[i] = y[i]*z[i];
    		double d = 1-z[i];
    		if (d > 0)
    			f += C[i]*d*d;
    	}
    
    	return(f);
    }
    
    void l2r_l2_svc_fun::grad(double *w, double *g)
    {
    	int i;
    	double *y=prob->y;
    	int l=prob->l;
    	int w_size=get_nr_variable();
    
    	sizeI = 0;
    	for (i=0;i<l;i++)
    		if (z[i] < 1)
    		{
    			z[sizeI] = C[i]*y[i]*(z[i]-1);
    			I[sizeI] = i;
    			sizeI++;
    		}
    	subXTv(z, g);
    
    #pragma omp parallel for private(i) schedule(static)
    	for(i=0;i<w_size;i++)
    		g[i] = w[i] + 2*g[i];
    }
    
    int l2r_l2_svc_fun::get_nr_variable(void)
    {
    	return prob->n;
    }
    
    void l2r_l2_svc_fun::Hv(double *s, double *Hs)
    {
    	int i;
    	int w_size=get_nr_variable();
    	feature_node **x=prob->x;
    
    	reduce_vectors->init();
    
    #pragma omp parallel for private(i) schedule(guided)
    	for(i=0;i<sizeI;i++)
    	{
    		feature_node * const xi=x[I[i]];
    		double xTs = sparse_operator::dot(s, xi);
    
    		xTs = C[I[i]]*xTs;
    
    		reduce_vectors->sum_scale_x(xTs, xi);
    	}
    	
    	reduce_vectors->reduce_sum(Hs);
    #pragma omp parallel for private(i) schedule(static)
    	for(i=0;i<w_size;i++)
    		Hs[i] = s[i] + 2*Hs[i];
    }
    
    void l2r_l2_svc_fun::Xv(double *v, double *Xv)
    {
    	int i;
    	int l=prob->l;
    	feature_node **x=prob->x;
    
    #pragma omp parallel for private(i) schedule(guided)
    	for(i=0;i<l;i++)
    		Xv[i]=sparse_operator::dot(v, x[i]);
    }
    
    void l2r_l2_svc_fun::subXTv(double *v, double *XTv)
    {
    	int i;
    	feature_node **x=prob->x;
    
    	reduce_vectors->init();
    
    #pragma omp parallel for private(i) schedule(guided)
    	for(i=0;i<sizeI;i++)
    		reduce_vectors->sum_scale_x(v[i], x[I[i]]);
    
    	reduce_vectors->reduce_sum(XTv);
    }
    
    class l2r_l2_svr_fun: public l2r_l2_svc_fun
    {
    public:
    	l2r_l2_svr_fun(const problem *prob, double *C, double p);
    
    	double fun(double *w);
    	void grad(double *w, double *g);
    
    private:
    	double p;
    };
    
    l2r_l2_svr_fun::l2r_l2_svr_fun(const problem *prob, double *C, double p):
    	l2r_l2_svc_fun(prob, C)
    {
    	this->p = p;
    }
    
    double l2r_l2_svr_fun::fun(double *w)
    {
    	int i;
    	double f=0;
    	double *y=prob->y;
    	int l=prob->l;
    	int w_size=get_nr_variable();
    	double d;
    
    	Xv(w, z);
    
    #pragma omp parallel for private(i) reduction(+:f) schedule(static)
    	for(i=0;i<w_size;i++)
    		f += w[i]*w[i];
    	f /= 2;
    #pragma omp parallel for private(i) reduction(+:f) schedule(static)
    	for(i=0;i<l;i++)
    	{
    		d = z[i] - y[i];
    		if(d < -p)
    			f += C[i]*(d+p)*(d+p);
    		else if(d > p)
    			f += C[i]*(d-p)*(d-p);
    	}
    
    	return(f);
    }
    
    void l2r_l2_svr_fun::grad(double *w, double *g)
    {
    	int i;
    	double *y=prob->y;
    	int l=prob->l;
    	int w_size=get_nr_variable();
    	double d;
    
    	sizeI = 0;
    	for(i=0;i<l;i++)
    	{
    		d = z[i] - y[i];
    
    		// generate index set I
    		if(d < -p)
    		{
    			z[sizeI] = C[i]*(d+p);
    			I[sizeI] = i;
    			sizeI++;
    		}
    		else if(d > p)
    		{
    			z[sizeI] = C[i]*(d-p);
    			I[sizeI] = i;
    			sizeI++;
    		}
    
    	}
    	subXTv(z, g);
    
    #pragma omp parallel for private(i) schedule(static)
    	for(i=0;i<w_size;i++)
    		g[i] = w[i] + 2*g[i];
    }
    
    // A coordinate descent algorithm for
    // multi-class support vector machines by Crammer and Singer
    //
    //  min_{\alpha}  0.5 \sum_m ||w_m(\alpha)||^2 + \sum_i \sum_m e^m_i alpha^m_i
    //    s.t.     \alpha^m_i <= C^m_i \forall m,i , \sum_m \alpha^m_i=0 \forall i
    //
    //  where e^m_i = 0 if y_i  = m,
    //        e^m_i = 1 if y_i != m,
    //  C^m_i = C if m  = y_i,
    //  C^m_i = 0 if m != y_i,
    //  and w_m(\alpha) = \sum_i \alpha^m_i x_i
    //
    // Given:
    // x, y, C
    // eps is the stopping tolerance
    //
    // solution will be put in w
    //
    // See Appendix of LIBLINEAR paper, Fan et al. (2008)
    
    #define GETI(i) ((int) prob->y[i])
    // To support weights for instances, use GETI(i) (i)
    
    class Solver_MCSVM_CS
    {
    	public:
    		Solver_MCSVM_CS(const problem *prob, int nr_class, double *C, double eps=0.1, int max_iter=100000);
    		~Solver_MCSVM_CS();
    		void Solve(double *w);
    	private:
    		void solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new);
    		bool be_shrunk(int i, int m, int yi, double alpha_i, double minG);
    		double *B, *C, *G;
    		int w_size, l;
    		int nr_class;
    		int max_iter;
    		double eps;
    		const problem *prob;
    };
    
    Solver_MCSVM_CS::Solver_MCSVM_CS(const problem *prob, int nr_class, double *weighted_C, double eps, int max_iter)
    {
    	this->w_size = prob->n;
    	this->l = prob->l;
    	this->nr_class = nr_class;
    	this->eps = eps;
    	this->max_iter = max_iter;
    	this->prob = prob;
    	this->B = new double[nr_class];
    	this->G = new double[nr_class];
    	this->C = weighted_C;
    }
    
    Solver_MCSVM_CS::~Solver_MCSVM_CS()
    {
    	delete[] B;
    	delete[] G;
    }
    
    int compare_double(const void *a, const void *b)
    {
    	if(*(double *)a > *(double *)b)
    		return -1;
    	if(*(double *)a < *(double *)b)
    		return 1;
    	return 0;
    }
    
    void Solver_MCSVM_CS::solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new)
    {
    	int r;
    	double *D;
    
    	clone(D, B, active_i);
    	if(yi < active_i)
    		D[yi] += A_i*C_yi;
    	qsort(D, active_i, sizeof(double), compare_double);
    
    	double beta = D[0] - A_i*C_yi;
    	for(r=1;r<active_i && beta<r*D[r];r++)
    		beta += D[r];
    	beta /= r;
    
    	for(r=0;r<active_i;r++)
    	{
    		if(r == yi)
    			alpha_new[r] = min(C_yi, (beta-B[r])/A_i);
    		else
    			alpha_new[r] = min((double)0, (beta - B[r])/A_i);
    	}
    	delete[] D;
    }
    
    bool Solver_MCSVM_CS::be_shrunk(int i, int m, int yi, double alpha_i, double minG)
    {
    	double bound = 0;
    	if(m == yi)
    		bound = C[GETI(i)];
    	if(alpha_i == bound && G[m] < minG)
    		return true;
    	return false;
    }
    
    void Solver_MCSVM_CS::Solve(double *w)
    {
    	int i, m, s;
    	int iter = 0;
    	double *alpha =  new double[l*nr_class];
    	double *alpha_new = new double[nr_class];
    	int *index = new int[l];
    	double *QD = new double[l];
    	int *d_ind = new int[nr_class];
    	double *d_val = new double[nr_class];
    	int *alpha_index = new int[nr_class*l];
    	int *y_index = new int[l];
    	int active_size = l;
    	int *active_size_i = new int[l];
    	double eps_shrink = max(10.0*eps, 1.0); // stopping tolerance for shrinking
    	bool start_from_all = true;
    
    	// Initial alpha can be set here. Note that
    	// sum_m alpha[i*nr_class+m] = 0, for all i=1,...,l-1
    	// alpha[i*nr_class+m] <= C[GETI(i)] if prob->y[i] == m
    	// alpha[i*nr_class+m] <= 0 if prob->y[i] != m
    	// If initial alpha isn't zero, uncomment the for loop below to initialize w
    	for(i=0;i<l*nr_class;i++)
    		alpha[i] = 0;
    
    	for(i=0;i<w_size*nr_class;i++)
    		w[i] = 0;
    	for(i=0;i<l;i++)
    	{
    		for(m=0;m<nr_class;m++)
    			alpha_index[i*nr_class+m] = m;
    		feature_node *xi = prob->x[i];
    		QD[i] = 0;
    		while(xi->index != -1)
    		{
    			double val = xi->value;
    			QD[i] += val*val;
    
    			// Uncomment the for loop if initial alpha isn't zero
    			// for(m=0; m<nr_class; m++)
    			//	w[(xi->index-1)*nr_class+m] += alpha[i*nr_class+m]*val;
    			xi++;
    		}
    		active_size_i[i] = nr_class;
    		y_index[i] = (int)prob->y[i];
    		index[i] = i;
    	}
    
    	while(iter < max_iter)
    	{
    		double stopping = -INF;
    		for(i=0;i<active_size;i++)
    		{
    			int j = i+rand_int(active_size-i);
    			swap(index[i], index[j]);
    		}
    		for(s=0;s<active_size;s++)
    		{
    			i = index[s];
    			double Ai = QD[i];
    			double *alpha_i = &alpha[i*nr_class];
    			int *alpha_index_i = &alpha_index[i*nr_class];
    
    			if(Ai > 0)
    			{
    				for(m=0;m<active_size_i[i];m++)
    					G[m] = 1;
    				if(y_index[i] < active_size_i[i])
    					G[y_index[i]] = 0;
    
    				feature_node *xi = prob->x[i];
    				while(xi->index!= -1)
    				{
    					double *w_i = &w[(xi->index-1)*nr_class];
    					for(m=0;m<active_size_i[i];m++)
    						G[m] += w_i[alpha_index_i[m]]*(xi->value);
    					xi++;
    				}
    
    				double minG = INF;
    				double maxG = -INF;
    				for(m=0;m<active_size_i[i];m++)
    				{
    					if(alpha_i[alpha_index_i[m]] < 0 && G[m] < minG)
    						minG = G[m];
    					if(G[m] > maxG)
    						maxG = G[m];
    				}
    				if(y_index[i] < active_size_i[i])
    					if(alpha_i[(int) prob->y[i]] < C[GETI(i)] && G[y_index[i]] < minG)
    						minG = G[y_index[i]];
    
    				for(m=0;m<active_size_i[i];m++)
    				{
    					if(be_shrunk(i, m, y_index[i], alpha_i[alpha_index_i[m]], minG))
    					{
    						active_size_i[i]--;
    						while(active_size_i[i]>m)
    						{
    							if(!be_shrunk(i, active_size_i[i], y_index[i],
    											alpha_i[alpha_index_i[active_size_i[i]]], minG))
    							{
    								swap(alpha_index_i[m], alpha_index_i[active_size_i[i]]);
    								swap(G[m], G[active_size_i[i]]);
    								if(y_index[i] == active_size_i[i])
    									y_index[i] = m;
    								else if(y_index[i] == m)
    									y_index[i] = active_size_i[i];
    								break;
    							}
    							active_size_i[i]--;
    						}
    					}
    				}
    
    				if(active_size_i[i] <= 1)
    				{
    					active_size--;
    					swap(index[s], index[active_size]);
    					s--;
    					continue;
    				}
    
    				if(maxG-minG <= 1e-12)
    					continue;
    				else
    					stopping = max(maxG - minG, stopping);
    
    				for(m=0;m<active_size_i[i];m++)
    					B[m] = G[m] - Ai*alpha_i[alpha_index_i[m]] ;
    
    				solve_sub_problem(Ai, y_index[i], C[GETI(i)], active_size_i[i], alpha_new);
    				int nz_d = 0;
    				for(m=0;m<active_size_i[i];m++)
    				{
    					double d = alpha_new[m] - alpha_i[alpha_index_i[m]];
    					alpha_i[alpha_index_i[m]] = alpha_new[m];
    					if(fabs(d) >= 1e-12)
    					{
    						d_ind[nz_d] = alpha_index_i[m];
    						d_val[nz_d] = d;
    						nz_d++;
    					}
    				}
    
    				xi = prob->x[i];
    				while(xi->index != -1)
    				{
    					double *w_i = &w[(xi->index-1)*nr_class];
    					for(m=0;m<nz_d;m++)
    						w_i[d_ind[m]] += d_val[m]*xi->value;
    					xi++;
    				}
    			}
    		}
    
    		iter++;
    		if(iter % 10 == 0)
    		{
    			info(".");
    		}
    
    		if(stopping < eps_shrink)
    		{
    			if(stopping < eps && start_from_all == true)
    				break;
    			else
    			{
    				active_size = l;
    				for(i=0;i<l;i++)
    					active_size_i[i] = nr_class;
    				info("*");
    				eps_shrink = max(eps_shrink/2, eps);
    				start_from_all = true;
    			}
    		}
    		else
    			start_from_all = false;
    	}
    
    	info("\noptimization finished, #iter = %d\n",iter);
    	if (iter >= max_iter)
    		info("\nWARNING: reaching max number of iterations\n");
    
    	// calculate objective value
    	double v = 0;
    	int nSV = 0;
    	for(i=0;i<w_size*nr_class;i++)
    		v += w[i]*w[i];
    	v = 0.5*v;
    	for(i=0;i<l*nr_class;i++)
    	{
    		v += alpha[i];
    		if(fabs(alpha[i]) > 0)
    			nSV++;
    	}
    	for(i=0;i<l;i++)
    		v -= alpha[i*nr_class+(int)prob->y[i]];
    	info("Objective value = %lf\n",v);
    	info("nSV = %d\n",nSV);
    
    	delete [] alpha;
    	delete [] alpha_new;
    	delete [] index;
    	delete [] QD;
    	delete [] d_ind;
    	delete [] d_val;
    	delete [] alpha_index;
    	delete [] y_index;
    	delete [] active_size_i;
    }
    
    // A coordinate descent algorithm for
    // L1-loss and L2-loss SVM dual problems
    //
    //  min_\alpha  0.5(\alpha^T (Q + D)\alpha) - e^T \alpha,
    //    s.t.      0 <= \alpha_i <= upper_bound_i,
    //
    //  where Qij = yi yj xi^T xj and
    //  D is a diagonal matrix
    //
    // In L1-SVM case:
    // 		upper_bound_i = Cp if y_i = 1
    // 		upper_bound_i = Cn if y_i = -1
    // 		D_ii = 0
    // In L2-SVM case:
    // 		upper_bound_i = INF
    // 		D_ii = 1/(2*Cp)	if y_i = 1
    // 		D_ii = 1/(2*Cn)	if y_i = -1
    //
    // Given:
    // x, y, Cp, Cn
    // eps is the stopping tolerance
    //
    // solution will be put in w
    //
    // See Algorithm 3 of Hsieh et al., ICML 2008
    
    #undef GETI
    #define GETI(i) (y[i]+1)
    // To support weights for instances, use GETI(i) (i)
    
    static void solve_l2r_l1l2_svc(
    	const problem *prob, double *w, double eps,
    	double Cp, double Cn, int solver_type, int _max_iter=1000)
    {
    	int l = prob->l;
    	int w_size = prob->n;
    	int i, s, iter = 0;
    	double C, d;
    	double *QD = new double[l];
    	int max_iter = _max_iter;
    	int *index = new int[l];
    	double *alpha = new double[l];
    	schar *y = new schar[l];
    	int active_size = l;
    
    	// PG: projected gradient, for shrinking and stopping
    	double PG;
    	double PGmax_old = INF;
    	double PGmin_old = -INF;
    	double PGmax_new, PGmin_new;
    
    	// for multi-core dual CD
    	// candidates: a block considered for gradient evaluation
    	// workingset: a subset of candidates for sequential CD updates
    	double eps1 = 0.1;
    	double min_eps1 = min(0.01*eps, eps1);
    	int init_candidates_size = 256;
    	int max_candidates_size = 4096;
    	int candidates_size = min(init_candidates_size, max_candidates_size);
    	double *Grad = new double[max_candidates_size];
    	int *workingset = new int[max_candidates_size];
    
    	// default solver_type: L2R_L2LOSS_SVC_DUAL
    	double diag[3] = {0.5/Cn, 0, 0.5/Cp};
    	double upper_bound[3] = {INF, 0, INF};
    	if(solver_type == L2R_L1LOSS_SVC_DUAL)
    	{
    		diag[0] = 0;
    		diag[2] = 0;
    		upper_bound[0] = Cn;
    		upper_bound[2] = Cp;
    	}
    
    	for(i=0; i<l; i++)
    	{
    		if(prob->y[i] > 0)
    		{
    			y[i] = +1;
    		}
    		else
    		{
    			y[i] = -1;
    		}
    	}
    
    	// Initial alpha can be set here. Note that
    	// 0 <= alpha[i] <= upper_bound[GETI(i)]
    	for(i=0; i<l; i++)
    		alpha[i] = 0;
    
    	for(i=0; i<w_size; i++)
    		w[i] = 0;
    	for(i=0; i<l; i++)
    	{
    		QD[i] = diag[GETI(i)];
    
    		feature_node * const xi = prob->x[i];
    		QD[i] += sparse_operator::nrm2_sq(xi);
    		sparse_operator::axpy(y[i]*alpha[i], xi, w);
    
    		index[i] = i;
    	}
    
    	while (iter < max_iter)
    	{
    		PGmax_new = -INF;
    		PGmin_new = INF;
    		int t = 0;
    		int num_updates_one_iter = 0;
    
    		for (i=0; i<active_size; i++)
    		{
    			int j = i+rand_int(active_size-i);
    			swap(index[i], index[j]);
    		}
    		while (t < active_size)
    		{
    			int send = min(candidates_size, active_size-t);
    
    #pragma omp parallel for private(s,i) schedule(static)
    			for (s=0; s<send; s++)
    			{
    				i = index[t+s];
    				Grad[s] = y[i]*sparse_operator::dot(w, prob->x[i])-1 + alpha[i]*diag[GETI(i)];
    			}
    
    			int workingset_size = 0;
    
    			for (s=0; s<send; s++)
    			{
    				PG = 0;
    				i = index[t+s];
    				C = upper_bound[GETI(i)];
    
    				// A cleaner if-else statement is used here for calculating projected gradient.
    				// We do not consider it in single-thread cddual due to slightly slower training
    				// in some data sets (e.g., 5% for covtype). The reasons are that more
    				// conditions are checked here and the computation within the if-else statement
    				// is lighter in single-thread cddual
    				if ((alpha[i] < C && Grad[s] < 0) ||
    					(alpha[i] > 0 && Grad[s] > 0))
    					PG = Grad[s];
    				else if ((alpha[i] == 0 && Grad[s] > PGmax_old) ||
    						 (alpha[i] == C && Grad[s] < PGmin_old))
    				{
    					active_size--;
    					send--;
    					if (t+send == active_size)
    						swap(index[t+s], index[t+send]);
    					else
    					{
    						int r = index[active_size];