tourselect.c  文件中共有两个函数:

selection (population *old_pop, population *new_pop)

individual* tournament (individual *ind1, individual *ind2)

首先,第一个函数代码如下:

 /* Routine for tournament selection, it creates a new_pop from old_pop by performing tournament selection and the crossover */
void selection (population *old_pop, population *new_pop)
{
int *a1, *a2;
int temp;
int i;
int rand;
individual *parent1, *parent2;
a1 = (int *)malloc(popsize*sizeof(int));
a2 = (int *)malloc(popsize*sizeof(int));
for (i=; i<popsize; i++)
{
a1[i] = a2[i] = i;
}
for (i=; i<popsize; i++)
{
rand = rnd (i, popsize-);
temp = a1[rand];
a1[rand] = a1[i];
a1[i] = temp;
rand = rnd (i, popsize-);
temp = a2[rand];
a2[rand] = a2[i];
a2[i] = temp;
}
for (i=; i<popsize; i+=)
{
parent1 = tournament (&old_pop->ind[a1[i]], &old_pop->ind[a1[i+]]);
parent2 = tournament (&old_pop->ind[a1[i+]], &old_pop->ind[a1[i+]]);
crossover (parent1, parent2, &new_pop->ind[i], &new_pop->ind[i+]);
parent1 = tournament (&old_pop->ind[a2[i]], &old_pop->ind[a2[i+]]);
parent2 = tournament (&old_pop->ind[a2[i+]], &old_pop->ind[a2[i+]]);
crossover (parent1, parent2, &new_pop->ind[i+], &new_pop->ind[i+]);
}
free (a1);
free (a2);
return;
}

其中,

    a1 = (int *)malloc(popsize*sizeof(int));
a2 = (int *)malloc(popsize*sizeof(int));

分别生成两个  种群个体大小的数组 a1  a2,这两个数组里面以后会分别保存乱序的种群个体序号。

    for (i=; i<popsize; i++)
{
a1[i] = a2[i] = i;
}

对两个数组进行初始话,顺序存放种群个体序号。

    for (i=; i<popsize; i++)
{
rand = rnd (i, popsize-);
temp = a1[rand];
a1[rand] = a1[i];
a1[i] = temp;
rand = rnd (i, popsize-);
temp = a2[rand];
a2[rand] = a2[i];
a2[i] = temp;
}

对a1, a2  数组中存放的个体序号打乱,其中打乱的次数为  popsize  ,该操作基本保证所有个体的序号基本不在其原有位置上。

(在高级面向对象语言中以上代码可以用一句库函数调用代替)

    for (i=; i<popsize; i+=)
{
parent1 = tournament (&old_pop->ind[a1[i]], &old_pop->ind[a1[i+]]);
parent2 = tournament (&old_pop->ind[a1[i+]], &old_pop->ind[a1[i+]]);
crossover (parent1, parent2, &new_pop->ind[i], &new_pop->ind[i+]);
parent1 = tournament (&old_pop->ind[a2[i]], &old_pop->ind[a2[i+]]);
parent2 = tournament (&old_pop->ind[a2[i+]], &old_pop->ind[a2[i+]]);
crossover (parent1, parent2, &new_pop->ind[i+], &new_pop->ind[i+]);
}

这部分代码完成了遗传算法中的  选择操作  和  交叉操作。

其中  old_pop  new_pop  都是相同种群个体大小的种群,其种群大小均为  popsize。

tournament   锦标赛法,这里面使用的是二元锦标赛选择法,循环体内共有4次  tournament  操作,该循环共执行  popsize/4  次,故共进行了  popsize  次二元锦标赛选择。由于每次选择出一个新个体,所以该方式选择出的新种群 new_pop  个体数   和   旧种群 old_pop  个体数一致。

同理,crossover  操作进行了  popsize/2  次 , (其中每次进行交叉操作的时候都是选择两个个体,每次判断选择的两个个体是否进行交叉都要根据给定的交叉概率进行判断),该循环体中crossover函数总共会对   popsize   个个体进行处理。

注意: crossover  操作  循环调用    popsize/2  次  而不是    popsize  次。

 /* Routine for binary tournament */
individual* tournament (individual *ind1, individual *ind2)
{
int flag;
flag = check_dominance (ind1, ind2);
if (flag==)
{
return (ind1);
}
if (flag==-)
{
return (ind2);
}
if (ind1->crowd_dist > ind2->crowd_dist)
{
return(ind1);
}
if (ind2->crowd_dist > ind1->crowd_dist)
{
return(ind2);
}
if ((randomperc()) <= 0.5)
{
return(ind1);
}
else
{
return(ind2);
}
}

二元锦标赛竞赛法比较简单,  其中调用  check_dominance  函数判断两个个体的支配关系,如果互不支配则判断两个个体的拥挤距离,如果都相同这则随机选择一个个体。

多目标遗传算法 ------ NSGA-II (部分源码解析)二元锦标赛选择 tourselect.c的更多相关文章

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

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

  2. 多目标遗传算法 ------ NSGA-II (部分源码解析) 交叉操作 crossover.c

    遗传算法中的交叉操作是 对NSGA-II  源码分析的  最后一部分, 这一部分也是我 从读该算法源代码和看该算法论文理解偏差最大的  函数模块. 这里,首先提一下,遗传算法的  交叉操作.变异操作都 ...

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

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

  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. 第三个Sprint冲刺第七天(燃尽图)

  2. MySQL基础~~编程语法

    常量 数值 字符串:单引号或者双引号括起来.包括普通字符串或者日期格式的字符串. 布尔值:false(FALSE)对应数字值为0.true(TRUE)对应数字值为1. NULL:可以参考http:// ...

  3. Activiti启动某个流程失败,页面报500

    现象:Activiti启动某个流程失败,页面报500,错误日志如下. 2017-06-19 10:50:09 [org.activiti.engine.impl.interceptor.Command ...

  4. MYSQL两个数据库字符集保持一致问题

    参考这篇文章:https://lzw.me/a/mysql-charset.html 还有一篇官方文档:https://dev.mysql.com/doc/refman/5.7/en/charset. ...

  5. No input file specified ci

    1. php.ini(/etc/php5/cgi/php.ini)的配置中这两项cgi.fix_pathinfo=1  (这个是自己添加的)

  6. pandas 级联 concat append

    连接的一个有用的快捷方式是在Series和DataFrame实例的append方法.这些方法实际上早于concat()方法. 它们沿axis=0连接 #encoding:utf8 import pan ...

  7. ubuntu python apache2 wsgi django框架

    在ubuntu上通过apatch2和wsgi部署django (亲手做过!!!) 一,我的python.django.apatch2版本: python:python -V 2.7.3 django: ...

  8. httprequest存储的是字符内容 而文本内容是以字节形式上传的;所以普通的取值方式无法从httprequest取到值

    httprequest存储的是字符内容 而文本内容是以字节形式上传的;所以普通的取值方式无法从httprequest取到值

  9. SQL 从一个表读取数据存到另一个表

    原来没有的创建的表select * into 表A form 表B where条件 原来存在的表insert into 表A select * from 表B where 条件 INSERT INTO ...

  10. 嵌入式启动jetty

    由于jetty8以上版本已经抛弃JDK1.6,公司统一开发JDK又一直不升级,所以我们使用jetty8 pom.xml <project xmlns="http://maven.apa ...