SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
libocas.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  * libocas.h: Implementation of the OCAS solver for training
8  * linear SVM classifiers.
9  *
10  * Copyright (C) 2008, 2009 Vojtech Franc, xfrancv@cmp.felk.cvut.cz
11  * Soeren Sonnenburg, soeren.sonnenburg@first.fraunhofer.de
12  * Implementation of SVM-Ocas solver.
13  *-------------------------------------------------------------------- */
14 
15 #include <shogun/lib/common.h>
16 
17 #ifndef libocas_h
18 #define libocas_h
19 #ifndef DOXYGEN_SHOULD_SKIP_THIS
20 namespace shogun
21 {
22 #define LIBOCAS_PLUS_INF (-log(0.0))
23 #define LIBOCAS_CALLOC(x,y) calloc(x,y)
24 #define LIBOCAS_FREE(x) SG_FREE(x)
25 #define LIBOCAS_INDEX(ROW,COL,NUM_ROWS) ((COL)*(NUM_ROWS)+(ROW))
26 #define LIBOCAS_MIN(A,B) ((A) > (B) ? (B) : (A))
27 #define LIBOCAS_MAX(A,B) ((A) < (B) ? (B) : (A))
28 #define LIBOCAS_ABS(A) ((A) < 0 ? -(A) : (A))
29 
30 typedef struct {
31  uint32_t nIter; /* number of iterations */
32  uint32_t nCutPlanes; /* number of cutitng buffered planes */
33  uint32_t nNZAlpha; /* number of non-zero Lagrangeans (effective number of CPs) */
34  uint32_t trn_err; /* number of training errors */
35  float64_t Q_P; /* primal objective value */
36  float64_t Q_D; /* dual objective value */
37  float64_t output_time; /* time spent in computing outputs */
38  float64_t sort_time; /* time spent in sorting */
39  float64_t add_time; /* time spent in adding examples to compute cutting planes */
40  float64_t w_time; /* time spent in computing parameter vector */
41  float64_t qp_solver_time; /* time spent in inner QP solver */
42  float64_t ocas_time; /* total time spent in svm_ocas_solver */
43  float64_t print_time; /* time spent in ocas_print function */
44  int8_t qp_exitflag; /* exitflag from the last call of the inner QP solver */
45  int8_t exitflag; /* 1 .. ocas.Q_P - ocas.Q_D <= TolRel*ABS(ocas.Q_P)
46  2 .. ocas.Q_P - ocas.Q_D <= TolAbs
47  3 .. ocas.Q_P <= QPBound
48  4 .. optimization time >= MaxTime
49  -1 .. ocas.nCutPlanes >= BufSize
50  -2 .. not enough memory for the solver */
51 } ocas_return_value_T;
52 
53 /* binary linear SVM solver */
54 ocas_return_value_T svm_ocas_solver(
55  float64_t C, /* regularizarion constant */
56  uint32_t nData, /* number of exmaples */
57  float64_t TolRel, /* halts if 1-Q_P/Q_D <= TolRel */
58  float64_t TolAbs, /* halts if Q_P-Q_D <= TolRel */
59  float64_t QPBound, /* halts if QP <= QPBound */
60  float64_t MaxTime, /* maximal time in seconds spent in optmization */
61  uint32_t BufSize, /* maximal number of buffered cutting planes */
62  uint8_t Method, /* 0..standard CP (SVM-Perf,BMRM), 1..OCAS */
63  void (*compute_W)(float64_t*, float64_t*, float64_t*, uint32_t, void*),
64  float64_t (*update_W)(float64_t, void*),
65  int (*add_new_cut)(float64_t*, uint32_t*, uint32_t, uint32_t, void*),
66  int (*compute_output)( float64_t*, void* ),
67  int (*sort)(float64_t*, float64_t*, uint32_t),
68  void (*ocas_print)(ocas_return_value_T),
69  void* user_data);
70 
71 /* binary linear SVM solver which allows using different C for each example*/
72 ocas_return_value_T svm_ocas_solver_difC(
73  float64_t *C, /* regularizarion constants for each example */
74  uint32_t nData, /* number of exmaples */
75  float64_t TolRel, /* halts if 1-Q_P/Q_D <= TolRel */
76  float64_t TolAbs, /* halts if Q_P-Q_D <= TolRel */
77  float64_t QPBound, /* halts if QP <= QPBound */
78  float64_t MaxTime, /* maximal time in seconds spent in optmization */
79  uint32_t BufSize, /* maximal number of buffered cutting planes */
80  uint8_t Method, /* 0..standard CP (SVM-Perf,BMRM), 1..OCAS */
81  void (*compute_W)(float64_t*, float64_t*, float64_t*, uint32_t, void*),
82  float64_t (*update_W)(float64_t, void*),
83  int (*add_new_cut)(float64_t*, uint32_t*, uint32_t, uint32_t, void*),
84  int (*compute_output)( float64_t*, void* ),
85  int (*sort)(float64_t*, float64_t*, uint32_t),
86  void (*ocas_print)(ocas_return_value_T),
87  void* user_data);
88 
89 /* multi-class (Singer-Crammer formulation) linear SVM solver */
90 ocas_return_value_T msvm_ocas_solver(
91  float64_t C,
92  float64_t *data_y,
93  uint32_t nY,
94  uint32_t nData,
95  float64_t TolRel,
96  float64_t TolAbs,
97  float64_t QPBound,
98  float64_t MaxTime,
99  uint32_t _BufSize,
100  uint8_t Method,
101  void (*compute_W)(float64_t*, float64_t*, float64_t*, uint32_t, void*),
102  float64_t (*update_W)(float64_t, void*),
103  int (*add_new_cut)(float64_t*, uint32_t*, uint32_t, void*),
104  int (*compute_output)(float64_t*, void* ),
105  int (*sort)(float64_t*, float64_t*, uint32_t),
106  void (*ocas_print)(ocas_return_value_T),
107  void* user_data);
108 
109 
110 /* binary linear SVM solver */
111 ocas_return_value_T svm_ocas_solver_nnw(
112  float64_t C, /* regularizarion constant */
113  uint32_t nData, /* number of exmaples */
114  uint32_t num_nnw, /* number of components of W which must non-negative*/
115  uint32_t* nnw_idx, /* indices of W which must be non-negative */
116  float64_t TolRel, /* halts if 1-Q_P/Q_D <= TolRel */
117  float64_t TolAbs, /* halts if Q_P-Q_D <= TolRel */
118  float64_t QPBound, /* halts if QP <= QPBound */
119  float64_t MaxTime, /* maximal time in seconds spent in optmization */
120  uint32_t BufSize, /* maximal number of buffered cutting planes */
121  uint8_t Method, /* 0..standard CP (SVM-Perf,BMRM), 1..OCAS */
122  int (*add_pw_constr)(uint32_t, uint32_t, void*),
123  void (*clip_neg_w)(uint32_t, uint32_t*, void*),
124  void (*compute_W)(float64_t*, float64_t*, float64_t*, uint32_t, void*),
125  float64_t (*update_W)(float64_t, void*),
126  int (*add_new_cut)(float64_t*, uint32_t*, uint32_t, uint32_t, void*),
127  int (*compute_output)( float64_t*, void* ),
128  int (*sort)(float64_t*, float64_t*, uint32_t),
129  void (*ocas_print)(ocas_return_value_T),
130  void* user_data);
131 
132 }
133 #endif // DOXYGEN_SHOULD_SKIP_THIS
134 #endif /* libocas_h */
135 

SHOGUN Machine Learning Toolbox - Documentation