⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scs.cpp

📁 (其中已经包含5个源码)所附源程序C或C++代码文件及其可执行文件 Scs.cpp 基本分类算法源程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
 child2->c[j]=mutation(parent1->c[j],pmutation,nmutation);
 child1->c[j]=mutation(parent2->c[j],pmutation,nmutation);
 j=j+1;
}
j=0;
while(j<sitecross)
{
 child1->c[j]=mutation(parent1->c[j],pmutation,nmutation);
 child2->c[j]=mutation(parent2->c[j],pmutation,nmutation);
 j=j+1;
}
inheritance=avg(parent1->strength,parent2->strength);
child1->strength=inheritance;child1->matchflag=0;
child1->ebid=0.0;child1->bid=0.0;
child1->specificity=counterspecificity(child1->c,nposition);
child2->strength=inheritance;child2->matchflag=0;
child2->ebid=0.0;child2->bid=0.0;
child2->specificity=counterspecificity(child2->c,nposition);
}

int worstofn(struct poptype *population, int n)
{
int j,worst,candidate;
float worststrength;
worst=rnd(0, population->nclassifier-1);
worststrength=population->classifier[worst].strength;
if(n>0)
{
   for(j=1;j<=n-1;j++)
 {
  candidate=rnd(0,population->nclassifier-1);
  if(worststrength>population->classifier[candidate].strength)
    {
    worst=candidate;
    worststrength=population->classifier[worst].strength;
    }
 }
}
return(worst);
}

int matchcount(struct classtype *classifier1,struct classtype *classifier2,int nposition)
{
int tempcount,j;
   if(classifier1->a==classifier2->a)
     tempcount=1;
   else
     tempcount=0;
  for(j=0;j<=nposition-1;j++)
     if( classifier1->c[j]==classifier2->c[j])    tempcount=tempcount+1;
   return(tempcount);    
}

int crowding(struct classtype *child,struct poptype *population,int &crowdingfactor,int crowdingsubpop)
{
int popmember,j,match,matchfmax,mostsimilar;
matchfmax=-1;  mostsimilar=0;
if(crowdingfactor<1)  crowdingfactor=1;
for(j=0;j<=crowdingfactor-1;j++)
{
   popmember=worstofn(population,crowdingsubpop);
   match=matchcount(child,&population->classifier[popmember],population->nposition);
   if(match>matchfmax)
    {
     matchfmax=match;
     mostsimilar=popmember;
    }
 }
return(mostsimilar);
}

void statistics(struct poptype *population)
{
 int j;
 population->fmaxstrength=population->classifier[0].strength;
 population->fminstrength=population->classifier[0].strength;
 population->sumstrength=population->classifier[0].strength;
j=1;
while(j<=population->nclassifier-1)
  {
  population->fmaxstrength=fmax(population->fmaxstrength,population->classifier[j].strength);
  population->fminstrength=fmin(population->fminstrength,population->classifier[j].strength);
  population->sumstrength=population->sumstrength+population->classifier[j].strength;
  j++;
  }
  population->avgstrength=population->sumstrength/population->nclassifier;
}

void ga(struct grecord *garec,struct poptype *population)
{
int j;
struct classtype *child;
int nbytes;
child=(struct classtype *)malloc(2*sizeof(struct classtype));
nbytes=population->nposition*sizeof(int);
child[0].c = (int *) malloc(nbytes);
child[1].c = (int *) malloc(nbytes);

statistics(population);
for(j=0;j<=garec->nselect-1;j++)
  {
   garec->mating[j].mate1=select(population);
   garec->mating[j].mate2=select(population);
   crossover(&population->classifier[garec->mating[j].mate1],&population->classifier[garec->mating[j].mate1],&child[0],&child[1], garec->pcrossover,
              garec->pmutation,garec->mating[j].sitecross,population->nposition,garec->ncrossover,garec->nmutation);
  
   garec->mating[j].mort1=crowding(&child[0],population,garec->crowdingfactor,garec->crowdingsubpop);
   population->sumstrength=population->sumstrength-population->classifier[garec->mating[j].mort1].strength+child[0].strength;
   population->classifier[garec->mating[j].mort1]=child[0];
   garec->mating[j].mort2=crowding(&child[1],population,garec->crowdingfactor,garec->crowdingsubpop);
   population->sumstrength=population->sumstrength-population->classifier[garec->mating[j].mort2].strength+child[1].strength;
   population->classifier[garec->mating[j].mort2]=child[1];
  }
}

