SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Cplex.h
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) 2006-2009 Soeren Sonnenburg
8  * Copyright (C) 2006-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef CCPLEX_H__
12 #define CCPLEX_H__
13 
14 #include <shogun/lib/config.h>
15 
16 #ifdef USE_CPLEX
17 extern "C" {
18 #include <ilcplex/cplex.h>
19 }
20 
21 #include <shogun/lib/common.h>
22 #include <shogun/base/SGObject.h>
23 
26 
27 namespace shogun
28 {
30 {
33 };
34 
42 class CCplex : public CSGObject
43 {
44 public:
45 
46  CCplex();
47  virtual ~CCplex();
48 
50  bool init(E_PROB_TYPE t, int32_t timeout=60);
51  bool cleanup();
52 
53  // A = [ E Z_w Z_x ] dim(A)=(num_dim+1, num_dim+1 + num_zero + num_bound)
54  // (+1 for bias!)
57  int32_t* idx_bound, int32_t num_bound, int32_t* w_zero,
58  int32_t num_zero, float64_t* vee, int32_t num_dim, bool use_bias);
59 
60  bool setup_lpboost(float64_t C, int32_t num_cols);
62  float64_t factor, SGSparseVectorEntry<float64_t>* h, int32_t len,
63  int32_t ulen, CBinaryLabels* label);
64 
65  // given N sparse inputs x_i, and corresponding labels y_i i=0...N-1
66  // create the following 1-norm SVM problem & transfer to cplex
67  //
69  // min_w sum_{i=0}^N ( w^+_i + w^-_i) + C \sum_{i=0}^N \xi_i
70  // w=[w^+ w^-]
71  // b, xi
72  //
73  // -y_i((w^+-w^-)^T x_i + b)-xi_i <= -1
74  // xi_i >= 0
75  // w_i >= 0 forall i=1...N
77  // min f^x
78  // Ax <= b
79  // -x <= 0
80  //
81  // lb= [ -inf, //b
82  // 2*dims [0], //w
83  // num_train [0] //xi
84  // ]
85  //
86  // ub= [ inf, //b
87  // 2*dims [inf], //w
88  // num_train [inf] //xi
89  // ]
90  //
91  // f= [0,2*dim[1], num_train*C]
92  // A= [-y', // b
93  // -y_ix_i // w_+
94  // +y_ix_i // w_-
95  // -1 //xi
96  // ]
97  //
98  // dim(A)=(n,1+2*dim+n)
99  //
100  // b = -1 -1 -1 -1 ...
101  bool setup_lpm(
102  float64_t C, CSparseFeatures<float64_t>* x, CBinaryLabels* y, bool use_bias);
103 
104  // call this to setup linear part
105  //
106  // setup lp, to minimize
107  // objective[0]*x_0 ... objective[cols-1]*x_{cols-1}
108  // w.r.t. x
109  // s.t. constraint_mat*x <= rhs
110  // lb[i] <= x[i] <= ub[i] for all i
111  bool setup_lp(
112  float64_t* objective, float64_t* constraints_mat, int32_t rows,
113  int32_t cols, float64_t* rhs, float64_t* lb, float64_t* ub);
114 
115 
116  // call this to setup quadratic part H
117  // x'*H*x
118  // call setup_lp before (to setup the linear part / linear constraints)
119  bool setup_qp(float64_t* H, int32_t dim);
120  bool optimize(float64_t* sol, float64_t* lambda=NULL);
121 
123  float64_t* H, int32_t rows, int32_t cols, int* &qmatbeg, int* &qmatcnt,
124  int* &qmatind, double* &qmatval);
125 
126  inline bool set_time_limit(float64_t seconds)
127  {
128  return CPXsetdblparam (env, CPX_PARAM_TILIM, seconds) == 0;
129  }
130  inline bool write_problem(char* filename)
131  {
132  return CPXwriteprob (env, lp, filename, NULL) == 0;
133  }
134 
135  inline bool write_Q(char* filename)
136  {
137 #if CPX_VERSION >= 1000 //CPXqpwrite has been deprecated in CPLEX 10
138  return CPXwriteprob (env, lp, filename, NULL) == 0;
139 #else
140  return CPXqpwrite (env, lp, filename) == 0;
141 #endif
142  }
143 
145  virtual const char* get_name() const { return "Cplex"; }
146 
147 protected:
148  CPXENVptr env;
149  CPXLPptr lp;
151 
153 };
154 }
155 #endif
156 #endif
Class CCplex to encapsulate access to the commercial cplex general purpose optimizer.
Definition: Cplex.h:42
E_PROB_TYPE
Definition: Cplex.h:29
bool setup_qp(float64_t *H, int32_t dim)
Definition: Cplex.cpp:581
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
bool write_problem(char *filename)
Definition: Cplex.h:130
bool optimize(float64_t *sol, float64_t *lambda=NULL)
Definition: Cplex.cpp:601
bool setup_lpboost(float64_t C, int32_t num_cols)
Definition: Cplex.cpp:261
bool dense_to_cplex_sparse(float64_t *H, int32_t rows, int32_t cols, int *&qmatbeg, int *&qmatcnt, int *&qmatind, double *&qmatval)
Definition: Cplex.cpp:503
virtual ~CCplex()
Definition: Cplex.cpp:28
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
bool lp_initialized
Definition: Cplex.h:150
bool add_lpboost_constraint(float64_t factor, SGSparseVectorEntry< float64_t > *h, int32_t len, int32_t ulen, CBinaryLabels *label)
Definition: Cplex.cpp:292
double float64_t
Definition: common.h:50
E_PROB_TYPE problem_type
Definition: Cplex.h:152
bool cleanup()
Definition: Cplex.cpp:469
bool setup_subgradientlpm_QP(float64_t C, CBinaryLabels *labels, CSparseFeatures< float64_t > *features, int32_t *idx_bound, int32_t num_bound, int32_t *w_zero, int32_t num_zero, float64_t *vee, int32_t num_dim, bool use_bias)
Definition: Cplex.cpp:87
bool setup_lpm(float64_t C, CSparseFeatures< float64_t > *x, CBinaryLabels *y, bool use_bias)
Definition: Cplex.cpp:322
virtual const char * get_name() const
Definition: Cplex.h:145
bool write_Q(char *filename)
Definition: Cplex.h:135
CPXENVptr env
Definition: Cplex.h:148
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
bool setup_lp(float64_t *objective, float64_t *constraints_mat, int32_t rows, int32_t cols, float64_t *rhs, float64_t *lb, float64_t *ub)
Definition: Cplex.cpp:531
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
CPXLPptr lp
Definition: Cplex.h:149

SHOGUN Machine Learning Toolbox - Documentation