N-queens with GA
#include
#include
#include

#define VEZIR 15
#define population 100
#define Mutation 2

int gens[population][VEZIR+1];
int tahta[VEZIR][VEZIR];
int COverMat[population][VEZIR];




void binMat(void)
{
int randomCrossOver;
srand((unsigned) time(NULL));
for (int i=0;i<=population-1;i++)
for (int a=0;a {
randomCrossOver=rand();
COverMat[a][i]=randomCrossOver%2;
}

}
void tahtayap(int x);
void hesapla(void)
{
for (int s=0;s int deger=0;
int m=0,n=0;
int i=0,j=0;
tahtayap(s);
for(i=0;i
j=gens[s][i];
m=i+1;
n=j-1;
while(m=0)
{
if(tahta[m][n]==1)
{
deger++;
}
m++;
n--;
}

m=i+1;
n=j+1;
while(m {
if(tahta[m][n]==1)
{
deger++;
}
m++;
n++;
}

m=i-1;
n=j-1;
while(m>=0 && n>=0)
{
if(tahta[m][n]==1)
{
deger++;
}
m--;
n--;
}

m=i-1;
n=j+1;
while(m>=0 && n {
if(tahta[m][n]==1)
{
deger++;
}
m--;
n++;
}

}
gens[s][VEZIR]=deger;
}


}

void iyilestirme(void)
{
for (int t=0;t int y=1;
while (y!=0) {
int b=0;
for (int i=0;i for (int j=i+1;j if (gens[t][j]==gens[t][i]) {
gens[t][j]=rand()%VEZIR;
b=b+1;
}
}
}
if (b<1) y=0;
}
}
}



void production(void) {
int i;
for (int j=0;j int ygen[VEZIR]={0};
int n1=0,n2=0,n3=0,n4=0;


binMat();
for (i=0;i if (COverMat[j][i]==0) { ygen[i]=gens[j][n1++]; }
if (COverMat[j][i]==1) { ygen[i]=gens[(population/2)+j][n2++]; }
}



hesapla();


n3=gens[j][VEZIR];
n4=gens[(population/2)+j][VEZIR];


if (n4>=n3) {
for (i=0;i gens[(population/2)+j][i]=ygen[i];}
}
if (n3>n4) {
for (i=0;i gens[j][i]=ygen[i];}
}

}
}



void tahtayaz(void) {
printf("\n");
if (VEZIR%2==0) {
int sayac=0,i=0;
printf("\n%c",201);
for (i=0;i<(VEZIR);i++) {
printf("%c",205);
}
printf("%c",187);
for (int k=0;k printf("\n%c",186);
for (int j=0;j sayac++;
if (tahta[k][j]==1) {printf("%c",254);}
if ((tahta[k][j]==0) && (sayac%2==0)) {
if (k%2==0) {printf("%c",176);}
if (k%2==1) {printf("%c",178);}
}
if ((tahta[k][j]==0) && (sayac%2==1)) {
if (k%2==0) {printf("%c",178);}
if (k%2==1) {printf("%c",176);}
}

}
printf("%c",186);

}
printf("\n%c",200);
for (i=0;i<(VEZIR);i++) {
printf("%c",205);
}
printf("%c",188);

}
if (VEZIR%2==1) {
int sayac2=0,i2=0;
printf("\n%c",201);
for (i2=0;i2<(VEZIR);i2++) {
printf("%c",205);
}
printf("%c",187);
for (int k2=0;k2 printf("\n%c",186);
for (int j2=0;j2 sayac2++;
if (tahta[k2][j2]==1) {printf("%c",254);}
if ((tahta[k2][j2]==0) && (sayac2%2==0)) {printf("%c",176);}

if ((tahta[k2][j2]==0) && (sayac2%2==1)) {printf("%c",178);}
}
printf("%c",186);
}
printf("\n%c",200);
for (i2=0;i2<(VEZIR);i2++) {
printf("%c",205);
}
printf("%c",188);
}

}


void crossover(void) {
srand((unsigned) time(NULL));
int x,y;
for (int j=0;j int dizi[VEZIR]={0};
x=rand()%(VEZIR-2);
y=rand()%(VEZIR-x);
for (int i=x;i dizi[i]=gens[j][i];
gens[j][i]=gens[j+(population/2)][i];
gens[j+(population/2)][i]=dizi[i];
}
}
}

void tahtayap(int x) {
for(int i=0;i for (int j=0;j tahta[i][j]=0;
}
}
for (int k=0;k tahta[k][gens[x][k]]=1;
}
}



void mutation2(void) {
int x,y,g1,t;
srand((unsigned) time(NULL));
for (int q=0;q
t=rand()%population;

x=rand()%VEZIR;
while((y=rand()%VEZIR)==x);

g1=gens[t][x];
gens[t][x]=gens[t][y];
gens[t][y]=g1;


}
}


void init(void) {

srand((unsigned) time(NULL));
for(int j=0;j for (int i=0;i gens[j][i]=rand()%VEZIR;
}
}
for (int s=0;s int y=1;
while (y!=0) {
int b=0;
for (int i=0;i for (int j=i+1;j if (gens[s][j]==gens[s][i]) {
gens[s][j]=rand()%VEZIR;
b=b+1;
}
}
}
if (b<1) y=0;
}

}
}

int main (void) {

int i,j=1,s=0,t,k,l;
init();
hesapla();
printf("\n Initial Population ");
for (k=0;k printf("\n");
for (l=0;l printf("%d ",gens[k][l]);
}
}



for (i=0;i if (gens[i][VEZIR]==0) {
printf("\nGeneration %d\n",s);
printf("\nGoal found at Chromosome %d, After Production \n",i);
for (t=0;t printf("%d_",gens[i][t]);
}
tahtayap(i);
tahtayaz();
j=0;
break;

}
}



while (j!=0) {


production();
iyilestirme();
hesapla();
if (j!=0) {
printf("\n Population After Reproduction ");
for (k=0;k printf("\n");
for (l=0;l printf("%d ",gens[k][l]);
}
}
}
else break;
for (i=0;i if (gens[i][VEZIR]==0) {
printf("\nGeneration %d\n",s);
printf("\nGoal found at Chromosome %d, After Reproduction\n",i);

for (t=0;t printf("%d_",gens[i][t]);
}
tahtayap(i);
tahtayaz();
j=0;
break;
}
}

mutation2();
iyilestirme();
hesapla();
if (j!=0) {
printf("\n Population After Mutation ");
for (k=0;k printf("\n");
for (l=0;l printf("%d ",gens[k][l]);
}
}
}
else break;

for (i=0;i if (gens[i][VEZIR]==0) {
printf("\nGeneration %d\n",s);
printf("\nGoal found at Chromosome %d, After Mutation \n",i);

for (t=0;t printf("%d_",gens[i][t]);
}
tahtayap(i);
tahtayaz();
j=0;
break;
}
}


crossover();
iyilestirme();
hesapla();
if (j!=0) {
printf("\n Population After CrossOver");
for (k=0;k printf("\n");
for (l=0;l printf("%d ",gens[k][l]);
}
}



for (i=0;i if (gens[i][VEZIR]==0) {
printf("\nGeneration %d\n",s);
printf("\nGoal found at Chromosome %d, After CrossOver \n",i);

for (t=0;t printf("%d_",gens[i][t]);
}
tahtayap(i);
tahtayaz();
j=0;
break;
}
}

}


s++;



}
scanf("%d",&l);

return 0;

}
Eklenen dosya: n-queens.rar