def draw(self): population = self.run pop_obj = population[1] front_no, max_front = nd_sort(pop_obj, 1) non_dominated = pop_obj[front_no == 1, :] if Global.M == 2: plt.scatter(non_dominated[0, :], non_dominated[1, :]) elif Global.M == 3: x, y, z = non_dominated[:, 0], non_dominated[:, 1], non_dominated[:, 2] ax = plt.subplot(111, projection='3d') ax.scatter(x, y, z, c='b') else: for i in range(len(non_dominated)): plt.plot(range(1, Global.M + 1), non_dominated[i, :])
def environment_selection(population, N): ''' environmental selection in NSGA-II :param population: current population :param N: number of selected individuals :return: next generation population ''' front_no, max_front = nd_sort(population[1], N) next_label = [False for i in range(front_no.size)] for i in range(front_no.size): if front_no[i] < max_front: next_label[i] = True crowd_dis = crowding_distance(population[1], front_no) last = [i for i in range(len(front_no)) if front_no[i] == max_front] rank = np.argsort(-crowd_dis[last]) delta_n = rank[:(N - int(np.sum(next_label)))] rest = [last[i] for i in delta_n] for i in rest: next_label[i] = True index = np.array([i for i in range(len(next_label)) if next_label[i]]) next_pop = [population[0][index, :], population[1][index, :]] return next_pop, front_no[index], crowd_dis[index], index
def run(self): start = time.clock() if self.decs is None: population = Global.initialize() else: population = Global.individual(self.decs) front_no, max_front = nd_sort(population[1], np.inf) crowd_dis = crowding_distance(population[1], front_no) evaluation = self.eva while self.eva >= 0: fit = np.vstack((front_no, crowd_dis)).T mating_pool = tournament(2, Global.N, fit) pop_dec, pop_obj = population[0], population[1] parent = [pop_dec[mating_pool, :], pop_obj[mating_pool, :]] offspring = Global.variation(parent[0],boundary=(Global.lower,Global.upper)) population = [np.vstack((population[0], Global.individual(offspring)[0])), np.vstack((population[1], Global.individual(offspring)[1]))] population, front_no, crowd_dis,_ = environment_selection(population, Global.N) self.eva = self.eva - Global.N if self.eva%(10*evaluation/self.ite) == 0: end = time.clock() print('Running time %10.2f, percentage %s'%(end-start,100*(evaluation-self.eva)/evaluation)) return population
def run(self): """ run the wof_nsgaii to obtain the final population :return: the final population """ start = time.clock() if self.decs is None: population = Global.initialize() else: population = Global.individual(self.decs) g_size = math.ceil(Global.d / self.group_no) group = [] for i in range(self.group_no): group.append([]) lower = np.tile(Global.lower, (Global.N, 1)) upper = np.tile(Global.upper, (Global.N, 1)) w_lower = np.zeros((1, self.group_no)) w_upper = np.ones((1, self.group_no)) * 2 evaluated = 0 evaluated = evaluated + Global.N front_no, max_front = nd_sort(population[1], np.inf) crowd_dis = crowding_distance(population[1], front_no) while evaluated < int(self.eva * self.delta): en1 = evaluated while evaluated < (self.t1 + en1): fit = np.vstack((front_no, -crowd_dis)).T mating_pool = tournament(2, Global.N, fit) pop_dec, pop_obj = population[0], population[1] parent = [pop_dec[mating_pool, :], pop_obj[mating_pool, :]] off_dec = Global.variation(parent[0], boundary=(Global.lower, Global.upper)) offspring = Global.individual(off_dec) evaluated = evaluated + Global.N print('Number of evaluation: %d, time %.2f' % (evaluated, -start + time.clock())) population = [ np.vstack((population[0], offspring[0])), np.vstack((population[1], offspring[1])) ] population, front_no, crowd_dis, _ = environment_selection( population, Global.N) crowd_index = np.argsort(-crowd_dis) pop_dec, pop_obj = population[0], population[1] x_q = [ pop_dec[crowd_index[:self.choose_no], :], pop_obj[crowd_index[:self.choose_no], :] ] k = 1 sq = [ np.tile(pop_dec, (self.choose_no + 1, 1)), np.tile(pop_obj, (self.choose_no + 1, 1)) ] while k <= self.choose_no: x_dec = np.zeros((self.weight_size, Global.d)) temp = x_q[0][k - 1, :] g_sort = np.argsort(temp) w = 2 * np.random.random((self.weight_size, self.group_no)) for i in range(self.group_no): group[i] = g_sort[int(g_size * i):int(g_size * (i + 1))] x_dec[:, group[i]] = np.tile( temp[group[i]], (self.weight_size, 1)) + self.p * ( np.tile(w[:, i].reshape(self.weight_size, 1), (1, g_size)) - 1) * np.tile( Global.upper[:, group[i]] - Global.lower[:, group[i]], (self.weight_size, 1)) x_dec = np.maximum(np.minimum(x_dec, Global.upper), Global.lower) en2 = evaluated X = Global.individual(x_dec) evaluated = evaluated + x_dec.shape[0] w_front, _ = nd_sort(X[1], np.inf) w_crowd = crowding_distance(X[1], w_front) while evaluated < (self.t2 + en2): fit = np.vstack((w_front, -w_crowd)).T w_mating_pool = tournament(2, self.weight_size, fit) w_offspring = Global.variation(w[w_mating_pool, :], boundary=(w_lower, w_upper)) for i in range(self.group_no): x_dec[:, group[i]] = np.tile(temp[group[i]], (self.weight_size, 1)) \ * np.tile(w_offspring[:, i].reshape((self.weight_size, 1)), (1, g_size)) x_offspring = Global.individual(x_dec) evaluated += self.weight_size # X can be update or not update? Reference given not update X, front_no, crowd_dis, next_label = environment_selection( [ np.vstack((X[0], x_offspring[0])), np.vstack((X[1], x_offspring[1])) ], self.weight_size) combine = np.vstack((w, w_offspring)) w = combine[next_label, :] s_dec = population[0].copy() for i in range(self.group_no): s_dec[:, group[i]] = s_dec[:, group[i]] + self.p * ( np.ones((Global.N, g_size)) * w[math.ceil(self.weight_size / 2), i] - 1) * (upper[:, group[i]] - lower[:, group[i]]) s_pop = Global.individual(s_dec) evaluated = evaluated + s_dec.shape[0] print('Number of evaluation: %d, time %.2f' % (evaluated, -start + time.clock())) sq[0][Global.N * (k - 1):Global.N * k, :] = s_pop[0][:, :] sq[1][Global.N * (k - 1):Global.N * k, :] = s_pop[1][:, :] k += 1 sq[0][Global.N * self.choose_no:, :] = population[0] sq[1][Global.N * self.choose_no:, :] = population[1] population, front_no, crowd_dis, _ = environment_selection( sq, Global.N) while evaluated < self.eva: fit = np.vstack((front_no, -crowd_dis)).T mating_pool = tournament(2, Global.N, fit) pop_dec, pop_obj = population[0], population[1] parent = [pop_dec[mating_pool, :], pop_obj[mating_pool, :]] offspring = Global.individual( Global.variation(parent[0], boundary=(Global.lower, Global.upper))) evaluated = evaluated + Global.N print('Number of evaluation: %d, time %.2f' % (evaluated, -start + time.clock())) population = [ np.vstack((population[0], offspring[0])), np.vstack((population[1], offspring[1])) ] population, front_no, crowd_dis, _ = environment_selection( population, Global.N) return population