summaryrefslogtreecommitdiffstats
path: root/SD-VBS/benchmarks/svm/src/c/getAlphaFromTrainSet.c
blob: 681f2c687dfe5e02e05286a90038bf4d338b67ff (plain) (blame)
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
/********************************
Author: Sravanthi Kota Venkata
********************************/

#include "svm.h"

alphaRet* getAlphaFromTrainSet(int N, F2D* trn1, F2D* trn2, int iterations)
{
    float tolerance, C, eps, *b;
    F2D *a_result, *b_result;
    int NumChanged, r, ExamineAll, cnt, d, dim, ret, iter, i;
    F2D *X, *Y;
    F2D *a, *e;

    b = malloc(sizeof(float));
    alphaRet* alpha;
    alpha = (alphaRet*)malloc(sizeof(alphaRet));
    tolerance = 0.001;
    C = 0.05;
    d = -1;
    dim = 256;
    eps = 0.001;
    a_result = fSetArray(iterations, N, 0);
    b_result = fSetArray(iterations, 1, 0);
    ret = 0;
    
    X = usps_read_partial( trn1, trn2, 0, 1, (N/iterations), iterations);
    
    for(iter=0; iter<iterations; iter++)
    {
        Y = usps_read_partial( trn1, trn2, iter, 0, N/iterations, iterations);
        
        a = fSetArray(N, 1, 0);
        arrayref(b,0) = 0;                  /** check if ptr **/
        e = fSetArray(N, 1, 0);
        ExamineAll = 1;
        cnt = 0;
        NumChanged = 0;

        while(NumChanged>0 || ExamineAll == 1)
        {
            cnt = cnt + 1;
            NumChanged = 0;
            if(ExamineAll == 1)
            {
                for(i=0; i<N; i++)
                {
                    ret = examineExample(i, a, b, C, e, X, Y, tolerance, N, eps, dim);
                    NumChanged = NumChanged + ret;
                }
            }
            else
            {
                for(i=0; i<N; i++)
                {
                    if( asubsref(a,i) > 0 && asubsref(a,i) <C )
                    {
                        ret = examineExample(i, a, b, C, e, X, Y, tolerance, N, eps, dim);
                        NumChanged = NumChanged + ret;
                    }
                }
            }
            if(ExamineAll == 1)
                ExamineAll = 0;
            else if(NumChanged == 0)
                ExamineAll = 1;
        }

        for(r=0; r<N; r++)
            subsref(a_result,iter,r) = asubsref(a,r);   /** a_result has size iteration,N .. Check **/
        asubsref(b_result,iter) = arrayref(b,0);

        fFreeHandle(Y);
        fFreeHandle(e);
        fFreeHandle(a);
    }
  
    alpha->C = C;
    alpha->d = d;
    alpha->dim = dim;
    alpha->eps = eps;
    alpha->a_result = a_result;
    alpha->b_result = b_result;
    alpha->a = a;
    alpha->b = arrayref(b,0);
    alpha->X = X;
    alpha->tolerance = tolerance;
    alpha->ret;
    
    free(b);
   
    return alpha; 

}