SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2007-2009 Soeren Sonnenburg 00008 * Copyright (C) 2007-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/lib/config.h> 00012 00013 #ifdef USE_CPLEX 00014 00015 #include <shogun/classifier/LPM.h> 00016 #include <shogun/labels/Labels.h> 00017 #include <shogun/labels/BinaryLabels.h> 00018 #include <shogun/mathematics/Math.h> 00019 #include <shogun/mathematics/Cplex.h> 00020 00021 using namespace shogun; 00022 00023 CLPM::CLPM() 00024 : CLinearMachine(), C1(1), C2(1), use_bias(true), epsilon(1e-3) 00025 { 00026 } 00027 00028 00029 CLPM::~CLPM() 00030 { 00031 } 00032 00033 bool CLPM::train_machine(CFeatures* data) 00034 { 00035 ASSERT(m_labels) 00036 if (data) 00037 { 00038 if (!data->has_property(FP_DOT)) 00039 SG_ERROR("Specified features are not of type CDotFeatures\n") 00040 set_features((CDotFeatures*) data); 00041 } 00042 ASSERT(features) 00043 int32_t num_train_labels=m_labels->get_num_labels(); 00044 int32_t num_feat=features->get_dim_feature_space(); 00045 int32_t num_vec=features->get_num_vectors(); 00046 00047 ASSERT(num_vec==num_train_labels) 00048 00049 w = SGVector<float64_t>(num_feat); 00050 00051 int32_t num_params=1+2*num_feat+num_vec; //b,w+,w-,xi 00052 float64_t* params=SG_MALLOC(float64_t, num_params); 00053 memset(params,0,sizeof(float64_t)*num_params); 00054 00055 CCplex solver; 00056 solver.init(E_LINEAR); 00057 SG_INFO("C=%f\n", C1) 00058 solver.setup_lpm(C1, (CSparseFeatures<float64_t>*) features, (CBinaryLabels*)m_labels, get_bias_enabled()); 00059 if (get_max_train_time()>0) 00060 solver.set_time_limit(get_max_train_time()); 00061 bool result=solver.optimize(params); 00062 solver.cleanup(); 00063 00064 set_bias(params[0]); 00065 for (int32_t i=0; i<num_feat; i++) 00066 w[i]=params[1+i]-params[1+num_feat+i]; 00067 00068 //#define LPM_DEBUG 00069 #ifdef LPM_DEBUG 00070 CMath::display_vector(params,num_params, "params"); 00071 SG_PRINT("bias=%f\n", bias) 00072 CMath::display_vector(w,w_dim, "w"); 00073 CMath::display_vector(¶ms[1],w_dim, "w+"); 00074 CMath::display_vector(¶ms[1+w_dim],w_dim, "w-"); 00075 #endif 00076 SG_FREE(params); 00077 00078 return result; 00079 } 00080 #endif