假设一组非支配解是frontier= 910132435781116100100写一个计算拥挤度的函数按照frontier解的顺序依次计算比如先计算910再计算13储存在distances列表中返回
def crowding_distance(frontier): distances = [0] * len(frontier) num_objs = len(frontier[0]) for obj_index in range(num_objs): sorted_frontier = sorted(frontier, key=lambda x: x[obj_index]) min_obj_val = sorted_frontier[0][obj_index] max_obj_val = sorted_frontier[-1][obj_index] if max_obj_val == min_obj_val: continue for i in range(1, len(sorted_frontier)-1): distances[i] += (sorted_frontier[i+1][obj_index] - sorted_frontier[i-1][obj_index]) / (max_obj_val - min_obj_val) return distances
frontier = [(9,10),(1,3),(2,4),(3,5),(7,8),(11,16),(100,100)] distances = crowding_distance(frontier) print(distances) # [1.5, 2.0, 2.0, 2.0, 2.5, 0.0, 0.0
原文地址: https://www.cveoy.top/t/topic/cfXI 著作权归作者所有。请勿转载和采集!