声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 3517|回复: 7

[人工智能] [原创]用VC++编的遗传算法

[复制链接]
发表于 2006-4-19 17:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
这是用实验函数--Ackley函数来检验遗传算法的优越性,我自己编写的,其中有点小毛病,就是种群数在20个左右效果最好,一旦增加种群,结果就不大对劲了。忙了好多天,也没调出来,还请各位大侠指点喽

  1. #include"iostream.h"
  2. #include<stdlib.h>
  3. #include <stdio.h>
  4. #include<math.h>

  5. #define pi 3.1415926

  6. class GA
  7. {
  8. private:
  9.   double N_random;
  10.         int N_gene[501][5][6];

  11.   double N_integer[501][5];
  12.   double N_integer1[501][5];
  13.   double t_index[501][5];
  14.   double t_index1[501][5];
  15.   double t_down[5],t_up[5];

  16.   double Energy[501];
  17.   double Energy1[501];

  18.     int m_i_th,m_i_th1;                           /* ?üá?×?′ó?μ?ù?úDD  */
  19. double Sum_energy;
  20.   /*float max;*/
  21.   double Fitness[501];
  22.   /*float Sum_fitness;*/
  23.   double q_fitness[501];
  24.   double q_random[501];
  25.     int i_qmax;
  26.     int N_parent[501][21];
  27.   
  28.     int N_newgene[501][21];
  29.     int N_supergene[501][21];             /* 3???è?é?ì?*/
  30.         int N_addgene[501][21];             /*??í¨?ùòò+213??ùòò*/

  31.         int Bottle;
  32.         int t_bottle;                       /*??Dòê±?D??±?á? */
  33.         int i_ch,j_ch;                      /* */

  34.         int ic,is,nlose;
  35.         int nochose[501];
  36.   double P_cr;

  37.         int N_select[501][21];
  38.     int N_crosspoint;
  39.     int N_offspringc[501][21];
  40.    

  41.         int jm,im,N_mutate;
  42.   double P_mp[501][21];
  43.     int judge;
  44.     int N_renewgene[501][5][6];

  45.         int i_length;

  46. public:
  47.           GA();
  48.    
  49. void N_changetointeger(void);
  50. void N_changetointeger1(void);
  51. void t_changetoindex(void);     
  52. void t_changetoindex1(void);
  53.              N_3to2();
  54.       E_nergy();
  55.       E_nergy1();
  56.           Makeorder();                   
  57.           Addgene();
  58.       Makeorder1();
  59.           Chose_super();
  60.           F_itness();
  61.      
  62.       crossover();
  63.       mutate();
  64.       N_2to3();
  65.       N_add2to3();
  66.           F_irst();
  67.           C_ircle();

  68. };

  69. GA::GA()
  70. {
  71.         int a;
  72.         int i,j,k;
  73.        
  74.         cout<<"ê?è???èoêy i_length=";
  75.     cin>>i_length;
  76. /*        cout<<"N_gene[][][]"<<endl;*/

  77.         for(i=1;i<=i_length;i++)
  78.     for(j=1;j<=4;j++)
  79.     for(k=1;k<=5;k++)
  80.         {   
  81.                 a=rand()%10000/1000;     
  82.                 N_gene[i][j][k]=a;      /* 2úéú?-ê????úêy×÷?a?ùòò  */
  83.                        
  84.         /*        cout<<N_gene[i][j][k]<<" " ;     /* ê?3??ùòò  */  
  85.         /*        if(j==4&&k==5)
  86.         cout<<endl;
  87.     */
  88.         }
  89.    
  90.          t_down[1]=-3.0,t_down[2]=4.1,t_down[3]=0,t_down[4]=0;
  91.          t_up[1]=12.1,t_up[2]=5.8,t_up[3]=0,t_up[4]=0;

  92.          
  93. }

  94. void GA::N_changetointeger(void)
  95. {   int i,j,k;
  96. /*        cout<<"N_changetointeger"<<endl;        */
  97. /*        judge=0;
  98.         for(i=1;i<i_length;i++)
  99.     for(j=1;j<=4;j++)
  100.     for(k=1;k<=5;k++)                        
  101.                 {
  102.                         if(N_gene[i][j][k]!=N_gene[i+1][j][k])
  103.                         judge=1;
  104.                        
  105.                 }
  106. */
  107.     for(i=1;i<=i_length;i++)
  108.     for(j=1;j<=4;j++)
  109.         {
  110.                 N_integer[i][j]=0;
  111.        for(k=1;k<=5;k++)                     /* ???-ê??ùòò×a?ˉ?a??êy  */

  112.            {   
  113.                 N_integer[i][j]=N_integer[i][j]+N_gene[i][j][k]*pow(10,(5-k));
  114.            }  
  115.           /* cout<<N_integer[i][j]<<" ";            /* ????êyê?3?  */
  116. /**/  /*        if(j==4)
  117.         cout<<endl;
  118.       */
  119.         }   
  120. }
  121. void GA::N_changetointeger1(void)
  122. {   int i,j,k;

  123.     for(i=1;i<=1.2*i_length;i++)
  124.     for(j=1;j<=4;j++)
  125.         {
  126.                 N_integer1[i][j]=0;
  127.        for(k=1;k<=5;k++)                     /* ???-ê??ùòò×a?ˉ?a??êy  */

  128.            {   
  129.                 N_integer1[i][j]=N_integer1[i][j]+N_renewgene[i][j][k]*pow(10,(5-k));
  130.            }  
  131.           
  132.         }   
  133. }

  134. void GA::t_changetoindex(void)
  135. {   int i,j;
  136. /*        cout<<"t_changetoindex"<<endl;  
  137. */        for(i=1;i<=i_length;i++)
  138.     for(j=1;j<=4;j++)                          /* ????êy×a?ˉ?aóDìa??o?ò?μ?êy  */   
  139.         {
  140.                 t_index[i][j]=t_down[j]+N_integer[i][j]*
  141.                                   ((t_up[j]-t_down[j])/(pow(10,5)-1));
  142.                
  143.         /*        cout<<t_index[i][j]<<" ";           
  144.                 if(j==4)
  145.         cout<<endl;
  146.     */
  147.         }      
  148. }
  149. void GA::t_changetoindex1(void)
  150. {   int i,j;

  151.         for(i=1;i<=1.2*i_length;i++)
  152.     for(j=1;j<=4;j++)                          /* ????êy×a?ˉ?aóDìa??o?ò?μ?êy  */   
  153.         {
  154.                 t_index1[i][j]=t_down[j]+N_integer1[i][j]*
  155.                           ((t_up[j]-t_down[j])/(pow(10,5)-1));       
  156.         }      
  157. }

  158. GA::N_3to2()                                   /* èy??êy×é×a?ˉ?a?t??êy×é*/
  159. {   
  160. /*        cout<<endl<<"3to2(èy??êy×é×a?ˉ?a?t??êy×é)"<<endl;  */
  161.     int i,j,k;
  162.         int  m=1;
  163.                            
  164.         for(i=1;i<=i_length;i++)
  165.     for(j=1;j<=4;j++)
  166.     for(k=1;k<=5;k++)
  167.         {
  168.                 if(m<=20)
  169.                 {
  170.                         N_newgene[i][m]=N_gene[i][j][k];          /* èy??êy×é×a?ˉ?a?t??êy×é*/
  171.                         m++;
  172.                         if(m>20)
  173.                         m=1;
  174.                 }
  175.          
  176.         }
  177. /*    cout<<endl<<"N_newgene[][]"<<endl;         /*ê?3?D?μ??t??êy×é*/

  178. /*        for(i=1;i<=i_length;i++)
  179.     for(j=1;j<=20;j++)
  180.         {
  181.                 cout<<N_newgene[i][j]<<" ";               
  182.        if(j==20)
  183.         cout<<endl;
  184.                
  185.         }
  186. */
  187. }

  188. GA::E_nergy()                           /* ?üá?oˉêy  */
  189. {                              
  190. cout<<endl<<"E_nergy()"<<endl;
  191. int i,j=1;
  192. int m;
  193. int max,Fo;
  194.     max=0;
  195.         for(i=1;i<=i_length;i++)
  196.    
  197.         {   Fo=21.5+t_index[i][j]*sin(4*pi*t_index[i][j])
  198.                    +t_index[i][j+1]*sin(20*pi*t_index[i][j+1]);
  199.            
  200.      /*   Energy[i]=Fo;
  201.         
  202.                 cout<<"Energy="<<Energy[i]<<"  ";
  203.               if(i%5==0)
  204.                           cout<<endl;
  205.                                               /* ê?3?×?′ó?üá?oˉêy?μ  */
  206.            if(Fo>=max)
  207.            {
  208.                    max=Fo;
  209.                    m_i_th=i;
  210.            }
  211.    
  212.         }
  213.    cout<<"±?′?μü′ú×?′ó?üá??ù?úμ?DD£o"<<m_i_th<<endl;
  214.    cout<<"Max Energy="<<max<<"  ";
  215.   /* cout<<"Fitness[i]="<<Fitness[m_i_th]<<"  ";*/
  216. }


  217. GA::Makeorder()
  218. {   cout<<endl;
  219.        
  220.         int i,j;
  221.    
  222.        
  223.         for(j=1;j<=i_length-1;j++)              /*  ???á1???DD??Dò*/
  224.     for(i=1;i<=i_length-j;i++)
  225.         {                                       /* ò?±?ìáè?3?à′3???è?é?ì?*/
  226.                 if(Energy[i]<Energy[i+1])
  227.                 {
  228.                         t_bottle=Energy[i+1],Energy[i+1]=Energy[i],
  229.                                 Energy[i]=t_bottle;
  230.             
  231.                 /*        for(i_ch=1;i_ch<i_length;i_ch++)*/
  232.             for(j_ch=1;j_ch<=20;j_ch++)              
  233.                         {
  234.                                 Bottle=N_newgene[i+1][j_ch],
  235.                                 N_newgene[i+1][j_ch]=N_newgene[i][j_ch],
  236.                                 N_newgene[i][j_ch]=Bottle;
  237.                         }
  238.                 }
  239.         }
  240. /*  cout<<"??Dòoóμ?Energy:"<<endl;
  241.         for(i=1;i<=i_length;i++)
  242.            {
  243.                 cout<<"Energy="<<Energy[i]<<"  ";
  244.            if(i%5==0)
  245.              cout<<endl;
  246.         }
  247. cout<<"??Dòoóμ?N_newgene:"<<endl;
  248.         for(i=1;i<=i_length;i++)
  249.     for(j=1;j<=20;j++)
  250.         {
  251.                 cout<<N_newgene[i][j]<<" ";               
  252.        if(j==20)
  253.         cout<<endl;
  254.                
  255.         }
  256. /*        for(i_ch=1;i_ch<=i_length;i_ch++)         /* ê?3???Dòoóμ??ùòò*//*
  257.    
  258.         {   cout<<"μú"<<i_ch<<"DD";         
  259.                 for(j_ch=1;j_ch<=20;j_ch++)
  260.                 {
  261.                   cout<<N_newgene[i_ch][j_ch]<<"  ";
  262.            if(j_ch==20)
  263.              cout<<endl;
  264.                 }
  265.         }
  266. */
  267. }

  268. GA::Addgene()
  269. {
  270.         int i,j;
  271.         int i_super=1;
  272.    
  273.         for(i=1;i<=i_length;i++)
  274.     for(j=1;j<=20;j++)
  275.         {
  276.                 N_addgene[i][j]=N_newgene[i][j];      /*??D?μ?N_newgene */
  277.         }                                         /*·??úN_addgeneà? */

  278.         for(i=i_length+1;i<=1.2*i_length;i++)
  279.     {
  280.                 for(j=1;j<=20;j++)
  281.                                                       /* ??N_supergene   */
  282.                 {
  283.                         N_addgene[i][j]=N_supergene[i_super][j];    /* 21·??úN_addgeneà?   */
  284.                 }
  285.      i_super++;
  286.         }
  287.    
  288.   

  289. }

  290. GA::E_nergy1()                           /* 213?oóμ??üá?oˉêy  */
  291. {      cout<<endl;                       
  292.    int i,j=1;
  293.    
  294.    int F1;
  295.     int max=0;
  296.         for(i=1;i<=1.2*i_length;i++)
  297.    
  298.         {   F1=21.5+t_index1[i][j]*sin(4*pi*t_index1[i][j])
  299.                    +t_index1[i][j+1]*sin(20*pi*t_index1[i][j+1]);
  300.            
  301.         Energy1[i]=F1;
  302.         /*        cout<<"Energy1="<<Energy1[i]<<"  ";
  303.               if(i%5==0)
  304.                           cout<<endl;
  305.         */       
  306.                 if(F1>=max)
  307.            {
  308.                    max=F1;
  309.                    m_i_th1=i;
  310.            }
  311.                           
  312.         }

  313.         /* cout<<"Energy1="<<max<<"  ";*/


  314. }

  315. GA::Makeorder1()
  316. {
  317.         cout<<endl;
  318.        
  319.         int i,j;
  320.            
  321.         for(j=1;j<=1.2*i_length-1;j++)              /*  ???á1???DD??Dò*/
  322.     for(i=1;i<=1.2*i_length-j;i++)
  323.         {                                       /* ò?±?ìáè?3?à′3???è?é?ì?*/
  324.                 if(Energy1[i]<Energy1[i+1])
  325.                 {
  326.                         t_bottle=Energy1[i+1],Energy1[i+1]=Energy1[i],
  327.                                 Energy1[i]=t_bottle;
  328.             
  329.                         /*for(i_ch=1;i_ch<1.2*i_length;i_ch++)*/
  330.             for(j_ch=1;j_ch<=20;j_ch++)              
  331.                         {
  332.                                 Bottle=N_addgene[i+1][j_ch],
  333.                                 N_addgene[i+1][j_ch]=N_addgene[i][j_ch],
  334.                                 N_addgene[i][j_ch]=Bottle;
  335.                         }
  336.                 }
  337.         }
  338.   
  339. /*    cout<<endl;
  340.         cout<<"??Dòoóμ?N_addgene:"<<endl;
  341.         for(i=1;i<=1.2*i_length;i++)
  342.     for(j=1;j<=20;j++)
  343.         {
  344.                 cout<<N_addgene[i][j]<<" ";               
  345.        if(j==20)
  346.        cout<<endl;
  347.         }
  348. */
  349.     for(i_ch=1;i_ch<i_length;i_ch++)       /*è??°??1--i_lengthμ??ùòò */
  350.     for(j_ch=1;j_ch<=20;j_ch++)            /*·??úN_newgeneà? */
  351.         {
  352.                 N_newgene[i_ch][j_ch]=N_addgene[i_ch][j_ch];
  353.         }
  354.        
  355. /*        cout<<"??Dòoóμ?N_newgene:"<<endl;
  356.         for(i=1;i<=i_length;i++)
  357.     for(j=1;j<=20;j++)
  358.         {
  359.                 cout<<N_newgene[i][j]<<" ";               
  360.        if(j==20)
  361.        cout<<endl;
  362.         }
  363.         */
  364. }


  365. GA::Chose_super()
  366. {   cout<<"N_supergene"<<endl;
  367.         int i,j;
  368.         for(i=1;i<=0.2*i_length;i++)
  369.     for(j=1;j<=20;j++)
  370.         {
  371.                 N_supergene[i][j]=N_newgene[i][j];         /* 3???è?é?ì?*/
  372.         /*        cout<<N_supergene[i][j]<<" ";
  373.                 if(j==20)
  374.                         cout<<endl;*/
  375.         }
  376.    
  377.         for(i=1;i<=0.2*i_length;i++)
  378.     for(j=1;j<=20;j++)
  379.         {
  380.                 N_newgene[i][j]=rand()%1000/100;         /*213??ùòò*/

  381.         }
  382.     cout<<"N_newgene&&N_offspringc:"<<endl;
  383.     for(i=1;i<=i_length;i++)
  384.     for(j=1;j<=20;j++)
  385.         {
  386.                 N_offspringc[i][j]=N_newgene[i][j];    /* ?aá???DDN_2to3×a?ˉ */
  387. /*          cout<<N_offspringc[i][j]<<" ";
  388.                 if(j==20)
  389.                         cout<<endl;*/
  390.         }
  391.   

  392. }


  393. GA::F_itness()                            /* êêó|?èoˉêy  */
  394. {   int i,j;

  395.     int i_parent;
  396. cout<<endl<<"F_itness()"<<endl;
  397.         Sum_energy=0;
  398.         for(i=1;i<=i_length;i++)
  399.         {   /*cout<<"Energy="<<Energy[i]<<"  ";
  400.               if(i%5==0)
  401.                           cout<<endl;
  402.         */
  403.                 Sum_energy=Sum_energy+fabs(Energy[i]);
  404.         }
  405. /*        cout<<endl;*/
  406. /*        cout<<endl<<"Sum_energy="<<Sum_energy<<"  ";  */
  407.         for(i=1;i<=i_length;i++)
  408.         {   Fitness[i]=(Energy[i])/(Sum_energy);
  409.        
  410.         /*        cout<<"Fitness="<<Fitness[i]<<"  ";*/
  411.     }

  412.    /* Sum_fitness=0;                                
  413. /*        for(i=1;i<=i_length;i++)
  414.         {   
  415.                 Sum_fitness=Sum_fitness+fabs(Fitness[i]);
  416.         }
  417.         cout<<"Sum_fitness="<<Sum_fitness<<"  ";
  418. */
  419. /*        cout<<endl;*/
  420.     q_fitness[0]=0;
  421.         for(i=1;i<=i_length;i++)
  422.         {
  423.                 q_fitness[i]=q_fitness[i-1]+Fitness[i];      /* ????à??y???ê*/
  424.           /* cout<<"q_fitness="<<q_fitness[i]<<endl;*/
  425.         }
  426.        
  427.         for(i=1;i<=i_length;i++)
  428.         {
  429.                 q_random[i]=rand()%100/100.0;
  430.         
  431.         }
  432.     for(i=1;i<=i_length;i++)
  433.         {
  434.                 /* cout<<"q_random="<<q_random[i]<<endl;*/
  435.         }

  436.     i_parent=1;
  437.         for(i=1;i<=i_length;i++)
  438.     for(i_qmax=1;i_qmax<=i_length;i_qmax++)
  439.         {                                                  /* ??×a????*/
  440.                 if(q_random[i]<=q_fitness[i_qmax])
  441.                 {   /*cout<<"q_random="<<q_random[i]<<endl;*/
  442.                         for(j=1;j<=20;j++)
  443.                         {
  444.                                 N_parent[i_parent][j]=N_newgene[i][j];
  445.                         }
  446.                  i_parent++;
  447.                        
  448.                 }

  449.         }

  450. }



  451. GA::crossover()                     /*??2?*/
  452. { /* cout<<endl;*/
  453.    
  454. /*        cout<<endl<<"crossover()"<<endl;*/
  455.         int a;
  456.        
  457.         int i=1,j,m;
  458.         ic=1,nlose=1;
  459.         is=1;

  460.         while(i<=i_length)                             /* ?ú4????èo?D???????? */
  461.         {
  462.                 N_random=rand()%10001/10000.0;    /* ?ú[0,1]????2úéú???úêy */
  463.                 P_cr=N_random;                     

  464.                 if(P_cr<0.8)                         /* ò?0.7μ????ê???????? */
  465.                 {  /* cout<<"μú"<<i<<"DD±?????"<<endl;
  466.                         cout<<"P_cr="<<P_cr<<endl;*/
  467.                         for(j=1;j<=20;j++)
  468.                         {                            /* ??·?o?ì??tμ?????·??úD?μ?êy×é */
  469.                                 N_select[ic][j]=N_parent[i][j];   
  470.                             
  471.                         /*        cout<<N_select[ic][j]<<" ";     
  472.                         if(j==20)                    /* ??·?o?ì??tμ?????ê?3? */
  473.             /*    cout<<endl;          */
  474.                         }                           
  475.             ic++;
  476.                 }       

  477.                 else
  478.                 {   
  479.                         nochose[nlose]=i;    /* ??2?·?o?ì??tμ?????μ?DDo?·??úD?μ?êy×é */  
  480.                 nlose++;
  481.                 /*        cout<<"μú"<<i<<"DD??±?????"<<endl;  */
  482.                 }
  483.                  
  484.                 i++;
  485.           }
  486.     /*  cout<<"i="<<i<<endl; */
  487.   /*  cout<<"12??2?μ?DDêy ic="<<ic-1<<endl;*/
  488.        

  489.     a=rand()%1900/100;                          /* ?ú[0,19]????2úéú???úêy */
  490.         N_crosspoint=a;                             /* ??μ?????£¨D?óú20£? */
  491.     cout<<"??2?μ? N_crosspoint="<<N_crosspoint<<endl;

  492.         for(i=1;i<ic-1;i++)
  493.         for(j=N_crosspoint;j<=20;j++)
  494.         {
  495.         N_offspringc[i+1][j]= N_select[i][j];
  496.         
  497.                 N_offspringc[i][j]= N_select[i+1][j];   
  498.                                                     /*?ú??2?μ?óò±?μ?è?é?ì???2?*/
  499.         }

  500.     for(i=1;i<ic-1;i++)
  501.         for(j=1;j<N_crosspoint;j++)
  502.         {
  503.         N_offspringc[i][j]= N_select[i][j];      /*?ú??2?μ?×ó±?μ?è?é?ì?±£á?*/
  504.         
  505.                 N_offspringc[i+1][j]= N_select[i+1][j];   
  506.                                                     
  507.         }
  508.   /*  cout<<"??2?oóμ?è?é?ì?"<<endl;
  509.         for(i=1;i<=ic-1;i++)
  510.     for(j=1;j<=20;j++)
  511.         {
  512.                 cout<<N_offspringc[i][j]<<" ";           /*ê?3?????oóμ?è?é?ì?*/
  513. /*           if(j==20)
  514.            cout<<endl;
  515.         }
  516. */
  517.      j=1;
  518.     for(i=ic;i<=i_length;i++)  
  519.         {
  520.                 for(m=1;m<=20;m++)                     /*??????2?μ?????μ?21·??úD?μ?êy×é */
  521.                 {   
  522.                         N_offspringc[i][m]=N_parent[nochose[j]][m];
  523.                 }
  524.                 j++;
  525.         }
  526. /*        cout<<endl<<"N_offspringc[][]="<<endl;
  527.                
  528.         for(i=1;i<=i_length;i++)
  529.     for(j=1;j<=20;j++)
  530.                 {
  531.                         cout<<N_offspringc[i][j]<<" ";
  532.                         if(j==20)
  533.                           cout<<endl;
  534.                 }
  535. */
  536. }


  537. GA::mutate()
  538. {        
  539. /*        cout<<endl;*/
  540. /*        cout<<"mutate()"<<endl;*/

  541.         int a,m;
  542.         m=0;
  543. /*        cout<<"±?òìμ??ùòòêy??£o"<<endl; */
  544.         for(im=1;im<=i_length;im++)
  545.     for(jm=1;jm<=20;jm++)
  546.         {   
  547.                 N_random=rand()%10001/10000.0;
  548.                 P_mp[im][jm]=N_random;     /*2úéú[0,1]?????ù?è·?2?μ????úêyDòáD*/
  549.         
  550.                 if(P_mp[im][jm]<0.15)        /*ò?20%μ????ê·¢éú±?òì*/
  551.                 {   m++;
  552.                 /*        cout<<"μú"<<im<<"DD ";
  553.             cout<<"μú"<<jm<<"áD?ùòòòa±?ì??? ";  
  554.         */
  555.         /**/        a=rand()%800/100;
  556.                         N_mutate=a;     /*2úéú[0,8]????·?2?μ????úêy×÷?aì???òò×ó*/
  557.            /* cout<<"ì????ùòò£o"<<a<<endl;
  558.         /**/       
  559.                         if(N_mutate==N_offspringc[im][jm])
  560.                         {
  561.                                 N_mutate++;         /*è?1?ì???òò×óoí?-à′?àí?£??ò±??ˉ*/
  562.                         }
  563.            
  564.                         N_offspringc[im][jm]=N_mutate;  /*ì???òò×óì???μ??-à′òò×ó*/
  565.                               
  566.                 }
  567.       
  568.                
  569.         }
  570. /*        cout<<m<<" ";*/
  571.     cout<<endl;
  572. /*        cout<<"±?òìoóμ?è?é?ì?£o"<<endl;
  573.         for(im=1;im<=i_length;im++)
  574.     for(jm=1;jm<=20;jm++)
  575.         {
  576.                 cout<<N_offspringc[im][jm]<<" ";
  577.                 if(jm==20)
  578.                         cout<<endl;
  579.     }
  580. */
  581. }


  582. GA::N_2to3()
  583. {   
  584. /*        cout<<endl<<"2to3(?t??êy×é×a?ˉ?aèy??êy×é)"<<endl;  */
  585.     int i,j,k;
  586.         int  m=1;
  587.                            
  588.         for(i=1;i<=i_length;i++)
  589.     for(j=1;j<=4;j++)
  590.     for(k=1;k<=5;k++)
  591.         {
  592.                 if(m<=20)
  593.                 {
  594.                                                                    /* ?t??êy×é×a?ˉ?aèy??êy×é*/
  595.             N_renewgene[i][j][k]=N_offspringc[i][m];
  596.             N_gene[i][j][k]=N_renewgene[i][j][k];
  597.                         m++;
  598.                         if(m>20)
  599.                         m=1;
  600.                 }
  601.          
  602.         }
  603.   
  604. }
  605. GA::N_add2to3()
  606. {
  607.         int i,j,k;
  608.         int  m=1;
  609.                            
  610.         for(i=1;i<=1.2*i_length;i++)
  611.     for(j=1;j<=4;j++)
  612.     for(k=1;k<=5;k++)
  613.         {
  614.                 if(m<=20)
  615.                 {
  616.                                                                    /* ?t??êy×é×a?ˉ?aèy??êy×é*/
  617.             N_renewgene[i][j][k]=N_addgene[i][m];
  618.             N_gene[i][j][k]=N_renewgene[i][j][k];
  619.                         m++;
  620.                         if(m>20)
  621.                         m=1;
  622.                 }
  623.          
  624.         }

  625. }
  626. GA::F_irst()
  627. {
  628.             N_changetointeger();
  629.             t_changetoindex();
  630.         N_3to2();
  631.         E_nergy();
  632.                 Makeorder();
  633.                 Chose_super();
  634.         N_2to3();
  635.         N_changetointeger();
  636.             t_changetoindex();

  637.                 E_nergy();
  638.             F_itness();

  639.         crossover();               
  640.             mutate();
  641.         Addgene();
  642.             N_2to3();

  643. }

  644. GA::C_ircle()
  645. {   
  646.     F_irst();

  647.         int count;
  648.     int total;
  649.        
  650.         cout<<"ê?è?μü′ú′?êy£o";
  651.     cin>>total;
  652.     for(count=2;count<=total;count++)
  653.            {
  654.                 cout<<endl<<endl<<"μú"<<count<<"′?μü′ú:";
  655.         cout<<endl<<"****************";
  656.                
  657.                 N_add2to3();
  658.                 N_changetointeger1();
  659.         t_changetoindex1();
  660.         
  661.         E_nergy1();
  662.         Makeorder1();
  663.         Chose_super();

  664.         N_2to3();
  665.         N_changetointeger();
  666.             t_changetoindex();
  667.         E_nergy();

  668.             F_itness();       
  669.                
  670.         crossover();
  671.                 mutate();
  672.         Addgene();
  673.             N_2to3();
  674.        
  675.         cout<<endl;
  676.         cout<<"*********************************"<<endl;
  677.         }

  678. }

  679. main()
  680. {   
  681.    GA gene1;

  682.   /* gene1.F_irst();*/
  683.   gene1.C_ircle();

  684.    cout<<endl;

  685. return(0);

  686. }
