Compare commits

...

2 Commits

Author SHA1 Message Date
pavanvpatil c3f46cb6a8
Merge pull request #12 from karthikmurakonda/testing-kmeans
kmeans implmented for all PMUs
2022-11-02 14:50:24 +05:30
pavanvpatil 5463e52ff6 kmeans implmented for all PMUs 2022-11-02 14:49:42 +05:30
4 changed files with 26969 additions and 9617 deletions

View File

@ -1,3 +1,3 @@
int *getRandoms(int lower, int upper, int count); int *getRandoms(int lower, int upper, int count);
long double distance(struct Point* A, struct Point* B); long double distance(struct Point* A, struct Point* B);
void Kmeans2(struct data_frame *df); bool Kmeans2(struct data_frame *df);

File diff suppressed because it is too large Load Diff

View File

@ -13,10 +13,17 @@ struct Point
long double minDist; // default infinite dist to nearest cluster long double minDist; // default infinite dist to nearest cluster
}; };
int count = 0; struct Kmeans2
struct Point *P; {
int idcode;
int count;
struct Point *P;
struct Kmeans2 *next;
};
long double distance(struct Point* A, struct Point* B) struct Kmeans2 *head_of_kmeans2 = NULL;
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))); return (((A->x - B->x) * (A->x - B->x)) + ((A->y - B->y) * (A->y - B->y)));
} }
@ -56,14 +63,31 @@ int *getRandoms(int lower, int upper, int count)
} }
} }
void Kmeans2(struct data_frame *df) bool Kmeans2(struct data_frame *df)
{ {
printf("count: %d\n",count); if (head_of_kmeans2 == NULL)
if(count==0)
{ {
P = (struct Point *)malloc(sizeof(struct Point) * 500); 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->next = NULL;
head_of_kmeans2->P = NULL;
return true;
} }
if (count != 500) else
{
struct Kmeans2 *temp = head_of_kmeans2;
struct Kmeans2 *previous = NULL;
while (temp != NULL)
{
if (temp->idcode == to_intconvertor(df->idcode))
{
printf("count: %d\n",temp->count);
if (temp->count == 0)
{
temp->P = (struct Point *)malloc(sizeof(struct Point) * 500);
}
if (temp->count != 500)
{ {
float CURR_FREQ; float CURR_FREQ;
if (df->dpmu[0]->fmt->freq == '0') if (df->dpmu[0]->fmt->freq == '0')
@ -113,11 +137,11 @@ void Kmeans2(struct data_frame *df)
CURR_vol = decode_ieee_single(s1); CURR_vol = decode_ieee_single(s1);
} }
} }
P[count].x = CURR_FREQ; temp->P[temp->count].x = CURR_FREQ;
P[count].y = CURR_vol; temp->P[temp->count].y = CURR_vol;
P[count].cluster = -1; temp->P[temp->count].cluster = -1;
P[count].minDist = __DBL_MAX__; temp->P[temp->count].minDist = __DBL_MAX__;
count++; temp->count++;
} }
else else
{ {
@ -127,8 +151,8 @@ void Kmeans2(struct data_frame *df)
struct Point *Centroids = (struct Point *)malloc(sizeof(struct Point) * no_of_clusters); struct Point *Centroids = (struct Point *)malloc(sizeof(struct Point) * no_of_clusters);
for (int i = 0; i < no_of_clusters; i++) for (int i = 0; i < no_of_clusters; i++)
{ {
Centroids[i].x = P[c[i]].x; Centroids[i].x = temp->P[c[i]].x;
Centroids[i].y = P[c[i]].y; Centroids[i].y = temp->P[c[i]].y;
Centroids[i].minDist = __DBL_MAX__; Centroids[i].minDist = __DBL_MAX__;
Centroids[i].cluster = -1; Centroids[i].cluster = -1;
} }
@ -141,11 +165,11 @@ void Kmeans2(struct data_frame *df)
{ {
for (int j = 0; j < 500; j++) for (int j = 0; j < 500; j++)
{ {
long double dist = distance(&Centroids[i], &P[j]); long double dist = distance(&Centroids[i], &temp->P[j]);
if (P[j].minDist > dist) if (temp->P[j].minDist > dist)
{ {
P[j].minDist = dist; temp->P[j].minDist = dist;
P[j].cluster = i; temp->P[j].cluster = i;
} }
} }
} }
@ -163,10 +187,10 @@ void Kmeans2(struct data_frame *df)
for (int i = 0; i < 500; i++) for (int i = 0; i < 500; i++)
{ {
nPoints[P[i].cluster]++; nPoints[temp->P[i].cluster]++;
Sumx[P[i].cluster] += P[i].x; Sumx[temp->P[i].cluster] += temp->P[i].x;
Sumy[P[i].cluster] += P[i].y; Sumy[temp->P[i].cluster] += temp->P[i].y;
P[i].minDist = __DBL_MAX__; temp->P[i].minDist = __DBL_MAX__;
} }
for (int i = 0; i < no_of_clusters; i++) for (int i = 0; i < no_of_clusters; i++)
@ -179,26 +203,43 @@ void Kmeans2(struct data_frame *df)
free(Sumx); free(Sumx);
free(Sumy); free(Sumy);
} }
count = 0; temp->count = 0;
FILE *fp; FILE *fp;
fp = fopen("kmeans.txt","a"); fp = fopen("kmeans.txt", "a");
for(int i=0;i<500;i++) for (int i = 0; i < 500; i++)
{ {
fprintf(fp,"%Lf, %Lf, %d\n",P[i].x,P[i].y,P[i].cluster); fprintf(fp, "%Lf, %Lf, %d\n", temp->P[i].x, temp->P[i].y, temp->P[i].cluster);
} }
fprintf(fp,"\n\n"); fprintf(fp, "\n\n");
for(int i=0;i<no_of_clusters;i++) for (int i = 0; i < no_of_clusters; i++)
{ {
fprintf(fp,"%d : %Lf, %Lf\n",i,Centroids[i].x,Centroids[i].y); 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(Centroids);
free(P); free(temp->P);
}
return true;
break;
}
previous = temp;
temp = temp->next;
}
if (temp == NULL)
{
struct Kmeans2 *bring = (struct Kmeans2 *)malloc(sizeof(struct Kmeans2));
bring->idcode = to_intconvertor(df->idcode);
bring->count = 0;
bring->next = NULL;
bring->P = NULL;
previous->next = bring;
return true;
}
} }
} }

View File

@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <pthread.h> #include <pthread.h>
#include "global.h" #include "global.h"
@ -154,7 +155,11 @@ gboolean update_images(gpointer* pars){
}else if (algorithm==1 && dimmension == 1){ }else if (algorithm==1 && dimmension == 1){
}else if (algorithm==1 && dimmension == 2){ }else if (algorithm==1 && dimmension == 2){
Kmeans2(df); if(!Kmeans2(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 == 0){ }else if (algorithm==2 && dimmension == 0){
if(!DTWvolDistance(df)){ if(!DTWvolDistance(df)){
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image); vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);