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

SHOGUN Machine Learning Toolbox - Documentation