-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbalance.c
283 lines (240 loc) · 7.87 KB
/
balance.c
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
/************************************************************************
* *
* Program package 'lvq_pak': *
* *
* balance.c *
* -balances the number of entries in codebook by shortest distances *
* *
* Version 3.0 *
* Date: 1 Mar 1995 *
* *
* NOTE: This program package is copyrighted in the sense that it *
* may be used for scientific purposes. The package as a whole, or *
* parts thereof, cannot be included or used in any commercial *
* application without written permission granted by its producents. *
* No programs contained in this package may be copied for commercial *
* distribution. *
* *
* All comments concerning this program package may be sent to the *
* e-mail address 'lvq@cochlea.hut.fi'. *
* *
************************************************************************/
#include <stdio.h>
#include <float.h>
#include "lvq_pak.h"
#include "lvq_rout.h"
#include "datafile.h"
#include "labels.h"
#define BAL 1.3
static char *usage[] = {
"balance - balances the number of entries in codebook by shortest distances\n",
"Required parameters:\n",
" -cin filename input codebook file\n",
" -din filename input data\n",
" -cout filename output codebook filename\n",
"Optional parameters:\n",
" -knn N use N nearest neighbours\n",
" -rand integer seed for random number generator. 0 is current time\n",
" -selfuncs name select a set of functions\n",
NULL};
struct entries *balance_codes(struct teach_params *teach, char *outfile)
{
long nod, nol, i;
int label;
int *noe, *diff, *class;
int note, knn = teach->knn;
float aver;
float *dists;
struct data_entry *entr, *tlab, tmp, *prev;
struct entries *red_codes;
struct entries *codes = teach->codes, *data = teach->data;
struct mindists *md = NULL;
DIST_FUNCTION *distance = teach->dist;
struct hitlist *more;
WINNER_FUNCTION *find_knn = teach->winner;
eptr p;
nol = number_of_labels();
/* Compute the medians of the shortest distances from each entry to
its nearest entry of the same class */
ifverbose(2)
fprintf(stderr, "Medians of the shortest distances are computed\n");
md = med_distances(codes, distance);
/* If serious imbalance exists, add entries to classes where
distances are large, and remove them from classes where distances
are small */
nol = md->num_classes;
noe = md->noe;
dists = md->dists;
class = md->class;
diff = (int *) oalloc(sizeof(int) * nol);
for (i = 0; i < nol; i++) {
diff[i] = 0;
}
aver = 0.0;
note = 0;
for (i = 0; i < nol; i++) {
if (noe[i] > 1) {
aver += dists[i];
note++;
}
}
aver /= note;
note = 0;
ifverbose(2)
fprintf(stderr, "Medians of different classes are compared\n");
for (i = 0; i < nol; i++) {
if ((aver > BAL * dists[i]) && (noe[i] > 1)) {
diff[i]--;
note++;
}
if (BAL * aver < dists[i]) {
diff[i]++;
note--;
}
}
/* Force-pick one codebook vector for each missing class */
for (i = 0; i < nol; i++) {
if (noe[i] == 0) {
entr = force_pick_code(data, i);
tlab = codes->entries;
while (tlab->next != NULL)
tlab = tlab ->next;
tlab->next = entr;
codes->num_entries++;
noe[i] = 1;
note--;
}
}
/* If there was net increase or decrease in number of entries */
for (i = 0; i < nol; i++) {
if ((aver > BAL * dists[i]) && ((noe[i]+diff[i]) > 1)) {
if (note < 0) {
diff[i]--;
note++;
}
}
if (BAL * aver < dists[i]) {
if (note > 0) {
diff[i]++;
note--;
}
}
}
ifverbose(1)
fprintf(stderr, "Some codebook vectors are removed\n");
/* Now remove entries from those classes where diff shows negative */
entr = codes->entries;
tmp.next = entr;
prev = &tmp;
while (entr != NULL) {
label = get_entry_label(entr);
for (i = 0; i < nol; i++)
if (class[i] == label)
break;
if (diff[i] < 0) {
diff[i]++;
tlab = entr;
entr = entr->next;
prev->next = entr;
tlab->next = NULL;
free_entry(tlab);
codes->num_entries--;
}
else
{
prev = entr;
entr = entr->next;
}
}
codes->entries = tmp.next;
ifverbose(1)
fprintf(stderr, "Some new codebook vectors are picked\n");
/* Pick the requested number of additional codes for each class */
more = new_hitlist();
for (i = 0; i < nol; i++)
{
while (diff[i] > 0)
{
add_hit(more, class[i]);
diff[i]--;
}
}
entr = pick_inside_codes(more, data, knn, find_knn);
free_hitlist(more);
/* Add new entries to the list */
tlab = codes->entries;
while (tlab->next != NULL)
tlab = tlab->next;
tlab->next = entr;
/* laske montako uutta */
ifverbose(1)
fprintf(stderr, "Codebook vectors are redistributed\n");
rewind_entries(data, &p);
nod = data->num_entries; /* number of data vectors */
teach->length = nod;
teach->alpha = 0.3;
red_codes = olvq1_training(teach, NULL, outfile);
if (red_codes == NULL)
return NULL;
/* Display the medians of the shortest distances */
ifverbose(2)
fprintf(stderr, "Medians of the shortest distances are computed\n");
free_mindists(md); /* free old information */
md = med_distances(red_codes, distance);
nol = md->num_classes;
noe = md->noe;
dists = md->dists;
class = md->class;
for (i = 0; i < nol; i++) {
if (verbose(1) > 0)
fprintf(stdout, "In class %9s %3d units, min dist.: %.3f\n",
find_conv_to_lab(class[i]), noe[i], dists[i]);
}
if (md)
free_mindists(md);
return(red_codes);
}
int main(int argc, char **argv)
{
char *in_data_file;
char *in_code_file;
char *out_code_file;
struct entries *data, *codes;
struct teach_params params;
int randomize;
long buffer = 0;
char *funcname = NULL;
global_options(argc, argv);
if (extract_parameter(argc, argv, "-help", OPTION2))
{
printhelp();
exit(0);
}
in_data_file = extract_parameter(argc, argv, IN_DATA_FILE, ALWAYS);
in_code_file = extract_parameter(argc, argv, IN_CODE_FILE, ALWAYS);
out_code_file = extract_parameter(argc, argv, OUT_CODE_FILE, ALWAYS);
params.knn = (int) oatoi(extract_parameter(argc, argv, KNN_NEIGHBORS, OPTION), 5);
randomize = (int) oatoi(extract_parameter(argc, argv, RANDOM, OPTION), 0);
buffer = oatoi(extract_parameter(argc, argv, "-buffer", OPTION), 0);
funcname = extract_parameter(argc, argv, "-selfuncs", OPTION);
ifverbose(2)
fprintf(stderr, "Input entries are read from file %s\n", in_data_file);
data = open_entries(in_data_file);
ifverbose(2)
fprintf(stderr, "Codebook entries are read from file %s\n", in_code_file);
codes = open_entries(in_code_file);
if (data->dimension != codes->dimension) {
fprintf(stderr, "Data and codes have different dimensions\n");
close_entries(codes);
close_entries(data);
exit(1);
}
init_random(randomize);
set_teach_params(¶ms, codes, data, buffer, funcname);
params.winner = find_winner_knn;
codes = balance_codes(¶ms, out_code_file);
ifverbose(2)
fprintf(stderr, "Codebook entries are saved to file %s\n", out_code_file);
save_entries(codes, out_code_file);
return(0);
}