-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscaleap.cpp
More file actions
154 lines (118 loc) · 3.18 KB
/
scaleap.cpp
File metadata and controls
154 lines (118 loc) · 3.18 KB
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
#include "scaleap.hpp"
using namespace std;
ScaleAP::ScaleAP(){}
ScaleAP::ScaleAP(Data *d, float l, unsigned int t, unsigned int s){
data = d;
lambda = l;
max_itr = t;
num_itr =0;
stop = s;
unsigned int size = data->get_size();
unsigned int N = data->get_N();
res1.resize(size, 0);
res2.resize(size, 0);
ava1.resize(size, 0);
ava2.resize(size, 0);
res_plus_ava.resize(size, 0);
examplar.resize(N, INT_MAX);
max_index.resize(N, 0);
max_res.resize(N, -DBL_MAX);
res_prev = &res1;
res = &res2;
ava_prev = &ava1;
ava = &ava2;
for(unsigned int i=0; i<N; ++i){
double tmp_max_sim = -DBL_MAX;
double tmp_max_index = 0;
for(unsigned int j=0; j<N; ++j){
(*res_prev)[N*i+j] = data->sim(i, j) - data->max_sim(i, j);
if((*res_prev)[N*i+j] > tmp_max_sim){
tmp_max_sim = (*res_prev)[N*i+j];
tmp_max_index = j;
}
}
max_index[i] = tmp_max_index;
}
}
void
ScaleAP::run(){
unsigned int stable_count = 0;
unsigned int prev_updates = 0;
unsigned int updates = 0;
for(num_itr=0; num_itr<max_itr; ++num_itr){
updates = one_aggregated_iteration();
updates == prev_updates || updates == 0 ? ++stable_count : stable_count = 0;
prev_updates = updates;
if(stable_count >= stop){
break;
}
}
}
unsigned int
ScaleAP::one_aggregated_iteration(){
unsigned int N = data->get_N();
unsigned int min_k = 0, max_k = 0;
double min_r = 0, max_r = 0;
// compute aggregated responsibilities
for(unsigned int i=0; i<N; ++i){
// compute r_t(i, i)
set_r(i,i, get_r(i,i));
// compute r_t(i, min_k)
min_k = data->min_index[i];
min_r = update_r(i, min_k);
// compute r_t(i, max_k)
max_k = max_index[i];
max_r = -DBL_MAX;
if(max_k == i){
max_r = get_r(i, i);
}else if(max_k != min_k){
max_r = update_r(i, max_k);
}
// exemplar computation
for(unsigned int j=0; j<N; ++j){
if(j != max_k && j != min_k && j != i){
if(max_k != min_k){
set_r(i, j, min_r + (1-lambda)*(data->delta_sim_vector[N*i+j]) + lambda*(get_r(i,j) - get_r(i,min_k)));
}else{
update_r(i,j);
}
}
res_plus_ava[N*i+j] = (*res)[N*i+j];
}
}
// compute availabilities
for(unsigned int i=0; i<N; ++i){
double tmp_max = -DBL_MAX;
unsigned int tmp_max_index = 0;
for(unsigned int j=0; j<N; ++j){
double tmp_ava = 0;
if(max_res[j]<0 && i!=j){
tmp_ava = (1-pow(lambda, (num_itr+1)))*get_r(j,j);
set_a(i, j, tmp_ava);
}else{
tmp_ava = update_a(i, j);
}
res_plus_ava[N*i+j] += tmp_ava;
// obtain max similarity for responsibility
if(data->sim(i,j)+tmp_ava > tmp_max){
tmp_max = data->sim(i,j)+tmp_ava;
tmp_max_index = j;
}
}
max_index[i] = tmp_max_index;
}
// find examplars
vector<double>::iterator it = res_plus_ava.begin();
unsigned int updates = 0;
for(unsigned int i=0; i<N; ++i){
unsigned int object = max_element(it+N*i, it+N*(i+1)) - (it+N*i);
if(examplar[i] != object){
examplar[i] = object;
++updates;
}
max_res[i] = -DBL_MAX;
}
flip(&res_prev, &res);
flip(&ava_prev, &ava);
return updates;
}