?? scs.cpp
字號:
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 + -