forked from cjlin1/liblinear
/
linear.cpp
4657 lines (4034 loc) · 95.2 KB
/
linear.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <locale.h>
#include <ctime>
#include <iostream>
#include <vector>
#include "linear.h"
#include "newton.h"
int liblinear_version = LIBLINEAR_VERSION;
typedef signed char schar;
template <class T> static inline void my_swap(T& x, T& y) { T t=x; x=y; y=t; }
#ifndef min
template <class T> static inline T min(T x,T y) { return (x<y)?x:y; }
#endif
#ifndef max
template <class T> static inline T max(T x,T y) { return (x>y)?x:y; }
#endif
template <class S, class T> static inline void clone(T*& dst, S* src, int n)
{
dst = new T[n];
memcpy((void *)dst,(void *)src,sizeof(T)*n);
}
// This value is used to define epsilon in the Solver function
// See Shark/Test/Algorithms/Trainers/LinearSvmTrainer.cpp
// of the Shark-ML package.
#define MAX_KKT_VIOLATION 1e-5
#define STOPPING_CRITERION 0.0009
/* #define MEASURE_HEAP_SATURATION */
#define TRACE_OPTIM_TRAJ
#ifdef TRACE_OPTIM_TRAJ
struct stopwatch{
std::clock_t start_time;
std::clock_t paused_time;
bool paused;
stopwatch(){
start_time = std::clock();
paused = false;
}
void pause(){
paused_time = std::clock();
paused = true;
}
void resume(){
if(paused){
// advance the start time
start_time += std::clock()-paused_time;
}
}
double get_time(){
return (std::clock() - start_time)/ (double)(CLOCKS_PER_SEC / 1000);
}
};
#endif
/* #define DIAGNOSTIC2 */
#define INF HUGE_VAL
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
static void print_string_stdout(const char *s)
{
fputs(s,stdout);
fflush(stdout);
}
static void print_null(const char *s) {}
static void (*liblinear_print_string) (const char *) = &print_string_stdout;
#if 1
static void info(const char *fmt,...)
{
char buf[BUFSIZ];
va_list ap;
va_start(ap,fmt);
vsprintf(buf,fmt,ap);
va_end(ap);
(*liblinear_print_string)(buf);
}
#else
static void info(const char *fmt,...) {}
#endif
class sparse_operator
{
public:
static double nrm2_sq(const feature_node *x)
{
double ret = 0;
while(x->index != -1)
{
ret += x->value*x->value;
x++;
}
return (ret);
}
static double dot(const double *s, const feature_node *x)
{
double ret = 0;
while(x->index != -1)
{
ret += s[x->index-1]*x->value;
x++;
}
return (ret);
}
static double sparse_dot(const feature_node *x1, const feature_node *x2)
{
double ret = 0;
while(x1->index != -1 && x2->index != -1)
{
if(x1->index == x2->index)
{
ret += x1->value * x2->value;
++x1;
++x2;
}
else
{
if(x1->index > x2->index)
++x2;
else
++x1;
}
}
return (ret);
}
static void axpy(const double a, const feature_node *x, double *y)
{
while(x->index != -1)
{
y[x->index-1] += a*x->value;
x++;
}
}
};
// L2-regularized empirical risk minimization
// min_w w^Tw/2 + \sum C_i \xi(w^Tx_i), where \xi() is the loss
class l2r_erm_fun: public function
{
public:
l2r_erm_fun(const problem *prob, const parameter *param, double *C);
~l2r_erm_fun();
double fun(double *w);
double linesearch_and_update(double *w, double *d, double *f, double *g, double alpha);
int get_nr_variable(void);
protected:
virtual double C_times_loss(int i, double wx_i) = 0;
void Xv(double *v, double *Xv);
void XTv(double *v, double *XTv);
double *C;
const problem *prob;
double *wx;
double *tmp; // a working array
double wTw;
int regularize_bias;
};
l2r_erm_fun::l2r_erm_fun(const problem *prob, const parameter *param, double *C)
{
int l=prob->l;
this->prob = prob;
wx = new double[l];
tmp = new double[l];
this->C = C;
this->regularize_bias = param->regularize_bias;
}
l2r_erm_fun::~l2r_erm_fun()
{
delete[] wx;
delete[] tmp;
}
double l2r_erm_fun::fun(double *w)
{
int i;
double f=0;
int l=prob->l;
int w_size=get_nr_variable();
wTw = 0;
Xv(w, wx);
for(i=0;i<w_size;i++)
wTw += w[i]*w[i];
if(regularize_bias == 0)
wTw -= w[w_size-1]*w[w_size-1];
for(i=0;i<l;i++)
f += C_times_loss(i, wx[i]);
f = f + 0.5 * wTw;
return(f);
}
int l2r_erm_fun::get_nr_variable(void)
{
return prob->n;
}
// On entry *f must be the function value of w
// On exit w is updated and *f is the new function value
double l2r_erm_fun::linesearch_and_update(double *w, double *s, double *f, double *g, double alpha)
{
int i;
int l = prob->l;
double sTs = 0;
double wTs = 0;
double gTs = 0;
double eta = 0.01;
int w_size = get_nr_variable();
int max_num_linesearch = 20;
double fold = *f;
Xv(s, tmp);
for (i=0;i<w_size;i++)
{
sTs += s[i] * s[i];
wTs += s[i] * w[i];
gTs += s[i] * g[i];
}
if(regularize_bias == 0)
{
// bias not used in calculating (w + \alpha s)^T (w + \alpha s)
sTs -= s[w_size-1] * s[w_size-1];
wTs -= s[w_size-1] * w[w_size-1];
}
int num_linesearch = 0;
for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++)
{
double loss = 0;
for(i=0;i<l;i++)
{
double inner_product = tmp[i] * alpha + wx[i];
loss += C_times_loss(i, inner_product);
}
*f = loss + (alpha * alpha * sTs + wTw) / 2.0 + alpha * wTs;
if (*f - fold <= eta * alpha * gTs)
{
for (i=0;i<l;i++)
wx[i] += alpha * tmp[i];
break;
}
else
alpha *= 0.5;
}
if (num_linesearch >= max_num_linesearch)
{
*f = fold;
return 0;
}
else
for (i=0;i<w_size;i++)
w[i] += alpha * s[i];
wTw += alpha * alpha * sTs + 2* alpha * wTs;
return alpha;
}
void l2r_erm_fun::Xv(double *v, double *Xv)
{
int i;
int l=prob->l;
feature_node **x=prob->x;
for(i=0;i<l;i++)
Xv[i]=sparse_operator::dot(v, x[i]);
}
void l2r_erm_fun::XTv(double *v, double *XTv)
{
int i;
int l=prob->l;
int w_size=get_nr_variable();
feature_node **x=prob->x;
for(i=0;i<w_size;i++)
XTv[i]=0;
for(i=0;i<l;i++)
sparse_operator::axpy(v[i], x[i], XTv);
}
class l2r_lr_fun: public l2r_erm_fun
{
public:
l2r_lr_fun(const problem *prob, const parameter *param, double *C);
~l2r_lr_fun();
void grad(double *w, double *g);
void Hv(double *s, double *Hs);
void get_diag_preconditioner(double *M);
private:
double *D;
double C_times_loss(int i, double wx_i);
};
l2r_lr_fun::l2r_lr_fun(const problem *prob, const parameter *param, double *C):
l2r_erm_fun(prob, param, C)
{
int l=prob->l;
D = new double[l];
}
l2r_lr_fun::~l2r_lr_fun()
{
delete[] D;
}
double l2r_lr_fun::C_times_loss(int i, double wx_i)
{
double ywx_i = wx_i * prob->y[i];
if (ywx_i >= 0)
return C[i]*log(1 + exp(-ywx_i));
else
return C[i]*(-ywx_i + log(1 + exp(ywx_i)));
}
void l2r_lr_fun::grad(double *w, double *g)
{
int i;
double *y=prob->y;
int l=prob->l;
int w_size=get_nr_variable();
for(i=0;i<l;i++)
{
tmp[i] = 1/(1 + exp(-y[i]*wx[i]));
D[i] = tmp[i]*(1-tmp[i]);
tmp[i] = C[i]*(tmp[i]-1)*y[i];
}
XTv(tmp, g);
for(i=0;i<w_size;i++)
g[i] = w[i] + g[i];
if(regularize_bias == 0)
g[w_size-1] -= w[w_size-1];
}
void l2r_lr_fun::get_diag_preconditioner(double *M)
{
int i;
int l = prob->l;
int w_size=get_nr_variable();
feature_node **x = prob->x;
for (i=0; i<w_size; i++)
M[i] = 1;
if(regularize_bias == 0)
M[w_size-1] = 0;
for (i=0; i<l; i++)
{
feature_node *xi = x[i];
while (xi->index!=-1)
{
M[xi->index-1] += xi->value*xi->value*C[i]*D[i];
xi++;
}
}
}
void l2r_lr_fun::Hv(double *s, double *Hs)
{
int i;
int l=prob->l;
int w_size=get_nr_variable();
feature_node **x=prob->x;
for(i=0;i<w_size;i++)
Hs[i] = 0;
for(i=0;i<l;i++)
{
feature_node * const xi=x[i];
double xTs = sparse_operator::dot(s, xi);
xTs = C[i]*D[i]*xTs;
sparse_operator::axpy(xTs, xi, Hs);
}
for(i=0;i<w_size;i++)
Hs[i] = s[i] + Hs[i];
if(regularize_bias == 0)
Hs[w_size-1] -= s[w_size-1];
}
class l2r_l2_svc_fun: public l2r_erm_fun
{
public:
l2r_l2_svc_fun(const problem *prob, const parameter *param, double *C);
~l2r_l2_svc_fun();
void grad(double *w, double *g);
void Hv(double *s, double *Hs);
void get_diag_preconditioner(double *M);
protected:
void subXTv(double *v, double *XTv);
int *I;
int sizeI;
private:
double C_times_loss(int i, double wx_i);
};
l2r_l2_svc_fun::l2r_l2_svc_fun(const problem *prob, const parameter *param, double *C):
l2r_erm_fun(prob, param, C)
{
I = new int[prob->l];
}
l2r_l2_svc_fun::~l2r_l2_svc_fun()
{
delete[] I;
}
double l2r_l2_svc_fun::C_times_loss(int i, double wx_i)
{
double d = 1 - prob->y[i] * wx_i;
if (d > 0)
return C[i] * d * d;
else
return 0;
}
void l2r_l2_svc_fun::grad(double *w, double *g)
{
int i;
double *y=prob->y;
int l=prob->l;
int w_size=get_nr_variable();
sizeI = 0;
for (i=0;i<l;i++)
{
tmp[i] = wx[i] * y[i];
if (tmp[i] < 1)
{
tmp[sizeI] = C[i]*y[i]*(tmp[i]-1);
I[sizeI] = i;
sizeI++;
}
}
subXTv(tmp, g);
for(i=0;i<w_size;i++)
g[i] = w[i] + 2*g[i];
if(regularize_bias == 0)
g[w_size-1] -= w[w_size-1];
}
void l2r_l2_svc_fun::get_diag_preconditioner(double *M)
{
int i;
int w_size=get_nr_variable();
feature_node **x = prob->x;
for (i=0; i<w_size; i++)
M[i] = 1;
if(regularize_bias == 0)
M[w_size-1] = 0;
for (i=0; i<sizeI; i++)
{
int idx = I[i];
feature_node *xi = x[idx];
while (xi->index!=-1)
{
M[xi->index-1] += xi->value*xi->value*C[idx]*2;
xi++;
}
}
}
void l2r_l2_svc_fun::Hv(double *s, double *Hs)
{
int i;
int w_size=get_nr_variable();
feature_node **x=prob->x;
for(i=0;i<w_size;i++)
Hs[i]=0;
for(i=0;i<sizeI;i++)
{
feature_node * const xi=x[I[i]];
double xTs = sparse_operator::dot(s, xi);
xTs = C[I[i]]*xTs;
sparse_operator::axpy(xTs, xi, Hs);
}
for(i=0;i<w_size;i++)
Hs[i] = s[i] + 2*Hs[i];
if(regularize_bias == 0)
Hs[w_size-1] -= s[w_size-1];
}
void l2r_l2_svc_fun::subXTv(double *v, double *XTv)
{
int i;
int w_size=get_nr_variable();
feature_node **x=prob->x;
for(i=0;i<w_size;i++)
XTv[i]=0;
for(i=0;i<sizeI;i++)
sparse_operator::axpy(v[i], x[I[i]], XTv);
}
class l2r_l2_svr_fun: public l2r_l2_svc_fun
{
public:
l2r_l2_svr_fun(const problem *prob, const parameter *param, double *C);
void grad(double *w, double *g);
private:
double C_times_loss(int i, double wx_i);
double p;
};
l2r_l2_svr_fun::l2r_l2_svr_fun(const problem *prob, const parameter *param, double *C):
l2r_l2_svc_fun(prob, param, C)
{
this->p = param->p;
this->regularize_bias = param->regularize_bias;
}
double l2r_l2_svr_fun::C_times_loss(int i, double wx_i)
{
double d = wx_i - prob->y[i];
if(d < -p)
return C[i]*(d+p)*(d+p);
else if(d > p)
return C[i]*(d-p)*(d-p);
return 0;
}
void l2r_l2_svr_fun::grad(double *w, double *g)
{
int i;
double *y=prob->y;
int l=prob->l;
int w_size=get_nr_variable();
double d;
sizeI = 0;
for(i=0;i<l;i++)
{
d = wx[i] - y[i];
// generate index set I
if(d < -p)
{
tmp[sizeI] = C[i]*(d+p);
I[sizeI] = i;
sizeI++;
}
else if(d > p)
{
tmp[sizeI] = C[i]*(d-p);
I[sizeI] = i;
sizeI++;
}
}
subXTv(tmp, g);
for(i=0;i<w_size;i++)
g[i] = w[i] + 2*g[i];
if(regularize_bias == 0)
g[w_size-1] -= w[w_size-1];
}
// Calculate the primal objective
std::pair<double,double> calc_WW_primal_obj(
const problem *prob,
int nr_class,
double * w,
double C
){
double w_val;
int w_size = prob->n;
int l = prob->l;
int idx, s;
double norm_w = 0;
for(idx = 0; idx < w_size; idx++){
for(s=0;s<nr_class;s++){
w_val = w[idx*nr_class+s];
norm_w += (w_val*w_val);
}
}
int i,yi;
const feature_node *xi;
double * wx = new double[nr_class];
double WW_hinge_risk = 0;
double val;
for(i = 0; i < l; i++){
yi = (int)prob->y[i];
for(s=0;s<nr_class;s++){
wx[s] = 0.0;
}
// compute the k dimension vector w'xi
for(xi = prob->x[i]; (idx=xi->index)!=-1;xi++){
for(s=0;s<nr_class;s++){
wx[s] += w[(idx-1)*nr_class+s]*xi->value;
}
}
for(s=0; s<nr_class; s++){
if(s != yi){
val = 1 - (wx[yi] - wx[s]);
if(val > 0){
WW_hinge_risk += val;
}
}
}
}
printf("%f,%f,", norm_w/2, C*WW_hinge_risk);
return std::pair<double,double>(norm_w/2, C*WW_hinge_risk);
/* return (norm_w/2) + C*WW_hinge_risk; */
}
// A coordinate descent algorithm for
// multi-class support vector machines by Weston and Watkins using reflection code theory
//
// UPDATE THIS!
// min_{\alpha} 0.5 \sum_m ||w_m(\alpha)||^2 + \sum_i \sum_m e^m_i alpha^m_i
// s.t. \alpha^m_i <= C^m_i \forall m,i , \sum_m \alpha^m_i=0 \forall i
//
// where e^m_i = 0 if y_i = m,
// e^m_i = 1 if y_i != m,
// C^m_i = C if m = y_i,
// C^m_i = 0 if m != y_i,
// and w_m(\alpha) = \sum_i \alpha^m_i x_i
//
// Given:
// x, y, C
// eps is the stopping tolerance
//
// solution will be put in w
//
template <class T>
struct T_heap {
int _size;
struct _tuple{
double val;
T data;
};
_tuple * a;
T_heap(int max_size)
{
_size = 0;
a = new _tuple[max_size];
}
~T_heap()
{
delete [] a;
}
bool cmp(const _tuple & left, const _tuple & right)
{
return left.val < right.val;
}
int size()
{
return _size;
}
void push(double val, T data)
{
a[_size].val = val;
a[_size].data = data;
_size++;
int i = _size-1;
while(i)
{
int p = (i-1)/2;
if(cmp(a[p], a[i]))
{
my_swap(a[i], a[p]);
i = p;
}
else
break;
}
}
void pop()
{
_size--;
a[0] = a[_size];
int i = 0;
while(i*2+1 < _size)
{
int l = i*2+1;
int r = i*2+2;
if(r < _size && cmp(a[l], a[r]))
l = r;
if(cmp(a[i], a[l]))
{
my_swap(a[i], a[l]);
i = l;
}
else
break;
}
}
_tuple top()
{
return a[0];
}
#ifdef DIAGNOSTIC2
void print_state(){
for(int i = 0; i < _size; i++){
std::cout << "(" << a[i].val << "," << a[i].data << "), ";
}
std::cout << std::endl;
}
#endif
};
class Solver_MCSVM_WW
{
public:
Solver_MCSVM_WW(const problem *prob, int nr_class, double C, double eps=0.1, int max_iter=5);
~Solver_MCSVM_WW();
void Solve(double *w);
private:
/* double epsilon = 0.1 * MAX_KKT_VIOLATION; */
double epsilon = 0;
double solve_sub_problem(
T_heap<bool> & UpDnHeap,
T_heap<int> & IdxHeap,
double * vSort,
int * vIdx,
double *beta);
double C;
int w_size, l; // l is the number of instances
int nr_class;
int max_iter;
double eps;
const problem *prob;
/* double * vSort; */
/* int * vIdx; */
};
Solver_MCSVM_WW::~Solver_MCSVM_WW()
{
}
Solver_MCSVM_WW::Solver_MCSVM_WW(const problem *prob, int nr_class, double C, double eps, int max_iter)
{
this->w_size = prob->n;
this->l = prob->l;
this->nr_class = nr_class;
this->eps = eps;
this->max_iter = max_iter;
this->prob = prob;
this->C = C;
}
#ifdef DIAGNOSTIC2
template <class T>
void print_array(T * v, int length){
printf("[");
for(int i = 0; i< length -1; i++){
printf("%f,", v[i]);
}
printf("%f]\n", v[length-1]);
}
#endif
double Solver_MCSVM_WW::solve_sub_problem(
T_heap<bool> & UpDnHeap,
T_heap<int> & IdxHeap,
double * vSort,
int * vIdx,
double * beta){
// modifies the following state variables
// double * vSort
// int * vIdx
//
// assumes that beta has been properly initialized with zeros
// EXPLANATION
// set * denote an arbitrary number strictly between 0 and C
//
// num_up num_mi num_dn
// | | |
// /----------\ /-----------\ /----------\
// | | | | | |
// [C, C, ..., C, *, *, ... , *, 0, 0, ..., 0] Optimizer
//
// [v1 .... v3 v4 ... v6 v7 ... v9] vSort
// ^ ^ ^ ^
// | | | |
// X Y U V
//
// X = vId[num_up-1].val
// Y = vId[num_up].val
// U = vId[num_up+num_mi-1].val
// V = vId[num_up+num_mi].val
int num_up = 0, num_mi = 0, num_dn;
double sum_v_mi = 0;
bool up;
double val;
int nvIdx = 0, nvSort = 0, nvPos = IdxHeap.size();
vSort[nvSort] = IdxHeap.top().val;
nvSort++;
#ifdef MEASURE_HEAP_SATURATION
int niter = 0;
#endif
while(UpDnHeap.size()>0){
#ifdef MEASURE_HEAP_SATURATION
niter++;
#endif
up = UpDnHeap.top().data;
val = UpDnHeap.top().val;
UpDnHeap.pop();
if(up){
sum_v_mi -= (val+C);
num_up++;
num_mi--;
}else{
/* std::cout << "idx: " << IdxHeap.top().data << " at " << nvIdx << "\n"; */
vIdx[nvIdx] = IdxHeap.top().data;
IdxHeap.pop();
if(nvSort < nvPos){
vSort[nvSort] = IdxHeap.top().val;
}
sum_v_mi += val;
num_mi++;
nvIdx++;
nvSort++;
}
double gamma = (C*num_up + sum_v_mi)/(num_mi+1.0);
num_dn = nr_class - 1 - num_up - num_mi;
bool kkt = 1;
if(num_up > 0){
kkt *= ((C+gamma) <= vSort[num_up-1]);
}
if(num_mi > 0){
kkt *= (vSort[num_up] <= (C + gamma));
kkt *= (gamma <= vSort[num_up+num_mi-1]);
}
if(num_dn > 0 && num_up+num_mi < nvPos){
kkt *= (vSort[num_up+num_mi] <= gamma);
}
/* double beta_sum = C*num_up + sum_v_mi - gamma * num_mi; */
if(kkt){
double beta_val;
for(int j = 0; j < nvIdx; j++){
if(j < num_up){
beta_val = C;
}else{
beta_val = vSort[j] - gamma;
/* beta_sum += beta_val; */
}
beta[vIdx[j]] = beta_val;
}
#ifdef MEASURE_HEAP_SATURATION
std::cout << niter << std::endl;
#endif
return C*num_up + sum_v_mi - gamma * num_mi;
}
}
printf("did not find kkt");
exit(0);
}
void Solver_MCSVM_WW::Solve(double *w){
#ifdef TRACE_OPTIM_TRAJ
stopwatch SW;
double dual_gap_init;
#endif
int i,s,j;
int idx;
double *alpha = new double[l*(nr_class-1)];
double *alpha_block_sums = new double[l];
double *alpha_new = new double[nr_class-1];
double *del_alpha_pi = new double[nr_class];
double *x_sq_norms = new double[l];
double *wxi = new double[nr_class];
double * vSort = new double[nr_class-1];
int * vIdx = new int[nr_class-1];
double * v_pos = new double[nr_class-1];
int * v_pos_idx = new int[nr_class-1];
int nv_pos;
T_heap<bool> UpDnHeap(2*(nr_class-1));
T_heap<int> IdxHeap(nr_class-1);
const feature_node *xi;
// Compute the squared norms of all the samples
for(i=0;i<l;i++)
{
x_sq_norms[i] = 0;
xi = prob->x[i];
while(xi->index != -1)
{
double val = xi->value;
x_sq_norms[i] += (val*val);
xi++;
}
}
int *index = new int[l];
for(i=0;i<l;i++)
{
index[i] = i;
alpha_block_sums[i] = 0;
}