#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
/* Change any of these parameters to match your needs */
#define POPSIZE 1000 /* population size */
#define MAXGENS 100 /* max. number of generations */
#define PXOVER 0.8 /* probability of crossover */
#define PMUTATION 0.15 /* probability of mutation */
#define TRUE 1
#define FALSE 0
#define BACKTRACK 60
#define H 1
#define P 2
#define NVARS 360 /* no. of problem variables */
int place[2*NVARS+1][2*NVARS+1]={0};
int sequence[NVARS]={0};
int protein[NVARS];
int generation; /* current generation no. */
int cur_best; /* best individual */
FILE *galog; /* an output file */
FILE *in;
struct genotype /* genotype (GT), a member of the population */
{
int gene[NVARS]; /* a string of variables */
int fitness; /* GT's fitness */
int upper[NVARS]; /* GT's variables upper bound */
int lower[NVARS]; /* GT's variables lower bound */
double rfitness; /* relative fitness */
double cfitness; /* cumulative fitness */
};
struct genotype population[POPSIZE+1]; /* population */
struct genotype newpopulation[POPSIZE+1]; /* new population; */
/* replaces the */
/* old generation */
/* Declaration of procedures used by this genetic algorithm */
void initialize(void);
double randval(double, double);
int randvalInt(int, int);
void evaluate(void);
void keep_the_best(void);
double randdouble(void);
int createSequence(int seq[],int len);
int checkSequence(int seq[],int len);
int createSequence(int seq[],int len)
{
int result=FALSE;
int row,colum,next;
int count=0;
int countp=0;
int nextp[5]={0};
int pa[3];
double p;
int n=1,i,j;
for(i=0;i<len*2+1;i++)
{
for(j=0;j<len*2+1;j++)
{
place[i][j]=0;
}
}
seq[0]=9;
place[len][len]=seq[0];
row=len;
colum=len;
row=row-1;
seq[n]=1;
next =1;
place[row][colum]=protein[n];
for(n=2;n<len;n++)
{
next= next+3;
p=randdouble();
if(p>0&&p<=0.33333)
{
}
else if(p>0.33333&&p<=0.66666)
{
next=next+1;
}
else
{
next = next+2;
}
next = next %4;
if(next == 0)
{
next=4;
}
switch(next)
{
case 1:
row--;
break;
case 2:
colum++;
break;
case 3:
row++;
break;
case 4:
colum--;
break;
}
if(place[row][colum] >0)
{
switch(next)
{
case 1:
row++;
break;
case 2:
colum--;
break;
case 3:
row--;
break;
case 4:
colum++;
break;
}
n=n-1;
next=seq[n];
count=1;
while(count<=BACKTRACK)
{
if(place[row-1][colum]<=0||place[row][colum+1]<=0||place[row+1][colum]<=0||place[row][colum-1]<=0)
{
countp=0;
nextp[1]=0;nextp[2]=0;nextp[3]=0;nextp[4]=0;
if(place[row-1][colum]<=0)
{
nextp[1]=1;
countp++;
}
if(place[row][colum+1]<=0)
{
nextp[2]=2;
countp++;
}
if(place[row+1][colum]<=0)
{
nextp[3]=3;
countp++;
}
if(place[row][colum-1]<=0)
{
nextp[4]=4;
countp++;
}
switch(countp)
{
case 1:
{
for(i=1;i<5;i++)
{
if(nextp[i]!=0)
next=nextp[i];
}
switch(next)
{
case 1:
row--;
break;
case 2:
colum++;
break;
case 3:
row++;
break;
case 4:
colum--;
break;
}
n=n+1;
seq[n]=next;
place[row][colum]=protein[n];
break;
}// case 1
case 2:
{
for(j=0;j<2;j++)
{
for(i=1;i<5;i++)
{
if(nextp[i]!=0)
{
pa[j]=nextp[i];
nextp[i]=0;
break;
}
}
}//for
p=rand()%1000/1000;
if(p<0.5)
next=pa[0];
else
next=pa[1];
switch(next)
{
case 1:
row--;
break;
case 2:
colum++;
break;
case 3:
row++;
break;
case 4:
colum--;
break;
}
n=n+1;
seq[n]=next;
place[row][colum]=protein[n];
break;
}//case 2
case 3:
{
for(j=0;j<3;j++)
{
for(i=1;i<5;i++)
{
if(nextp[i]!=0)
{
pa[j]=nextp[i];
nextp[i]=0;
break;
}
}
}//for
p=rand()%1000/1000;
if(p<0.333333)
next=pa[0];
else if(p>=0.333333&&p<0.666666)
next=pa[1];
else
next=pa[2];
switch(next)
{
case 1:
row--;
break;
case 2:
colum++;
break;
case 3:
row++;
break;
case 4:
colum--;
break;
}
n=n+1;
seq[n]=next;
place[row][colum]=protein[n];
break;
}//case 3
}
break;
}//if
else
{
switch(next)
{
case 1:
row++;
break;
case 2:
colum--;
break;
case 3:
row--;
break;
case 4:
colum++;
break;
}
n=n-1;
next=seq[n];
count++;
}
}//while
if(count>BACKTRACK)
{
return result;
}
}//if
else
{
seq[n]=next;
place[row][colum]=protein[n];
}
}//for
result =TRUE;
return result;
}
int checkSequence(int seq[],int len)
{
int row,colum,next;
int n,i,j;
for(i=0;i<len*2+1;i++)
{
for(j=0;j<len*2+1;j++)
{
place[i][j]=0;
}
}
seq[0]=9;
place[len][len]=seq[0];
row=len;
colum=len;
for(n=1;n<len;n++)
{
next= seq[n];
switch(next)
{
case 1:
row--;
break;
case 2:
colum++;
break;
case 3:
row++;
break;
case 4:
colum--;
break;
default:
break;
}
if(place[row][colum] >0)
{
return FALSE; //failture to create
}
place[row][colum]=protein[n];
}//for(n=1;n<len;n++)
return TRUE;
}
/***************************************************************/
/* Initialization function: Initializes the values of genes */
/* within the variables bounds. It also initializes (to zero) */
/* all fitness values for each member of the population. It */
/* reads upper and lower bounds of each variable from the */
/* input file `gadata.txt'. It randomly generates values */
/* between these bounds for each gene of each genotype in the */
/* population. The format of the input file `gadata.txt' is */
/* var1_lower_bound var1_upper bound */
/* var2_lower_bound var2_upper bound ... */
/***************************************************************/
void initialize(void)
{
int i, j;
int lbound=1, ubound=4;
for (i = 0; i < POPSIZE; i++)
{
//total=0;
population[i].fitness = 0;
population[i].rfitness = 0;
population[i].cfitness = 0;
for(j=0;j<NVARS;j++)
{
population[i].lower[j] = lbound;
population[i].upper[j]= ubound;
}
while(createSequence(population[i].gene,NVARS) != TRUE)
{
}
}
}
/***********************************************************/
/* Random value generator: Generates a value within bounds */
/***********************************************************/
double randval(double low, double high)
{
double val;
val = randdouble()*(high - low) + low;
return(val);
}
int randvalInt(int low, int high)
{
double val;
val = floor(randdouble()*(high - low+1) + low);
return (int)(val);
}
/***********************************************************/
/* Random a double value between 0~1 */
/***********************************************************/
double randdouble()
{
double val;
val = rand()*1.0 / RAND_MAX ;
return val ;
}
/*************************************************************/
/* Evaluation function: This takes a user defined function. */
/* Each time this is changed, the code has to be recompiled. */
/*************************************************************/
void evaluate(void)
{
int i,j,k,m;
int x=0,y=0;
int resultfit;
for (i = 0; i < POPSIZE; i++)
{
resultfit=0;
if(!checkSequence(population[i].gene, NVARS))
{
while(createSequence(population[i].gene,NVARS)!=TRUE)
{
}
}
for(j=0;j<NVARS;j++)
{
if(protein[j] != H)
{
continue;
}
for(k=j+3;k<NVARS;k++)
{
if(protein[k] != H)
{
continue;
}
x=0;
y=0;
for(m=j+1;m<=k;m++)
{
switch(population[i].gene[m])
{
case 1:
y--;
break;
case 2:
x++;
break;
case 3:
y++;
break;
case 4:
x--;
break;
}//switch
}
if((x==-1 || x==1) && y==0)
{
resultfit--;
}
if((y==-1 || y==1) && x==0)
{
resultfit--;
}
}
}
population[i].fitness = resultfit;
}//for
}
/***************************************************************/
/* Keep_the_best function: This function keeps track of the */
/* best member of the population. Note that the last entry in */
/* the array Population holds a copy of the best individual */
/***************************************************************/
void keep_the_best()
{
int mem;
int i;
cur_best = 0; /* stores the index of the best individual */
for (mem = 0; mem < POPSIZE; mem++)
{
if (population[mem].fitness < population[POPSIZE].fitness)
{
cur_best = mem;
population[POPSIZE].fitness = population[mem].fitness;
}
}
/* once the best member in the population is found, copy the genes */
for (i = 0; i < NVARS; i++)
{
population[POPSIZE].gene[i] = population[cur_best].gene[i];
}
}
/**************************************************************/
/* Main function: Each generation involves selecting the best */
/* members, performing crossover & mutation and then */
/* evaluating the resulting population, until the terminating */
/* condition is satisfied */
/**************************************************************/
int main()
{
int i;
DWORD start, stop;
start = GetTickCount();
srand((unsigned)time(NULL));
if((in = fopen("in.txt","r"))==NULL)
{
printf("file cannot read!");
exit(0);
}
for(i=0;i<NVARS;i++)
{
fscanf(in,"%d",&protein[i]);
}
if ((galog = fopen("log.txt","w+"))==NULL)
{
printf("file cannot write!");
exit(0);
}
initialize(); //load parameters from file and generate the random gene
evaluate(); //calculate the fitness of each one
keep_the_best();//put the best one into the last position of this generation
fprintf(galog,"\n\n Simulation completed\n");
fprintf(galog,"\n Best member: \n");
printf("\n Best member: \n");
for (i = 0; i < NVARS; i++)
{
fprintf (galog," %d",population[POPSIZE].gene[i]);
printf ("%-2d",population[POPSIZE].gene[i]);
}
fprintf(galog,"\n\n Best fitness = %5d",population[POPSIZE].fitness);
printf("\n\n Best fitness = %5d\n",population[POPSIZE].fitness);
fclose(galog);
stop = GetTickCount();
printf("time: %d ms\n", stop - start);
fclose(galog);
return 0;
}
/***************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
/* Change any of these parameters to match your needs */
#define POPSIZE 1000 /* population size */
#define MAXGENS 100 /* max. number of generations */
#define PXOVER 0.8 /* probability of crossover */
#define PMUTATION 0.15 /* probability of mutation */
#define TRUE 1
#define FALSE 0
#define BACKTRACK 60
#define H 1
#define P 2
#define NVARS 360 /* no. of problem variables */
int place[2*NVARS+1][2*NVARS+1]={0};
int sequence[NVARS]={0};
int protein[NVARS];
int generation; /* current generation no. */
int cur_best; /* best individual */
FILE *galog; /* an output file */
FILE *in;
struct genotype /* genotype (GT), a member of the population */
{
int gene[NVARS]; /* a string of variables */
int fitness; /* GT's fitness */
int upper[NVARS]; /* GT's variables upper bound */
int lower[NVARS]; /* GT's variables lower bound */
double rfitness; /* relative fitness */
double cfitness; /* cumulative fitness */
};
struct genotype population[POPSIZE+1]; /* population */
struct genotype newpopulation[POPSIZE+1]; /* new population; */
/* replaces the */
/* old generation */
/* Declaration of procedures used by this genetic algorithm */
void initialize(void);
double randval(double, double);
int randvalInt(int, int);
void evaluate(void);
void keep_the_best(void);
double randdouble(void);
int createSequence(int seq[],int len);
int checkSequence(int seq[],int len);
int createSequence(int seq[],int len)
{
int result=FALSE;
int row,colum,next;
int count=0;
int countp=0;
int nextp[5]={0};
int pa[3];
double p;
int n=1,i,j;
for(i=0;i<len*2+1;i++)
{
for(j=0;j<len*2+1;j++)
{
place[i][j]=0;
}
}
seq[0]=9;
place[len][len]=seq[0];
row=len;
colum=len;
row=row-1;
seq[n]=1;
next =1;
place[row][colum]=protein[n];
for(n=2;n<len;n++)
{
next= next+3;
p=randdouble();
if(p>0&&p<=0.33333)
{
}
else if(p>0.33333&&p<=0.66666)
{
next=next+1;
}
else
{
next = next+2;
}
next = next %4;
if(next == 0)
{
next=4;
}
switch(next)
{
case 1:
row--;
break;
case 2:
colum++;
break;
case 3:
row++;
break;
case 4:
colum--;
break;
}
if(place[row][colum] >0)
{
switch(next)
{
case 1:
row++;
break;
case 2:
colum--;
break;
case 3:
row--;
break;
case 4:
colum++;
break;
}
n=n-1;
next=seq[n];
count=1;
while(count<=BACKTRACK)
{
if(place[row-1][colum]<=0||place[row][colum+1]<=0||place[row+1][colum]<=0||place[row][colum-1]<=0)
{
countp=0;
nextp[1]=0;nextp[2]=0;nextp[3]=0;nextp[4]=0;
if(place[row-1][colum]<=0)
{
nextp[1]=1;
countp++;
}
if(place[row][colum+1]<=0)
{
nextp[2]=2;
countp++;
}
if(place[row+1][colum]<=0)
{
nextp[3]=3;
countp++;
}
if(place[row][colum-1]<=0)
{
nextp[4]=4;
countp++;
}
switch(countp)
{
case 1:
{
for(i=1;i<5;i++)
{
if(nextp[i]!=0)
next=nextp[i];
}
switch(next)
{
case 1:
row--;
break;
case 2:
colum++;
break;
case 3:
row++;
break;
case 4:
colum--;
break;
}
n=n+1;
seq[n]=next;
place[row][colum]=protein[n];
break;
}// case 1
case 2:
{
for(j=0;j<2;j++)
{
for(i=1;i<5;i++)
{
if(nextp[i]!=0)
{
pa[j]=nextp[i];
nextp[i]=0;
break;
}
}
}//for
p=rand()%1000/1000;
if(p<0.5)
next=pa[0];
else
next=pa[1];
switch(next)
{
case 1:
row--;
break;
case 2:
colum++;
break;
case 3:
row++;
break;
case 4:
colum--;
break;
}
n=n+1;
seq[n]=next;
place[row][colum]=protein[n];
break;
}//case 2
case 3:
{
for(j=0;j<3;j++)
{
for(i=1;i<5;i++)
{
if(nextp[i]!=0)
{
pa[j]=nextp[i];
nextp[i]=0;
break;
}
}
}//for
p=rand()%1000/1000;
if(p<0.333333)
next=pa[0];
else if(p>=0.333333&&p<0.666666)
next=pa[1];
else
next=pa[2];
switch(next)
{
case 1:
row--;
break;
case 2:
colum++;
break;
case 3:
row++;
break;
case 4:
colum--;
break;
}
n=n+1;
seq[n]=next;
place[row][colum]=protein[n];
break;
}//case 3
}
break;
}//if
else
{
switch(next)
{
case 1:
row++;
break;
case 2:
colum--;
break;
case 3:
row--;
break;
case 4:
colum++;
break;
}
n=n-1;
next=seq[n];
count++;
}
}//while
if(count>BACKTRACK)
{
return result;
}
}//if
else
{
seq[n]=next;
place[row][colum]=protein[n];
}
}//for
result =TRUE;
return result;
}
int checkSequence(int seq[],int len)
{
int row,colum,next;
int n,i,j;
for(i=0;i<len*2+1;i++)
{
for(j=0;j<len*2+1;j++)
{
place[i][j]=0;
}
}
seq[0]=9;
place[len][len]=seq[0];
row=len;
colum=len;
for(n=1;n<len;n++)
{
next= seq[n];
switch(next)
{
case 1:
row--;
break;
case 2:
colum++;
break;
case 3:
row++;
break;
case 4:
colum--;
break;
default:
break;
}
if(place[row][colum] >0)
{
return FALSE; //failture to create
}
place[row][colum]=protein[n];
}//for(n=1;n<len;n++)
return TRUE;
}
/***************************************************************/
/* Initialization function: Initializes the values of genes */
/* within the variables bounds. It also initializes (to zero) */
/* all fitness values for each member of the population. It */
/* reads upper and lower bounds of each variable from the */
/* input file `gadata.txt'. It randomly generates values */
/* between these bounds for each gene of each genotype in the */
/* population. The format of the input file `gadata.txt' is */
/* var1_lower_bound var1_upper bound */
/* var2_lower_bound var2_upper bound ... */
/***************************************************************/
void initialize(void)
{
int i, j;
int lbound=1, ubound=4;
for (i = 0; i < POPSIZE; i++)
{
//total=0;
population[i].fitness = 0;
population[i].rfitness = 0;
population[i].cfitness = 0;
for(j=0;j<NVARS;j++)
{
population[i].lower[j] = lbound;
population[i].upper[j]= ubound;
}
while(createSequence(population[i].gene,NVARS) != TRUE)
{
}
}
}
/***********************************************************/
/* Random value generator: Generates a value within bounds */
/***********************************************************/
double randval(double low, double high)
{
double val;
val = randdouble()*(high - low) + low;
return(val);
}
int randvalInt(int low, int high)
{
double val;
val = floor(randdouble()*(high - low+1) + low);
return (int)(val);
}
/***********************************************************/
/* Random a double value between 0~1 */
/***********************************************************/
double randdouble()
{
double val;
val = rand()*1.0 / RAND_MAX ;
return val ;
}
/*************************************************************/
/* Evaluation function: This takes a user defined function. */
/* Each time this is changed, the code has to be recompiled. */
/*************************************************************/
void evaluate(void)
{
int i,j,k,m;
int x=0,y=0;
int resultfit;
for (i = 0; i < POPSIZE; i++)
{
resultfit=0;
if(!checkSequence(population[i].gene, NVARS))
{
while(createSequence(population[i].gene,NVARS)!=TRUE)
{
}
}
for(j=0;j<NVARS;j++)
{
if(protein[j] != H)
{
continue;
}
for(k=j+3;k<NVARS;k++)
{
if(protein[k] != H)
{
continue;
}
x=0;
y=0;
for(m=j+1;m<=k;m++)
{
switch(population[i].gene[m])
{
case 1:
y--;
break;
case 2:
x++;
break;
case 3:
y++;
break;
case 4:
x--;
break;
}//switch
}
if((x==-1 || x==1) && y==0)
{
resultfit--;
}
if((y==-1 || y==1) && x==0)
{
resultfit--;
}
}
}
population[i].fitness = resultfit;
}//for
}
/***************************************************************/
/* Keep_the_best function: This function keeps track of the */
/* best member of the population. Note that the last entry in */
/* the array Population holds a copy of the best individual */
/***************************************************************/
void keep_the_best()
{
int mem;
int i;
cur_best = 0; /* stores the index of the best individual */
for (mem = 0; mem < POPSIZE; mem++)
{
if (population[mem].fitness < population[POPSIZE].fitness)
{
cur_best = mem;
population[POPSIZE].fitness = population[mem].fitness;
}
}
/* once the best member in the population is found, copy the genes */
for (i = 0; i < NVARS; i++)
{
population[POPSIZE].gene[i] = population[cur_best].gene[i];
}
}
/**************************************************************/
/* Main function: Each generation involves selecting the best */
/* members, performing crossover & mutation and then */
/* evaluating the resulting population, until the terminating */
/* condition is satisfied */
/**************************************************************/
int main()
{
int i;
DWORD start, stop;
start = GetTickCount();
srand((unsigned)time(NULL));
if((in = fopen("in.txt","r"))==NULL)
{
printf("file cannot read!");
exit(0);
}
for(i=0;i<NVARS;i++)
{
fscanf(in,"%d",&protein[i]);
}
if ((galog = fopen("log.txt","w+"))==NULL)
{
printf("file cannot write!");
exit(0);
}
initialize(); //load parameters from file and generate the random gene
evaluate(); //calculate the fitness of each one
keep_the_best();//put the best one into the last position of this generation
fprintf(galog,"\n\n Simulation completed\n");
fprintf(galog,"\n Best member: \n");
printf("\n Best member: \n");
for (i = 0; i < NVARS; i++)
{
fprintf (galog," %d",population[POPSIZE].gene[i]);
printf ("%-2d",population[POPSIZE].gene[i]);
}
fprintf(galog,"\n\n Best fitness = %5d",population[POPSIZE].fitness);
printf("\n\n Best fitness = %5d\n",population[POPSIZE].fitness);
fclose(galog);
stop = GetTickCount();
printf("time: %d ms\n", stop - start);
fclose(galog);
return 0;
}
/***************************************************************/
蛋白质折叠模拟遗传算法
本文介绍了一种使用遗传算法模拟蛋白质折叠过程的方法。通过定义特定的参数和规则,该算法能够在二维网格上放置氨基酸序列,模拟蛋白质的折叠过程并评估其稳定性。通过对不同序列进行迭代优化,寻找最佳折叠方式。
373

被折叠的 条评论
为什么被折叠?



