SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CPLEXSVM.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
12 #include <shogun/lib/common.h>
13 
14 #ifdef USE_CPLEX
15 #include <shogun/io/SGIO.h>
18 #include <shogun/features/Labels.h>
19 
20 using namespace shogun;
21 
23 : CSVM()
24 {
25 }
26 
28 {
29 }
30 
32 {
33  bool result = false;
34  CCplex cplex;
35 
36  if (data)
37  {
38  if (labels->get_num_labels() != data->get_num_vectors())
39  SG_ERROR("Number of training vectors does not match number of labels\n");
40  kernel->init(data, data);
41  }
42 
43  if (cplex.init(E_QP))
44  {
45  int32_t n,m;
46  int32_t num_label=0;
47  float64_t* y = labels->get_labels(num_label);
48  float64_t* H = kernel->get_kernel_matrix<float64_t>(m, n, NULL);
49  ASSERT(n>0 && n==m && n==num_label);
50  float64_t* alphas=SG_MALLOC(float64_t, n);
53 
54  //hessian y'y.*K
55  for (int32_t i=0; i<n; i++)
56  {
57  lb[i]=0;
58  ub[i]=get_C1();
59 
60  for (int32_t j=0; j<n; j++)
61  H[i*n+j]*=y[j]*y[i];
62  }
63 
64  //feed qp to cplex
65 
66 
67  int32_t j=0;
68  for (int32_t i=0; i<n; i++)
69  {
70  if (alphas[i]>0)
71  {
72  //set_alpha(j, alphas[i]*labels->get_label(i)/etas[1]);
73  set_alpha(j, alphas[i]*labels->get_label(i));
74  set_support_vector(j, i);
75  j++;
76  }
77  }
78  //compute_objective();
79  SG_INFO( "obj = %.16f, rho = %.16f\n",get_objective(),get_bias());
80  SG_INFO( "Number of SV: %ld\n", get_num_support_vectors());
81 
82  SG_FREE(alphas);
83  SG_FREE(lb);
84  SG_FREE(ub);
85  SG_FREE(H);
86 
87  result = true;
88  }
89 
90  if (!result)
91  SG_ERROR( "cplex svm failed");
92 
93  return result;
94 }
95 #endif

SHOGUN Machine Learning Toolbox - Documentation