遗传算法中的交叉操作是 对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的更多相关文章

  1. 多目标遗传算法 ------ NSGA-II (部分源码解析)介绍

    NSGA(非支配排序遗传算法).NSGA-II(带精英策略的快速非支配排序遗传算法),都是基于遗传算法的多目标优化算法,是基于pareto最优解讨论的多目标优化. 在官网: http://www.ii ...

  2. 多目标遗传算法 ------ NSGA-II (部分源码解析)目标函数 problemdef.c

    /* Test problem definitions */ # include <stdio.h> # include <stdlib.h> # include <ma ...

  3. 多目标遗传算法 ------ NSGA-II (部分源码解析)二元锦标赛选择 tourselect.c

    tourselect.c  文件中共有两个函数: selection (population *old_pop, population *new_pop) individual* tournament ...

  4. 多目标遗传算法 ------ NSGA-II (部分源码解析)状态报告 打印 report.c

    /* Routines for storing population data into files */ # include <stdio.h> # include <stdlib ...

  5. 多目标遗传算法 ------ NSGA-II (部分源码解析) 拥挤距离计算 crowddist.c

    /* Crowding distance computation routines */ # include <stdio.h> # include <stdlib.h> # ...

  6. 多目标遗传算法 ------ NSGA-II (部分源码解析)README 算法的部分英文解释

    This is the Readme file for NSGA-II code. About the Algorithm--------------------------------------- ...

  7. 多目标遗传算法 ------ NSGA-II (部分源码解析) 实数、二进制编码的变异操作 mutation.c

    遗传算法的变异操作 /* Mutation routines */ # include <stdio.h> # include <stdlib.h> # include < ...

  8. 多目标遗传算法 ------ NSGA-II (部分源码解析)两个个体支配判断 dominance.c

    /* Domination checking routines */ # include <stdio.h> # include <stdlib.h> # include &l ...

  9. 多目标遗传算法 ------ NSGA-II (部分源码解析) 临时种群生成新父代种群 fillnds.c

    /* Nond-domination based selection routines */ # include <stdio.h> # include <stdlib.h> ...

随机推荐

  1. js动态添加table 数据tr td

    成果库修改:      要求主题列表随成果类型改变而改变      网上查询资料后开工,在成果类型下拉框添加change()事件触发Dwr,查询主题集合——动态创建/编辑Table      概要代码 ...

  2. jquery获取元素到屏幕底的可视距离

    jquery获取元素到屏幕底的可视距离 要打对号的图里的height(我自称为可视高度:滚动条未滑到最底端)  不是打叉图里的到页面底部(滚动条到最底部时的height)(offset().top方法 ...

  3. 条件注释判断浏览器版本<!--[if lt IE 9]>(转载)

    <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--> <!--[if IE]> 所有的IE可识别 <![ ...

  4. mac在 aliyun linux ecs实例上安装 jdk tomcat mysql

    用了一个ftp 工具 把 gz rpm 等 传递到ecs 上 -- 用这个Transmit 用ssh远程登录,然后依次安装 jdk tomcat  mysql 到 /usr/local/... 设置环 ...

  5. excel - 相等判断

    函数使用例子: =IF(EVALUATE(B23)=C23,"正确","错误") 即判断单元格'B23'与'C23'是否相等,若为true,则返回"正 ...

  6. ls命令解析

    ls 列出目录的内容.它可是我们所经常使用的命令,那么你了解它所有的功能吗?下面让我们来看看吧! 命令格式 ls [OPTION]... [FILE]... 参数说明 -a , --all 显示所有文 ...

  7. 纯js制作遮罩层对话框 -- g皓皓

    //本文支持js在线工具测试.转载请注明出处. <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> < ...

  8. C# Trim方法去除字符串两端的指定字符

    var str= ",2,3,4,6,7,"; var str2 = str.Trim(new char[] { ',' }); //去除字符串str两端的','字符. //则st ...

  9. oracle之分组内的字符串连接

    实现效果: 例如下面的数据[php] groupid        personid        name 1          a          超级管理员2          b       ...

  10. Uncaught SyntaxError: Unexpected end of input

    js报错  原因:输入的意外终止…… 页面代码写的不规范啊……其中的某条语句,没有正常结束…… 或者部分语句“‘’”双引号,单引号没有配对好,被转义了之类的……错误造成的 代码: <script ...