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 for(i=0;i<l*nr_class;i++) alpha[i] = 0; for(i=0;i<n*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) { QD[i] += (xi->value)*(xi->value); xi++; } active_size_i[i] = nr_class; y_index[i] = prob->y[i]; index[i] = i; } while(iter < max_iter) { double stopping = -INF; for(i=0;i<active_size;i++) { int j = i+rand()%(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[prob->y[i]] < C[prob->y[i]] && G[y_index[i]] < minG) minG = G[y_index[i]]; for(m=0;m<active_size_i[i];m++) { if(be_shrunken(m, y_index[i], alpha_i[alpha_index_i[m]], minG)) { active_size_i[i]--; while(active_size_i[i]>m) { if(!be_shrunken(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[prob->y[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("."); info_flush(); } 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("*"); info_flush(); 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("Warning: reaching max number of iterations\n"); // calculate objective value double v = 0; int nSV = 0; for(i=0;i<n*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+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; }
void Solver<TQ>::do_shrinking() { float Gmax1 = -INF; // max { -y_i * grad(f)_i | i in I_up(\alpha) } float Gmax2 = -INF; // max { y_i * grad(f)_i | i in I_low(\alpha) } // find maximal violating pair first for(int i=0;i<active_size;i++) { if(y[i]==+1) { if(!is_upper_bound(i)) { if(-G[i] >= Gmax1) Gmax1 = -G[i]; } if(!is_lower_bound(i)) { if(G[i] >= Gmax2) Gmax2 = G[i]; } } else { if(!is_upper_bound(i)) { if(-G[i] >= Gmax2) Gmax2 = -G[i]; } if(!is_lower_bound(i)) { if(G[i] >= Gmax1) Gmax1 = G[i]; } } } // shrink for(int i=0;i<active_size;i++) if (be_shrunken(i, Gmax1, Gmax2)) { active_size--; while (active_size > i) { if (!be_shrunken(active_size, Gmax1, Gmax2)) { swap_index(i,active_size); break; } active_size--; } } // unshrink, check all variables again before final iterations if(unshrinked || Gmax1 + Gmax2 > eps*10) return; unshrinked = true; reconstruct_gradient(); for(int i=l-1;i>=active_size;i--) if (!be_shrunken(i, Gmax1, Gmax2)) { while (active_size < i) { if (be_shrunken(active_size, Gmax1, Gmax2)) { swap_index(i,active_size); break; } active_size++; } active_size++; } }