def moea(name, solsize, popsize, wscalar_, moea_type, max_gen=float('inf'), timeLimit=float('inf')): from platypus import HUX, BitFlip, TournamentSelector from platypus import Problem, Binary from platypus import NSGAII, NSGAIII, SPEA2 from platyplus.operators import varOr from platyplus.algorithms import SMSEMOA time_start = time.perf_counter() logger.info('Running '+moea_type+' in '+name) prMutation = 0.1 prVariation = 1-prMutation vartor = varOr(HUX(), BitFlip(1), prVariation, prMutation) def evalKnapsack(x): return wscalar_.fobj([xi[0] for xi in x]) problem = Problem(wscalar_.N, wscalar_.M) problem.types[:] = [Binary(1) for i in range(wscalar_.N)] problem.function = evalKnapsack if moea_type in ['NSGAII', 'NSGAII-2', 'NSGAII-4']: alg = NSGAII(problem, population_size=popsize, selector=TournamentSelector(1), variator=vartor) elif moea_type in ['NSGAIII', 'NSGAIII-2', 'NSGAIII-4']: alg = NSGAIII(problem, divisions_outer=3, population_size=popsize, selector=TournamentSelector(1), variator=vartor) elif moea_type in ['SPEA2', 'SPEA2-2', 'SPEA2-4']: alg = SPEA2(problem, population_size=popsize, selector=TournamentSelector(1), variator=vartor) elif moea_type in ['SMSdom']: alg = SMSEMOA(problem, population_size=popsize, selector=TournamentSelector(1), variator=vartor, selection_method = 'nbr_dom') elif moea_type in ['SMShv']: alg = SMSEMOA(problem, population_size=popsize, selector=TournamentSelector(1), variator=vartor, selection_method = 'hv_contr') gen = 1 while gen<max_gen and time.perf_counter()-time_start<timeLimit: alg.step() gen+=1 alg.population_size = solsize alg.step() moeaSols = [evalKnapsack(s.variables) for s in alg.result] moea_time = time.perf_counter() - time_start logger.info(moea_type+' in '+name+' finnished.') return moeaSols, moea_time
problem = EMSGMOP_problem(n, m, s_i, SA_i, p_i, d_hat_j_tmp, t_hat_ij_tmp, d_ij, e_ij, theat_ij, fr_ij) # 默认求最小目标值 problem.directions[:] = Problem.MINIMIZE # algorithm = EpsMOEA(problem, epsilons=0.5) # algorithm = MOEAD(problem) # algorithm = SMPSO(problem) # algorithm = OMOPSO(problem, epsilons=0.5) # algorithm = EpsNSGAII(problem, epsilons=0.5) # algorithm = SPEA2(problem) # algorithm = GDE3(problem) algorithm = NSGAII(problem) # algorithm = NSGAIII(problem,divisions_outer=25) algorithm.population_size = 10 algorithm.run(Algorithm_Run_NUM) # feasible_solutions = [s for s in algorithm.result if s.feasible] # if len(feasible_solutions) > 0: # print(feasible_solutions.objectives) # print("------------------") # print("Population size: ") # print(len(algorithm.result)) # # The output shows on each line the objectives for a Pareto optimal solution: # for solution in algorithm.result: # print(solution.variables) objectives_result = algorithm.result[0].objectives x_result = algorithm.result[0].variables
def EMSG(Algorithm_Run_NUM, n, m, s_i, SA_i, p_i, d_hat_j_tmp, t_hat_ij_tmp, d_ij, e_ij, theat_ij, fr_ij): ProcessPoolExecutor(max_workers=8) print("-------EMSGMOP:") problem = EMSGMOP_problem(n, m, s_i, SA_i, p_i, d_hat_j_tmp, t_hat_ij_tmp, d_ij, e_ij, theat_ij, fr_ij) # 默认求最小目标值 # problem.directions[:] = Problem.MINIMIZE # algorithm = EpsMOEA(problem, epsilons=0.5) # algorithm = MOEAD(problem) # algorithm = SMPSO(problem) # algorithm = OMOPSO(problem, epsilons=0.5) # algorithm = EpsNSGAII(problem, epsilons=0.5) # algorithm = SPEA2(problem) # algorithm = GDE3(problem) algorithm = NSGAII(problem) # algorithm = NSGAIII(problem,divisions_outer=25) algorithm.population_size = 10 algorithm.run(Algorithm_Run_NUM) # feasible_solutions = [s for s in algorithm.result if s.feasible] # if len(feasible_solutions) > 0: # print(feasible_solutions.objectives) # print("------------------") # print("Population size: ") # print(len(algorithm.result)) # # The output shows on each line the objectives for a Pareto optimal solution: # for solution in algorithm.result: # print(solution.variables) objectives_result = algorithm.result[0].objectives x_result = algorithm.result[0].variables x_matrix = np.zeros([n, m]) for i in range(n): for j in range(m): x_matrix[i][j] = round(x_result[j + i * m]) r_s_i = x_matrix.sum(axis=1) r_d_j = x_matrix.sum(axis=0) print("objectives_result:") print(objectives_result) # print("x_matrix:") # print(x_matrix) print("r_s_i:") print(r_s_i) print("std of r_s_i-s_i:") print(np.std(r_s_i - s_i)) print("r_d_j:") print(r_d_j) # plot the results using matplotlib import matplotlib.pyplot as plt plt.scatter([s.objectives[0] for s in algorithm.result], [s.objectives[1] for s in algorithm.result]) plt.xlabel("$f_1(x)$") plt.ylabel("$f_2(x)$") plt.show() return objectives_result, x_matrix '''
def moea(name, solsize, popsize, wscalar_, moea_type, max_gen=float('inf'), timeLimit=float('inf')): from platypus import Problem, TournamentSelector from platypus import NSGAII, NSGAIII, SPEA2 from platyplus.operators import varOr, mutGauss, cxUniform from platyplus.types import RealGauss from platyplus.algorithms import SMSEMOA N = wscalar_.xdim M = wscalar_.M time_start = time.perf_counter() logger.info('Running ' + moea_type + ' in ' + name) prMutation = 0.1 prVariation = 1 - prMutation vartor = varOr(cxUniform(), mutGauss(), prVariation, prMutation) def eval_(theta): return wscalar_.f(np.array(theta)) problem = Problem(N, M) problem.types[:] = [RealGauss() for i in range(N)] problem.function = eval_ if moea_type == 'NSGAII': alg = NSGAII(problem, population_size=popsize, selector=TournamentSelector(1), variator=vartor) elif moea_type == 'NSGAIII': alg = NSGAIII(problem, divisions_outer=3, population_size=popsize, selector=TournamentSelector(1), variator=vartor) elif moea_type == 'SPEA2': alg = SPEA2(problem, population_size=popsize, selector=TournamentSelector(1), variator=vartor) elif moea_type == 'SMSdom': alg = SMSEMOA(problem, population_size=popsize, selector=TournamentSelector(1), variator=vartor, selection_method='nbr_dom') elif moea_type == 'SMShv': alg = SMSEMOA(problem, population_size=popsize, selector=TournamentSelector(1), variator=vartor, selection_method='hv_contr') gen = 1 while gen < max_gen and time.perf_counter() - time_start < timeLimit: alg.step() gen += 1 alg.population_size = solsize alg.step() moeaSols = [eval_(s.variables) for s in alg.result] moea_time = time.perf_counter() - time_start logger.info(moea_type + ' in ' + name + ' finnished.') return moeaSols, moea_time