commit last state
[ppam-mpi.git] / code / src / Classification / getClass.c
1 #include "Algorithm/get_dissimilarities.h"
2 #include "TimeSeries/deserialize.h"
3 #include <math.h>
4 #include "Algorithm/compute_coefficients.h"
5 #include <string.h>
6 #include "Util/utils.h"
7
8 uint32_t* get_class(PowerCurve* data, uint32_t nbSeries, PowerCurve* medoids,
9 uint32_t nbClusters, uint32_t nbValues, uint32_t p_for_dissims, double* DISTOR)
10 {
11 // nbReducedCoordinates = smallest power of 2 which is above nbValues
12 uint32_t nbReducedCoordinates = (uint32_t)ceil(log2(nbValues));
13
14 // Preprocessing to reduce dimension of both data and medoids
15 Real* reducedCoordinates_data = (Real*) malloc(nbSeries * nbReducedCoordinates * sizeof(Real));
16 compute_coefficients(data, nbSeries, nbValues,
17 reducedCoordinates_data, 0, nbReducedCoordinates);
18 Real* reducedCoordinates_medoids = (Real*) malloc(nbClusters * nbReducedCoordinates * sizeof(Real));
19 compute_coefficients(medoids, nbClusters, nbValues,
20 reducedCoordinates_medoids, 0, nbReducedCoordinates);
21
22 Real* dissimilarities = get_dissimilarities_inter(reducedCoordinates_data, nbSeries,
23 reducedCoordinates_medoids, nbClusters, nbReducedCoordinates, p_for_dissims);
24 free(reducedCoordinates_data);
25 free(reducedCoordinates_medoids);
26
27 // 3] Finally, assign each row to the least dissimilar center
28 uint32_t* result = (uint32_t*) malloc(nbSeries*sizeof(uint32_t));
29 for (uint32_t i=0; i<nbSeries; i++)
30 {
31 uint32_t minIndex = 0;
32 Real minDissim = dissimilarities[i*nbClusters + 0];
33 for (uint32_t j=1; j<nbClusters; j++)
34 {
35 if (dissimilarities[i*nbClusters + j] < minDissim)
36 {
37 minDissim = dissimilarities[i*nbClusters + j];
38 minIndex = j;
39 }
40 }
41 result[i] = minIndex + 1;
42 (*DISTOR) += minDissim;
43 }
44 free(dissimilarities);
45 return result;
46 }