📄 fsubset.c
字号:
#include <stdlib.h>
#include <stdio.h>
#define BOOL int
#define INT int
#define FLOAT float
#define TRUE 1
#define FALSE 0
/*相关测试参数*/
#define MAXSETNUM 13 /*最大的特征数目*/
#define STARTNUM 13 /*开始的特征数目*/
#define STOPNUM 1/*结束时的特征数目*/
#define DATAFILE "wine.data"
#define TESTFILE "wine.data"
#define DATAFROM 0
#define DATAEND 120
#define TESTFROM 120
#define TESTEND 178
INT weight[STARTNUM-STOPNUM]; /***WEIGHT*/
double allweight;
/*蚁群相关参数*/
#define MAXGENERATION 20 /*每次feature改变蚁群算法执行次数*/
#define ANTNUM 15
#define beta 1 /*与信息素相关的权重*/
#define q0 0.6 /*蚁群算法中探索与利用的比例权重*/
#define gama 0.8 /*全局信息素更新相关因子 r(t+1)=(1-gama)*r(t)+gama*delta*/
#define alpha 0.9 /*信息素挥发因子 r(t+1)=(1-alpha)*r(t)+alpha*r0*/
#define r0 0.001 /*信息素初始化值*/
/*随机数生成函数*/
extern long int seed;
long int seed = 23456789;
double ran01(long *);
/***********************随机数发生器参数**********/
#define IA 1680
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
float GetError(char*);
void RegetFeature(int[],int,char*,char *,int,int);
FLOAT pheromone[MAXSETNUM+1][MAXSETNUM+1];/*信息素保存*/
typedef struct{
BOOL subset[MAXSETNUM];/*已经选择的路径*/
INT UnSelectedSet[MAXSETNUM]; /*保存了当前尚未被访问过的结点信息*/
INT maxfnum;/*记录当前测试的feature个数 */
INT curfnum;/*已选择的feature个数*/
INT curfeature;/*当前所在feature*/
BOOL stopflag;/*停止运行标志位*/
FLOAT accurcy;/*当前feature后的测试结果*/
}ANT;
ANT ants[ANTNUM];
/******************/
typedef struct{
BOOL subset[MAXSETNUM];/*保存路径*/
INT featurenum;
FLOAT accuracy;/*保存测试集的测试率*/
FLOAT low_accuracy;
}FROUTE;
FROUTE gloableroute,localroute; /*保存全局最优和本次最优*/
void AntsRun();
void InitialAnts(INT );
void InitialPH();
void AllAntsConstructSolution(INT,INT);
void AntConstructSolution(INT,INT,INT);
void AddNewNode(INT,INT,INT);
INT AddRandomNode(INT); /*随机选取一个特征点*/
INT AddNodeByACO(INT); /*按蚁群优化规则选取一个特征点*/
void OutPutTest(INT);
void UpdateLocalBestRoute();
void InitialWeight();
void AdjustWeight();
INT iteration=0;
FLOAT outacc[MAXGENERATION*ANTNUM];
INT fnum[MAXGENERATION*ANTNUM];
void ForMatLab();
main()
{
float error;
int i;
/*
BOOL set[28]={0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0};
RegetFeature(set,MAXSETNUM,"data4.txt","15.txt",1500,0);
system("svmtrain.exe -s 0 -t 2 -c 2 15.txt");
RegetFeature(set,MAXSETNUM,"data4.txt","16.txt",2500,1500);
system("svmpredict 15.txt 15.txt.model 3.txt");
return;
*/
// RegetFeature(set,MAXSETNUM,"test.txt","25.txt",2100,0);
// return;
AntsRun();
ForMatLab();
//return;
printf("\n/*******************************************************/\n");
printf("\nglobal accuracy:%f******features:",gloableroute.accuracy);
for(i=0;i<MAXSETNUM;i++)
{
if(gloableroute.subset[i])
printf("%d,",i+1);
}
printf("\n/*******************************************************/\n");
RegetFeature(gloableroute.subset,MAXSETNUM,DATAFILE,"12.txt",DATAEND,DATAFROM);
// RegetFeature(set,MAXSETNUM,"data4.txt","12.txt",300,0);
system("svmtrain.exe -s 1 -c 1.00 12.txt");
// system("svmpredict 12.txt 12.txt.model 3.txt");
RegetFeature(gloableroute.subset,MAXSETNUM,TESTFILE,"22.txt",TESTEND,TESTFROM);
// RegetFeature(set,MAXSETNUM,"data4.txt","22.txt",390,300);
//system("svmtrain.exe -s 1 22.txt");
system("svmpredict 22.txt 12.txt.model 3.txt");
error=GetError("3.txt");
printf("%f",error);
}
void ForMatLab()
{
FILE *file=fopen("matlab.txt","w");
INT i;
for(i=0;i<ANTNUM*MAXGENERATION;i++)
{
fprintf(file,"%f%c",100-outacc[i],44);
}
fprintf(file,"***********************************");
for(i=0;i<ANTNUM*MAXGENERATION;i++)
{
fprintf(file,"%d%c",fnum[i],44);
}
fclose(file);
}
/*
INT GetRecord(char* openfile)
{
FILE *file;
file=fopen(openfile,"r");
INT i=0;
while(1)
{
}
}
*/
void AntConstructSolution(INT ant,INT frunnum,INT generation)
{
int i=0;
while(!ants[ant].stopflag)
{
AddNewNode(ant,frunnum,generation);
if(ants[ant].curfnum>=ants[ant].maxfnum)
{
ants[ant].stopflag=TRUE;
}
}
/*开始测试*/
// getchar();
// printf("frunnum:%d ",frunnum);
RegetFeature(ants[ant].subset,MAXSETNUM,DATAFILE,"12.txt",DATAEND,DATAFROM);
system("svmtrain.exe -s 1 -c 1.00 12.txt");
system("svmpredict 12.txt 12.txt.model 3.txt");
RegetFeature(ants[ant].subset,MAXSETNUM,TESTFILE,"22.txt",TESTEND,TESTFROM);
system("svmpredict 22.txt 12.txt.model 3.txt");
ants[ant].accurcy=GetError("3.txt");
if(ants[ant].accurcy>localroute.accuracy ||
(ants[ant].accurcy==localroute.accuracy&&ants[ant].maxfnum<localroute.featurenum))
{
localroute.accuracy=ants[ant].accurcy;
localroute.featurenum=ants[ant].maxfnum;
for(i=0;i<MAXSETNUM;i++)
{
localroute.subset[i]=ants[ant].subset[i];
}
}
if(ants[ant].accurcy<gloableroute.low_accuracy)
{
gloableroute.low_accuracy=ants[ant].accurcy;
}
if((ants[ant].accurcy>gloableroute.accuracy&&gloableroute.accuracy<95)|| (ants[ant].accurcy>=95&&ants[ant].maxfnum<gloableroute.featurenum))
{
gloableroute.accuracy=ants[ant].accurcy;
gloableroute.featurenum=ants[ant].maxfnum;
for(i=0;i<MAXSETNUM;i++)
{
gloableroute.subset[i]=ants[ant].subset[i];
}
}
//getchar();
OutPutTest(ant);
/*以下开始计算该选择路径的结果*/
}
/*蚁群算法开始执行程序*/
void AntsRun()
{
INT frunnum,generation,i;/*特征改变次数*/
gloableroute.accuracy=0.0;
gloableroute.low_accuracy=1.0;
gloableroute.featurenum=0;
// InitialWeight(); /*test*/
InitialPH();
localroute.accuracy=0.0;
for(generation=0;generation<MAXGENERATION;generation++)/*对于当前特征数蚁群开始循环*/
{
InitialAnts(frunnum); /*初始化蚁群*/
AllAntsConstructSolution(frunnum,generation);/*蚁群开始构造解*/
UpdateLocalBestRoute();
// AdjustWeight(); /*test*/
//getchar();
// printf("\nglobal accuracy:%f******",gloableroute.accuracy);
printf("%f,%d,%d\n",gloableroute.accuracy,gloableroute.featurenum,iteration);
//getchar();
}
}
void InitialWeight()
{
INT i=0;
for(i=0;i<STARTNUM-STOPNUM;i++)
{
weight[i]=1;
allweight++;
}
}
void AdjustWeight()
{
INT wclass=3,i=0;
FLOAT subaccuracy=gloableroute.accuracy-gloableroute.low_accuracy;
FLOAT fraccuracy=subaccuracy/wclass;
allweight=0;
for(i=0;i<ANTNUM;i++)
{
weight[ants[i].maxfnum-STOPNUM]+=(INT)(ants[i].accurcy-gloableroute.low_accuracy)/fraccuracy;
allweight+=weight[ants[i].maxfnum-STOPNUM];
}
}
void UpdateLocalBestRoute()
{
INT i,j;
FLOAT error;
error=1-localroute.accuracy/100;
if(error==0)
error=0.00001;
error=r0/error;
for(i=0;i<MAXSETNUM;i++)
{
if(!localroute.subset[i])
{
continue;
}
pheromone[0][i+1]=(1-gama)*pheromone[0][i+1]+gama*error;
for(j=0;j<MAXSETNUM;j++)
{
if(i==j)
continue;
if(!localroute.subset[j])
continue;
pheromone[j+1][i+1]=(1-gama)*pheromone[j+1][i+1]+gama*error;
pheromone[i+1][j+1]=pheromone[j+1][i+1];
}
}
}
void AllAntsConstructSolution(INT frunnum,INT generation)
{
int i;
for(i=0;i<ANTNUM;i++)
{
AntConstructSolution(i,frunnum,generation);
}
}
void OutPutTest(INT ant)
{
int i=0;
outacc[iteration]=gloableroute.accuracy;
fnum[iteration]=gloableroute.featurenum;
iteration++;
printf("%f,%d,%d\n",gloableroute.accuracy,gloableroute.featurenum,iteration);
/*
if(gloableroute.accuracy>98){
printf("\niteration:%d featurenum:%d",iteration,gloableroute.featurenum);
exit(1);
}
*/
return;
for(i=0;i<MAXSETNUM;i++)
{
if(ants[ant].subset[i])
printf("%d,",i+1);
}
printf("************\n");
}
void AddNewNode(INT ant,INT frunnum,INT generation)
{
ants[ant].curfeature=AddNodeByACO(ant); /*否则按蚁群优化算法规则加入一个点*/
ants[ant].curfnum++;
ants[ant].subset[ants[ant].curfeature-1]=TRUE; /*置已访问标志点*/
return;
}
INT AddRandomNode(INT ant)
{
INT unselectednum=MAXSETNUM-ants[ant].curfnum;/*当前未被选中的点的个数*/
INT k=(INT)(ran01(&seed)*unselectednum),returnvalue;
if(k>unselectednum)
{
k--;
}
returnvalue=ants[ant].UnSelectedSet[k];
ants[ant].UnSelectedSet[k]=ants[ant].UnSelectedSet[unselectednum-1]; /*将最后一个数放入到K的位置*/
return returnvalue;
}
INT AddNodeByACO(INT ant)
{
INT unselectednum=MAXSETNUM-ants[ant].curfnum,i=0;/*当前未被选中的点的个数*/
INT curfet=ants[ant].curfeature,maxfetnum,fet; /*当前所在位置*/
FLOAT tempPH[MAXSETNUM],allpossibility=0.0,allPH=0.0,rancount,maxph=0.0;
for(i=0;i<unselectednum;i++)
{
tempPH[i]=pheromone[curfet][ants[ant].UnSelectedSet[i]]; /*提取未被访问的特征点的信息素信息*/
/*此处只考虑了信息素导引,因为在特征点选取中,不能很好的反映其启发式因子*/
allPH+=tempPH[i];
/***************************/
if(tempPH[i]>maxph)
{
maxph=tempPH[i];
maxfetnum=i;
}
}
rancount=ran01(&seed);
if(rancount>=q0)
{
/*提取最大信息素特征点*/
;
}
else
{
for(i=0;i<unselectednum;i++)
{
tempPH[i]/=allPH;
allpossibility+=tempPH[i];
if(allpossibility>=rancount)
{
maxfetnum=i;
break;
}
}
if(maxfetnum>=unselectednum)
{
maxfetnum--;
}
}
fet=ants[ant].UnSelectedSet[maxfetnum];
ants[ant].UnSelectedSet[maxfetnum]=ants[ant].UnSelectedSet[unselectednum-1];
return fet;
}
void InitialPH()
{
INT i,j;
for(i=0;i<MAXSETNUM+1;i++)
{
for(j=0;j<MAXSETNUM+1;j++)
{
pheromone[i][j]=r0;
}
}
}
/*蚁群初始化函数*/
void InitialAnts(INT frunnum)
{
INT i=0,j=0,randfru;
FLOAT poss[STARTNUM-STOPNUM],ranposs,allposs;
INT curnum;
for(i=0;i<STARTNUM-STOPNUM;i++)
{
poss[i]=weight[i]/allweight;
}
for(i=0;i<ANTNUM;i++)
{
/*改进的方面 测试中*/
if(gloableroute.accuracy>=95){
curnum=gloableroute.featurenum+1;
}
else
curnum=STARTNUM;
randfru=(INT)((curnum-STOPNUM)*ran01(&seed))+STOPNUM;
if(randfru<=STOPNUM)
randfru=STOPNUM;
if(randfru>=curnum)
randfru=curnum-1;
// frunnum=randfru;
/*依概率选取
ranposs=ran01(&seed);
allposs=0.0;
for(j=0;j<STARTNUM-STOPNUM;j++)
{
allposs+=poss[j];
if(allposs>ranposs)
{
break;
}
}
if(j>=STARTNUM-STOPNUM)
j--;
frunnum=j+STOPNUM;*/
/******************/
ants[i].maxfnum=randfru;
ants[i].curfnum=0;
ants[i].accurcy=0.0;
ants[i].stopflag=FALSE;
ants[i].curfeature=0; /*置当前所在位置为虚拟点0*/
for(j=0;j<MAXSETNUM;j++)
{
ants[i].subset[j]=FALSE; /*置所有特征均未被选择*/
ants[i].UnSelectedSet[j]=j+1; /*置当前所有未被访问的特征点*/
}
}
}
float GetError(char* imagefile)
{
FILE* file=fopen(imagefile,"r");
float error;
if(file==NULL)
{
printf("can't open file %s",imagefile);
exit(1);
}
fscanf(file,"%f",&error);
fclose(file);
//printf("%f",error);
return error;
}
void RegetFeature2(BOOL* subset,int setnum,char* openfile,char* writefile,int testnum,int from)
{
FILE *rfile,*wfile;
int intout,i,j=0,k;
FLOAT temp[MAXSETNUM];
BOOL breakflag=FALSE;
FLOAT fout,minfout,maxfout;
rfile=fopen(openfile,"r");
wfile=fopen(writefile,"w");
for(k=0;k<testnum;k++)
{
j=0;
for(j=0;j<MAXSETNUM;j++)
{
fscanf(rfile,"%f,",&temp[j]);
}
fscanf(rfile,"%d",&intout);
// fscanf(rfile,"%d.",&intout);
if(k>=from)
{
fprintf(wfile,"%d ",intout);
i=0;
for(j=0;j<setnum;j++)
{
if(!subset[j]) /*若未被*/
continue;
i++;
fprintf(wfile,"%d:%f ",i,temp[j]);
}
fprintf(wfile,"\n");
}
/*for discordant*/
// fscanf(rfile,"|%d",&intout);
/*****************/
}
fclose(rfile);
fclose(wfile);
}
void RegetFeature(BOOL* subset,int setnum,char* openfile,char* writefile,int testnum,int from)
{
FILE *rfile,*wfile;
int intout,i,j=0,k;
BOOL breakflag=FALSE;
FLOAT fout,minfout,maxfout;
rfile=fopen(openfile,"r");
wfile=fopen(writefile,"w");
for(k=0;k<testnum;k++)
{
j=0;
/*
if(feof(rfile)){
break;
}
*/
/*fscanf(rfile,"%f,",&fout);breat cancer*/
/*
if(feof(rfile))
{
break;
}
*/
fscanf(rfile,"%d",&intout);
if(k>=from)
fprintf(wfile,"%d ",intout);
/*
fscanf(rfile,",%d",&fout);
fscanf(rfile,",%d",&fout);
*/
for(i=0;i<setnum;i++)
{
/*
if(feof(rfile)){
breakflag=TRUE;
break;
}
*/
fscanf(rfile,",%f",&fout);
if(!subset[i]) /*若未被*/
continue;
j++;
minfout=(int)fout-0.00001;
maxfout=(int)fout+0.00001;
if(k>=from){
if(fout>minfout && fout<maxfout)
fprintf(wfile,"%d:%d ",j,(int)fout);
else
fprintf(wfile,"%d:%f ",j,fout);
}
}
//fscanf(rfile,",%d",&fout);
if(k>=from)
fprintf(wfile,"\n");
/*
if(breakflag)
break;
*/
}
fclose(rfile);
fclose(wfile);
}
/*****************************0-1随机数生成函数*****************************/
double ran01( long *idum )
/*
FUNCTION: generate a random number that is uniformly distributed in [0,1]
INPUT: pointer to variable with the current seed
OUTPUT: random number uniformly distributed in [0,1]
(SIDE)EFFECTS: random number seed is modified (important, this has to be done!)
ORIGIN: numerical recipes in C
*/
{
long k;
double ans;
k =(*idum)/IQ;
*idum = IA * (*idum - k * IQ) - IR * k;
if (*idum < 0 ) *idum += IM;
ans = AM * (*idum);
if(10*ans>1)
return 1;
return 10*ans;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -