/* 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. python学习_数据处理编程实例(一)

    目的:用一个实例总结学习到的with语句,函数,列表推导,集合,排序,字符分割等内容 要求:分别以james,julie,mikey,sarah四个学生的名字建立文本文件,分别存储各自的成绩,时间格式 ...

  2. C语言学习总结(一) 基本语法

    第一章--C语言的基本概念 丹尼斯 里奇 一.什么是C语言? 定义:是一个面向过程的计算机高级语言--不需要任何运行环境便能运行的程序语言: 发展:目前是C11      (K&R C—> ...

  3. Java NIO回炉

    重回东软了,据说可能要做一个跟文件相关的项目,于是决定把Java NIO的内容再捡起来,看看. 为什么要使用NIO,其实在低连接数的情况下,NIO的性能是要低于IO的:但是在高并发的情况下,确实NIO ...

  4. RESTheart官方文档

    作者:Andrea Di Cesare, Maurizio Turatti RESTHeart是SoftInstigate公司创建并开发的开源项目. MongoDB的WEB操作接口 RESTHEART ...

  5. Java按正则提取字符串

    在Java开发中,有时会遇到一些比较别扭的规则从字符串中提取子字符串,规则无疑是写正则表达式来表达了,那按照正则来提取子字符串就会用到java.util.regex包. java.util.regex ...

  6. 计算几何模板 (bzoj 1336,poj 2451 ,poj3968)

    poj 3968 (bzoj 2642) 二分+半平面交,每次不用排序,这是几个算几版综合. #include<iostream> #include<cstdio> #incl ...

  7. Kaggle Competition Past Solutions

    Kaggle Competition Past Solutions We learn more from code, and from great code. Not necessarily alwa ...

  8. win7 安装SQL Server 2005 开发版 图文教程

    转自win7 安装SQL Server 2005 开发版 图文教程 ----------------------------写在安装前------------------------------ 一. ...

  9. Network Wars

    zoj2676:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1676 题意:给出一个带权无向图 ,每条边e有一个权 .求将点 ...

  10. The APR based Apache Tomcat Native library

    Tomcat启动的时候出现下面这样的提示: 2015-11-06 14:24:12 org.apache.catalina.core.AprLifecycleListener init 信息: The ...