void reportga(struct grecord *garec,struct poptype *population )
{
  int j;
 skip(1);
 rep<<"           遗传算法报告: "<<endl;
 rep<<" --------------------------------------------"<<endl;
 rep<<"  对数   父1   父2  交叉位置  子1   子2  "<<endl;
 rep<<" ---------------------------------------------"<<endl;
   for(j=0; j<=garec->nselect-1;j++)
   {
   rep<<setw(6)<<j<<setw(6)<< garec->mating[j].mate1;
   rep<<setw(6)<<garec->mating[j].mate2<<setw(6)<<garec->mating[j].sitecross;
   rep<<setw(6)<<garec->mating[j].mort1<<setw(6)<<garec->mating[j].mort2<<endl;
   }
 rep<<" 统计数据: "<<endl;
 rep<<"----------------------"<<endl;
 rep<<" 平均权值 ="<<population->avgstrength<<endl;
 rep<<" 最大权值 ="<<population->fmaxstrength<<endl;
 rep<<" 最小权值 ="<<population->fminstrength<<endl;
 rep<<" 权值和   ="<<population->sumstrength<<endl;
 rep<<" 交叉数目 ="<<garec->ncrossover<<endl;
 rep<<" 变异数目 ="<<garec->nmutation<<endl;
}

float rndreal(float lo ,float hi)    /*在浮点数lo和hi之间产生一个随机实数*/
{
    return((float)(rand()/32767. * (hi - lo)) + lo);
}

void initrandomnormaldeviate()      /* 随机标准差的初始化 */
{
    rndcalcflag = 1;
}

double randomnormaldeviate()        /* 产生随机标准差 */
{
    double t, rndx1;
    if(rndcalcflag)
    {   rndx1 = sqrt(- 2.0*log((double) rand()/32767.));
        t = 6.2831853072 * (double) rand()/32767.;
        rndx2 = rndx1 * sin(t);
        rndcalcflag = 0;
        return(rndx1 * cos(t));
    }
    else
    {
        rndcalcflag = 1;
        return(rndx2);
    }
}


float noise(float mu ,float sigma)    /* 产生具有给定均值和标准均方差的随机数 */
{
    return((float)(randomnormaldeviate()*sigma) + mu);
}

void skip(int skipcount)
{
    int j;
    for (j = 1; j <= skipcount; j++) rep<<endl;
}

int flip(float prob)             /* 以一定概率产生0或1 */
{
    if(prob==1.0)
        return(1);
    else
        return((rand()/32767. <= prob));
}

int rnd(int low,int high) /*在整数low和high之间产生一个随机整数*/
{
    int i;
      if(low >= high)
        i = low;
    else
    {
        i = (int)(rand()/32767. * (high - low + 1)) + low;
        if(i > high) i = high;
    }
    return(i);
}


float fmax(float x,float y)
{
if(x>y)
return(x);
else
return(y);
}

float fmin(float x,float y)
{
if(x<y)
return(x);
else
return(y);
}

float avg(float x,float y)
{
return((float)(0.5*(x+y)));
}

int halt()
{
const times=1000;
int temp;
int j;
/* int ch;*/
j=0;
 do
 {
  j++;
 }while(j<times);
 temp=(j<times);
 /*
 cout<<"是否继续?(1/0)";
 cin>>ch;
 if(ch==1) 
	return(0);
 else
    return(1); */
 return(temp);
}

void initmalloc()     /*为全局数据变量分配空间 */
{
  int nbytes,ncbytes;
  int j;
  int fmaxc,fmaxp;
  cfile>>fmaxp;
  cfile>>fmaxc;
  ncbytes =fmaxc*(fmaxp*sizeof(int)+3*sizeof(int)+3*sizeof(float));
     if((pop->classifier = (struct classtype *) malloc(ncbytes)) == NULL)
        { cout<<"malloc: out of memory making !!"<<endl;
         exit(-1);
        } 
   nbytes=fmaxp*sizeof(int);
   for(j=0;j<=fmaxc-1;j++)
   {
      if((pop->classifier[j].c = (int *) malloc(nbytes)) == NULL)
        { cout<<"malloc: out of memory making !!"<<endl;
        exit(-1);
	  }
   }
   nbytes=fmaxp*sizeof(int);
     if((envmessage = (int *) malloc(nbytes)) == NULL)
        { cout<<"malloc: out of memory making !!\n"<<endl;
        exit(-1);
        }
   if((matchl->clist = (int *) malloc(pop->nclassifier*sizeof(int))) == NULL)
    { cout<<"malloc: out of memory making !!\n"<<endl;
     exit(-1);
    }

   nbytes=fmaxp*sizeof(int);
       if((environrec.signal = (int *) malloc(nbytes)) == NULL)
        { cout<<"malloc: out of memory making !!\n"<<endl;
        exit(-1);
        }
   nbytes = fmaxmating*sizeof(struct mrecord);
       if((garec.mating = (struct mrecord *) malloc(nbytes)) == NULL)
        { cout<<"malloc: out of memory making !!\n"<<endl;
        exit(-1);
        }
  pop->nclassifier=fmaxc;
  pop->nposition=fmaxp;
}

