SHOGUN  v3.1.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CCSOSVM.cpp
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) 2012 Viktor Gal
8  * Copyright (C) 2008 Chun-Nam Yu
9  */
10 
13 
14 using namespace shogun;
15 
18 {
19  init();
20 }
21 
23  : CLinearStructuredOutputMachine(model, model->get_labels())
24 {
25  init();
26 
27  if (w.vlen)
28  {
29  set_w(w);
30  }
31  else
32  {
34  m_w.zero();
35  }
36 }
37 
39 {
40 #ifdef USE_MOSEK
41  MSK_deleteenv(&m_msk_env);
42 #endif
43 }
44 
45 int32_t CCCSOSVM::mosek_qp_optimize(float64_t** G, float64_t* delta, float64_t* alpha, int32_t k, float64_t* dual_obj, float64_t rho)
46 {
47 #ifdef USE_MOSEK
48  int32_t t;
49  index_t Q_size = k*(k+1)/2;
51  MSKlidxt *aptrb;
52  MSKlidxt *aptre;
53  MSKidxt *asub;
54  SGVector<float64_t> aval(k);
55  MSKboundkeye bkc[1];
56  float64_t blc[1];
57  float64_t buc[1];
58  MSKboundkeye *bkx;
59  SGVector<float64_t> blx(k);
60  SGVector<float64_t> bux(k);
61  MSKidxt *qsubi,*qsubj;
62  SGVector<float64_t> qval(Q_size);
63 
64  MSKtask_t task;
65  MSKrescodee r;
66 
67  aptrb = (MSKlidxt*) SG_MALLOC(MSKlidxt, k);
68  aptre = (MSKlidxt*) SG_MALLOC(MSKlidxt, k);
69  asub = (MSKidxt*) SG_MALLOC(MSKidxt, k);
70  bkx = (MSKboundkeye*) SG_MALLOC(MSKboundkeye, k);
71  qsubi = (MSKidxt*) SG_MALLOC(MSKidxt, Q_size);
72  qsubj = (MSKidxt*) SG_MALLOC(MSKidxt, Q_size);
73 
74 
75  /* DEBUG */
76  /*
77  for (int32_t i=0;i<k;i++)
78  printf("delta: %.4f\n", delta[i]);
79 
80  printf("G:\n");
81  for (int32_t i=0;i<k;i++)
82  {
83  for (int32_t j=0;j<k;j++)
84  printf("%.4f ", G[i][j]);
85  printf("\n");
86  }
87  fflush(stdout);
88  */
89  /* DEBUG */
90 
91  for (int32_t i=0; i < k;i++)
92  {
93  c[i] = -delta[i];
94  aptrb[i] = i;
95  aptre[i] = i+1;
96  asub[i] = 0;
97  aval[i] = 1.0;
98  bkx[i] = MSK_BK_LO;
99  blx[i] = 0.0;
100  bux[i] = MSK_INFINITY;
101  }
102  bkc[0] = MSK_BK_FX;
103  blc[0] = m_C;
104  buc[0] = m_C;
105  /*
106  bkc[0] = MSK_BK_UP;
107  blc[0] = -MSK_INFINITY;
108  buc[0] = m_C;
109  */
110 
111  /* create the optimization task */
112  r = MSK_maketask(m_msk_env, 1, k, &task);
113 
114  if (r != MSK_RES_OK)
115  SG_ERROR("Could not create MOSEK task: %d\n", r)
116 
117  r = MSK_inputdata(task,
118  1,k,
119  1,k,
120  c,0.0,
121  aptrb,aptre,
122  asub,aval,
123  bkc,blc,buc,
124  bkx,blx,bux);
125 
126  if (r != MSK_RES_OK)
127  SG_ERROR("Error setting input data: %d\n", r)
128 
129  /* coefficients for the Gram matrix */
130  t = 0;
131  for (int32_t i=0;i<k;i++)
132  {
133  for (int32_t j=0;j<=i;j++)
134  {
135  qsubi[t] = i;
136  qsubj[t] = j;
137  qval[t] = G[i][j]/(1+rho);
138  t++;
139  }
140  }
141 
142  r = MSK_putqobj(task, k*(k+1)/2, qsubi, qsubj, qval);
143  if (r != MSK_RES_OK)
144  SG_ERROR("Error MSK_putqobj: %d\n", r)
145 
146  /* DEBUG */
147  /*
148  printf("t: %ld\n", t);
149  for (int32_t i=0;i<t;i++) {
150  printf("qsubi: %d, qsubj: %d, qval: %.4f\n", qsubi[i], qsubj[i], qval[i]);
151  }
152  fflush(stdout);
153  */
154  /* DEBUG */
155 
156  /* set relative tolerance gap (DEFAULT = 1E-8)*/
157  MSK_putdouparam(task, MSK_DPAR_INTPNT_TOL_REL_GAP, 1E-8);
158 
159  r = MSK_optimize(task);
160 
161  if (r != MSK_RES_OK)
162  SG_ERROR("Error MSK_optimize: %d\n", r)
163 
164  MSK_getsolutionslice(task,
165  MSK_SOL_ITR,
166  MSK_SOL_ITEM_XX,
167  0,
168  k,
169  alpha);
170 
171  /* output the objective value */
172  MSK_getprimalobj(task, MSK_SOL_ITR, dual_obj);
173 
174  MSK_deletetask(&task);
175 
176  /* free the memory */
177  SG_FREE(aptrb);
178  SG_FREE(aptre);
179  SG_FREE(asub);
180  SG_FREE(bkx);
181  SG_FREE(qsubi);
182  SG_FREE(qsubj);
183 
184  return r;
185 #else
186  return -1;
187 #endif
188 }
189 
191 {
192  if (data)
193  set_features(data);
194 
195  SGVector<float64_t> alpha;
196  float64_t** G; /* Gram matrix */
197  DynArray<SGSparseVector<float64_t> > dXc; /* constraint matrix */
198  // DOC **dXc; /* constraint matrix */
199  SGVector<float64_t> delta; /* rhs of constraints */
200  SGSparseVector<float64_t> new_constraint;
201  float64_t dual_obj=0, alphasum;
202  int32_t iter, size_active;
203  float64_t value;
204  SGVector<int32_t> idle; /* for cleaning up */
205  float64_t margin;
206  float64_t primal_obj;
207  SGVector<float64_t> proximal_rhs;
208  SGVector<float64_t> gammaG0;
209  float64_t min_rho = 0.001;
210  float64_t serious_counter=0;
211  float64_t rho = 1.0; /* temporarily set it to 1 first */
212 
213  float64_t expected_descent, primal_obj_b=-1, reg_master_obj;
214  int32_t null_step=1;
215  float64_t kappa=0.1;
216  float64_t temp_var;
217  float64_t proximal_term, primal_lower_bound;
218 
219  float64_t v_k;
220  float64_t obj_difference;
221  SGVector<float64_t> cut_error; // cut_error[i] = alpha_{k,i} at current center x_k
222  float64_t sigma_k;
223  float64_t m2 = 0.2;
224  float64_t m3 = 0.9;
225  float64_t gTd;
226  float64_t last_sigma_k=0;
227 
228  float64_t initial_primal_obj;
229  int32_t suff_decrease_cond=0;
230  float64_t decrease_proportion = 0.2; // start from 0.2 first
231 
232  float64_t z_k_norm;
233  float64_t last_z_k_norm=0;
234 
235  /* warm start */
236  SGVector<float64_t> w_b = m_w.clone();
237 
238  iter = 0;
239  size_active = 0;
240  G = NULL;
241 
242  new_constraint = find_cutting_plane(&margin);
243  value = margin - new_constraint.dense_dot(1.0, m_w.vector, m_w.vlen, 0);
244 
245  primal_obj_b = primal_obj = 0.5*m_w.dot(m_w.vector, m_w.vector, m_w.vlen)+m_C*value;
246  primal_lower_bound = 0;
247  expected_descent = -primal_obj_b;
248  initial_primal_obj = primal_obj_b;
249 
250  SG_INFO("Running CCCP inner loop solver: ")
251 
252  while ((!suff_decrease_cond) && (expected_descent<-m_eps) && (iter<m_max_iter))
253  {
254  ++iter;
255  ++size_active;
256 
257  SG_DEBUG("ITER %d\n", iter)
258  SG_PRINT(".")
259 
260  /* add constraint */
261  dXc.resize_array(size_active);
262  dXc[size_active - 1] = new_constraint;
263  // dXc[size_active - 1].add(new_constraint);
264  /*
265  dXc = (DOC**)realloc(dXc, sizeof(DOC*)*size_active);
266  dXc[size_active-1] = (DOC*)malloc(sizeof(DOC));
267  dXc[size_active-1]->fvec = new_constraint;
268  dXc[size_active-1]->slackid = 1; // only one common slackid (one-slack)
269  dXc[size_active-1]->costfactor = 1.0;
270  */
271  delta.resize_vector(size_active);
272  delta[size_active-1] = margin;
273  alpha.resize_vector(size_active);
274  alpha[size_active-1] = 0.0;
275  idle.resize_vector(size_active);
276  idle[size_active-1] = 0;
277  /* proximal point */
278  proximal_rhs.resize_vector(size_active);
279  cut_error.resize_vector(size_active);
280  // note g_i = - new_constraint
281  cut_error[size_active-1] = m_C*(new_constraint.dense_dot(1.0, w_b.vector, w_b.vlen, 0) - new_constraint.dense_dot(1.0, m_w.vector, m_w.vlen, 0));
282  cut_error[size_active-1] += (primal_obj_b - 0.5*w_b.dot(w_b.vector, w_b.vector, w_b.vlen));
283  cut_error[size_active-1] -= (primal_obj - 0.5*m_w.dot(m_w.vector, m_w.vector, m_w.vlen));
284 
285  gammaG0.resize_vector(size_active);
286 
287  /* update Gram matrix */
288  G = SG_REALLOC(float64_t*, G, size_active-1, size_active);
289  G[size_active - 1] = NULL;
290  for (index_t j=0; j < size_active;j++)
291  {
292  G[j] = SG_REALLOC(float64_t, G[j], size_active-1, size_active);
293  }
294  for (index_t j=0; j < size_active-1; j++)
295  {
296  G[size_active-1][j] = dXc[size_active-1].sparse_dot(dXc[j]);
297  G[j][size_active-1] = G[size_active-1][j];
298  }
299  G[size_active-1][size_active-1] = dXc[size_active-1].sparse_dot(dXc[size_active-1]);
300 
301  /* update gammaG0 */
302  if (null_step==1)
303  {
304  gammaG0[size_active-1] = dXc[size_active-1].dense_dot(1.0, w_b.vector, w_b.vlen, 0);
305  }
306  else
307  {
308  for (index_t i = 0; i < size_active; i++)
309  gammaG0[i] = dXc[i].dense_dot(1.0, w_b.vector, w_b.vlen, 0);
310  }
311 
312  /* update proximal_rhs */
313  for (index_t i = 0; i < size_active; i++)
314  {
315  switch(m_qp_type)
316  {
317  case MOSEK:
318  proximal_rhs[i] = delta[i] - rho/(1+rho)*gammaG0[i];
319  break;
320  case SVMLIGHT:
321  proximal_rhs[i] = (1+rho)*delta[i] - rho*gammaG0[i];
322  break;
323  default:
324  SG_ERROR("Invalid QPType: %d\n", m_qp_type)
325  }
326  }
327 
328  switch(m_qp_type)
329  {
330  case MOSEK:
331  /* solve QP to update alpha */
332  dual_obj = 0;
333  mosek_qp_optimize(G, proximal_rhs.vector, alpha.vector, size_active, &dual_obj, rho);
334  break;
335  case SVMLIGHT:
336  /* TODO: port required functionality from the latest SVM^light into shogun
337  * in order to be able to support this
338  *
339  if (size_active>1)
340  {
341  if (svmModel!=NULL)
342  free_model(svmModel,0);
343  svmModel = (MODEL*)my_malloc(sizeof(MODEL));
344  svm_learn_optimization(dXc,proximal_rhs,size_active,sm->sizePsi,&lparm,&kparm,NULL,svmModel,alpha);
345  }
346  else
347  {
348  ASSERT(size_active==1)
349  alpha[0] = m_C;
350  }
351  */
352  break;
353  default:
354  SG_ERROR("Invalid QPType: %d\n", m_qp_type)
355  }
356 
357  /* DEBUG */
358  //printf("r: %d\n", r); fflush(stdout);
359  //printf("dual: %.16lf\n", dual_obj);
360  /* END DEBUG */
361 
362  m_w.zero();
363  for (index_t j = 0; j < size_active; j++)
364  {
365  if (alpha[j]>m_C*m_alpha_thrld)
366  {
367  // TODO: move this to SGVector
368  // basically it's vector[i]= scale*sparsevector[i]
369  for (index_t k = 0; k < dXc[j].num_feat_entries; k++)
370  {
371  index_t idx = dXc[j].features[k].feat_index;
372  m_w[idx] += alpha[j]/(1+rho)*dXc[j].features[k].entry;
373  }
374  }
375  }
376 
377  if (m_qp_type == SVMLIGHT)
378  {
379  /* compute dual obj */
380  dual_obj = +0.5*(1+rho)*m_w.dot(m_w.vector, m_w.vector, m_w.vlen);
381  for (int32_t j=0;j<size_active;j++)
382  dual_obj -= proximal_rhs[j]/(1+rho)*alpha[j];
383  }
384 
385  z_k_norm = CMath::sqrt(m_w.dot(m_w.vector, m_w.vector, m_w.vlen));
386  m_w.vec1_plus_scalar_times_vec2(m_w.vector, rho/(1+rho), w_b.vector, w_b.vlen);
387 
388  /* detect if step size too small */
389  sigma_k = 0;
390  alphasum = 0;
391  for (index_t j = 0; j < size_active; j++)
392  {
393  sigma_k += alpha[j]*cut_error[j];
394  alphasum+=alpha[j];
395  }
396  sigma_k/=m_C;
397  gTd = -m_C*(new_constraint.dense_dot(1.0, m_w.vector, m_w.vlen, 0)
398  - new_constraint.dense_dot(1.0, w_b.vector, w_b.vlen, 0));
399 
400  for (index_t j = 0; j < size_active; j++)
401  SG_DEBUG("alpha[%d]: %.8g, cut_error[%d]: %.8g\n", j, alpha[j], j, cut_error[j])
402  SG_DEBUG("sigma_k: %.8g\n", sigma_k)
403  SG_DEBUG("alphasum: %.8g\n", alphasum)
404  SG_DEBUG("g^T d: %.8g\n", gTd)
405 
406  /* update cleanup information */
407  for (index_t j = 0; j < size_active; j++)
408  {
409  if (alpha[j]<m_alpha_thrld*m_C)
410  idle[j]++;
411  else
412  idle[j]=0;
413  }
414 
415  new_constraint = find_cutting_plane(&margin);
416  value = margin - new_constraint.dense_dot(1.0, m_w.vector, m_w.vlen, 0);
417 
418  /* print primal objective */
419  primal_obj = 0.5*m_w.dot(m_w.vector, m_w.vector, m_w.vlen)+m_C*value;
420 
421  SG_DEBUG("ITER PRIMAL_OBJ %.4f\n", primal_obj)
422 
423  temp_var = w_b.dot(w_b.vector, w_b.vector, w_b.vlen);
424  proximal_term = 0.0;
425  for (index_t i=0; i < m_model->get_dim(); i++)
426  proximal_term += (m_w[i]-w_b[i])*(m_w[i]-w_b[i]);
427 
428  reg_master_obj = -dual_obj+0.5*rho*temp_var/(1+rho);
429  expected_descent = reg_master_obj - primal_obj_b;
430 
431  v_k = (reg_master_obj - proximal_term*rho/2) - primal_obj_b;
432 
433  primal_lower_bound = CMath::max(primal_lower_bound, reg_master_obj - 0.5*rho*(1+rho)*proximal_term);
434 
435  SG_DEBUG("ITER REG_MASTER_OBJ: %.4f\n", reg_master_obj)
436  SG_DEBUG("ITER EXPECTED_DESCENT: %.4f\n", expected_descent)
437  SG_DEBUG("ITER PRIMLA_OBJ_B: %.4f\n", primal_obj_b)
438  SG_DEBUG("ITER RHO: %.4f\n", rho)
439  SG_DEBUG("ITER ||w-w_b||^2: %.4f\n", proximal_term)
440  SG_DEBUG("ITER PRIMAL_LOWER_BOUND: %.4f\n", primal_lower_bound)
441  SG_DEBUG("ITER V_K: %.4f\n", v_k)
442  SG_DEBUG("ITER margin: %.4f\n", margin)
443  SG_DEBUG("ITER psi*-psi: %.4f\n", value-margin)
444 
445  obj_difference = primal_obj - primal_obj_b;
446 
447  if (primal_obj<primal_obj_b+kappa*expected_descent)
448  {
449  /* extra condition to be met */
450  if ((gTd>m2*v_k)||(rho<min_rho+1E-8))
451  {
452  SG_DEBUG("SERIOUS STEP\n")
453 
454  /* update cut_error */
455  for (index_t i = 0; i < size_active; i++)
456  {
457  cut_error[i] -= (primal_obj_b - 0.5*w_b.dot(w_b.vector, w_b.vector, w_b.vlen));
458  cut_error[i] -= m_C*dXc[i].dense_dot(1.0, w_b.vector, w_b.vlen, 0);
459  cut_error[i] += (primal_obj - 0.5*m_w.dot(m_w, m_w, m_w.vlen));
460  cut_error[i] += m_C*dXc[i].dense_dot(1.0, m_w.vector, m_w.vlen, 0);
461  }
462  primal_obj_b = primal_obj;
463  /* copy w_b <- m_w */
464  for (index_t i=0; i < m_model->get_dim(); i++)
465  {
466  w_b[i] = m_w[i];
467  }
468  null_step = 0;
469  serious_counter++;
470  }
471  else
472  {
473  /* increase step size */
474  SG_DEBUG("NULL STEP: SS(ii) FAILS.\n")
475 
476  serious_counter--;
477  rho = CMath::max(rho/10,min_rho);
478  }
479  }
480  else
481  { /* no sufficient decrease */
482  serious_counter--;
483 
484  if ((cut_error[size_active-1]>m3*last_sigma_k)&&(CMath::abs(obj_difference)>last_z_k_norm+last_sigma_k))
485  {
486  SG_DEBUG("NULL STEP: NS(ii) FAILS.\n")
487  rho = CMath::min(10*rho,m_max_rho);
488  }
489  else
490  SG_DEBUG("NULL STEP\n")
491  }
492  /* update last_sigma_k */
493  last_sigma_k = sigma_k;
494  last_z_k_norm = z_k_norm;
495 
496 
497  /* break away from while loop if more than certain proportioal decrease in primal objective */
498  if (primal_obj_b/initial_primal_obj<1-decrease_proportion)
499  suff_decrease_cond = 1;
500 
501  /* clean up */
502  if (iter % m_cleanup_check == 0)
503  {
504  size_active = resize_cleanup(size_active, idle, alpha, delta, gammaG0, proximal_rhs, &G, dXc, cut_error);
505  ASSERT(size_active == proximal_rhs.vlen)
506  }
507  } // end cutting plane while loop
508 
509  SG_INFO(" Inner loop optimization finished.\n")
510 
511  for (index_t j = 0; j < size_active; j++)
512  SG_FREE(G[j]);
513  SG_FREE(G);
514 
515  /* copy */
516  for (index_t i=0; i < m_model->get_dim(); i++)
517  m_w[i] = w_b[i];
518 
519  m_primal_obj = primal_obj_b;
520 
521  return true;
522 }
523 
524 SGSparseVector<float64_t> CCCSOSVM::find_cutting_plane(float64_t* margin)
525 {
526  SGVector<float64_t> new_constraint(m_model->get_dim());
527  int32_t psi_size = m_model->get_dim();
528 
529  index_t num_samples = m_model->get_features()->get_num_vectors();
530  /* find cutting plane */
531  *margin = 0;
532  new_constraint.zero();
533  for (index_t i = 0; i < num_samples; i++)
534  {
535  CResultSet* result = m_model->argmax(m_w, i);
536  new_constraint.add(result->psi_truth);
537  result->psi_pred.scale(-1.0);
538  new_constraint.add(result->psi_pred);
539  /*
540  printf("%.16lf %.16lf\n",
541  SGVector<float64_t>::dot(result->psi_truth.vector, result->psi_truth.vector, result->psi_truth.vlen),
542  SGVector<float64_t>::dot(result->psi_pred.vector, result->psi_pred.vector, result->psi_pred.vlen));
543  */
544  *margin += result->delta;
545  SG_UNREF(result);
546  }
547  /* scaling */
548  float64_t scale = 1/(float64_t)num_samples;
549  new_constraint.scale(scale);
550  *margin *= scale;
551 
552  /* find the nnz elements in new_constraint */
553  index_t l = 0;
554  for (index_t i=0; i < psi_size; i++)
555  {
556  if (CMath::abs(new_constraint[i])>1E-10)
557  l++; // non-zero
558  }
559  /* TODO: does this really work good?
560  l = CMath::get_num_nonzero(new_constraint.vector, new_constraint.vlen);
561  */
562  /* create a sparse vector of the nnz of new_constraint */
563  SGSparseVector<float64_t> cut_plane(l);
564  index_t k = 0;
565  for (index_t i = 0; i < psi_size; i++)
566  {
567  if (CMath::abs(new_constraint[i])>1E-10)
568  {
569  cut_plane.features[k].feat_index = i;
570  cut_plane.features[k].entry = new_constraint[i];
571  k++;
572  }
573  }
574 
575  return cut_plane;
576 }
577 
578 int32_t CCCSOSVM::resize_cleanup(int32_t size_active, SGVector<int32_t>& idle, SGVector<float64_t>&alpha,
579  SGVector<float64_t>& delta, SGVector<float64_t>& gammaG0,
580  SGVector<float64_t>& proximal_rhs, float64_t ***ptr_G,
582 {
583  int32_t i, j, new_size_active;
584  index_t k;
585 
586  float64_t **G = *ptr_G;
587 
588  i=0;
589  while ((i<size_active)&&(idle[i]<m_idle_iter))
590  i++;
591 
592  j=i;
593  while((j<size_active)&&(idle[j]>=m_idle_iter))
594  j++;
595 
596  while (j<size_active)
597  {
598  /* copying */
599  alpha[i] = alpha[j];
600  delta[i] = delta[j];
601  gammaG0[i] = gammaG0[j];
602  cut_error[i] = cut_error[j];
603 
604  SG_FREE(G[i]);
605  G[i] = G[j];
606  G[j] = NULL;
607  // free_example(dXc[i],0);
608  dXc[i] = dXc[j];
609 // dXc[j] = NULL;
610 
611  i++;
612  j++;
613  while((j<size_active)&&(idle[j]>=m_idle_iter))
614  j++;
615  }
616  for (k=i;k<size_active;k++)
617  {
618  if (G[k]!=NULL) SG_FREE(G[k]);
619 // if (dXc[k].num_feat_entries > 0) SG_UNREF(dXc[k]);
620  }
621  new_size_active = i;
622  alpha.resize_vector(new_size_active);
623  delta.resize_vector(new_size_active);
624  gammaG0.resize_vector(new_size_active);
625  proximal_rhs.resize_vector(new_size_active);
626  G = SG_REALLOC(float64_t*, G, size_active, new_size_active);
627  dXc.resize_array(new_size_active);
628  cut_error.resize_vector(new_size_active);
629 
630  /* resize G and idle */
631  i=0;
632  while ((i<size_active)&&(idle[i]<m_idle_iter))
633  i++;
634 
635  j=i;
636  while((j<size_active)&&(idle[j]>=m_idle_iter))
637  j++;
638 
639  while (j<size_active)
640  {
641  idle[i] = idle[j];
642  for (k=0;k<new_size_active;k++)
643  G[k][i] = G[k][j];
644 
645  i++;
646  j++;
647  while((j<size_active)&&(idle[j]>=m_idle_iter))
648  j++;
649  }
650  idle.resize_vector(new_size_active);
651  for (k=0;k<new_size_active;k++)
652  G[k] = SG_REALLOC(float64_t, G[k], size_active, new_size_active);
653 
654  *ptr_G = G;
655 
656  return new_size_active;
657 }
658 
659 void CCCSOSVM::init()
660 {
661  m_C = 1.0;
662  m_eps = 1E-3;
663  m_alpha_thrld = 1E-14;
664  m_cleanup_check = 100;
665  m_idle_iter = 20;
666  m_max_iter = 1000;
667  m_max_rho = m_C;
668  m_primal_obj = CMath::INFTY;
669  m_qp_type = MOSEK;
670 
671 #ifdef USE_MOSEK
672  MSKrescodee r = MSK_RES_OK;
673 
674  /* create mosek environment */
675 #if (MSK_VERSION_MAJOR == 6)
676  r = MSK_makeenv(&m_msk_env, NULL, NULL, NULL, NULL);
677 #elif (MSK_VERSION_MAJOR == 7)
678  r = MSK_makeenv(&m_msk_env, NULL);
679 #else
680  #error "Unsupported Mosek version"
681 #endif
682 
683  /* check return code */
684  if (r != MSK_RES_OK)
685  SG_ERROR("Error while creating mosek env: %d\n", r)
686 
687  /* initialize the environment */
688  r = MSK_initenv(m_msk_env);
689  if (r != MSK_RES_OK)
690  SG_ERROR("Error while initializing mosek env: %d\n", r)
691 #endif
692 
693  SG_ADD(&m_C, "m_C", "C", MS_NOT_AVAILABLE);
694  SG_ADD(&m_eps, "m_eps", "Epsilon", MS_NOT_AVAILABLE);
695  SG_ADD(&m_alpha_thrld, "m_alpha_thrld", "Alpha threshold", MS_NOT_AVAILABLE);
696  SG_ADD(&m_cleanup_check, "m_cleanup_check", "Cleanup after given number of iterations", MS_NOT_AVAILABLE);
697  SG_ADD(&m_idle_iter, "m_idle_iter", "Maximum number of idle iteration", MS_NOT_AVAILABLE);
698  SG_ADD(&m_max_iter, "m_max_iter", "Maximum number of iterations", MS_NOT_AVAILABLE);
699  SG_ADD(&m_max_rho, "m_max_rho", "Max rho", MS_NOT_AVAILABLE);
700  SG_ADD(&m_primal_obj, "m_primal_obj", "Primal objective value", MS_NOT_AVAILABLE);
701  SG_ADD((machine_int_t*) &m_qp_type, "m_qp_type", "QP Solver Type", MS_NOT_AVAILABLE);
702 }
703 
705 {
706  return CT_CCSOSVM;
707 }
SGVector< float64_t > psi_truth
EMachineType
Definition: Machine.h:33
#define SG_INFO(...)
Definition: SGIO.h:120
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
compute dot product between v1 and v2 (blas optimized)
Definition: SGVector.h:344
int32_t index_t
Definition: common.h:60
static const float64_t INFTY
infinity
Definition: Math.h:1329
#define SG_UNREF(x)
Definition: SGRefObject.h:35
virtual int32_t get_num_vectors() const =0
#define SG_ERROR(...)
Definition: SGIO.h:131
virtual int32_t get_dim() const =0
bool train_machine(CFeatures *data=NULL)
Definition: CCSOSVM.cpp:190
void scale(T alpha)
scale vector inplace
Definition: SGVector.cpp:1200
#define SG_PRINT(...)
Definition: SGIO.h:139
#define ASSERT(x)
Definition: SGIO.h:203
Template Dynamic array class that creates an array that can be used like a list or an array...
Definition: DynArray.h:30
double float64_t
Definition: common.h:48
virtual ~CCCSOSVM()
Definition: CCSOSVM.cpp:38
static T max(T a, T b)
return the maximum of two integers
Definition: Math.h:165
SGVector< T > clone() const
Definition: SGVector.cpp:257
bool resize_array(int32_t n, bool exact_resize=false)
Definition: DynArray.h:338
Class CStructuredModel that represents the application specific model and contains most of the applic...
void set_w(SGVector< float64_t > W)
Definition: CCSOSVM.h:65
#define SG_DEBUG(...)
Definition: SGIO.h:109
T dense_dot(T alpha, T *vec, int32_t dim, T b)
int machine_int_t
Definition: common.h:57
virtual CResultSet * argmax(SGVector< float64_t > w, int32_t feat_idx, bool const training=true)=0
The class Features is the base class of all feature objects.
Definition: Features.h:62
static T min(T a, T b)
return the minimum of two integers
Definition: Math.h:158
virtual EMachineType get_classifier_type()
Definition: CCSOSVM.cpp:704
SGVector< float64_t > psi_pred
void resize_vector(int32_t n)
Definition: SGVector.cpp:307
#define SG_ADD(...)
Definition: SGObject.h:71
static float32_t sqrt(float32_t x)
x^0.5
Definition: Math.h:250
#define delta
Definition: sfa.cpp:23
index_t vlen
Definition: SGVector.h:706
static void vec1_plus_scalar_times_vec2(T *vec1, const T scalar, const T *vec2, int32_t n)
x=x+alpha*y
Definition: SGVector.cpp:580
static T abs(T a)
return the absolute value of a number
Definition: Math.h:184

SHOGUN Machine Learning Toolbox - Documentation