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

SHOGUN Machine Learning Toolbox - Documentation