private static void solve_epsilon_svr(SvmProblem prob, SvmParameter param, double[] alpha, SolutionInfo si) { int l = prob.Lenght; double[] alpha2 = new double[2 * l]; double[] linear_term = new double[2 * l]; sbyte[] y = new sbyte[2 * l]; int i; for (i = 0; i < l; i++) { alpha2[i] = 0; linear_term[i] = param.P - prob.Y[i]; y[i] = 1; alpha2[i + l] = 0; linear_term[i + l] = param.P + prob.Y[i]; y[i + l] = -1; } Solver s = new Solver(); s.Solve(2 * l, new SvrQ(prob, param), linear_term, y, alpha2, param.C, param.C, param.Eps, si, param.Shrinking); double sum_alpha = 0; for (i = 0; i < l; i++) { alpha[i] = alpha2[i] - alpha2[i + l]; sum_alpha += Math.Abs(alpha[i]); } Svm.info("nu = " + sum_alpha / (param.C * l) + "\n"); }
private static void solve_one_class(SvmProblem prob, SvmParameter param, double[] alpha, SolutionInfo si) { int l = prob.Lenght; double[] zeros = new double[l]; sbyte[] ones = new sbyte[l]; int i; int n = (int)(param.Nu * prob.Lenght); // # of alpha's at upper bound for (i = 0; i < n; i++) alpha[i] = 1; if (n < prob.Lenght) alpha[n] = param.Nu * prob.Lenght - n; for (i = n + 1; i < l; i++) alpha[i] = 0; for (i = 0; i < l; i++) { zeros[i] = 0; ones[i] = 1; } var s = new Solver(); s.Solve(l, new OneClassQ(prob, param), zeros, ones, alpha, 1.0, 1.0, param.Eps, si, param.Shrinking); }
private static void solve_c_svc(SvmProblem prob, SvmParameter param, double[] alpha, SolutionInfo si, double Cp, double Cn) { int l = prob.Lenght; double[] minus_ones = new double[l]; sbyte[] y = new sbyte[l]; for (int i = 0; i < l; i++) { alpha[i] = 0; minus_ones[i] = -1; if (prob.Y[i] > 0) { y[i] = +1; } else { y[i] = -1; } } Solver s = new Solver(); s.Solve(l, new SvcQ(prob, param, y), minus_ones, y, alpha, Cp, Cn, param.Eps, si, param.Shrinking); double sum_alpha = 0; for (int i = 0; i < l; i++) { sum_alpha += alpha[i]; } if (Cp == Cn) { Svm.info("nu = " + sum_alpha / (Cp * prob.Lenght) + "\n"); } for (int i = 0; i < l; i++) { alpha[i] *= y[i]; } }
private static void solve_nu_svr(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si) { int l = prob.l; double C = param.C; double[] alpha2 = new double[2*l]; double[] linear_term = new double[2*l]; sbyte[] y = new sbyte[2*l]; int i; double sum = C * param.nu * l / 2; for(i=0;i<l;i++) { alpha2[i] = alpha2[i+l] = Math.Min(sum,C); sum -= alpha2[i]; linear_term[i] = - prob.y[i]; y[i] = 1; linear_term[i+l] = prob.y[i]; y[i+l] = -1; } Solver_NU s = new Solver_NU(); s.Solve(2*l, new SVR_Q(prob,param), linear_term, y, alpha2, C, C, param.eps, si, param.shrinking); svm.info("epsilon = "+(-si.r)+"\n"); for(i=0;i<l;i++) alpha[i] = alpha2[i] - alpha2[i+l]; }
private static void solve_one_class(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si) { int l = prob.l; double[] zeros = new double[l]; sbyte[] ones = new sbyte[l]; int i; int n = (int)(param.nu*prob.l); // # of alpha's at upper bound for(i=0;i<n;i++) alpha[i] = 1; if(n<prob.l) alpha[n] = param.nu * prob.l - n; for(i=n+1;i<l;i++) alpha[i] = 0; for(i=0;i<l;i++) { zeros[i] = 0; ones[i] = 1; } Solver s = new Solver(); s.Solve(l, new ONE_CLASS_Q(prob,param), zeros, ones, alpha, 1.0, 1.0, param.eps, si, param.shrinking); }
private static void solve_nu_svc(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si) { int i; int l = prob.l; double nu = param.nu; sbyte[] y = new sbyte[l]; for(i=0;i<l;i++) if(prob.y[i]>0) y[i] = +1; else y[i] = -1; double sum_pos = nu*l/2; double sum_neg = nu*l/2; for(i=0;i<l;i++) if(y[i] == +1) { alpha[i] = Math.Min(1.0,sum_pos); sum_pos -= alpha[i]; } else { alpha[i] = Math.Min(1.0,sum_neg); sum_neg -= alpha[i]; } double[] zeros = new double[l]; for(i=0;i<l;i++) zeros[i] = 0; Solver_NU s = new Solver_NU(); s.Solve(l, new SVC_Q(prob,param,y), zeros, y, alpha, 1.0, 1.0, param.eps, si, param.shrinking); double r = si.r; svm.info("C = "+1/r+"\n"); for(i=0;i<l;i++) alpha[i] *= y[i]/r; si.rho /= r; si.obj /= (r*r); si.upper_bound_p = 1/r; si.upper_bound_n = 1/r; }
private static void solve_c_svc(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si, double Cp, double Cn) { int l = prob.l; double[] minus_ones = new double[l]; sbyte[] y = new sbyte[l]; int i; for(i=0;i<l;i++) { alpha[i] = 0; minus_ones[i] = -1; if(prob.y[i] > 0) y[i] = +1; else y[i] = -1; } Solver s = new Solver(); s.Solve(l, new SVC_Q(prob,param,y), minus_ones, y, alpha, Cp, Cn, param.eps, si, param.shrinking); double sum_alpha=0; for(i=0;i<l;i++) sum_alpha += alpha[i]; if (Cp==Cn) svm.info("nu = "+sum_alpha/(Cp*prob.l)+"\n"); for(i=0;i<l;i++) alpha[i] *= y[i]; }