def get_alorithm(name, method_args): if name == 'ga': from pymoo.algorithms.so_genetic_algorithm import ga return ga(**method_args) elif name == 'nsga2': from pymoo.algorithms.nsga2 import nsga2 return nsga2(**method_args) elif name == 'rnsga2': from pymoo.algorithms.rnsga2 import rnsga2 return rnsga2(**method_args) elif name == 'nsga3': from pymoo.algorithms.nsga3 import nsga3 return nsga3(**method_args) elif name == 'unsga3': from pymoo.algorithms.unsga3 import unsga3 return unsga3(**method_args) elif name == 'rnsga3': from pymoo.algorithms.rnsga3 import rnsga3 return rnsga3(**method_args) elif name == 'moead': from pymoo.algorithms.moead import moead return moead(**method_args) elif name == 'de': from pymoo.algorithms.so_de import de return de(**method_args) else: raise Exception("Algorithm not known.")
def test_same_seed_same_result(self): problem = get_problem("zdt3") algorithm = nsga2(pop_size=100, elimate_duplicates=True) res1 = minimize(problem, algorithm, ('n_gen', 20), seed=1) res2 = minimize(problem, algorithm, ('n_gen', 20), seed=1) self.assertEqual(res1.X.shape, res2.X.shape) self.assertTrue(np.all(np.allclose(res1.X, res2.X)))
def test_no_pareto_front_given(self): class ZDT1NoPF(ZDT): def _evaluate(self, x, out, *args, **kwargs): f1 = x[:, 0] g = 1 + 9.0 / (self.n_var - 1) * np.sum(x[:, 1:], axis=1) f2 = g * (1 - np.power((f1 / g), 0.5)) out["F"] = np.column_stack([f1, f2]) algorithm = nsga2(pop_size=100, elimate_duplicates=True) minimize(ZDT1NoPF(), algorithm, ('n_gen', 20), seed=1, verbose=True)
def test_crossover(self): for crossover in ['real_de', 'real_sbx', 'real_exp']: print(crossover) method = ga(pop_size=20, crossover=get_crossover(crossover, prob=0.95)) minimize(get_problem("sphere"), method, ("n_gen", 20)) for crossover in [ 'bin_ux', 'bin_hux', 'bin_one_point', 'bin_two_point' ]: print(crossover) method = nsga2(pop_size=20, crossover=get_crossover(crossover, prob=0.95)) minimize(get_problem("zdt5"), method, ("n_gen", 20))
def rnsga2(ref_points, epsilon=0.001, normalization="front", weights=None, extreme_points_as_reference_points=False, **kwargs): """ Parameters ---------- ref_points : {ref_points} epsilon : float weights : np.array normalization : {{'no', 'front', 'ever'}} extreme_points_as_reference_points : bool Returns ------- rnsga2 : :class:`~pymoo.model.algorithm.Algorithm` Returns an RNSGA2 algorithm object. """ rnsga2 = nsga2(**kwargs) rnsga2.epsilon = epsilon rnsga2.weights = weights rnsga2.normalization = normalization rnsga2.selection = RandomSelection() rnsga2.survival = RankAndModifiedCrowdingSurvival( ref_points, epsilon, weights, normalization, extreme_points_as_reference_points) return rnsga2
def solve(fd: FoodDistribution): problem = SolverProblem(fd) method = nsga2(pop_size=70) t = time.time() print('start') res = minimize(problem, method, termination=('n_gen', 20), seed=2, save_history=True, disp=True) print('end: ', time.time() - t) plt.figure(1) plt.clf() for g, a in enumerate(res.history): a.opt = a.pop.copy() a.opt = a.opt[a.opt.collect(lambda ind: ind.feasible)[:, 0]] I = NonDominatedSorting().do(a.opt.get("F"), only_non_dominated_front=True) a.opt = a.opt[I] X, F, CV, G = a.opt.get("X", "F", "CV", "G") plt.figure(1) plt.scatter(F[:, 0], F[:, 1], c=[[g / len(res.history), 0, 0]], s=5**2) plt.figure(2) plt.clf() plt.scatter(F[:, 0], F[:, 1], s=5**2) plt.xlabel('remaining demand') plt.ylabel('cost') plt.savefig('../g_{}.eps'.format(g), format='eps') plt.figure(1) #plotting.plot(res.F, no_fill=True, show=False) plt.xlabel('remaining demand') plt.ylabel('cost') plt.show() plt.savefig('../scatter.eps', format='eps') return res.X
n_runs = 100 # problems to be investigated # problems = ['zdt1', 'zdt2', 'zdt3', 'zdt4', 'zdt6'] problems = setup.keys() # path were the files for this experiment are saved path = os.path.join(prefix, name) for _problem in problems: s = setup[_problem] problem = s['problem'] method = nsga2(pop_size=s['pop_size'], crossover=s['crossover'], mutation=s['mutation'], eliminate_duplicates=True) termination = s['termination'] for run in range(1, n_runs + 1): fname = "%s_%s.run" % (_problem, run) _in = os.path.join(path, fname) _out = "results/%s/%s/%s_%s.out" % (name, _problem.replace( "_", "/"), _problem, run) data = { 'args': [problem, method, termination], 'kwargs': { 'seed': run,
f1 = x[:, 0] c = anp.sum(x[:, 1:], axis=1) g = 1.0 + 9.0 * c / (self.n_var - 1) f2 = g * (1 - anp.power((f1 * 1.0 / g), 2)) out["F"] = anp.column_stack([f1, f2]) problem = ZDT2(n_var=30) # create the algorithm instance by specifying the intended parameters # from pymoo.algorithms.NSGAII import NSGAII # algorithm = NSGAII("real", pop_size=100, verbose=True) from pymoo.algorithms.nsga2 import nsga2 algorithm = nsga2("real", pop_size=100, verbose=True) start_time = time.time() # save the history in an object to observe the convergence over generations history = [] # number of generations to run it n_gen = 200 # solve the problem and return the results X, F, G = algorithm.solve(problem, evaluator=(100 * n_gen), seed=2, return_only_feasible=False, return_only_non_dominated=False,
def _calc_pareto_front(self): return -15 def _calc_pareto_set(self): return anp.array([ 1, 1, ]) myProblem = myProblem() method = nsga2(pop_size=50, sampling=RandomSampling(var_type=np.int), crossover=SimulatedBinaryCrossover(prob=0.9, eta=15, var_type=np.int), mutation=PolynomialMutation(prob=None, eta=20), elimate_duplicates=True) res = minimize(myProblem, method, termination=('n_gen', 50), disp=False) # print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F)) print("Best solution found: %s" % res.X) print("Function value: %s" % res.F) print("Constraint violation: %s" % res.CV) # plotting.plot(res.F, no_fill=True) np.save('Best_sol.npy', res.X) np.save('Func_val.npy', res.F)
def run_nsgaii_solver_pb(name): # initialize engine eng = matlab.engine.start_matlab() # add path to matlab benchmarks repertory, for example: eng.addpath("../problems") # initialize dictionnary dict_problems = dict() # BK1 dict_problems["BK1"] = (eng.BK1, 2, 2, np.array([-5, -5]), np.array([10, 10])) # CL1 F_p = 10 sigma_p = 10 lb = np.array([ F_p / sigma_p, math.sqrt(2) * F_p / sigma_p, math.sqrt(2) * F_p / sigma_p, F_p / sigma_p, ]) dict_problems["CL1"] = (eng.CL1, 4, 2, np.copy(lb), (3 * F_p / sigma_p) * np.ones(4)) # Deb41 function lb = np.array([0.1, 0.0]) dict_problems["Deb41"] = (eng.Deb41, 2, 2, lb, np.ones(2)) # Deb512a function dict_problems["Deb512a"] = (eng.Deb512a, 2, 2, np.zeros(2), np.ones(2)) # Deb512b function dict_problems["Deb512b"] = (eng.Deb512b, 2, 2, np.zeros(2), np.ones(2)) # Deb512c function dict_problems["Deb512c"] = (eng.Deb512c, 2, 2, np.zeros(2), np.ones(2)) # Deb513 function: dict_problems["Deb513"] = (eng.Deb513, 2, 2, np.zeros(2), np.ones(2)) # Deb521a function dict_problems["Deb521a"] = (eng.Deb521a, 2, 2, np.zeros(2), np.ones(2)) # Deb521b function dict_problems["Deb521b"] = (eng.Deb521b, 2, 2, np.zeros(2), np.ones(2)) # Deb53 function dict_problems["Deb53"] = (eng.Deb53, 2, 2, np.zeros(2), np.ones(2)) # DG01 function dict_problems["DG01"] = (eng.DG01, 1, 2, -10 * np.ones(1), 13 * np.ones(1)) # DPAM1 function dict_problems["DPAM1"] = (eng.DPAM1, 10, 2, -0.3 * np.ones(10), 0.3 * np.ones(10)) # DTLZ1 function dict_problems["DTLZ1"] = (eng.DTLZ1, 7, 3, np.zeros(7), np.ones(7)) # DTLZ1n2 function dict_problems["DTLZ1n2"] = (eng.DTLZ1n2, 2, 2, np.zeros(2), np.ones(2)) # DTLZ2 function dict_problems["DTLZ2"] = (eng.DTLZ2, 12, 3, np.zeros(12), np.ones(12)) # DTLZn2 function dict_problems["DTLZ2n2"] = (eng.DTLZ2n2, 2, 2, np.zeros(2), np.ones(2)) # DTLZ3 function dict_problems["DTLZ3"] = (eng.DTLZ3, 12, 3, np.zeros(12), np.ones(12)) # DTLZ3n2 function dict_problems["DTLZ3n2"] = (eng.DTLZ3n2, 2, 2, np.zeros(2), np.ones(2)) # DTLZ4 function dict_problems["DTLZ4"] = (eng.DTLZ4, 12, 3, np.zeros(12), np.ones(12)) # DTLZ4n2 function dict_problems["DTLZ4n2"] = (eng.DTLZ4n2, 2, 2, np.zeros(2), np.ones(2)) # DTLZ5 function dict_problems["DTLZ5"] = (eng.DTLZ5, 12, 3, np.zeros(12), np.ones(12)) # DTLZ5n2 function dict_problems["DTLZ5n2"] = (eng.DTLZ5n2, 2, 2, np.zeros(2), np.ones(2)) # DTLZ6 function dict_problems["DTLZ6"] = (eng.DTLZ6, 22, 3, np.zeros(22), np.ones(22)) # DTLZ6n2 function dict_problems["DTLZ6n2"] = (eng.DTLZ6n2, 2, 2, np.zeros(2), np.ones(2)) # ex005 function dict_problems["ex005"] = (eng.ex005, 2, 2, np.array([-1, 1]), np.array([2, 2])) # Far1 function dict_problems["Far1"] = (eng.Far1, 2, 2, -1 * np.ones(2), np.ones(2)) # FES1 function dict_problems["FES1"] = (eng.FES1, 10, 2, np.zeros(10), np.ones(10)) # FES2 function dict_problems["FES2"] = (eng.FES2, 10, 3, np.zeros(10), np.ones(10)) # FES3 function dict_problems["FES3"] = (eng.FES3, 10, 4, np.zeros(10), np.ones(10)) # Fonseca function dict_problems["Fonseca"] = (eng.Fonseca, 2, 2, -4 * np.ones(2), 4 * np.ones(2)) # I1 function dict_problems["I1"] = (eng.I1, 8, 3, np.zeros(8), np.ones(8)) # I2 function dict_problems["I2"] = (eng.I2, 8, 3, np.zeros(8), np.ones(8)) # I3 function dict_problems["I3"] = (eng.I3, 8, 3, np.zeros(8), np.ones(8)) # I4 function dict_problems["I4"] = (eng.I4, 8, 3, np.zeros(8), np.ones(8)) # I5 function dict_problems["I5"] = (eng.I5, 8, 3, np.zeros(8), np.ones(8)) # IKK1 function dict_problems["IKK1"] = (eng.IKK1, 2, 3, -50 * np.ones(2), 50 * np.ones(2)) # IM1 function dict_problems["IM1"] = (eng.IM1, 2, 2, np.ones(2), np.array([4, 2])) # Jin1 function dict_problems["Jin1"] = (eng.Jin1, 2, 2, np.zeros(2), np.ones(2)) # Jin2 function dict_problems["Jin2"] = (eng.Jin2, 2, 2, np.zeros(2), np.ones(2)) # Jin3 function dict_problems["Jin3"] = (eng.Jin3, 2, 2, np.zeros(2), np.ones(2)) # Jin4 function dict_problems["Jin4"] = (eng.Jin4, 2, 2, np.zeros(2), np.ones(2)) # Kursawe function dict_problems["Kursawe"] = (eng.Kursawe, 3, 2, -5 * np.ones(3), 5 * np.ones(3)) # L1ZDT4 function dict_problems["L1ZDT4"] = ( eng.L1ZDT4, 10, 1, np.concatenate([np.array([0]), -5 * np.ones(9)]), np.concatenate([np.array([1]), 5 * np.ones(9)]), ) # L2ZDT1 function dict_problems["L2ZDT1"] = (eng.L2ZDT1, 30, 2, np.zeros(30), np.ones(30)) # L2ZDT2 function dict_problems["L2ZDT2"] = (eng.L2ZDT2, 30, 2, np.zeros(30), np.ones(30)) # L2ZDT3 function dict_problems["L2ZDT3"] = (eng.L2ZDT3, 30, 2, np.zeros(30), np.ones(30)) # L2ZDT4 function dict_problems["L2ZDT4"] = (eng.L2ZDT4, 30, 2, np.zeros(30), np.ones(30)) # L2ZDT6 function dict_problems["L2ZDT6"] = (eng.L2ZDT6, 10, 2, np.zeros(10), np.ones(10)) # L3ZDT1 function dict_problems["L3ZDT1"] = (eng.L3ZDT1, 30, 2, np.zeros(30), np.ones(30)) # L3ZDT2 function dict_problems["L3ZDT2"] = (eng.L3ZDT2, 30, 2, np.zeros(30), np.ones(30)) # L3ZDT3 function dict_problems["L3ZDT3"] = (eng.L3ZDT3, 30, 2, np.zeros(30), np.ones(30)) # L3ZDT4 function dict_problems["L3ZDT4"] = (eng.L3ZDT4, 30, 2, np.zeros(30), np.ones(30)) # L3ZDT6 function dict_problems["L3ZDT6"] = (eng.L3ZDT6, 10, 2, np.zeros(10), np.ones(10)) # LE1 function dict_problems["LE1"] = (eng.LE1, 2, 2, np.zeros(2), np.ones(2)) # lovison1 function dict_problems["lovison1"] = (eng.lovison1, 2, 2, np.zeros(2), 3 * np.ones(2)) # lovison2 function dict_problems["lovison2"] = (eng.lovison2, 2, 2, -0.5 * np.ones(2), np.array([0, 0.5])) # lovison3 function dict_problems["lovison3"] = (eng.lovison3, 2, 2, np.array([0, -4]), np.array([6, 4])) # lovison4 function dict_problems["lovison4"] = (eng.lovison4, 2, 2, np.array([0, -1]), np.array([6, 1])) # lovison5 function dict_problems["lovison5"] = (eng.lovison5, 3, 3, -1 * np.ones(3), 4 * np.ones(3)) # lovison6 function dict_problems["lovison6"] = (eng.lovison6, 3, 3, -1 * np.ones(3), 4 * np.ones(3)) # LRS1 function dict_problems["LRS1"] = (eng.LRS1, 2, 2, -50 * np.ones(2), 50 * np.ones(2)) # MHHM1 function dict_problems["MHHM1"] = (eng.MHHM1, 1, 3, np.zeros(1), np.ones(1)) # MHHM2 function dict_problems["MHHM2"] = (eng.MHHM2, 2, 3, np.zeros(2), np.ones(2)) # MLF1 function dict_problems["MLF1"] = (eng.MLF1, 1, 2, np.zeros(1), 20 * np.ones(1)) # MLF2 function dict_problems["MLF2"] = (eng.MLF2, 2, 2, -2 * np.ones(2), 2 * np.ones(2)) # MOP1 function dict_problems["MOP1"] = (eng.MOP1, 1, 2, -10**(-5) * np.ones(1), 10**(5) * np.ones(1)) # MOP2 function dict_problems["MOP2"] = (eng.MOP2, 4, 2, -4 * np.ones(4), 4 * np.ones(4)) # MOP3 function dict_problems["MOP3"] = (eng.MOP3, 2, 2, -math.pi * np.ones(2), math.pi * np.ones(2)) # MOP4 function dict_problems["MOP4"] = (eng.MOP4, 3, 2, -5 * np.ones(3), 5 * np.ones(3)) # MOP5 function dict_problems["MOP5"] = (eng.MOP5, 2, 3, -30 * np.ones(2), 30 * np.ones(2)) # MOP6 function dict_problems["MOP6"] = (eng.MOP6, 2, 2, np.zeros(2), np.ones(2)) # MOP7 function dict_problems["MOP7"] = (eng.MOP7, 2, 3, -400 * np.ones(2), 400 * np.ones(2)) # OKA1 function lb = np.array( [6 * math.sin(math.pi / 12), -2 * math.pi * math.sin(math.pi / 12)]) ub = np.array([ 6 * math.sin(math.pi / 12) + 2 * math.pi * math.cos(math.pi / 12), 6 * math.cos(math.pi / 12), ]) dict_problems["OKA1"] = (eng.OKA1, 2, 2, np.copy(lb), np.copy(ub)) # OKA2 function dict_problems["OKA2"] = ( eng.OKA2, 3, 2, np.array([-math.pi, -5, -5]), np.array([math.pi, 5, 5]), ) # QV1 function dict_problems["QV1"] = (eng.QV1, 10, 2, -5.12 * np.ones(10), 5.12 * np.ones(10)) # Sch1 function dict_problems["Sch1"] = (eng.Sch1, 1, 2, np.zeros(1), 5 * np.ones(1)) # SK1 function dict_problems["SK1"] = (eng.SK1, 1, 2, np.array([-10]), np.array([10])) # SK2 function dict_problems["SK2"] = (eng.SK2, 4, 2, -10 * np.ones(4), 10 * np.ones(4)) # SP1 function dict_problems["SP1"] = (eng.SP1, 2, 2, -1 * np.ones(2), 5 * np.ones(2)) # SSFYY1 function dict_problems["SSFYY1"] = (eng.SSFYY1, 2, 2, -100 * np.ones(2), 100 * np.ones(2)) # SSFYY2 function dict_problems["SSFYY2"] = (eng.SSFYY2, 1, 2, -100 * np.ones(1), 100 * np.ones(1)) # TKLY1 function dict_problems["TKLY1"] = (eng.TKLY1, 4, 2, np.array([0.1, 0, 0, 0]), np.ones(4)) # VFM1 function dict_problems["VFM1"] = (eng.VFM1, 2, 3, -2 * np.ones(2), 2 * np.ones(2)) # VU1 function dict_problems["VU1"] = (eng.VU1, 2, 2, -3 * np.ones(2), 3 * np.ones(2)) # VU2 function dict_problems["VU2"] = (eng.VU2, 2, 2, -3 * np.ones(2), 3 * np.ones(2)) # WFG1 function dict_problems["WFG1"] = (eng.WFG1, 8, 3, np.zeros(8), 2 * np.linspace(1, 8, 8)) # WFG2 function dict_problems["WFG2"] = (eng.WFG2, 8, 3, np.zeros(8), 2 * np.linspace(1, 8, 8)) # WFG3 function dict_problems["WFG3"] = (eng.WFG3, 8, 3, np.zeros(8), 2 * np.linspace(1, 8, 8)) # WFG4 function dict_problems["WFG4"] = (eng.WFG4, 8, 3, np.zeros(8), 2 * np.linspace(1, 8, 8)) # WFG5 function dict_problems["WFG5"] = (eng.WFG5, 8, 3, np.zeros(8), 2 * np.linspace(1, 8, 8)) # WFG6 function dict_problems["WFG6"] = (eng.WFG6, 8, 3, np.zeros(8), 2 * np.linspace(1, 8, 8)) # WFG7 function dict_problems["WFG7"] = (eng.WFG7, 8, 3, np.zeros(8), 2 * np.linspace(1, 8, 8)) # WFG8 function dict_problems["WFG8"] = (eng.WFG8, 8, 3, np.zeros(8), 2 * np.linspace(1, 8, 8)) # WFG9 function dict_problems["WFG9"] = (eng.WFG9, 8, 3, np.zeros(8), 2 * np.linspace(1, 8, 8)) # ZDT1 function dict_problems["ZDT1"] = (eng.ZDT1, 30, 2, np.zeros(30), np.ones(30)) # ZDT2 function dict_problems["ZDT2"] = (eng.ZDT2, 30, 2, np.zeros(30), np.ones(30)) # ZDT3 function dict_problems["ZDT3"] = (eng.ZDT3, 30, 2, np.zeros(30), np.ones(30)) # ZDT4 function dict_problems["ZDT4"] = ( eng.ZDT4, 10, 2, np.concatenate([np.array([0]), -5 * np.ones(9)]), np.concatenate([np.array([1]), 5 * np.ones(9)]), ) # ZDT6 function dict_problems["ZDT6"] = (eng.ZDT6, 10, 2, np.zeros(10), np.ones(10)) # ZLT1 function dict_problems["ZLT1"] = (eng.ZLT1, 10, 3, -1000 * np.ones(10), 1000 * np.ones(10)) class MXProblem(Problem): def __init__(self, id_problem): #, n_var, n_obj, xl, xu): #super().__init__(n_var=n_var, n_obj=n_obj, xl=xl, xu=xu) super().__init__() self.id_problem = id_problem self.x_cache = [] self.f_cache = [] # add problems properties probproperties = dict_problems[id_problem] self.n_var = probproperties[1] self.n_obj = probproperties[2] self.xl = probproperties[3] self.xu = probproperties[4] # x represents a population array def _evaluate(self, x, out, *args, **kwargs): # convert list of x to list of matlab element l_x_matlab = [ matlab.double(copy.copy(x[i, :].tolist())) for i in range(x.shape[0]) ] self.x_cache += x.tolist() # get result of the matlab function for the list of x l_f_matlab = [] for elt in l_x_matlab: l_f_matlab += [ dict_problems[self.id_problem][0](eng.transpose(elt), nargout=1) ] for elt in l_f_matlab: self.f_cache += [[elt[i][0] for i in range(elt.size[0])]] # convert result to objective function f l_f = [] for elt in l_f_matlab: l_f += [[elt[i][0] for i in range(elt.size[0])]] l_x_matlab = [] l_f_matlab = [] out["F"] = np.asarray(l_f) def write_cache(problem, filename): with open(filename, "w") as f: f.write(str(problem.n_var) + " " + str(problem.n_obj) + "\n") for elt_x, elt_f in zip(problem.x_cache, problem.f_cache): for x in elt_x: f.write(str(x) + " ") for y in elt_f: f.write(str(y) + " ") f.write("\n") foldername = "" # add foldername to which cache files will be generated for seed in [ 1, 4734, 6652, 3507, 1121, 3500, 5816, 2006, 9622, 6117, 1571, 4117, 3758, 8045, 6554, 8521, 4889, 5893, 9123, 7238, 3089, 4197, 6489, 8, 7551, 7621, 4809, 8034, 3487, 3680, 5363, 7786, 104, 8171, 7505, 7075, 9656, 4569, 3121, 4187, 201, 951, 1093, 3147, 1978, 1475, 2896, 9814, 8803, 1084 ]: method = nsga2(pop_size=100) problem = MXProblem(name) minimize(problem, method, termination=("n_gen", 200), seed=seed, save_history=True, disp=False) write_cache(problem, foldername + name + "_nsgaii_" + str(seed) + ".txt") return 0
x = anp.linspace(0, 1, n_pareto_points) return anp.array([x, 1 - anp.power(x, 2)]).T def _evaluate(self, x, out, *args, **kwargs): f1 = x[:, 0] c = anp.sum(x[:, 1:], axis=1) g = 1.0 + 9.0 * c / (self.n_var - 1) f2 = g * (1 - anp.power((f1 * 1.0 / g), 2)) out["F"] = anp.column_stack([f1, f2]) problem = ZDT2() # print(problem) # # load a test or define your own problem # problem = get_problem("zdt1") # get the optimal solution of the problem for the purpose of comparison pf = problem.pareto_front() # create the algorithm object method = nsga2(pop_size=100, elimate_duplicates=True) # execute the optimization res = minimize(problem, method, termination=('n_gen', 200), pf=pf, disp=True) # plot the results as a scatter plot plotting.plot(pf, res.F, labels=["Pareto-Front", "F"])