种群解码函数 decode_pop 为包装函数, 核心调用函数为 decode_ind , 对每个个体进行解码。
1 /* Routines to decode the population */
 2 
 3 # include <stdio.h>
 4 # include <stdlib.h>
 5 # include <math.h>
 6 
 7 # include "global.h"
 8 # include "rand.h"
 9 
10 /* Function to decode a population to find out the binary variable values based on its bit pattern */
11 void decode_pop (population *pop)
12 {
13     int i;
14     if (nbin!=0)
15     {
16         for (i=0; i<popsize; i++)
17         {
18             decode_ind (&(pop->ind[i]));
19         }
20     }
21     return;
22 }核心解码操作:
1 /* Function to decode an individual to find out the binary variable values based on its bit pattern */
 2 void decode_ind (individual *ind)
 3 {
 4     int j, k;
 5     int sum;
 6     if (nbin!=0)
 7     {
 8         for (j=0; j<nbin; j++)
 9         {
10             sum=0;
11             for (k=0; k<nbits[j]; k++)
12             {
13                 if (ind->gene[j][k]==1)
14                 {
15                     sum += pow(2,nbits[j]-1-k);
16                 }
17             }
18             ind->xbin[j] = min_binvar[j] + (double)sum*(max_binvar[j] - min_binvar[j])/(double)(pow(2,nbits[j])-1);
19         }
20     }
21     return;
22 }其中,15行,
sum += pow(2,nbits[j]-1-k);将个体某变量的比特编码 转换为 实数。
ind->xbin[j] = min_binvar[j] + (double)sum*(max_binvar[j] - min_binvar[j])/(double)(pow(2,nbits[j])-1);因为需要考虑精度问题,所以二进制编码的个体长度表示的范围空间(double)(pow(2,nbits[j])-1)在考虑精度的情况下 要大于 该变量的 范围空间 ( max_binvar[j] - min_binvar[j] ) 。
因此, 需要进行空间映射转换,以上代码便是此意。










