SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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/labels/Labels.h>
20 
21 using namespace shogun;
22 
24 : CLinearMachine(), C1(1), C2(1), use_bias(true), epsilon(1e-3)
25 {
26 }
27 
28 
30 {
31 }
32 
34 {
36  if (data)
37  {
38  if (!data->has_property(FP_DOT))
39  SG_ERROR("Specified features are not of type CDotFeatures\n")
40  set_features((CDotFeatures*) data);
41  }
43  int32_t num_train_labels=m_labels->get_num_labels();
44  int32_t num_feat=features->get_dim_feature_space();
45  int32_t num_vec=features->get_num_vectors();
46 
47  ASSERT(num_vec==num_train_labels)
48 
49  w = SGVector<float64_t>(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)
59  if (get_max_train_time()>0)
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