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-2009 Soeren Sonnenburg
00008  * Copyright (C) 2006-2009 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 
00040 class CCplex : public CSGObject
00041 {
00042 public:
00043 
00044     CCplex();
00045     virtual ~CCplex();
00046 
00048     bool init(E_PROB_TYPE t, int32_t timeout=60);
00049     bool cleanup();
00050 
00051     // A = [ E Z_w Z_x ] dim(A)=(num_dim+1, num_dim+1 + num_zero + num_bound)
00052     // (+1 for bias!)
00053     bool setup_subgradientlpm_QP(
00054         float64_t C, CLabels* labels, CSparseFeatures<float64_t>* features,
00055         int32_t* idx_bound, int32_t num_bound, int32_t* w_zero,
00056         int32_t num_zero, float64_t* vee, int32_t num_dim, bool use_bias);
00057 
00058     bool setup_lpboost(float64_t C, int32_t num_cols);
00059     bool add_lpboost_constraint(
00060         float64_t factor, TSparseEntry<float64_t>* h, int32_t len,
00061         int32_t ulen, CLabels* label);
00062 
00063     // given N sparse inputs x_i, and corresponding labels y_i i=0...N-1
00064     // create the following 1-norm SVM problem & transfer to cplex
00065     // 
00067     // min_w        sum_{i=0}^N ( w^+_i + w^-_i) + C \sum_{i=0}^N \xi_i
00068     // w=[w^+ w^-]
00069     // b, xi
00070     // 
00071     // -y_i((w^+-w^-)^T x_i + b)-xi_i <= -1
00072     // xi_i >= 0 
00073     // w_i >= 0    forall i=1...N
00075     // min f^x
00076     // Ax <= b
00077     // -x <= 0
00078     // 
00079     // lb= [ -inf, //b
00080     //    2*dims [0], //w
00081     //    num_train [0] //xi 
00082     //  ]
00083     // 
00084     // ub= [ inf, //b
00085     //    2*dims [inf], //w
00086     //    num_train [inf] //xi 
00087     //  ]
00088     // 
00089     // f= [0,2*dim[1], num_train*C]
00090     // A= [-y', // b
00091     //  -y_ix_i // w_+
00092     //  +y_ix_i // w_-
00093     //  -1 //xi
00094     //  ]
00095     // 
00096     //  dim(A)=(n,1+2*dim+n)
00097     // 
00098     // b =  -1 -1 -1 -1 ... 
00099     bool setup_lpm(
00100         float64_t C, CSparseFeatures<float64_t>* x, CLabels* y, bool use_bias);
00101 
00102     // call this to setup linear part
00103     //
00104     // setup lp, to minimize
00105     // objective[0]*x_0 ... objective[cols-1]*x_{cols-1}
00106     // w.r.t. x
00107     // s.t. constraint_mat*x <= rhs
00108     // lb[i] <= x[i] <= ub[i] for all i
00109     bool setup_lp(
00110         float64_t* objective, float64_t* constraints_mat, int32_t rows,
00111         int32_t cols, float64_t* rhs, float64_t* lb, float64_t* ub);
00112 
00113 
00114     // call this to setup quadratic part H
00115     // x'*H*x
00116     // call setup_lp before (to setup the linear part / linear constraints)
00117     bool setup_qp(float64_t* H, int32_t dim);
00118     bool optimize(float64_t* sol, float64_t* lambda=NULL);
00119 
00120     bool dense_to_cplex_sparse(
00121         float64_t* H, int32_t rows, int32_t cols, int* &qmatbeg, int* &qmatcnt,
00122         int* &qmatind, double* &qmatval);
00123 
00124     inline bool set_time_limit(float64_t seconds)
00125     {
00126         return CPXsetdblparam (env, CPX_PARAM_TILIM, seconds) == 0;
00127     }
00128     inline bool write_problem(char* filename)
00129     {
00130         return CPXwriteprob (env, lp, filename, NULL) == 0;
00131     }
00132 
00133     inline bool write_Q(char* filename)
00134     {
00135 #if CPX_VERSION >= 1000 //CPXqpwrite has been deprecated in CPLEX 10
00136         return CPXwriteprob (env, lp, filename, NULL) == 0;
00137 #else
00138         return CPXqpwrite (env, lp, filename) == 0;
00139 #endif
00140     }
00141 
00143     inline virtual const char* get_name() const { return "Cplex"; }
00144 
00145 protected:
00146   CPXENVptr     env;
00147   CPXLPptr      lp;
00148   bool          lp_initialized;
00149 
00150   E_PROB_TYPE problem_type;
00151 };
00152 #endif
00153 #endif

SHOGUN Machine Learning Toolbox - Documentation