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. Leetcode 279. 完全平方数

    题目描述: https://leetcode-cn.com/problems/perfect-squares/ 解题思路: 同样是dp,一开始的想法是,对于每个数i做拆分为j和(i-j),利用动态转移 ...

  2. 软件工程(GZSD2015)学生博客列表

    2015年贵州师范大学软件工程课程学生博客列表 陈小丽 郑倩 唐洁 周娟 李利思 肖俊 罗文豪 周静 徐明艳 毛涛 邓洪虹 岳庆 李盼 安坤 何亚 涂江凤 张义平 杨明颢 杨家堂 胡贵玲 寿克霞 吴明 ...

  3. org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unexpected failure during bean definition parsing Offending resource: class path resource [applicationC

    这个错误是 org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration proble ...

  4. Resolved validation conflict with readonly

    /** * Bug绕过去方案WorkAround * Bug描述: * JQuery的Validation的和form的input元素设为readonly,一对不可调和的矛盾: * 一个设置为requ ...

  5. Java使用HTTPClient3.0.1开发的公众平台消息模板的推送功能

    package com.company.product.manager.busniess.impl; import java.io.IOException;import java.nio.charse ...

  6. FICO基础知识(四)

    What is the organizational structure for CO? Operating Concern 经营组织 Controlling Area 成本控制范围 Profit c ...

  7. python 协程库gevent学习--gevent数据结构及实战(三)

    gevent学习系列第三章,前面两章分析了大量常用几个函数的源码以及实现原理.这一章重点偏向实战了,按照官方给出的gevent学习指南,我将依次分析官方给出的7个数据结构.以及给出几个相应使用他们的例 ...

  8. html5 简介

    html5基于html.html dom.xhtml的新版本. 为html5建立的一些新规则: 基于html.dom.xhtml和javascript: 减少对外部插件的需求,比如flash: 更多取 ...

  9. 微信小程序入門學習資料鏈接

    https://blog.csdn.net/github_38847071/article/details/73250258 https://blog.csdn.net/lily2016n/artic ...

  10. indicator function指示函数

    指示函数   在集合论中,指示函数是定义在某集合X上的函数,表示其中有哪些元素属于某一子集A. 中文名 指示函数 外文名 indicator function 相关学科 数学.组合数学 其他称呼 特征 ...