/* 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的更多相关文章

  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 (部分源码解析)二元锦标赛选择 tourselect.c

    tourselect.c  文件中共有两个函数: selection (population *old_pop, population *new_pop) individual* tournament ...

  5. 多目标遗传算法 ------ NSGA-II (部分源码解析)状态报告 打印 report.c

    /* Routines for storing population data into files */ # include <stdio.h> # include <stdlib ...

  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. linux正确重启MySQL的方法

    修改了my.cnf,需要重启MySQL服务,正确重启MYSQL方法请看下面的文章 由于是从源码包安装的Mysql,所以系统中是没有红帽常用的servcie mysqld restart这个脚本 只好手 ...

  2. PHP框架原理

    本文主要来聊聊框架理论,但不针对任何一款框架,不过任何一款框架都离不开这个理论,首先我们了解下框架的来龙去脉,任何技术的出现都是为了解决某个问题,之前的博客有讲过smarty,其存在就是为了html和 ...

  3. 解析sql中的表名

    最近的项目需求中需要解析sql得表名,由于只需要表名我觉得应该用相对粗暴一点的方式来解析 初步思路: 1.转义字符:去除两个引号连在一起的 2.字符串: 去除所有被引号包裹的 3.括号:识别括号处理 ...

  4. Csharp Winfrom 多串口通信

    Csharp 多串口通信 顾名思义,多串口通信,普通的PC机一般只有一个串口,现在很多家用的PC都没有串口,那么问题来了,如何保证多串口呢? 有一种神器,MOXA CP-168U Series PCI ...

  5. shell编程的一些例子2

    控制语句: 1.if语句 demo_if #!/bin/bash if [ $# -ne 1 ] then echo "参数多于一个" exit 1 fi if [ -f &quo ...

  6. 黑马程序员-------.net基础知识三

    条件执行语句 if 语句 语法: [csharp] view plaincopyprint? if(条件) { 语句1;语句2:语句3: ··· } 执行过程: 先判断条件是否为true ,如果为tr ...

  7. Docker入门

    -----------------------------------------Docker入门教程(一)介绍Docker入门教程(二)命令Docker入门教程(三)DockerFileDocker ...

  8. hdu 4738

    桥的应用! 虽然以前做过强联通分量的题,但刷的很水,所以比赛的时候一直想不起来是桥的应用: 反省一下~~~学习一下! 思路,找到权值最小的桥:用tarjin算法! 代码: #include<cs ...

  9. Highcharts Pie 饼图提示标签IE下重叠解决方法,及json数据绑定方法

    一.提示标签重叠解决方法: series: [{ startAngle:90,//添加这个属性,就可以解决 type: 'pie', name: '充值方式' }] 不知道为什么,上述方法不行了.第一 ...

  10. JAVA 抛出与声明异常

    在编程过程中,我们往往会遇到这种情况,在当前环境中无法解决,比如用户传入的参数错误,IO设备问题等.此时,就要从当前环境中抛出异常提交给上级来处理. 在JAVA语言中,使用throw关键字来抛出异常. ...