Merge pull request #13 from karthikmurakonda/testing-kmeans
Testing kmeans
This commit is contained in:
commit
f7339217d6
|
@ -3,3 +3,5 @@
|
|||
int DTWfreqDistance(struct data_frame *df);
|
||||
|
||||
int DTWvolDistance(struct data_frame *df);
|
||||
|
||||
int DTWfreqvolDistance(struct data_frame *df);
|
|
@ -1,3 +1,4 @@
|
|||
int *getRandoms(int lower, int upper, int count);
|
||||
long double distance(struct Point *A, struct Point *B);
|
||||
bool Kmeans2(struct data_frame *df);
|
||||
long double distance2(struct centroid *A, struct Point *B);
|
|
@ -76,6 +76,7 @@ gboolean attack_detect_freq(struct data_frame *df)
|
|||
printf("avg freq: %Lf\n", temp->AVERAGE_OF_FREQUENCY);
|
||||
return TRUE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
previous = temp;
|
||||
temp = temp->next;
|
||||
|
@ -174,6 +175,7 @@ gboolean attack_detect_vol(struct data_frame *df)
|
|||
printf("avg vol: %Lf\n", temp->AVERAGE_OF_VOLTAGE);
|
||||
return TRUE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
previous = temp;
|
||||
temp = temp->next;
|
||||
|
|
|
@ -137,6 +137,7 @@ int DTWfreqDistance(struct data_frame *df)
|
|||
free(DTW);
|
||||
temp->count_track1 = 1;
|
||||
}
|
||||
return temp->result;
|
||||
break;
|
||||
}
|
||||
previous = temp;
|
||||
|
@ -154,7 +155,6 @@ int DTWfreqDistance(struct data_frame *df)
|
|||
previous->next = bring;
|
||||
return 1;
|
||||
}
|
||||
return temp->result;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -293,6 +293,7 @@ int DTWvolDistance(struct data_frame *df)
|
|||
free(DTW);
|
||||
temp->count_track1 = 1;
|
||||
}
|
||||
return temp->result;
|
||||
break;
|
||||
}
|
||||
previous = temp;
|
||||
|
@ -310,7 +311,6 @@ int DTWvolDistance(struct data_frame *df)
|
|||
previous->next = bring;
|
||||
return 1;
|
||||
}
|
||||
return temp->result;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,12 +13,23 @@ struct Point
|
|||
long double minDist; // default infinite dist to nearest cluster
|
||||
};
|
||||
|
||||
struct centroid
|
||||
{
|
||||
long double x, y;
|
||||
long double max_radius;
|
||||
unsigned long long int count;
|
||||
};
|
||||
|
||||
struct Kmeans2
|
||||
{
|
||||
int idcode;
|
||||
int count;
|
||||
struct Point *P;
|
||||
struct Kmeans2 *next;
|
||||
struct centroid *C;
|
||||
int no_of_clusters;
|
||||
int isIntialized;
|
||||
bool result;
|
||||
};
|
||||
|
||||
struct Kmeans2 *head_of_kmeans2 = NULL;
|
||||
|
@ -28,6 +39,11 @@ long double distance(struct Point *A, struct Point *B)
|
|||
return (((A->x - B->x) * (A->x - B->x)) + ((A->y - B->y) * (A->y - B->y)));
|
||||
}
|
||||
|
||||
long double distance2(struct centroid *A, struct Point *B)
|
||||
{
|
||||
return (((A->x - B->x) * (A->x - B->x)) + ((A->y - B->y) * (A->y - B->y)));
|
||||
}
|
||||
|
||||
int *getRandoms(int lower, int upper, int count)
|
||||
{
|
||||
srand(time(0));
|
||||
|
@ -70,8 +86,10 @@ bool Kmeans2(struct data_frame *df)
|
|||
head_of_kmeans2 = (struct Kmeans2 *)malloc(sizeof(struct Kmeans2));
|
||||
head_of_kmeans2->idcode = to_intconvertor(df->idcode);
|
||||
head_of_kmeans2->count = 0;
|
||||
head_of_kmeans2->isIntialized = 0;
|
||||
head_of_kmeans2->next = NULL;
|
||||
head_of_kmeans2->P = NULL;
|
||||
head_of_kmeans2->result = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
|
@ -85,9 +103,132 @@ bool Kmeans2(struct data_frame *df)
|
|||
printf("count: %d\n", temp->count);
|
||||
if (temp->count == 0)
|
||||
{
|
||||
if (temp->isIntialized == 0)
|
||||
temp->P = (struct Point *)malloc(sizeof(struct Point) * 500);
|
||||
else
|
||||
temp->P = (struct Point *)malloc(sizeof(struct Point) * 100);
|
||||
}
|
||||
if (temp->count != 500)
|
||||
if (temp->isIntialized == 1 && temp->count != 100)
|
||||
{
|
||||
float CURR_FREQ;
|
||||
if (df->dpmu[0]->fmt->freq == '0')
|
||||
{
|
||||
CURR_FREQ = 50 + to_intconvertor(df->dpmu[0]->freq) * 1e-3;
|
||||
}
|
||||
else
|
||||
{
|
||||
CURR_FREQ = decode_ieee_single(df->dpmu[0]->freq);
|
||||
}
|
||||
float CURR_vol;
|
||||
if (df->dpmu[0]->fmt->phasor == '0')
|
||||
{
|
||||
if (df->dpmu[0]->fmt->polar == '0')
|
||||
{
|
||||
unsigned char s1[2];
|
||||
unsigned char s2[2];
|
||||
strncpy(s1, df->dpmu[0]->phasors[0], 2);
|
||||
strncpy(s2, df->dpmu[0]->phasors[0] + 2, 2);
|
||||
long double v1 = to_intconvertor(s1);
|
||||
long double v2 = to_intconvertor(s2);
|
||||
CURR_vol = sqrt((v1 * v1) + (v2 * v2));
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned char s1[2];
|
||||
strncpy(s1, df->dpmu[0]->phasors[0], 2);
|
||||
CURR_vol = to_intconvertor(s1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (df->dpmu[0]->fmt->polar == '0')
|
||||
{
|
||||
unsigned char s1[4];
|
||||
unsigned char s2[4];
|
||||
strncpy(s1, df->dpmu[0]->phasors[0], 4);
|
||||
strncpy(s2, df->dpmu[0]->phasors[0] + 2, 4);
|
||||
long double v1 = decode_ieee_single(s1);
|
||||
long double v2 = decode_ieee_single(s2);
|
||||
CURR_vol = sqrt((v1 * v1) + (v2 * v2));
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned char s1[4];
|
||||
strncpy(s1, df->dpmu[0]->phasors[0], 4);
|
||||
CURR_vol = decode_ieee_single(s1);
|
||||
}
|
||||
}
|
||||
temp->P[temp->count].x = CURR_FREQ;
|
||||
temp->P[temp->count].y = CURR_vol;
|
||||
temp->P[temp->count].cluster = -1;
|
||||
temp->P[temp->count].minDist = __DBL_MAX__;
|
||||
temp->count++;
|
||||
|
||||
bool result = false;
|
||||
for (int i = 0; i < temp->no_of_clusters; i++)
|
||||
{
|
||||
long double dist = distance2(&temp->C[i], &temp->P[temp->count - 1]);
|
||||
if (dist <= temp->C[i].max_radius && dist < temp->P[temp->count - 1].minDist)
|
||||
{
|
||||
temp->P[temp->count - 1].cluster = i;
|
||||
temp->P[temp->count - 1].minDist = dist;
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
if (result == false)
|
||||
temp->P[temp->count - 1].cluster = -1;
|
||||
temp->result = result;
|
||||
}
|
||||
else if (temp->isIntialized == 1)
|
||||
{
|
||||
int *nPoints = (int *)malloc(sizeof(int) * temp->no_of_clusters);
|
||||
long double *Sumx = (long double *)malloc(sizeof(long double) * temp->no_of_clusters);
|
||||
long double *Sumy = (long double *)malloc(sizeof(long double) * temp->no_of_clusters);
|
||||
|
||||
for (int i = 0; i < temp->no_of_clusters; i++)
|
||||
{
|
||||
nPoints[i] = 0;
|
||||
Sumx[i] = 0;
|
||||
Sumy[i] = 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
if (temp->P[i].cluster != -1)
|
||||
{
|
||||
nPoints[temp->P[i].cluster]++;
|
||||
Sumx[temp->P[i].cluster] += temp->P[i].x;
|
||||
Sumy[temp->P[i].cluster] += temp->P[i].y;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < temp->no_of_clusters; i++)
|
||||
{
|
||||
temp->C[i].x = ((temp->C[i].count * temp->C[i].x) + Sumx[i]) / (temp->C[i].count + nPoints[i]);
|
||||
temp->C[i].y = ((temp->C[i].count * temp->C[i].y) + Sumx[i]) / (temp->C[i].count + nPoints[i]);
|
||||
temp->C[i].count += nPoints[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < temp->no_of_clusters; i++)
|
||||
{
|
||||
for (int j = 0; j < 100; j++)
|
||||
{
|
||||
if (temp->P[j].cluster == i)
|
||||
{
|
||||
long double dist = distance2(&temp->C[i], &temp->P[j]);
|
||||
if (temp->C[i].max_radius < dist)
|
||||
temp->C[i].max_radius = dist;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
temp->count = 0;
|
||||
free(temp->P);
|
||||
free(Sumx);
|
||||
free(Sumy);
|
||||
free(nPoints);
|
||||
}
|
||||
if (temp->count != 500 && temp->isIntialized == 0)
|
||||
{
|
||||
float CURR_FREQ;
|
||||
if (df->dpmu[0]->fmt->freq == '0')
|
||||
|
@ -143,7 +284,7 @@ bool Kmeans2(struct data_frame *df)
|
|||
temp->P[temp->count].minDist = __DBL_MAX__;
|
||||
temp->count++;
|
||||
}
|
||||
else
|
||||
else if (temp->isIntialized == 0)
|
||||
{
|
||||
int no_of_clusters = 5;
|
||||
int epochs = 20;
|
||||
|
@ -199,33 +340,68 @@ bool Kmeans2(struct data_frame *df)
|
|||
Centroids[i].y = Sumy[i] / nPoints[i];
|
||||
}
|
||||
|
||||
if (epochs == 0)
|
||||
{
|
||||
int count_centroids = 0;
|
||||
for (int i = 0; i < no_of_clusters; i++)
|
||||
{
|
||||
if (!isnanl(Centroids[i].x))
|
||||
count_centroids++;
|
||||
}
|
||||
|
||||
temp->no_of_clusters = count_centroids;
|
||||
temp->C = (struct centroid *)malloc(sizeof(struct centroid) * count_centroids);
|
||||
int track = 0;
|
||||
for (int i = 0; i < no_of_clusters; i++)
|
||||
{
|
||||
if (!isnanl(Centroids[i].x))
|
||||
{
|
||||
temp->C[track].x = Centroids[i].x;
|
||||
temp->C[track].y = Centroids[i].y;
|
||||
temp->C[track].max_radius = 0;
|
||||
temp->C[track].count = nPoints[i];
|
||||
for (int j = 0; j < 500; j++)
|
||||
{
|
||||
if (temp->P[j].cluster == i)
|
||||
{
|
||||
long double dist = distance2(&temp->C[track], &temp->P[j]);
|
||||
if (temp->C[track].max_radius < dist)
|
||||
temp->C[track].max_radius = dist;
|
||||
}
|
||||
}
|
||||
track++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(nPoints);
|
||||
free(Sumx);
|
||||
free(Sumy);
|
||||
}
|
||||
temp->count = 0;
|
||||
FILE *fp;
|
||||
fp = fopen("kmeans.txt", "a");
|
||||
// FILE *fp;
|
||||
// fp = fopen("kmeans.txt", "a");
|
||||
|
||||
for (int i = 0; i < 500; i++)
|
||||
{
|
||||
fprintf(fp, "%Lf, %Lf, %d\n", temp->P[i].x, temp->P[i].y, temp->P[i].cluster);
|
||||
}
|
||||
fprintf(fp, "\n\n");
|
||||
// for (int i = 0; i < 500; i++)
|
||||
// {
|
||||
// fprintf(fp, "%Lf, %Lf, %d\n", temp->P[i].x, temp->P[i].y, temp->P[i].cluster);
|
||||
// }
|
||||
// fprintf(fp, "\n\n");
|
||||
|
||||
for (int i = 0; i < no_of_clusters; i++)
|
||||
{
|
||||
fprintf(fp, "%d : %Lf, %Lf\n", i, Centroids[i].x, Centroids[i].y);
|
||||
}
|
||||
// for (int i = 0; i < no_of_clusters; i++)
|
||||
// {
|
||||
// fprintf(fp, "%d : %Lf, %Lf\n", i, Centroids[i].x, Centroids[i].y);
|
||||
// }
|
||||
|
||||
fprintf(fp, "\n\n");
|
||||
// fprintf(fp, "\n\n");
|
||||
|
||||
fclose(fp);
|
||||
// fclose(fp);
|
||||
|
||||
free(Centroids);
|
||||
free(temp->P);
|
||||
temp->isIntialized = 1;
|
||||
}
|
||||
return true;
|
||||
return temp->result;
|
||||
break;
|
||||
}
|
||||
previous = temp;
|
||||
|
@ -237,7 +413,9 @@ bool Kmeans2(struct data_frame *df)
|
|||
bring->idcode = to_intconvertor(df->idcode);
|
||||
bring->count = 0;
|
||||
bring->next = NULL;
|
||||
bring->isIntialized = 0;
|
||||
bring->P = NULL;
|
||||
bring->result = true;
|
||||
previous->next = bring;
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -129,13 +129,13 @@ gboolean update_images(gpointer* pars){
|
|||
}
|
||||
}else if(curr_measurement == 3){
|
||||
if(algorithm==0 && dimmension == 0){
|
||||
if (!attack_detect_vol(df)){
|
||||
if (!attack_detect_freq(df)){
|
||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
||||
}else{
|
||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
||||
}
|
||||
}else if (algorithm==0 && dimmension == 1){
|
||||
if (!attack_detect_freq(df)){
|
||||
if (!attack_detect_vol(df)){
|
||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
||||
}else{
|
||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
||||
|
@ -161,19 +161,23 @@ gboolean update_images(gpointer* pars){
|
|||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
||||
}
|
||||
}else if (algorithm==2 && dimmension == 0){
|
||||
if(!DTWvolDistance(df)){
|
||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
||||
}else{
|
||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
||||
}
|
||||
}else if (algorithm==2 && dimmension == 1){
|
||||
if(!DTWfreqDistance(df)){
|
||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
||||
}else{
|
||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
||||
}
|
||||
}else if (algorithm==2 && dimmension == 1){
|
||||
if(!DTWvolDistance(df)){
|
||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
||||
}else{
|
||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
||||
}
|
||||
}else if (algorithm==2 && dimmension == 2){
|
||||
|
||||
if(!DTWfreqvolDistance(df)){
|
||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
||||
}else{
|
||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue