CPLEXSVM.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "classifier/svm/CPLEXSVM.h"
00012 #include "lib/common.h"
00013
00014 #ifdef USE_CPLEX
00015 #include "lib/io.h"
00016 #include "lib/Mathematics.h"
00017 #include "lib/Cplex.h"
00018 #include "features/Labels.h"
00019
00020 CCPLEXSVM::CCPLEXSVM()
00021 : CSVM()
00022 {
00023 }
00024
00025 CCPLEXSVM::~CCPLEXSVM()
00026 {
00027 }
00028
00029 bool CCPLEXSVM::train()
00030 {
00031 bool result = false;
00032 CCplex cplex;
00033
00034 if (cplex.init(E_QP))
00035 {
00036 INT n,m;
00037 INT num_label=0;
00038 DREAL* y = labels->get_labels(num_label);
00039 DREAL* H = kernel->get_kernel_matrix_real(m, n, NULL);
00040 ASSERT(n>0 && n==m && n==num_label);
00041 DREAL* alphas=new DREAL[n];
00042 DREAL* lb=new DREAL[n];
00043 DREAL* ub=new DREAL[n];
00044
00045
00046 for (int i=0; i<n; i++)
00047 {
00048 lb[i]=0;
00049 ub[i]=get_C1();
00050
00051 for (int j=0; j<n; j++)
00052 H[i*n+j]*=y[j]*y[i];
00053 }
00054
00055
00056
00057
00058 int j=0;
00059 for (int i=0; i<n; i++)
00060 {
00061 if (alphas[i]>0)
00062 {
00063
00064 set_alpha(j, alphas[i]*labels->get_label(i));
00065 set_support_vector(j, i);
00066 j++;
00067 }
00068 }
00069 compute_objective();
00070 SG_INFO( "obj = %.16f, rho = %.16f\n",get_objective(),get_bias());
00071 SG_INFO( "Number of SV: %ld\n", get_num_support_vectors());
00072
00073 delete[] alphas;
00074 delete[] lb;
00075 delete[] ub;
00076 delete[] H;
00077 delete[] y;
00078
00079 result = true;
00080 }
00081
00082 if (!result)
00083 SG_ERROR( "cplex svm failed");
00084
00085 return result;
00086 }
00087 #endif