遗传算法中的交叉操作是 对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. oracle 自治事物 -- autonomous transaction

    一 使用规则 : 在begin 之前申明  : PRAGMA AUTONOMOUS_TRANSACTION; 二 使用理解:autonomous transaction 是一个独立的事务,这一点是理解 ...

  2. HttpClient 发送图片

    var httpClient = new HttpClient(); using (FileStream fs = new FileStream("C:\\1.jpg", File ...

  3. Core Python Notes

    开发需要在读 Python 核心编程,一些 Point 记录如下. ******************************************** 版本相关 标准版的 Python 是用 C ...

  4. js 函数参数形式

    1. var a = function(b,c){ console.log(arguments);}a("1","cc"); ->  ["1&q ...

  5. uva 1378 A Funny Stone Game (博弈-SG)

    题目链接:http://vjudge.net/problem/viewProblem.action?id=41555 把第i堆的每个石子看出一堆个数为n-i的石子,转换为组合游戏 #include & ...

  6. c语言中3n+1溢出问题解决

    3n+1问题是一个简单有趣而又没有解决的数学问题.这个问题是由L. Collatz在1937年提出的.克拉兹问题(Collatz problem)也被叫做hailstone问题.3n+1问题.Hass ...

  7. C++函数覆盖的思考

    最近碰到一些问题,一开始很难调试和解决,最后发现原来是在基类函数的模板方法中对子类需要重写的函数没有使用virtual,如下 class Base { public: void say(){test( ...

  8. PHP搜索Solr文档(含高亮)

    <?php $options = array ( 'hostname' => 'localhost', 'port' => '8080', 'path' => 'solr/he ...

  9. Centos 6安装python3.5

    安装python3.5 安装步骤如下 :1 准备编译环境(环境如果不对的话,可能遇到各种问题,比如wget无法下载https链接的文件) yum groupinstall 'Development T ...

  10. C语言基础学习基本数据类型-Char类型

    char类型 char类型用于储存字母和标点之类的字符.但是在技术实现上char却是整数类型.为了处理字符,计算机使用一种数字编码,用特定的整数表示特定的字符.字符变量输入输出用%c符号.定义语法如下 ...