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

SHOGUN Machine Learning Toolbox - Documentation