复制代码

[ 本帖最后由 风花雪月 于 2006-11-12 06:28 编辑 ]
回复
分享到:

使用道具 举报

发表于 2006-4-21 11:05 | 显示全部楼层

帮个忙

<P>哥们我现在要做关于遗传算法的毕业设计,能不帮个忙呢QQ261211250,谢谢</P>
发表于 2006-5-15 10:48 | 显示全部楼层

回复:(Hamlet)[原创]用VC++编的遗传算法

有什么具体问题请说明
发表于 2006-7-24 18:31 | 显示全部楼层
你的算法问题很大,效果也不理想,多多改善
发表于 2007-5-2 14:16 | 显示全部楼层
有没有遗传算法在Qos路由选择中的应用程序啊
发表于 2007-5-5 20:37 | 显示全部楼层
发表于 2007-5-8 10:14 | 显示全部楼层
还没有我的效果好,参考
http://forum.vibunion.com/forum/ ... =page%3D1#pid222193

希望能讨论讨论。
发表于 2007-5-10 01:52 | 显示全部楼层
原帖由 assist 于 2007-5-8 10:14 发表
还没有我的效果好,参考
http://forum.vibunion.com/forum/viewthread.php?tid=41877&pid=222193&page=1&extra=page%3D1#pid222193

希望能讨论讨论。


做个比较发出来看看
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-28 04:28 , Processed in 0.062449 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表