SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MKLMulticlassGLPK.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) 2009 Alexander Binder
8  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  *
10  * Update to patch 0.10.0 - thanks to Eric aka Yoo (thereisnoknife@gmail.com)
11  *
12  */
13 
15 #include <shogun/io/SGIO.h>
16 #include <vector>
17 
18 #ifdef USE_GLPK
19 #include <glpk.h>
20 #endif
21 
22 
23 using namespace shogun;
24 
26 {
27  numkernels = 0;
28 #ifdef USE_GLPK
29  //makes glpk quiet
30  glp_term_out(GLP_OFF);
31  linearproblem=NULL;
32 #endif
33 }
35 {
36 #if defined(USE_GLPK)
37  if (linearproblem)
38  {
39  glp_delete_prob((glp_prob*) linearproblem);
40  linearproblem=NULL;
41  }
42 
43 #endif
44 }
45 
47 {
48  SG_ERROR(
49  " MKLMulticlassGLPK MKLMulticlassGLPK::operator=(...): must "
50  "not be called, glpk structure is currently not copyable");
51  return (*this);
52 
53 }
55 {
56  SG_ERROR(
57  " MKLMulticlassGLPK::MKLMulticlassGLPK(MKLMulticlassGLPK & gl):"
58  " must not be called, glpk structure is currently not copyable");
59 
60 }
61 
62 void MKLMulticlassGLPK::setup(const int32_t numkernels2)
63 {
64 #if defined(USE_GLPK)
65  numkernels=numkernels2;
66  if (numkernels<=1)
67  {
68  SG_ERROR("void glpkwrapper::setup(const int32_tnumkernels): input "
69  "numkernels out of bounds: %d\n",numkernels);
70  }
71 
72  if (!linearproblem)
73  {
74  linearproblem=glp_create_prob();
75  }
76 
77  glp_set_obj_dir((glp_prob*)linearproblem, GLP_MAX);
78 
79  glp_add_cols((glp_prob*)linearproblem,1+numkernels);
80 
81  //set up theta
82  glp_set_col_bnds((glp_prob*)linearproblem,1,GLP_FR,0.0,0.0);
83  glp_set_obj_coef((glp_prob*)linearproblem,1,1.0);
84 
85  //set up betas
86  int32_t offset=2;
87  for (int32_t i=0; i<numkernels;++i)
88  {
89  glp_set_col_bnds((glp_prob*)linearproblem,offset+i,GLP_DB,0.0,1.0);
90  glp_set_obj_coef((glp_prob*)linearproblem,offset+i,0.0);
91  }
92 
93  //set sumupconstraint32_t/sum_l \beta_l=1
94  glp_add_rows((glp_prob*)linearproblem,1);
95 
96  int32_t*betainds(NULL);
97  betainds=SG_MALLOC(int, 1+numkernels);
98  for (int32_t i=0; i<numkernels;++i)
99  {
100  betainds[1+i]=2+i; // coefficient for theta stays zero, therefore
101  //start at 2 not at 1 !
102  }
103 
104  float64_t *betacoeffs = SG_MALLOC(float64_t, 1+numkernels);
105  betacoeffs[0]=0;
106  for (int32_t i=0; i<numkernels;++i)
107  {
108  betacoeffs[1+i]=1;
109  }
110 
111  glp_set_mat_row((glp_prob*)linearproblem,1,numkernels, betainds,betacoeffs);
112  glp_set_row_bnds((glp_prob*)linearproblem,1,GLP_FX,1.0,1.0);
113 
114  SG_FREE(betainds);
115  betainds=NULL;
116 
117  SG_FREE(betacoeffs);
118  betacoeffs=NULL;
119 #else
120  SG_ERROR(
121  "glpk.h from GNU glpk not included at compile time necessary "
122  "here\n");
123 #endif
124 
125 }
126 
127 void MKLMulticlassGLPK::addconstraint(const ::std::vector<float64_t> & normw2,
128  const float64_t sumofpositivealphas)
129 {
130 #if defined(USE_GLPK)
131 
132  ASSERT ((int)normw2.size()==numkernels)
133  ASSERT (sumofpositivealphas>=0)
134 
135  glp_add_rows((glp_prob*)linearproblem,1);
136 
137  int32_t curconstraint=glp_get_num_rows((glp_prob*)linearproblem);
138 
139  int32_t *betainds(NULL);
140  betainds=SG_MALLOC(int, 1+1+numkernels);
141 
142  betainds[1]=1;
143  for (int32_t i=0; i<numkernels;++i)
144  {
145  betainds[2+i]=2+i; // coefficient for theta stays zero, therefore start
146  //at 2 not at 1 !
147  }
148 
149  float64_t *betacoeffs(NULL);
150  betacoeffs=new float64_t[1+1+numkernels];
151 
152  betacoeffs[1]=-1;
153 
154  for (int32_t i=0; i<numkernels;++i)
155  {
156  betacoeffs[2+i]=0.5*normw2[i];
157  }
158  glp_set_mat_row((glp_prob*)linearproblem,curconstraint,1+numkernels, betainds,
159  betacoeffs);
160  glp_set_row_bnds((glp_prob*)linearproblem,curconstraint,GLP_LO,sumofpositivealphas,
161  sumofpositivealphas);
162 
163  SG_FREE(betainds);
164  betainds=NULL;
165 
166  SG_FREE(betacoeffs);
167  betacoeffs=NULL;
168 
169 #else
170  SG_ERROR(
171  "glpk.h from GNU glpk not included at compile time necessary "
172  "here\n");
173 #endif
174 }
175 
176 void MKLMulticlassGLPK::computeweights(std::vector<float64_t> & weights2)
177 {
178 #if defined(USE_GLPK)
179  weights2.resize(numkernels);
180 
181  glp_simplex((glp_prob*) linearproblem,NULL);
182 
183  float64_t sum=0;
184  for (int32_t i=0; i< numkernels;++i)
185  {
186  weights2[i]=glp_get_col_prim((glp_prob*) linearproblem, i+2);
187  weights2[i]= ::std::max(0.0, ::std::min(1.0,weights2[i]));
188  sum+= weights2[i];
189  }
190 
191  if (sum>0)
192  {
193  for (int32_t i=0; i< numkernels;++i)
194  {
195  weights2[i]/=sum;
196  }
197  }
198  else
199  SG_ERROR("void glpkwrapper::computeweights(std::vector<float64_t> & "
200  "weights2): sum of weights nonpositive %f\n",sum);
201 #else
202  SG_ERROR(
203  "glpk.h from GNU glpk not included at compile time necessary "
204  "here\n");
205 #endif
206 }
virtual void computeweights(std::vector< float64_t > &weights2)
MKLMulticlassGLPK is a helper class for MKLMulticlass.
#define SG_ERROR(...)
Definition: SGIO.h:129
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
MKLMulticlassGLPK operator=(MKLMulticlassGLPK &gl)
virtual void addconstraint(const ::std::vector< float64_t > &normw2, const float64_t sumofpositivealphas)
Matrix::Scalar max(Matrix m)
Definition: Redux.h:66
virtual void setup(const int32_t numkernels2)

SHOGUN Machine Learning Toolbox - Documentation