遗传算法解八皇后问题

来源:互联网 发布:javascript艺术编程pdf 编辑:程序博客网 时间:2024/04/29 05:09

这个问题解得不算漂亮,供大家参考改进吧。

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <time.h>  
  5. #define N 16//父母产生的代数  
  6.  
  7. char *crossover(char* father,char* mother)//交叉函数,返回值为交叉后的子代,具体原理不在这里阐述,在设计文档中有详细说明  
  8. {  
  9.     int i,j,k;  
  10.     char son[16],rnd[16],fath[16],moth[16];  
  11.     for ( i = 0; i < 8; i++)//生成交叉算子  
  12.     {  
  13.         rnd[i]=rand()%2+'0';   
  14.     }  
  15.     strcpy(fath,father);  
  16.     strcpy(moth,mother);  
  17.  
  18.     for ( i = 0; i < 8; i ++)//根据交叉算子对父母进行交叉  
  19.     {  
  20.         if (rnd[i] == '1')  
  21.         {  
  22.             for ( j = 0; j < 8; j++)  
  23.             {  
  24.                 if (fath[j] != '0')  
  25.                 {  
  26.                     break;  
  27.                 }  
  28.             }  
  29.             son[i]=fath[j];  
  30.             for ( k = 0; k < 8; k++)  
  31.             {  
  32.                 if (moth[k] == fath[j])  
  33.                 {  
  34.                     moth[k]='0';  
  35.                     break;  
  36.                 }  
  37.             }  
  38.             fath[j]='0';  
  39.         }  
  40.         else 
  41.         {  
  42.             for ( j = 0; j < 8; j++)  
  43.             {  
  44.                 if (moth[j] != '0')  
  45.                 {  
  46.                     break;  
  47.                 }  
  48.             }  
  49.             son[i]=moth[j];  
  50.             for ( k = 0; k < 8; k++)  
  51.             {  
  52.                 if (fath[k] == moth[j])  
  53.                 {  
  54.                     fath[k]='0';  
  55.                     break;  
  56.                 }  
  57.             }  
  58.             moth[j]='0';  
  59.         }  
  60.     }  
  61.     son[8]=0;  
  62.     return son;  
  63. }  
  64.  
  65. int check(char result[])//检查有多少在一条线上的皇后  
  66. {  
  67.     int i,j,k=0;  
  68.     for ( i = 0; i < 8; i++)  
  69.     {  
  70.         for ( j = i+1; j < 8; j++)  
  71.         {  
  72.             if (abs(result[i]-result[j]) == j-i)  
  73.             {  
  74.                 k++;  
  75.             }  
  76.         }  
  77.     }  
  78.     return k;  
  79. }  
  80.  
  81. int main()  
  82. {  
  83.     int i,j,k,p,q,min,max=0,flag,sign,boundary=1000,s[N];  
  84.     char father[16]="12345678",mother[16]="87654321";  
  85.     char son[N][16],temp[16],result[100][16];  
  86.     srand((unsigned)time(NULL));  
  87.     for ( i = 0; i < 8; i++)//生成父母串  
  88.     {  
  89.         j=rand()%8;  
  90.         k=father[j];  
  91.         father[j]=father[i];  
  92.         father[i]=k;  
  93.         j=rand()%8;  
  94.         k=mother[j];  
  95.         mother[j]=mother[i];  
  96.         mother[i]=k;  
  97.     }  
  98.     sign=0;  
  99.     for ( i = 0; i <= boundary; i++)//控制繁殖代数  
  100.     {  
  101.         if (i == boundary&&sign == 1)//动态控制,如果发现不够继续繁殖3000代  
  102.         {  
  103.             boundary+=3000;  
  104.             sign=0;  
  105.         }  
  106.         for ( j = 0; j < N; j++)//交叉和变异,对子代评估  
  107.         {  
  108.             strcpy(son[j],crossover(mother,father));  
  109.             p=rand()%8;  
  110.             q=rand()%8;  
  111.             k=son[j][p];  
  112.             son[j][p]=son[j][q];  
  113.             son[j][q]=k;  
  114.             s[j]=check(son[j]);  
  115.         }  
  116.         for ( j = 0; j < N; j++)//对评估结果排序,将已经符合条件的挑出  
  117.         {  
  118.             for ( k = j, min = j; k < N; k++)  
  119.             {  
  120.                 if (s[min] > s[k])  
  121.                 {  
  122.                     min=k;  
  123.                 }  
  124.             }  
  125.             k=s[j];  
  126.             s[j]=s[min];  
  127.             s[min]=k;  
  128.             strcpy(temp,son[j]);  
  129.             strcpy(son[j],son[min]);  
  130.             strcpy(son[min],temp);  
  131.             if (s[j] == 0)  
  132.             {  
  133.                 flag=0;  
  134.                 for ( k = 0; k < max; k++)  
  135.                 {  
  136.                     if (!strcmp(son[j],result[k]))  
  137.                     {  
  138.                         flag=1;  
  139.                     }  
  140.                 }  
  141.                 if (!flag)  
  142.                 {  
  143.                     sign=1;  
  144.                     strcpy(result[max],son[j]);//符合的放入结果数组  
  145.                     max++;//已经产生的符合条件的子代  
  146.                     printf("%02d.%s\n",max,son[j]);  
  147.                 }  
  148.             }  
  149.         }  
  150.         strcpy(father,son[0]);//选择两个最优的成为下一代的父母  
  151.         strcpy(mother,son[1]);  
  152.     }  
  153.     printf("%d\n",boundary);//最终繁殖代数  
  154.     system("pause");  
  155.     return 0;  

 

本文出自 “天才鸟蛋” 博客,请务必保留此出处http://curley.blog.51cto.com/1627940/497963

0 0
原创粉丝点击