SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LPM.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) 2007-2009 Soeren Sonnenburg
8  * Copyright (C) 2007-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/config.h>
12 
13 #ifdef USE_CPLEX
14 
15 #include <shogun/classifier/LPM.h>
16 #include <shogun/features/Labels.h>
19 
20 using namespace shogun;
21 
23 : CLinearClassifier(), C1(1), C2(1), use_bias(true), epsilon(1e-3)
24 {
25 }
26 
27 
29 {
30 }
31 
33 {
34  ASSERT(labels);
35  if (data)
36  {
37  if (!data->has_property(FP_DOT))
38  SG_ERROR("Specified features are not of type CDotFeatures\n");
39  set_features((CDotFeatures*) data);
40  }
41  ASSERT(features);
42  int32_t num_train_labels=labels->get_num_labels();
43  int32_t num_feat=features->get_dim_feature_space();
44  int32_t num_vec=features->get_num_vectors();
45 
46  ASSERT(num_vec==num_train_labels);
47  SG_FREE(w);
48  w=SG_MALLOC(float64_t, num_feat);
49  w_dim=num_feat;
50 
51  int32_t num_params=1+2*num_feat+num_vec; //b,w+,w-,xi
52  float64_t* params=SG_MALLOC(float64_t, num_params);
53  memset(params,0,sizeof(float64_t)*num_params);
54 
55  CCplex solver;
56  solver.init(E_LINEAR);
57  SG_INFO("C=%f\n", C1);
58  solver.setup_lpm(C1, (CSparseFeatures<float64_t>*) features, labels, get_bias_enabled());
59  if (get_max_train_time()>0)
60  solver.set_time_limit(get_max_train_time());
61  bool result=solver.optimize(params);
62  solver.cleanup();
63 
64  set_bias(params[0]);
65  for (int32_t i=0; i<num_feat; i++)
66  w[i]=params[1+i]-params[1+num_feat+i];
67 
68 //#define LPM_DEBUG
69 #ifdef LPM_DEBUG
70  CMath::display_vector(params,num_params, "params");
71  SG_PRINT("bias=%f\n", bias);
72  CMath::display_vector(w,w_dim, "w");
73  CMath::display_vector(&params[1],w_dim, "w+");
74  CMath::display_vector(&params[1+w_dim],w_dim, "w-");
75 #endif
76  SG_FREE(params);
77 
78  return result;
79 }
80 #endif

SHOGUN Machine Learning Toolbox - Documentation