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

📄 sin.c

📁 用C语言实现的遗传算法的程序,实现的是y=x+10*sin(5*x)+5*cos(10*x)求最大值.实现环境为win-tc.
💻 C
字号:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define POPSIZE 4
#define GENE    10
#define CROSSP  0.50
#define MUTATIONP  0.1
#define MAX 629
int *decToBin(int value);
int binToDec(int (*p)[GENE],int n);
void vary(int *para);
void xcross(struct binarypop* Binary ,int a,int b);
void average(struct decimalpop* DD) ;
int max;
FILE *file;
struct binarypop
{
    int chromosome[POPSIZE][GENE];
} ;


struct decimalpop
{
    int  a[POPSIZE];
};

struct fitness
{
    int fit[POPSIZE];
    int smid[POPSIZE];
} ;


void generateChromosome(struct decimalpop* D)
{   int i,b;

    srand(time(NULL));
    for(i=0;i<POPSIZE;i++)
    {
        b=(rand())%MAX;
        D->a[i]=b;
        /*printf("%d  ",b);*/

    }


}
void  toBinary(struct decimalpop* D,struct binarypop* B)
{   int i,j;
    int *p;

    for(i=0;i<POPSIZE;i++){
     p=decToBin(D->a[i]);
     for(j=0;j<GENE;j++) {
             B->chromosome[i][j]=*p++;
             /*printf("%d  \n",*p);*/
             /*p++;*/
             }

    }
   /*getch();*/
}


void  toDecimal(struct binarypop* B,struct decimalpop* D )
{
    int i;
    int (*p)[GENE];
    p=B->chromosome;

    for(i=0;i<POPSIZE;i++){
    D->a[i]=binToDec(p,i);
    }
}


void computeFitness(struct decimalpop* D,struct fitness* F)
{   int i;
    float aver;

    F->fit[0]=(D->a[0])/100.0+10*sin(5*(D->a[0]/100.0)+6*cos(3*D->a[0]/100.0));/* y=x+10*sin(5*x)+6*cos(3*x) */
    F->smid[0]=F->fit[0] ;

    if(F->fit[0]>max)
    max=F->fit[0];

    for(i=1;i<POPSIZE;i++){
        F->fit[i]=( D->a[i] )/100.0+10*sin(5* (D->a[i]) /100.0)+6*cos( 3*(D->a[i])/100.0);
        F->smid[i]=(F->smid[i])+(F->fit[i]);

        if(F->fit[i]>max){
        max=F->fit[i];
              }
        else {

        }
        }
    aver=(F->smid[POPSIZE-1])/POPSIZE;
    printf("Aver fitness is %f\n",aver);
    printf("The max is %d\n",max);
    fprintf(file,"The max is %d\n",max);
}

void elect(struct binarypop* Bb,struct fitness* Fitness)
{
    int i,j,b,k;
    struct binarypop bbb;

    srand(time(NULL));
    for(i=0;i<POPSIZE;i++)
    {
        b=(rand())%(Fitness->smid[POPSIZE-1]);

        if(b<=Fitness->smid[0]){
                for(k=0;k<GENE;k++)
                bbb.chromosome[i][k]=Bb->chromosome[0][k];
                }
        else
                for(j=0;j<POPSIZE;j++){
                if(b>Fitness->smid[j]&&b<=Fitness->smid[j+1]) {
                for(k=0;k<GENE;k++){
                bbb.chromosome[i][k]=Bb->chromosome[j][k];

                        }

                    }

                }

    }





}
void cross(struct binarypop* Binary)
{   int i,j,one=0,mem; /*one is one chromosome which is crossed; and mem is another one*/
    int ready=0;     /*when there are two choromosome was sure, cross begin */
    float pp;        /*this determines whether cross*/

    srand(time(NULL));



          for(mem=0;mem<POPSIZE;mem++){
           pp=rand()%1000/1000.0;
           if(pp<CROSSP)
           {
                ready++;
                if(ready%2==0)
                xcross(Binary,one,mem);
                else one=mem;

           }
          }




}

void xcross(struct binarypop* Binary ,int a,int b)
{
   int i;
   int position;
   int temp;
   srand(time(NULL));
   position=rand()%GENE;



   for(i=position+1;i<GENE;i++) {
   temp=Binary->chromosome[a][i];
   Binary->chromosome[a][i]=  Binary->chromosome[b][i];
   Binary->chromosome[b][i]=temp;
   }
}


void mutation(struct binarypop* M)
{   int i,j;
    float pp;
    srand(time(NULL));
    for(i=0;i<POPSIZE;i++)
    for(j=0;j<GENE;j++){
    pp=rand()%1000/1000.0;
    if(pp<MUTATIONP){
            vary(&(M->chromosome[i][j]));
        }
    }
}
void vary(int* para)
{
    if(*para==1)
    *para=0;
    else
    *para=1;

}

void main()
{   int generation=0,i,j;
    struct binarypop* B;

    struct decimalpop* D;
    struct fitness* F;

    D=(struct decimalpop*)malloc(sizeof(struct decimalpop));
    if(D==NULL)
    printf("It's failure to malloc mem for D\n");
    else{
    printf("success to malloc\n");}
    B=(struct binarypop*)malloc(sizeof(struct binarypop));
    if(B==NULL)
    printf("It's failure to malloc mem for B\n");
    else{
    printf("success to malloc\n");}

    F=(struct fitness*)malloc(sizeof(struct fitness));
    if(F==NULL)
       printf("It's failure to malloc mem for C\n");
       else{
    printf("success to malloc\n");}
    file=fopen("ga.txt","w+");
    if(file==NULL)
    {
        printf("cannot open this file\n");
        exit(0);
    }
    else
    {
        printf("file success\n");
    }







    generateChromosome(D);

    do{

    toBinary(D,B);


    computeFitness(D,F);


    elect(B,F);
    cross(B);
    mutation(B);
    toDecimal(B,D);
    average(D);
    generation++;

    printf("generation is %d\n",generation);
    }while(generation<100);
    free(B);
    free(D);
    free(F);
    fclose(file);
    getch();

}

int by[GENE]={0};
int *decToBin(int value)
{
    int i=GENE,j;
    for(j=0;j<GENE;j++)
    {by[j]=0;
     }
    while(value){
      by[--i]=value%2;
      value=value/2;
      }
    return by;
}

int binToDec(int (*p)[GENE],int n)
{
    int a=0,i;
    for(i=0;i<GENE;i++){
    a+=(*(*(p+n)+i))<<(GENE-1-i);
    }
    return a;
}

void average(struct decimalpop* DD)
{   int i;
    int sum=0;
    float aver;
    for(i=0;i<POPSIZE;i++)
    sum=sum+DD->a[i];
    aver=sum/POPSIZE;
    printf("The aver is %f\n",aver);

}


⌨️ 快捷键说明

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