34 #define DELTA_ERR_MAX 0.1
39 typedef struct cell_s {
64 for (i=0; i<
dim; i++) {
65 dist += (a[i] - b[i])*(a[i] - b[i]);
80 memcpy(res, vect, dim*
sizeof(
int));
87 for (; cells; cells=cells->
next)
95 int i, pick=0,
diff, diff_min = INT_MAX;
96 for (i=0; i<elbg->
numCB; i++)
99 if (
diff < diff_min) {
140 int numpoints[2] = {0,0};
141 int *newcentroid[2] = {
147 memset(newcentroid[0], 0, 2 * dim *
sizeof(*newcentroid[0]));
152 for (tempcell = cells; tempcell; tempcell=tempcell->
next) {
156 for (i=0; i<
dim; i++)
157 newcentroid[idx][i] += points[tempcell->
index*dim + i];
160 vect_division(centroid[0], newcentroid[0], numpoints[0], dim);
161 vect_division(centroid[1], newcentroid[1], numpoints[1], dim);
163 for (tempcell = cells; tempcell; tempcell=tempcell->
next) {
166 int idx = dist[0] > dist[1];
167 newutility[idx] += dist[idx];
170 return newutility[0] + newutility[1];
177 int *
min = newcentroid_i;
178 int *max = newcentroid_p;
181 for (i=0; i< elbg->
dim; i++) {
186 for (tempcell = elbg->
cells[huc]; tempcell; tempcell = tempcell->
next)
187 for(i=0; i<elbg->
dim; i++) {
192 for (i=0; i<elbg->
dim; i++) {
193 int ni = min[i] + (max[i] - min[i])/3;
194 int np = min[i] + (2*(max[i] - min[i]))/3;
195 newcentroid_i[i] = ni;
196 newcentroid_p[i] = np;
218 *pp = elbg->
cells[indexes[0]];
221 tempdata = elbg->
cells[indexes[1]];
227 newcentroid[0], elbg->
dim, INT_MAX) >
229 newcentroid[1], elbg->
dim, INT_MAX);
231 tempdata->
next = elbg->
cells[indexes[idx]];
232 elbg->
cells[indexes[idx]] = tempdata;
233 tempdata = tempcell2;
242 for (i=0; i < elbg->
numCB; i++) {
254 elbg->
utility[idx] = newutility;
255 for (tempcell=elbg->
cells[idx]; tempcell; tempcell=tempcell->
next)
268 int j, k, olderror=0, newerror, cont=0;
270 int *newcentroid[3] = {
278 olderror += elbg->
utility[idx[j]];
280 memset(newcentroid[2], 0, elbg->
dim*
sizeof(
int));
283 for (tempcell=elbg->
cells[idx[2*k]]; tempcell; tempcell=tempcell->
next) {
285 for (j=0; j<elbg->
dim; j++)
296 newerror = newutility[2];
299 elbg->
cells[idx[1]]);
301 if (olderror > newerror) {
304 elbg->
error += newerror - olderror;
322 for (idx[0]=0; idx[0] < elbg->
numCB; idx[0]++)
330 if (idx[1] != idx[0] && idx[1] != idx[2])
335 #define BIG_PRIME 433494437LL
338 int numCB,
int max_steps,
int *closest_cb,
343 if (numpoints > 24*numCB) {
349 for (i=0; i<numpoints/8; i++) {
351 memcpy(temp_points + i*dim, points + k*dim, dim*
sizeof(
int));
355 numCB, 2 * max_steps, closest_cb, rand_state);
361 numCB, 2 * max_steps, closest_cb, rand_state);
365 for (i=0; i < numCB; i++)
366 memcpy(codebook + i*dim, points + ((i*
BIG_PRIME)%numpoints)*dim,
372 int numCB,
int max_steps,
int *closest_cb,
378 int i, j, k, last_error, steps = 0,
ret = 0;
383 int best_dist, best_idx = 0;
385 elbg->
error = INT_MAX;
396 if (!dist_cb || !size_part || !list_buffer || !elbg->
cells ||
405 free_cells = list_buffer;
406 last_error = elbg->
error;
408 memset(elbg->
utility, 0, numCB*
sizeof(
int));
409 memset(elbg->
cells, 0, numCB*
sizeof(
cell *));
415 for (i=0; i < numpoints; i++) {
417 for (k=0; k < elbg->
numCB; k++) {
419 if (dist < best_dist) {
425 dist_cb[i] = best_dist;
426 elbg->
error += dist_cb[i];
428 free_cells->
index = i;
436 memset(size_part, 0, numCB*
sizeof(
int));
440 for (i=0; i < numpoints; i++) {
442 for (j=0; j < elbg->
dim; j++)
447 for (i=0; i < elbg->
numCB; i++)
452 (steps < max_steps));