Saturday, May 22, 2010

Does anybody knows how to implement the Magic Square in C++ using Genetic (Evolving) Algorithms?

What will be the population at the begining?


Which is the best function to choose or not a chromo?


What is the best encoding scheme for the algorithm?

Does anybody knows how to implement the Magic Square in C++ using Genetic (Evolving) Algorithms?
Yes I do know. The reason I know is that I did my own homework back in school.
Reply:yes its called a third year dissertaion in computer science...:P
Reply:you can call me on my cell I will tell u.
Reply:#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;process.h%26gt;


#include%26lt;math.h%26gt;


class magic


{


int i,j,t,l,n,m,a[20][20];


public:





magic()


{


cout%26lt;%26lt;"Magic Square\n";


}





void get()


{


cout%26lt;%26lt;"Enter the value of n: ";


cin%26gt;%26gt;n;


cout%26lt;%26lt;"\n";


}





void square()


{


if(n%2==0)


{


cout%26lt;%26lt;"\n Only odd numbers are used";


getch();


exit(0);


}


else


{


for(i=0;i%26lt;n;i++)


for(j=0;j%26lt;n;j++)


a[i][j]=0;


m=1;


i=0;


j=n/2;


t=n*n;


a[i][j]=m;


while(m%26lt;=t)


{


m++;


i--;


j--;


if(i%26lt;0 %26amp;%26amp; j%26lt;0)


{


i+=2;


j++;


}


else if(i%26lt;0 %26amp;%26amp; j%26gt;=0)


i=n-1;


else if(i%26gt;=0 %26amp;%26amp; j%26lt;0)


j=n-1;


if(a[i][j]==0)


a[i][j]=m;


else


{


j++;


i+=2;


a[i][j]=m;


}


}


}


}





void disp()


{


for(i=0;i%26lt;n;i++)


{


for(j=0;j%26lt;n;j++)


cout%26lt;%26lt;a[i][j]%26lt;%26lt;"\t";


cout%26lt;%26lt;endl;


}


for(i=0;i%26lt;n;i++)


{


l=0;


for(j=0;j%26lt;n;j++)


{


l=l+a[i][j];


}


}


cout%26lt;%26lt;"\n The addition of each rows, columns and diagonals are equal to: "%26lt;%26lt;l%26lt;%26lt;"\n";


getch();


}


};





void main()


{


char ch;


a:


clrscr();


magic m1;


m1.get();


m1.square();


m1.disp();


cout%26lt;%26lt;"\n Do u want 2 continue: (Y/N)";


cin%26gt;%26gt;ch;


if(ch=='y' || ch=='Y')


goto a;


else


exit(0);


getch();


}


No comments:

Post a Comment