Merge pull request #12 from karthikmurakonda/testing-kmeans

kmeans implmented for all PMUs
This commit is contained in:
pavanvpatil 2022-11-02 14:50:24 +05:30 committed by GitHub
commit c3f46cb6a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26969 additions and 9617 deletions

View File

@ -1,3 +1,3 @@
int *getRandoms(int lower, int upper, int count);
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,8 +13,15 @@ struct Point
long double minDist; // default infinite dist to nearest cluster
};
int count = 0;
struct Kmeans2
{
int idcode;
int count;
struct Point *P;
struct Kmeans2 *next;
};
struct Kmeans2 *head_of_kmeans2 = NULL;
long double distance(struct Point *A, struct Point *B)
{
@ -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(count==0)
if (head_of_kmeans2 == NULL)
{
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;
if (df->dpmu[0]->fmt->freq == '0')
@ -113,11 +137,11 @@ void Kmeans2(struct data_frame *df)
CURR_vol = decode_ieee_single(s1);
}
}
P[count].x = CURR_FREQ;
P[count].y = CURR_vol;
P[count].cluster = -1;
P[count].minDist = __DBL_MAX__;
count++;
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++;
}
else
{
@ -127,8 +151,8 @@ void Kmeans2(struct data_frame *df)
struct Point *Centroids = (struct Point *)malloc(sizeof(struct Point) * no_of_clusters);
for (int i = 0; i < no_of_clusters; i++)
{
Centroids[i].x = P[c[i]].x;
Centroids[i].y = P[c[i]].y;
Centroids[i].x = temp->P[c[i]].x;
Centroids[i].y = temp->P[c[i]].y;
Centroids[i].minDist = __DBL_MAX__;
Centroids[i].cluster = -1;
}
@ -141,11 +165,11 @@ void Kmeans2(struct data_frame *df)
{
for (int j = 0; j < 500; j++)
{
long double dist = distance(&Centroids[i], &P[j]);
if (P[j].minDist > dist)
long double dist = distance(&Centroids[i], &temp->P[j]);
if (temp->P[j].minDist > dist)
{
P[j].minDist = dist;
P[j].cluster = i;
temp->P[j].minDist = dist;
temp->P[j].cluster = i;
}
}
}
@ -163,10 +187,10 @@ void Kmeans2(struct data_frame *df)
for (int i = 0; i < 500; i++)
{
nPoints[P[i].cluster]++;
Sumx[P[i].cluster] += P[i].x;
Sumy[P[i].cluster] += P[i].y;
P[i].minDist = __DBL_MAX__;
nPoints[temp->P[i].cluster]++;
Sumx[temp->P[i].cluster] += temp->P[i].x;
Sumy[temp->P[i].cluster] += temp->P[i].y;
temp->P[i].minDist = __DBL_MAX__;
}
for (int i = 0; i < no_of_clusters; i++)
@ -179,13 +203,13 @@ void Kmeans2(struct data_frame *df)
free(Sumx);
free(Sumy);
}
count = 0;
temp->count = 0;
FILE *fp;
fp = fopen("kmeans.txt", "a");
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");
@ -199,6 +223,23 @@ void Kmeans2(struct data_frame *df)
fclose(fp);
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 <stdio.h>
#include <stdbool.h>
#include <gtk/gtk.h>
#include <pthread.h>
#include "global.h"
@ -154,7 +155,11 @@ gboolean update_images(gpointer* pars){
}else if (algorithm==1 && dimmension == 1){
}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){
if(!DTWvolDistance(df)){
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);