Cplex.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) 2006-2008 Soeren Sonnenburg
00008  * Copyright (C) 2006-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #ifndef CCPLEX_H__
00012 #define CCPLEX_H__
00013 
00014 #include "lib/config.h"
00015 
00016 #ifdef USE_CPLEX
00017 extern "C" {
00018 #include <ilcplex/cplex.h>
00019 }
00020 
00021 #include "lib/common.h"
00022 #include "base/SGObject.h"
00023 
00024 #include "features/SparseFeatures.h"
00025 #include "features/Labels.h"
00026 
00027 enum E_PROB_TYPE
00028 {
00029     E_LINEAR,
00030     E_QP
00031 };
00032 
00033 class CCplex : public CSGObject
00034 {
00035 public:
00036 
00037     CCplex();
00038     ~CCplex();
00039 
00041     bool init(E_PROB_TYPE t, INT timeout=60);
00042     bool cleanup();
00043 
00044     // A = [ E Z_w Z_x ] dim(A)=(num_dim+1, num_dim+1 + num_zero + num_bound)
00045     // (+1 for bias!)
00046     bool setup_subgradientlpm_QP(DREAL C, CLabels* labels, CSparseFeatures<DREAL>* features, INT* idx_bound, INT num_bound,
00047             INT* w_zero, INT num_zero,
00048             DREAL* vee, INT num_dim,
00049             bool use_bias);
00050 
00051     bool setup_lpboost(DREAL C, INT num_cols);
00052     bool add_lpboost_constraint(DREAL factor, TSparseEntry<DREAL>* h, INT len, INT ulen, CLabels* label);
00053 
00090     bool setup_lpm(DREAL C, CSparseFeatures<DREAL>* x, CLabels* y, bool use_bias);
00091 
00099     bool setup_lp(DREAL* objective, DREAL* constraints_mat, INT rows, INT cols, DREAL* rhs, DREAL* lb, DREAL* ub);
00100 
00101 
00105     bool setup_qp(DREAL* H, INT dim);
00106     bool optimize(DREAL* sol, DREAL* lambda=NULL);
00107 
00108     bool dense_to_cplex_sparse(DREAL* H, INT rows, INT cols, int* &qmatbeg, int* &qmatcnt, int* &qmatind, double* &qmatval);
00109 
00110     inline bool set_time_limit(DREAL seconds)
00111     {
00112         return CPXsetdblparam (env, CPX_PARAM_TILIM, seconds) == 0;
00113     }
00114     inline bool write_problem(char* filename)
00115     {
00116         return CPXwriteprob (env, lp, filename, NULL) == 0;
00117     }
00118 
00119     inline bool write_Q(char* filename)
00120     {
00121 #if CPX_VERSION >= 1000 //CPXqpwrite has been deprecated in CPLEX 10
00122         return CPXwriteprob (env, lp, filename, NULL) == 0;
00123 #else
00124         return CPXqpwrite (env, lp, filename) == 0;
00125 #endif
00126     }
00127 protected:
00128   CPXENVptr     env;
00129   CPXLPptr      lp;
00130   bool          lp_initialized;
00131 
00132   E_PROB_TYPE problem_type;
00133 };
00134 #endif
00135 #endif

SHOGUN Machine Learning Toolbox - Documentation