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

SHOGUN Machine Learning Toolbox - Documentation