/* 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. CSS 高级

    1.CSS 盒模型(Box Model) 所有 HTML 元素都可以看作是盒子,在 CSS 中,“Box Model”这一术语主要是在布局时使用. CSS 盒模型(Box Model)规定了处理元素内 ...

  2. 负载均衡-多台机子session不起效:把php.ini中file改为memcache存储

    一 开启memcache服务 二 修改php.ini中session配置 php/lib/php.ini session.save_handler = memcache session.save_pa ...

  3. 转换rgb为16进制颜色值

    function rgb2hex(rgb) { rgb = rgb.match(/^rgb\((\d+),(\d+),(\d+)\)$/); function hex(x) { return (&qu ...

  4. PHP-traits新特性详解

    自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 traits. Traits 是一种为类似 PHP 的单继承语言而准备的代码复用机制.Trait 为了减少单继承语言的限制,使开发人 ...

  5. OpenCart框架运行流程介绍

    框架运行流程介绍 这样的一个get请求http://hostname/index.php?route=common/home 发生了什么? 1. 开始执行入口文件index.php. 2. requi ...

  6. QQ空间的“神奇”图片

    近几天好多朋友问我qq空间出现的神奇图片原理,最近比较烦,事情比较多,一直没理.加上我对PHP之类的语言也一知半解. 今天闲了看了一下QQ空间,发现这个很早以前就有人写过这样的帖子了 看别人解释 (转 ...

  7. phpwind9.0 顶部和底部版权信息永久性修改

    过了pw头部和底部版权修改方法,但是每次升级程序后版权又变成了默认的了,还得重新修改,其实有个方法可以永久性修改,底部和顶部随着主题走. pw9全局主题位于/themes/site/目录下,  前面文 ...

  8. [BZOJ 3530] [Sdoi2014] 数数 【AC自动机+DP】

    题目链接:BZOJ - 3530 题目分析 明显是 AC自动机+DP,外加数位统计. WZY 神犇出的良心省选题,然而去年我太弱..比现在还要弱得多.. 其实现在做这道题,我自己也没想出完整解法.. ...

  9. eclipse+maven搭建cxf webservice 完整例子

    开发环境是eclipse , maven. 在开发java webservice时,有两个比较流行的框架:axis2和cxf.cxf可以无缝的和spring集成,而axis2需要打包成aar文件,在t ...

  10. [topcoder]TopographicalImage

    BFS.这里用了queue,以及在数据结构里存了上一个的高度.也可以递归调用完成BFS,在调用之前做判断:http://community.topcoder.com/stat?c=problem_so ...