多目标遗传算法 ------ NSGA-II (部分源码解析) 二进制编码的个体解码操作 decode.c

种群解码函数  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] ) 。

因此, 需要进行空间映射转换,以上代码便是此意。

 

posted on   Angry_Panda  阅读(1391)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示