多目标遗传算法 ------ NSGA-II (部分源码解析) 交叉操作 crossover.c
遗传算法中的交叉操作是 对NSGA-II 源码分析的 最后一部分, 这一部分也是我 从读该算法源代码和看该算法论文理解偏差最大的 函数模块。
这里,首先提一下,遗传算法的 交叉操作、变异操作都是需要设定概率的, 即交叉概率和变异概率。
假设种群个体 大小为 popsize , 那么交叉操作需要进行 popsize/2 次 , 变异操作需要进行 popsize 次, 其中每次操作的时候都需要随机生成一个随机数来与给定的概率进行判断,若小于给定的概率则继续执行否则退出该操作。
如果继续操作的话 需要注意一个问题 对两个个体交叉 ,或 对单个个体进行变异 都是对 个体的所有变量 进行操作,其中变异对二进制编码的个体来说是对每个个体的每个变量 的二进制编码的 每个比特位进行变异。
包装函数,通过对标识位判断编码形式来决定 具体的核心功能 所调用的函数。
/* Crossover routines */ # include <stdio.h>
# include <stdlib.h>
# include <math.h> # include "global.h"
# include "rand.h" /* Function to cross two individuals */
void crossover (individual *parent1, individual *parent2, individual *child1, individual *child2)
{
if (nreal!=)
{
realcross (parent1, parent2, child1, child2);
}
if (nbin!=)
{
bincross (parent1, parent2, child1, child2);
}
return;
}
实数编码的 两个个体交叉操作, 采用 SBX 方式交叉。需要交叉的两个个体 每个变量 都进行交叉操作。
/* Routine for real variable SBX crossover */
void realcross (individual *parent1, individual *parent2, individual *child1, individual *child2)
{
int i;
double rand;
double y1, y2, yl, yu;
double alpha, beta, betaq;
if (randomperc() <= pcross_real)
{
nrealcross++;
for (i=; i<nreal; i++)
{
if (randomperc() <= 0.5 )
{
if (parent1->xreal[i] < parent2->xreal[i])
{
y1 = parent1->xreal[i];
y2 = parent2->xreal[i];
}
else
{
y1 = parent2->xreal[i];
y2 = parent1->xreal[i];
}
if (fabs(parent1->xreal[i]-parent2->xreal[i]) > EPS)
{
yl = min_realvar[i];
yu = max_realvar[i];
rand = randomperc();
beta = 1.0 + (2.0*(y1-yl)/(y2-y1));
alpha = 2.0 - pow(beta,-(eta_c+1.0));
if (rand <= (1.0/alpha))
{
betaq = pow ((rand*alpha),(1.0/(eta_c+1.0)));
}
else
{
betaq = pow ((1.0/(2.0 - rand*alpha)),(1.0/(eta_c+1.0)));
}
child1->xreal[i] = 0.5*((y1+y2)-betaq*(y2-y1));
beta = 1.0 + (2.0*(yu-y2)/(y2-y1));
alpha = 2.0 - pow(beta,-(eta_c+1.0));
if (rand <= (1.0/alpha))
{
betaq = pow ((rand*alpha),(1.0/(eta_c+1.0)));
}
else
{
betaq = pow ((1.0/(2.0 - rand*alpha)),(1.0/(eta_c+1.0)));
}
child2->xreal[i] = 0.5*((y1+y2)+betaq*(y2-y1));
if (child1->xreal[i]<yl)
{
child1->xreal[i]=yl;
}
if (child1->xreal[i]>yu)
{
child1->xreal[i]=yu;
}
if (child2->xreal[i]<yl)
{
child2->xreal[i]=yl;
}
if (child2->xreal[i]>yu)
{
child2->xreal[i]=yu;
}
}
else
{
child1->xreal[i] = parent1->xreal[i];
child2->xreal[i] = parent2->xreal[i];
}
}
else
{
child1->xreal[i] = parent1->xreal[i];
child2->xreal[i] = parent2->xreal[i];
}
}
}
else
{
for (i=; i<nreal; i++)
{
child1->xreal[i] = parent1->xreal[i];
child2->xreal[i] = parent2->xreal[i];
}
}
return;
}
二进制编码 的 交叉操作:
(双点 交叉) 两个个体的 每个对应的变量 彼此交叉操作。这里每个变量的 二进制编码 段 随机选取两点, 将中间部门的二进制段 互换,两个交叉点。
/* Routine for two point binary crossover */
void bincross (individual *parent1, individual *parent2, individual *child1, individual *child2)
{
int i, j;
double rand;
int temp, site1, site2;
for (i=; i<nbin; i++)
{
rand = randomperc();
if (rand <= pcross_bin)
{
nbincross++;
site1 = rnd(,nbits[i]-);
site2 = rnd(,nbits[i]-);
if (site1 > site2)
{
temp = site1;
site1 = site2;
site2 = temp;
}
for (j=; j<site1; j++)
{
child1->gene[i][j] = parent1->gene[i][j];
child2->gene[i][j] = parent2->gene[i][j];
}
for (j=site1; j<site2; j++)
{
child1->gene[i][j] = parent2->gene[i][j];
child2->gene[i][j] = parent1->gene[i][j];
}
for (j=site2; j<nbits[i]; j++)
{
child1->gene[i][j] = parent1->gene[i][j];
child2->gene[i][j] = parent2->gene[i][j];
}
}
else
{
for (j=; j<nbits[i]; j++)
{
child1->gene[i][j] = parent1->gene[i][j];
child2->gene[i][j] = parent2->gene[i][j];
}
}
}
return;
}
对于 遗传算法的交叉操作, 有一点是需要注意的 那就是 交叉操作 执行 popsize/2 次, 而且是按照种群个体的排序顺序(按照个体的序号)依次取出两个个体进行交叉操作,而每次执行交叉操作时是否真的进行交叉还要比对随机生成的随机数 与 给定的交叉概率大小 。
多目标遗传算法 ------ NSGA-II (部分源码解析) 交叉操作 crossover.c的更多相关文章
- 多目标遗传算法 ------ NSGA-II (部分源码解析)介绍
NSGA(非支配排序遗传算法).NSGA-II(带精英策略的快速非支配排序遗传算法),都是基于遗传算法的多目标优化算法,是基于pareto最优解讨论的多目标优化. 在官网: http://www.ii ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析)目标函数 problemdef.c
/* Test problem definitions */ # include <stdio.h> # include <stdlib.h> # include <ma ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析)二元锦标赛选择 tourselect.c
tourselect.c 文件中共有两个函数: selection (population *old_pop, population *new_pop) individual* tournament ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析)状态报告 打印 report.c
/* Routines for storing population data into files */ # include <stdio.h> # include <stdlib ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 拥挤距离计算 crowddist.c
/* Crowding distance computation routines */ # include <stdio.h> # include <stdlib.h> # ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析)README 算法的部分英文解释
This is the Readme file for NSGA-II code. About the Algorithm--------------------------------------- ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 实数、二进制编码的变异操作 mutation.c
遗传算法的变异操作 /* Mutation routines */ # include <stdio.h> # include <stdlib.h> # include < ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析)两个个体支配判断 dominance.c
/* Domination checking routines */ # include <stdio.h> # include <stdlib.h> # include &l ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 临时种群生成新父代种群 fillnds.c
/* Nond-domination based selection routines */ # include <stdio.h> # include <stdlib.h> ...
随机推荐
- JS正则表达式验证账号、手机号、电话、邮箱、货币
验证帐号是否合法验证规则:字母.数字.下划线组成,字母开头,4-16位. function checkUser(str){ var re = /^[a-zA-z]\w{3,15}$/; if(re.t ...
- Wpf ListBox数据绑定实例1--绑定字典集合
1.使用ListBox绑定Dictionary字典数据 ListBox常用事件SelectionChanged private void bindListBox() { Dictionary<s ...
- 给div命名,使逻辑更加清晰
在上一小节中,我们把一些标签放进<div>里,划分出一个独立的逻辑部分.为了使逻辑更加清晰,我们可以为这一个独立的逻辑部分设置一个名称,用id属性来为<div>提供唯一的名称, ...
- ArcGis(01)——地图切片以及发布底图服务
ArcGis(01)——地图切片以及发布底图服务 环境 操作系统:win10_x64 Gis版本:Arcis server 10.2 准备 1.tif格式地图资源 2.Arcis server 10. ...
- POJ 2230 (欧拉路)
分析: 基础的欧拉路算法,变化在于要求每条边正向和反向各走一遍. 链式前向星构图,只要标记走过的单向边,边找边输出即可. code #include <iostream> #include ...
- locate 不能使用
当需要查找一个文件,只知道文件名不知道路径,我们通常用locate,由于公司的服务器使用最小化安装的所以当locate 文件名,报错,提 示-bash: locate: command not fou ...
- 19 Remove Nth Node From End of List(去掉链表中倒数第n个节点Easy)
题目意思:去掉链表中倒数第n个节点 思路:1.两次遍历,没什么技术含量,第一次遍历计算长度,第二次遍历找到倒数第k个,代码不写了 2.一次遍历,两个指针,用指针间的距离去计算. ps:特别注意删掉 ...
- oracle创建表空间、用户、用户授权、删除表空间、删除用户
--创建临时表空间 create temporary tablespace test_temp --test_temp表空间名称 tempfile 'E:\oracle\product\10.2.0\ ...
- JS动态加载 js css
1.动态加载js function loadScript( url ){ var script = document.createElement( "script" ); scri ...
- Python学习笔记:02数据类型
Python 数据类型 python中标准的数据类型有 基础类型 整型(长整型) 浮点型 复数型 布尔型 序列类型 字符串 列表 元组 字典 整型 整型和长整型并不严格区分,整型int的表达范围和计算 ...