void generatecfile()
{
    int nclassifier,nposition;      
    float pgeneral,cbid,bidsigma,bidtax,lifetax,bid1,bid2,ebid1,ebid2;  
    float strength;
	int i,j;
	float temprand;
    ofstream rcfile("c:\\scs\\cfile.txt");   

    cout<<" 分类器前件码长=";
    cin>>nposition;
    rcfile<<nposition<<endl;
		
	cout<<" 分类器数目= ";
    cin>>nclassifier;
	rcfile<<nclassifier<<endl;
    
	cout<<" 随机概率= ";
    cin>>pgeneral;
	rcfile<<pgeneral<<endl;

	cout<<" 投标系数= ";   
    cin>>cbid;
    rcfile<<cbid<<endl;

    cout<<" 有效投标中随机噪声的均方差 = ";
    cin>>bidsigma;
	rcfile<<bidsigma<<endl;
	
    cout<<" 投标税= ";
	cin>>bidtax;
	rcfile<<bidtax<<endl;

    cout<<" 存活税  = ";
    cin>>lifetax;
	rcfile<<lifetax<<endl;

	cout<<" 投标确定性基数 = ";
    cin>>bid1;
	rcfile<<bid1<<endl;

    cout<<" 投标确定性倍率 = ";
    cin>>bid2;
	rcfile<<bid2<<endl;

    cout<<" 有效投标确定性基数 = ";
	cin>>ebid1;
	rcfile<<ebid1<<endl;

	cout<<" 有效投标确定性倍率 = ";
    cin>>ebid2;
    rcfile<<ebid2<<endl;
  
	cout<<" 分类器初始权值 = ";
    cin>>strength;
    
   for(i=0;i<=nclassifier-1;i++)
   {
	   for(j=0;j<=nposition-1;j++)
	   {
		temprand=(float)(rand()/32767.);
	   if(temprand<1./3)
	    rcfile<<"0";
	   else if((temprand>=1/3.)&&(temprand<2/3.))
	    rcfile<<"1";
	   else
	   rcfile<<"#";
	   }
       rcfile<<":";
	   temprand=(float)(rand()/32767.);
	   if(temprand<1/2.)
	    rcfile<<"0";
	   else
	    rcfile<<"1";
       rcfile<<" "<<strength<<endl;	   
   }
   rcfile<<"n"<<endl;
}

void freeall()    /* 释放内存空间 */
{
int j;
free(pop->classifier);
for(j=0;j<=pop->nclassifier-1;j++)
  free(pop->classifier[j].c);
free(matchl->clist);
free(envmessage); 
free(environrec.signal);
free(garec.mating);
   }

void main()  /*  主程序 */
{
/* generatecfile();*/
initialization();
detectors(&environrec,envmessage);
report();
do {
    timekeeper(&timekeeprec);
    environment(&environrec);
    detectors(&environrec,envmessage);
	matchl->nactive=0;
	matchclassifiers(pop,envmessage,matchl);
	aoc(pop,matchl,&clearingrec);
    environrec.classifieroutput=pop->classifier[clearingrec.winner].a;
    reinforcement(&reinforcementrec,pop,&clearingrec,&environrec);
    if(timekeeprec.reportflag)   report();
    if(timekeeprec.consolereportflag)   consolereport(&reinforcementrec);
    if(timekeeprec.plotreportflag)   plotreport(&reinforcementrec);
    advance(&clearingrec);
    if(timekeeprec.gaflag)
     {
     ga(&garec,pop);
     if(timekeeprec.reportflag)   reportga(&garec,pop);
     }
   }while(halt()==0);
   report();
   freeall();
 }


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -