kmeans implmented for all PMUs
This commit is contained in:
parent
ef9fd10e2f
commit
5463e52ff6
|
@ -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);
|
36268
iPDC/kmeans.txt
36268
iPDC/kmeans.txt
File diff suppressed because it is too large
Load Diff
|
@ -13,10 +13,17 @@ struct Point
|
|||
long double minDist; // default infinite dist to nearest cluster
|
||||
};
|
||||
|
||||
int count = 0;
|
||||
struct Point *P;
|
||||
struct Kmeans2
|
||||
{
|
||||
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)));
|
||||
}
|
||||
|
@ -56,149 +63,183 @@ 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);
|
||||
}
|
||||
if (count != 500)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
P[count].x = CURR_FREQ;
|
||||
P[count].y = CURR_vol;
|
||||
P[count].cluster = -1;
|
||||
P[count].minDist = __DBL_MAX__;
|
||||
count++;
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
int no_of_clusters = 5;
|
||||
int epochs = 20;
|
||||
int *c = getRandoms(0, 499, no_of_clusters);
|
||||
struct Point *Centroids = (struct Point *)malloc(sizeof(struct Point) * no_of_clusters);
|
||||
for (int i = 0; i < no_of_clusters; i++)
|
||||
struct Kmeans2 *temp = head_of_kmeans2;
|
||||
struct Kmeans2 *previous = NULL;
|
||||
while (temp != NULL)
|
||||
{
|
||||
Centroids[i].x = P[c[i]].x;
|
||||
Centroids[i].y = P[c[i]].y;
|
||||
Centroids[i].minDist = __DBL_MAX__;
|
||||
Centroids[i].cluster = -1;
|
||||
}
|
||||
|
||||
free(c);
|
||||
|
||||
while (epochs--)
|
||||
{
|
||||
for (int i = 0; i < no_of_clusters; i++)
|
||||
if (temp->idcode == to_intconvertor(df->idcode))
|
||||
{
|
||||
for (int j = 0; j < 500; j++)
|
||||
printf("count: %d\n",temp->count);
|
||||
if (temp->count == 0)
|
||||
{
|
||||
long double dist = distance(&Centroids[i], &P[j]);
|
||||
if (P[j].minDist > dist)
|
||||
{
|
||||
P[j].minDist = dist;
|
||||
P[j].cluster = i;
|
||||
}
|
||||
temp->P = (struct Point *)malloc(sizeof(struct Point) * 500);
|
||||
}
|
||||
if (temp->count != 500)
|
||||
{
|
||||
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++;
|
||||
}
|
||||
else
|
||||
{
|
||||
int no_of_clusters = 5;
|
||||
int epochs = 20;
|
||||
int *c = getRandoms(0, 499, no_of_clusters);
|
||||
struct Point *Centroids = (struct Point *)malloc(sizeof(struct Point) * no_of_clusters);
|
||||
for (int i = 0; i < no_of_clusters; i++)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
free(c);
|
||||
|
||||
while (epochs--)
|
||||
{
|
||||
for (int i = 0; i < no_of_clusters; i++)
|
||||
{
|
||||
for (int j = 0; j < 500; j++)
|
||||
{
|
||||
long double dist = distance(&Centroids[i], &temp->P[j]);
|
||||
if (temp->P[j].minDist > dist)
|
||||
{
|
||||
temp->P[j].minDist = dist;
|
||||
temp->P[j].cluster = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int *nPoints = (int *)malloc(sizeof(int) * no_of_clusters);
|
||||
long double *Sumx = (long double *)malloc(sizeof(long double) * no_of_clusters);
|
||||
long double *Sumy = (long double *)malloc(sizeof(long double) * no_of_clusters);
|
||||
|
||||
for (int i = 0; i < no_of_clusters; i++)
|
||||
{
|
||||
nPoints[i] = 0;
|
||||
Sumx[i] = 0;
|
||||
Sumy[i] = 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 500; i++)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
Centroids[i].x = Sumx[i] / nPoints[i];
|
||||
Centroids[i].y = Sumy[i] / nPoints[i];
|
||||
}
|
||||
|
||||
free(nPoints);
|
||||
free(Sumx);
|
||||
free(Sumy);
|
||||
}
|
||||
temp->count = 0;
|
||||
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 < no_of_clusters; i++)
|
||||
{
|
||||
fprintf(fp, "%d : %Lf, %Lf\n", i, Centroids[i].x, Centroids[i].y);
|
||||
}
|
||||
|
||||
fprintf(fp, "\n\n");
|
||||
|
||||
fclose(fp);
|
||||
|
||||
free(Centroids);
|
||||
free(temp->P);
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
|
||||
int *nPoints = (int *)malloc(sizeof(int) * no_of_clusters);
|
||||
long double *Sumx = (long double *)malloc(sizeof(long double) * no_of_clusters);
|
||||
long double *Sumy = (long double *)malloc(sizeof(long double) * no_of_clusters);
|
||||
|
||||
for (int i = 0; i < no_of_clusters; i++)
|
||||
{
|
||||
nPoints[i] = 0;
|
||||
Sumx[i] = 0;
|
||||
Sumy[i] = 0;
|
||||
}
|
||||
|
||||
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__;
|
||||
}
|
||||
|
||||
for (int i = 0; i < no_of_clusters; i++)
|
||||
{
|
||||
Centroids[i].x = Sumx[i] / nPoints[i];
|
||||
Centroids[i].y = Sumy[i] / nPoints[i];
|
||||
}
|
||||
|
||||
free(nPoints);
|
||||
free(Sumx);
|
||||
free(Sumy);
|
||||
previous = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
count = 0;
|
||||
FILE *fp;
|
||||
fp = fopen("kmeans.txt","a");
|
||||
|
||||
for(int i=0;i<500;i++)
|
||||
if (temp == NULL)
|
||||
{
|
||||
fprintf(fp,"%Lf, %Lf, %d\n",P[i].x,P[i].y,P[i].cluster);
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
fprintf(fp,"\n\n");
|
||||
|
||||
fclose(fp);
|
||||
|
||||
free(Centroids);
|
||||
free(P);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue