SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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/labels/Labels.h>
19 
20 using namespace shogun;
21 
23 : CSVM()
24 {
25 }
26 
28 {
29 }
30 
32 {
35 
36  bool result = false;
37  CCplex cplex;
38 
39  if (data)
40  {
41  if (m_labels->get_num_labels() != data->get_num_vectors())
42  {
43  SG_ERROR("%s::train_machine(): Number of training vectors (%d) does"
44  " not match number of labels (%d)\n", get_name(),
46  }
47  kernel->init(data, data);
48  }
49 
50  if (cplex.init(E_QP))
51  {
52  int32_t n,m;
53  int32_t num_label=0;
54  SGVector<float64_t> y=((CBinaryLabels*)m_labels)->get_labels();
56  m=H.num_rows;
57  n=H.num_cols;
58  ASSERT(n>0 && n==m && n==num_label)
59  float64_t* alphas=SG_MALLOC(float64_t, n);
60  float64_t* lb=SG_MALLOC(float64_t, n);
61  float64_t* ub=SG_MALLOC(float64_t, n);
62 
63  //hessian y'y.*K
64  for (int32_t i=0; i<n; i++)
65  {
66  lb[i]=0;
67  ub[i]=get_C1();
68 
69  for (int32_t j=0; j<n; j++)
70  H[i*n+j]*=y[j]*y[i];
71  }
72 
73  //feed qp to cplex
74 
75 
76  int32_t j=0;
77  for (int32_t i=0; i<n; i++)
78  {
79  if (alphas[i]>0)
80  {
81  //set_alpha(j, alphas[i]*labels->get_label(i)/etas[1]);
82  set_alpha(j, alphas[i]*((CBinaryLabels*) m_labels)->get_int_label(i));
83  set_support_vector(j, i);
84  j++;
85  }
86  }
87  //compute_objective();
88  SG_INFO("obj = %.16f, rho = %.16f\n",get_objective(),get_bias())
89  SG_INFO("Number of SV: %ld\n", get_num_support_vectors())
90 
91  SG_FREE(alphas);
92  SG_FREE(lb);
93  SG_FREE(ub);
94 
95  result = true;
96  }
97 
98  if (!result)
99  SG_ERROR("cplex svm failed")
100 
101  return result;
102 }
103 #endif
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:98
Class CCplex to encapsulate access to the commercial cplex general purpose optimizer.
Definition: Cplex.h:42
#define SG_INFO(...)
Definition: SGIO.h:118
virtual ELabelType get_label_type() const =0
binary labels +1/-1
Definition: LabelTypes.h:18
static float64_t * H
Definition: libbmrm.cpp:27
virtual int32_t get_num_labels() const =0
virtual int32_t get_num_vectors() const =0
virtual ~CCPLEXSVM()
Definition: CPLEXSVM.cpp:27
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
index_t num_cols
Definition: SGMatrix.h:378
SGMatrix< float64_t > get_kernel_matrix()
Definition: Kernel.h:219
index_t num_rows
Definition: SGMatrix.h:376
virtual const char * get_name() const
Definition: SVM.h:234
float64_t get_C1()
Definition: SVM.h:161
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
bool set_alpha(int32_t idx, float64_t val)
bool set_support_vector(int32_t idx, int32_t val)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
A generic Support Vector Machine Interface.
Definition: SVM.h:49
virtual bool train_machine(CFeatures *data=NULL)
Definition: CPLEXSVM.cpp:31
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
float64_t get_objective()
Definition: SVM.h:218

SHOGUN Machine Learning Toolbox - Documentation