MKL.h

Go to the documentation of this file.
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) 2009 Soeren Sonnenburg
00008  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 #ifndef __MKL_H__
00011 #define __MKL_H__
00012 
00013 #ifdef USE_GLPK
00014 #include <glpk.h>
00015 #endif
00016 
00017 #ifdef USE_CPLEX
00018 extern "C" {
00019 #include <ilcplex/cplex.h>
00020 }
00021 #endif
00022 
00023 #include "lib/common.h"
00024 #include "features/Features.h"
00025 #include "kernel/Kernel.h"
00026 #include "classifier/svm/SVM.h"
00027 
00028 namespace shogun
00029 {
00083 class CMKL : public CSVM
00084 {
00085     public:
00090         CMKL(CSVM* s=NULL);
00091 
00094         virtual ~CMKL();
00095 
00100         inline void set_constraint_generator(CSVM* s)
00101         {
00102             set_svm(s);
00103         }
00104 
00109         inline void set_svm(CSVM* s)
00110         {
00111             SG_REF(s);
00112             SG_UNREF(svm);
00113             svm=s;
00114         }
00115 
00120         inline CSVM* get_svm()
00121         {
00122             SG_REF(svm);
00123             return svm;
00124         }
00125 
00126 
00135         virtual bool train(CFeatures* data=NULL);
00136 
00141         inline void set_C_mkl(float64_t C) { C_mkl = C; }
00142 
00147         inline void set_mkl_norm(float64_t norm)
00148         {
00149             if (norm<1)
00150                 SG_ERROR("Norm must be >= 1, e.g., 1-norm is the standard MKL; norms>1 nonsparse MKL\n");
00151             mkl_norm = norm;
00152         }
00153 
00159         inline void set_interleaved_optimization_enabled(bool enable)
00160         {
00161             interleaved_optimization=enable;
00162         }
00163 
00168         inline bool get_interleaved_optimization_enabled()
00169         {
00170             return interleaved_optimization;
00171         }
00172 
00177         inline float64_t compute_mkl_primal_objective()
00178         {
00179             return compute_svm_primal_objective();
00180         }
00181 
00186         virtual float64_t compute_mkl_dual_objective();
00187 
00192         inline void set_mkl_epsilon(float64_t eps) { mkl_epsilon=eps; }
00193 
00198         inline float64_t get_mkl_epsilon() { return mkl_epsilon; }
00199 
00204         inline int32_t get_mkl_iterations() { return mkl_iterations; }
00205 
00216         virtual bool perform_mkl_step(const float64_t* sumw, float64_t suma);
00217 
00224         static bool perform_mkl_step_helper (CMKL* mkl,
00225                 const float64_t* sumw, const float64_t suma)
00226         {
00227             return mkl->perform_mkl_step(sumw, suma);
00228         }
00229 
00230 
00234         virtual float64_t compute_sum_alpha()=0;
00235 
00240         virtual void compute_sum_beta(float64_t* sumw);
00241 
00242     protected:
00246         virtual void init_training()=0;
00247 
00263         void perform_mkl_step(float64_t* beta, float64_t* old_beta, int num_kernels,
00264                 int32_t* label, int32_t* active2dnum,
00265                 float64_t* a, float64_t* lin, float64_t* sumw, int32_t& inner_iters);
00266 
00267 
00281         float64_t compute_optimal_betas_via_cplex(float64_t* beta, const float64_t* old_beta, int32_t num_kernels,
00282                 const float64_t* sumw, float64_t suma, int32_t& inner_iters);
00283 
00296         float64_t compute_optimal_betas_via_glpk(float64_t* beta, const float64_t* old_beta,
00297                 int num_kernels, const float64_t* sumw, float64_t suma, int32_t& inner_iters);
00298 
00310         float64_t compute_optimal_betas_directly(
00311                 float64_t* beta, const float64_t* old_beta, const int32_t num_kernels,
00312                 const float64_t* sumw, const float64_t suma, const float64_t mkl_objective);
00313 
00325         float64_t compute_optimal_betas_newton(float64_t* beta, const float64_t* old_beta,
00326                 int32_t num_kernels, const float64_t* sumw, float64_t suma, float64_t mkl_objective);
00327 
00332         virtual bool converged()
00333         {
00334             return w_gap<mkl_epsilon;
00335         }
00336 
00338         void init_solver();
00339 
00340 #ifdef USE_CPLEX
00341 
00345         bool init_cplex();
00346 
00348         void set_qnorm_constraints(float64_t* beta, int32_t num_kernels);
00349 
00354         bool cleanup_cplex();
00355 #endif
00356 
00357 #ifdef USE_GLPK
00358 
00362         bool init_glpk();
00363 
00368         bool cleanup_glpk();
00369 
00374         bool check_lpx_status(LPX *lp);
00375 #endif
00376 
00378         inline virtual const char* get_name() const { return "MKL"; }
00379 
00380     protected:
00382         CSVM* svm;
00384         float64_t C_mkl;
00386         float64_t mkl_norm;
00388         int32_t mkl_iterations;
00390         float64_t mkl_epsilon;
00392         bool interleaved_optimization;
00393 
00395         float64_t* W;
00396 
00398         float64_t w_gap;
00400         float64_t rho;
00401 
00402 #ifdef USE_CPLEX
00403 
00404         CPXENVptr     env;
00406         CPXLPptr      lp_cplex;
00407 #endif
00408 
00409 #ifdef USE_GLPK
00410 
00411         LPX* lp_glpk;
00412 #endif
00413 
00414         bool lp_initialized ;
00415 };
00416 }
00417 #endif //__MKL_H__

SHOGUN Machine Learning Toolbox - Documentation