多目标遗传算法 ------ NSGA-II (部分源码解析) 拥挤距离计算 crowddist.c
/* Crowding distance computation routines */ # include <stdio.h>
# include <stdlib.h>
# include <math.h> # include "global.h"
# include "rand.h" /* Routine to compute crowding distance based on ojbective function values when the population in in the form of a list */
void assign_crowding_distance_list (population *pop, list *lst, int front_size)
{
int **obj_array;
int *dist;
int i, j;
list *temp;
temp = lst;
if (front_size==)
{
pop->ind[lst->index].crowd_dist = INF;
return;
}
if (front_size==)
{
pop->ind[lst->index].crowd_dist = INF;
pop->ind[lst->child->index].crowd_dist = INF;
return;
}
obj_array = (int **)malloc(nobj*sizeof(int));
dist = (int *)malloc(front_size*sizeof(int));
for (i=; i<nobj; i++)
{
obj_array[i] = (int *)malloc(front_size*sizeof(int));
}
for (j=; j<front_size; j++)
{
dist[j] = temp->index;
temp = temp->child;
}
assign_crowding_distance (pop, dist, obj_array, front_size);
free (dist);
for (i=; i<nobj; i++)
{
free (obj_array[i]);
}
free (obj_array);
return;
} /* Routine to compute crowding distance based on objective function values when the population in in the form of an array */
void assign_crowding_distance_indices (population *pop, int c1, int c2)
{
int **obj_array;
int *dist;
int i, j;
int front_size;
front_size = c2-c1+;
if (front_size==)
{
pop->ind[c1].crowd_dist = INF;
return;
}
if (front_size==)
{
pop->ind[c1].crowd_dist = INF;
pop->ind[c2].crowd_dist = INF;
return;
}
obj_array = (int **)malloc(nobj*sizeof(int));
dist = (int *)malloc(front_size*sizeof(int));
for (i=; i<nobj; i++)
{
obj_array[i] = (int *)malloc(front_size*sizeof(int));
}
for (j=; j<front_size; j++)
{
dist[j] = c1++;
}
assign_crowding_distance (pop, dist, obj_array, front_size);
free (dist);
for (i=; i<nobj; i++)
{
free (obj_array[i]);
}
free (obj_array);
return;
}
以上代码里的两个函数都为包装函数,最终的计算都是需要调用下面的函数
assign_crowding_distance (population *pop, int *dist, int **obj_array, int front_size) 。
其中,加入一定的判断过程,对一个层里面只有两个个体的情况直接对这两个个体的拥挤距离设定为无穷。
距离计算的核心代码,如下:
/* Routine to compute crowding distances */
void assign_crowding_distance (population *pop, int *dist, int **obj_array, int front_size)
{
int i, j;
for (i=; i<nobj; i++)
{
for (j=; j<front_size; j++)
{
obj_array[i][j] = dist[j];
}
quicksort_front_obj (pop, i, obj_array[i], front_size);
}
for (j=; j<front_size; j++)
{
pop->ind[dist[j]].crowd_dist = 0.0;
} for (i=; i<nobj; i++)
{
pop->ind[obj_array[i][]].crowd_dist = INF;
} for (i=; i<nobj; i++)
{
for (j=; j<front_size-; j++)
{
if (pop->ind[obj_array[i][j]].crowd_dist != INF)
{
if (pop->ind[obj_array[i][front_size-]].obj[i] == pop->ind[obj_array[i][]].obj[i])
{
pop->ind[obj_array[i][j]].crowd_dist += 0.0;
}
else
{
pop->ind[obj_array[i][j]].crowd_dist += (pop->ind[obj_array[i][j+]].obj[i] - pop->ind[obj_array[i][j-]].obj[i])/(pop->ind[obj_array[i][front_size-]].obj[i] - pop->ind[obj_array[i][]].obj[i]);
}
}
}
} for (j=; j<front_size; j++)
{
if (pop->ind[dist[j]].crowd_dist != INF)
{
pop->ind[dist[j]].crowd_dist = (pop->ind[dist[j]].crowd_dist)/nobj;
}
}
return;
}
5 行 到 12行, 初始化 多目标索引矩阵,并对其不同目标列进行索引排序(按照目标函数值的大小)。
13行 到 16行, 个体的拥挤距离初始化。
18行 到 21行, 对按照某目标函数排序后最小的个体的拥挤距离赋值为无穷(并没有对两端赋值无穷,而是最小端的个体)。
27行,如果一个个体的拥挤距离已经被赋值为无穷,则对其不再计算。
29行 到 32行,如果某目标函数的排序后所有个体的该目标函数值相同,则该目标函数贡献的距离为 0 。
pop->ind[obj_array[i][j]].crowd_dist += (pop->ind[obj_array[i][j+]].obj[i] - pop->ind[obj_array[i][j-]].obj[i])/(pop->ind[obj_array[i][front_size-]].obj[i] - pop->ind[obj_array[i][]].obj[i]);
35行代码, 某个个体在某一个目标函数上的拥挤距离 为 该个体在该目标函数上前后个体距离之差除以该层该目标的最大最小值之差。
41 行 到 47行, 将拥挤距离不为无穷的所有个体的拥挤距离除以目标函数值个数(归一化操作) 。
多目标遗传算法 ------ NSGA-II (部分源码解析) 拥挤距离计算 crowddist.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 (部分源码解析)二元锦标赛选择 tourselect.c
tourselect.c 文件中共有两个函数: selection (population *old_pop, population *new_pop) individual* tournament ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析)状态报告 打印 report.c
/* Routines for storing population data into files */ # include <stdio.h> # include <stdlib ...
- 多目标遗传算法 ------ 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> ...
随机推荐
- Couldn't get lock for %t/vertx.log
今天在启动vertx框架的项目时,报“Couldn't get lock for %t/vertx.log”的错误. 解决方案: 1,找出vertx.log的目录.一般在(C:\Users\Admin ...
- python数据库操作之pymysql模块和sqlalchemy模块(项目必备)
pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 1.下载安装 pip3 install pymysql 2.操作数据库 (1).执行sql #! ...
- AOP和IOC理解
在百度上看到一篇很有意思的文章,是对AOP的一种解释,如下:(摘自:百度文库的 AOP和IOC最容易理解的说明(Spring知识小计)): IOC,依赖倒置的意思, 所谓依赖,从程序的角度看,就是比如 ...
- 转 Web APi之认证(Authentication)两种实现方式【二】(十三)
前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再废叙述废话. 序言 对于所谓的认证说到 ...
- java 容器类大集结
这个世界是程序员的世界,归根到底是数据的世界,要统治这个世界,首先要学会征服数据. 没有最好的,只有最合适的,如何在不同的环境先选择最优的存储的结构呢?且看下文分解: 以下内容部分来自网络,参考: h ...
- XMLHttpRequest2的进步之处
本文参考自:XMLHttpRequest2 新技巧 (重点保留demo,方便自己日后查阅) HTML5是现在web开发中的热点,虽然关于web app和local app一直有争论,但是从技术学习的角 ...
- 浅谈 Android 开发文化
Hello,亲爱的读者朋友们(希望你们是 Android 开发者,或者正在成为 Androider 的路上-)! 质量从用户反馈很清凉然后我们就只能看 CPU 原来的想法是但是事实上不是这些但是我们可 ...
- draw9patch超详细教程
这篇文章是android开发人员的必备知识,内容摘选自网络,友我为大家整理和总结,不求完美,但是有用. 视频教程地址:http://player.youku.com/player.php/sid/XM ...
- AD认证
这两天接触到一个新的知识点,AD验证.什么是AD验证?Active Directory——活动目录,活动目录只是LDAP的一个实现,提供LDAP认证.Radius认证和NTML认证,都是标准认证方式 ...
- bzoj1034
贪心 尽可能让最强的赢,最弱的赢,都不行则最弱打最强 感性的想,我肯定要尽可能的赢,而且赢的要对等 实在不能赢就拿最小的拼,所谓的田忌赛马策略 由于总分一定,己方最差即己方最好时对方的分数 ..] o ...