📄 eca.cpp
字号:
// ECA.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "ECA.h"
#include "Globals.h"
#include "CCluster.h"
#include "CRecord.h"
#include "float.h"
#include "math.h"
#include "sort.h"
#include "CVerify.h"
#include "mmsystem.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
extern float cluster_decomposition_algorithm(long density,float ratio,CPtrArray &data,CPtrArray &array,CPtrArray &outline);
extern void competitive_cluster_algorithm(CPtrArray &array,long count);
extern void cluster_nerge_algorithm(CPtrArray &array,CPtrArray &outline);
extern void sort_cluster(CPtrArray &array);
extern BOOL is_merge_cluster(CCluster *c1,CCluster *c2,long threshold);
extern BOOL have_boundary_points(CCluster *c1,CCluster *c2,float r);
extern void normalize(CPtrArray &data);
extern float cal_davies_bouldin_validity_index(CPtrArray &array);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString dataset = "D:\\cluster.dat"; if(argc >= 2){dataset = argv[1];}
CString output = "D:\\output.dat"; if(argc >= 3){output = argv[2];}
long test = 10; if(argc >= 4){test = max(1,atoi(argv[3]));}
float ratio = 2.0; if(argc >= 5){ratio = max(1,atof(argv[4]));}
long density = 5; if(argc >= 6){density = max(1,atoi(argv[5]));}
CString nor = "N"; if(argc >= 7){nor = argv[6];}
CPtrArray data;
if(read_data(dataset,data) == FALSE){
return nRetCode;
}
if(nor == "Y" || nor == "y"){
normalize(data);
}
long start_time = timeGetTime();
CPtrArray array,outline;
float r = cluster_decomposition_algorithm(density,ratio,data,array,outline);
long t = 0;
while(t < test){
long count = (long)array.GetCount();
competitive_cluster_algorithm(array,(long)data.GetSize());
if(array.GetSize() == count) {
t++;
} else {
t = 0;
}
}
cluster_nerge_algorithm(array,outline);
long end_time = timeGetTime();
printf("Input file : %s\n",dataset);
printf("Output file : %s\n",output);
printf("Runtime : %.3lf\n",(end_time-start_time)/1000.0);
printf("Radius : %.3lf\n",r);
printf("Within-cluster distance : nearest neighor distance\n");
printf("Between-clusters distance : single linkage\n");
printf("Davies Bouldin validity index : %.3lf\n",cal_davies_bouldin_validity_index(array));
CPtrList verify;
for(long i=0;i<array.GetSize();i++){
CCluster *c = (CCluster *)array[i];
for(long j=0;j<c->get_record_array().GetSize();j++){
CRecord *r = (CRecord *)c->get_record_array().GetAt(j);
CString str;
str.Format("%s-%s",c->get_label(),r->get_label());
BOOL ok = FALSE;
POSITION pos = verify.GetHeadPosition();
while(pos){
CVerify *v = (CVerify *)verify.GetNext(pos);
if(v->get_label() == str){
v->add_count();
ok = TRUE;
break;
}
}
if(ok == FALSE){
CVerify *v = new CVerify(str,c->get_label(),r->get_label());
verify.AddTail(v);
}
}
}
CPtrList list;
float diff = 0;
while(!verify.IsEmpty()){
CVerify *v = (CVerify *)verify.RemoveHead();
POSITION pos = verify.GetHeadPosition();
while(pos){
POSITION prev = pos;
CVerify *vv = (CVerify *)verify.GetNext(pos);
if(strcmp(v->get_name1(),vv->get_name1()) != 0){
continue;
}
verify.RemoveAt(prev);
if(v->get_count() > vv->get_count()){
diff += vv->get_count();
delete vv;
} else {
diff += v->get_count();
delete v;
v = vv;
}
}
list.AddTail(v);
}
verify.AddTail(&list);
list.RemoveAll();
while(!verify.IsEmpty()){
CVerify *v = (CVerify *)verify.RemoveHead();
POSITION pos = verify.GetHeadPosition();
while(pos){
POSITION prev = pos;
CVerify *vv = (CVerify *)verify.GetNext(pos);
if(strcmp(v->get_name2(),vv->get_name2()) != 0){
continue;
}
verify.RemoveAt(prev);
if(v->get_count() > vv->get_count()){
diff += vv->get_count();
delete vv;
} else {
diff += v->get_count();
delete v;
v = vv;
}
}
list.AddTail(v);
}
verify.AddTail(&list);
list.RemoveAll();
// printf("Criterion function : %.3lf\n",mse);
printf("Accuracy : %.3lf\n",1.0-diff/data.GetSize());
while(!verify.IsEmpty()){
CVerify *v = (CVerify *)verify.RemoveHead();
delete v;
}
write_data(output,array);
for(long i=0;i<outline.GetSize();i++){
CCluster *c = (CCluster *)outline[i];
delete c;
}
for(long i=0;i<array.GetSize();i++){
CCluster *c = (CCluster *)array[i];
delete c;
}
for(long i=0;i<data.GetSize();i++){
CRecord *r = (CRecord *)data[i];
delete r;
}
}
return nRetCode;
}
float cluster_decomposition_algorithm(long density,float ratio,CPtrArray &data,CPtrArray &array,CPtrArray &outline)
{
/*
//衡
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -