SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
Class CCplex to encapsulate access to the commercial cplex general purpose optimizer.
Definition: Cplex.h:42
#define SG_INFO(...)
Definition: SGIO.h:118
bool init(E_PROB_TYPE t, int32_t timeout=60)
init cplex with problem type t and retry timeout 60 seconds
Definition: Cplex.cpp:33
bool set_time_limit(float64_t seconds)
Definition: Cplex.h:126
virtual int32_t get_num_labels() const =0
bool optimize(float64_t *sol, float64_t *lambda=NULL)
Definition: Cplex.cpp:601
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
virtual void set_features(CDotFeatures *feat)
Definition: LPM.h:59
Features that support dot products among other operations.
Definition: DotFeatures.h:44
virtual int32_t get_dim_feature_space() const =0
virtual ~CLPM()
Definition: LPM.cpp:29
#define SG_PRINT(...)
Definition: SGIO.h:137
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
bool cleanup()
Definition: Cplex.cpp:469
float64_t get_max_train_time()
Definition: Machine.cpp:87
SGVector< float64_t > w
virtual bool train_machine(CFeatures *data=NULL)
Definition: LPM.cpp:33
bool setup_lpm(float64_t C, CSparseFeatures< float64_t > *x, CBinaryLabels *y, bool use_bias)
Definition: Cplex.cpp:322
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
CDotFeatures * features
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
bool get_bias_enabled()
Definition: LPM.h:80
The class Features is the base class of all feature objects.
Definition: Features.h:68
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
virtual void set_bias(float64_t b)
bool has_property(EFeatureProperty p) const
Definition: Features.cpp:295
float64_t C1
Definition: LPM.h:100

SHOGUN Machine Learning Toolbox - Documentation