多目标遗传算法 ------ NSGA-II (部分源码解析) 快速排序代码 sort.c
1 /* Routines for randomized recursive quick-sort */ 2 3 # include <stdio.h> 4 # include <stdlib.h> 5 # include <math.h> 6 7 # include "global.h" 8 # include "rand.h" 9 10 /* Randomized quick sort routine to sort a population based on a particular objective chosen */ 11 void quicksort_front_obj(population *pop, int objcount, int obj_array[], int obj_array_size) 12 { 13 q_sort_front_obj (pop, objcount, obj_array, 0, obj_array_size-1); 14 return; 15 } 16 17 /* Actual implementation of the randomized quick sort used to sort a population based on a particular objective chosen */ 18 void q_sort_front_obj(population *pop, int objcount, int obj_array[], int left, int right) 19 { 20 int index; 21 int temp; 22 int i, j; 23 double pivot; 24 if (left<right) 25 { 26 index = rnd (left, right); 27 temp = obj_array[right]; 28 obj_array[right] = obj_array[index]; 29 obj_array[index] = temp; 30 pivot = pop->ind[obj_array[right]].obj[objcount]; 31 i = left-1; 32 for (j=left; j<right; j++) 33 { 34 if (pop->ind[obj_array[j]].obj[objcount] <= pivot) 35 { 36 i+=1; 37 temp = obj_array[j]; 38 obj_array[j] = obj_array[i]; 39 obj_array[i] = temp; 40 } 41 } 42 index=i+1; 43 temp = obj_array[index]; 44 obj_array[index] = obj_array[right]; 45 obj_array[right] = temp; 46 q_sort_front_obj (pop, objcount, obj_array, left, index-1); 47 q_sort_front_obj (pop, objcount, obj_array, index+1, right); 48 } 49 return; 50 }
按照个体的不同 目标函数 序号(objcount), 对种群序号数组obj_array按照拥挤距离进行快速排序。
1 /* Randomized quick sort routine to sort a population based on crowding distance */ 2 void quicksort_dist(population *pop, int *dist, int front_size) 3 { 4 q_sort_dist (pop, dist, 0, front_size-1); 5 return; 6 } 7 8 /* Actual implementation of the randomized quick sort used to sort a population based on crowding distance */ 9 void q_sort_dist(population *pop, int *dist, int left, int right) 10 { 11 int index; 12 int temp; 13 int i, j; 14 double pivot; 15 if (left<right) 16 { 17 index = rnd (left, right); 18 temp = dist[right]; 19 dist[right] = dist[index]; 20 dist[index] = temp; 21 pivot = pop->ind[dist[right]].crowd_dist; 22 i = left-1; 23 for (j=left; j<right; j++) 24 { 25 if (pop->ind[dist[j]].crowd_dist <= pivot) 26 { 27 i+=1; 28 temp = dist[j]; 29 dist[j] = dist[i]; 30 dist[i] = temp; 31 } 32 } 33 index=i+1; 34 temp = dist[index]; 35 dist[index] = dist[right]; 36 dist[right] = temp; 37 q_sort_dist (pop, dist, left, index-1); 38 q_sort_dist (pop, dist, index+1, right); 39 } 40 return; 41 }
将带排序的个体索引序号 按照 拥挤距离 排序。(快速排序法)
本博客是博主个人学习时的一些记录,不保证是为原创,个别文章加入了转载的源地址,还有个别文章是汇总网上多份资料所成,在这之中也必有疏漏未加标注处,如有侵权请与博主联系。
如果未特殊标注则为原创,遵循 CC 4.0 BY-SA 版权协议。
posted on 2017-01-09 13:11 Angry_Panda 阅读(1177) 评论(0) 编辑 收藏 举报