多目标遗传算法 ------ NSGA-II (部分源码解析)二元锦标赛选择 tourselect.c
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的更多相关文章
- 多目标遗传算法 ------ NSGA-II (部分源码解析)介绍
NSGA(非支配排序遗传算法).NSGA-II(带精英策略的快速非支配排序遗传算法),都是基于遗传算法的多目标优化算法,是基于pareto最优解讨论的多目标优化. 在官网: http://www.ii ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 交叉操作 crossover.c
遗传算法中的交叉操作是 对NSGA-II 源码分析的 最后一部分, 这一部分也是我 从读该算法源代码和看该算法论文理解偏差最大的 函数模块. 这里,首先提一下,遗传算法的 交叉操作.变异操作都 ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析)目标函数 problemdef.c
/* Test problem definitions */ # include <stdio.h> # include <stdlib.h> # include <ma ...
- 多目标遗传算法 ------ 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> ...
随机推荐
- Xcode自动选择证书
从xcode3时代习惯了手动选择证书,即 Provisioning Profile和 Code Signing Identify. 而随着团队扩大,应用量增多,需要管理的证书也越来越多,每次从长长的l ...
- python 函数及变量作用域及装饰器decorator @详解
一.函数及变量的作用 在python程序中,函数都会创建一个新的作用域,又称为命名空间,当函数遇到变量时,Python就会到该函数的命名空间来寻找变量,因为Python一切都是对象,而在命名空间中 ...
- [BUAA2017软工]第1次个人项目 数独
[BUAA软工]第1次作业 个人项目 数独 一.项目地址 github地址:https://github.com/BuaaAlen/sudoku 二.PSP表格 三.解题思路描述 在拿到这个题目时,我 ...
- 『编程题全队』Beta 阶段冲刺博客二
1.提供当天站立式会议照片一张 2.每个人的工作 (有work item 的ID) (1) 昨天已完成的工作 孙志威: 1.添加了SubTask类,完成基本UI 2.为SubTask类添加了展开/收缩 ...
- JavaScript 编程易错点整理
Case 1: 通过getElementById("id")获得是一个DOM元素节点对象: 通过getElementsByTagName("tagName")获 ...
- #Leetcode# 788. Rotated Digits
https://leetcode.com/problems/rotated-digits/ X is a good number if after rotating each digit indivi ...
- Jquery 组 checkbox全选按钮
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...
- 暂时刷完leetcode的一点小体会
两年前,在实习生笔试的时候,笔试百度,对试卷上很多问题感到不知所云,毫无悬念的挂了 读研两年,今年代笔百度,发现算法题都见过,或者有思路,但一时之间居然都想不到很好的解法,而且很少手写思路,手写代码, ...
- Linux常用指令-ssh
目录 ssh远程登陆 ssh免密码登陆 生成公钥和私钥 将公钥复制到其他从机 文件说明 id_rsa id_rsa.pub authorized_keys known_host SSH(远程连接工具) ...
- delphi获取一个窗口的所有子窗口(包括嵌套)
unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...