多目标遗传算法 ------ NSGA-II (部分源码解析) 临时种群生成新父代种群 fillnds.c
1 /* Nond-domination based selection routines */ 2 3 # include <stdio.h> 4 # include <stdlib.h> 5 # include <math.h> 6 7 # include "global.h" 8 # include "rand.h" 9 10 /* Routine to perform non-dominated sorting */ 11 void fill_nondominated_sort (population *mixed_pop, population *new_pop) 12 { 13 int flag; 14 int i, j; 15 int end; 16 int front_size; 17 int archieve_size; 18 int rank=1; 19 list *pool; 20 list *elite; 21 list *temp1, *temp2; 22 pool = (list *)malloc(sizeof(list)); 23 elite = (list *)malloc(sizeof(list)); 24 front_size = 0; 25 archieve_size=0; 26 pool->index = -1; 27 pool->parent = NULL; 28 pool->child = NULL; 29 elite->index = -1; 30 elite->parent = NULL; 31 elite->child = NULL; 32 temp1 = pool; 33 for (i=0; i<2*popsize; i++) 34 { 35 insert (temp1,i); 36 temp1 = temp1->child; 37 } 38 i=0; 39 do 40 { 41 temp1 = pool->child; 42 insert (elite, temp1->index); 43 front_size = 1; 44 temp2 = elite->child; 45 temp1 = del (temp1); 46 temp1 = temp1->child; 47 do 48 { 49 temp2 = elite->child; 50 if (temp1==NULL) 51 { 52 break; 53 } 54 do 55 { 56 end = 0; 57 flag = check_dominance (&(mixed_pop->ind[temp1->index]), &(mixed_pop->ind[temp2->index])); 58 if (flag == 1) 59 { 60 insert (pool, temp2->index); 61 temp2 = del (temp2); 62 front_size--; 63 temp2 = temp2->child; 64 } 65 if (flag == 0) 66 { 67 temp2 = temp2->child; 68 } 69 if (flag == -1) 70 { 71 end = 1; 72 } 73 } 74 while (end!=1 && temp2!=NULL); 75 if (flag == 0 || flag == 1) 76 { 77 insert (elite, temp1->index); 78 front_size++; 79 temp1 = del (temp1); 80 } 81 temp1 = temp1->child; 82 } 83 while (temp1 != NULL); 84 temp2 = elite->child; 85 j=i; 86 if ( (archieve_size+front_size) <= popsize) 87 { 88 do 89 { 90 copy_ind (&mixed_pop->ind[temp2->index], &new_pop->ind[i]); 91 new_pop->ind[i].rank = rank; 92 archieve_size+=1; 93 temp2 = temp2->child; 94 i+=1; 95 } 96 while (temp2 != NULL); 97 assign_crowding_distance_indices (new_pop, j, i-1); 98 rank+=1; 99 } 100 else 101 { 102 crowding_fill (mixed_pop, new_pop, i, front_size, elite); 103 archieve_size = popsize; 104 for (j=i; j<popsize; j++) 105 { 106 new_pop->ind[j].rank = rank; 107 } 108 } 109 temp2 = elite->child; 110 do 111 { 112 temp2 = del (temp2); 113 temp2 = temp2->child; 114 } 115 while (elite->child !=NULL); 116 } 117 while (archieve_size < popsize); 118 while (pool!=NULL) 119 { 120 temp1 = pool; 121 pool = pool->child; 122 free (temp1); 123 } 124 while (elite!=NULL) 125 { 126 temp1 = elite; 127 elite = elite->child; 128 free (temp1); 129 } 130 return; 131 }
以上代码,83行代码之前和 rank.c 中代码基本一致,其功能就是选出当前种群的非支配解。
85行到99行代码,意思是,如果该层个体加入到新种群中后个体总数不超过设定的种群个体数则直接加入,
97行代码,调用 assign_crowding_distance_indices , 计算加入个体的拥挤距离。
101行代码 到 108行代码,如果超出总体数量则对该层个体进行选择,并对选择出的个体计算拥挤距离。
此功能 调用
crowding_fill (mixed_pop, new_pop, i, front_size, elite) 函数实现,对该函数分析详见下面分析。
1 /* Routine to fill a population with individuals in the decreasing order of crowding distance */ 2 void crowding_fill (population *mixed_pop, population *new_pop, int count, int front_size, list *elite) 3 { 4 int *dist; 5 list *temp; 6 int i, j; 7 assign_crowding_distance_list (mixed_pop, elite->child, front_size); 8 dist = (int *)malloc(front_size*sizeof(int)); 9 temp = elite->child; 10 for (j=0; j<front_size; j++) 11 { 12 dist[j] = temp->index; 13 temp = temp->child; 14 } 15 quicksort_dist (mixed_pop, dist, front_size); 16 for (i=count, j=front_size-1; i<popsize; i++, j--) 17 { 18 copy_ind(&mixed_pop->ind[dist[j]], &new_pop->ind[i]); 19 } 20 free (dist); 21 return; 22 }
首先,对该层个体进行拥挤距离判断,由于这些个体还没有加入到新种群中(矩阵),仍然使用链表保持所以调用函数
assign_crowding_distance_list (mixed_pop, elite->child, front_size);
对该层个体进行拥挤距离计算,计算后的距离信息保存在临时种群中 mixed_pop 。
10行 到 14 行 , 用 数组 dist 保存 链表中对应个体的序号(mixed_pop中的序号)。
15行, 对 mixed_pop 中的 dist对应的个体进行拥挤距离排序, 之后dist数组对应个个体序号便是根据拥挤距离排序的。
16行 到 19行, 对排序后的个体索引 dist, 从最大拥挤距离开始选择个体填入到新种群中直到填满为止 。
本博客是博主个人学习时的一些记录,不保证是为原创,个别文章加入了转载的源地址,还有个别文章是汇总网上多份资料所成,在这之中也必有疏漏未加标注处,如有侵权请与博主联系。
如果未特殊标注则为原创,遵循 CC 4.0 BY-SA 版权协议。
posted on 2017-01-09 12:42 Angry_Panda 阅读(788) 评论(0) 编辑 收藏 举报