def test_multiObjOptimization(self): algorithm = NSGA2(pop_size=10, n_offsprings=10, sampling=get_sampling("real_random"), crossover=get_crossover("real_sbx", prob=0.9, eta=15), mutation=get_mutation("real_pm", eta=20), eliminate_duplicates=True) termination = get_termination("n_gen", 5) bayer = self.datasetUtils.readCFAImages() twoComplement = self.datasetUtils.twoComplementMatrix(bayer) twoComplement = twoComplement.astype("float32") problem = MyProblem(twoComplement) res = minimize(problem, algorithm, termination, save_history=True, verbose=True) # Objective Space res.F = 1 / res.F plot = Scatter(title="Objective Space") plot.add(res.F) plot.show() print("Best filter{}".format(np.reshape(res.opt[-1].X, [2, 2])))
def main(args): # preferences if args.prefer is not None: preferences = {} for p in args.prefer.split("+"): k, v = p.split("#") if k == 'top1': preferences[k] = 100 - float(v) # assuming top-1 accuracy else: preferences[k] = float(v) weights = np.fromiter(preferences.values(), dtype=float) archive = json.load(open(args.expr))['archive'] subnets, top1, sec_obj = [v[0] for v in archive], [v[1] for v in archive ], [v[2] for v in archive] sort_idx = np.argsort(top1) F = np.column_stack((top1, sec_obj))[sort_idx, :] front = NonDominatedSorting().do(F, only_non_dominated_front=True) pf = F[front, :] ps = np.array(subnets)[sort_idx][front] if args.prefer is not None: # choose the architectures thats closest to the preferences I = get_decomposition("asf").do(pf, weights).argsort()[:args.n] else: # choose the architectures with highest trade-off dm = HighTradeoffPoints(n_survive=args.n) I = dm.do(pf) # always add most accurate architectures I = np.append(I, 0) # create the supernet from evaluator import OFAEvaluator supernet = OFAEvaluator(model_path=args.supernet_path) for idx in I: save = os.path.join(args.save, "net-flops@{:.0f}".format(pf[idx, 1])) os.makedirs(save, exist_ok=True) subnet, _ = supernet.sample({ 'ks': ps[idx]['ks'], 'e': ps[idx]['e'], 'd': ps[idx]['d'] }) with open(os.path.join(save, "net.subnet"), 'w') as handle: json.dump(ps[idx], handle) supernet.save_net_config(save, subnet, "net.config") supernet.save_net(save, subnet, "net.inherited") if _DEBUG: print(ps[I]) plot = Scatter() plot.add(pf, alpha=0.2) plot.add(pf[I, :], color="red", s=100) plot.show() return
def run(countryname, capacity): problem = ScheduleProblem(country_name=countryname, critical_capacity=capacity, record_all=True) algorithm = NSGA2( pop_size=100, n_offsprings=100, sampling=get_sampling("int_random"), crossover=get_crossover("int_sbx", prob=0.9, eta=15), mutation=get_mutation("int_pm", eta=20), eliminate_duplicates=True ) termination = get_termination("n_gen", 100) res = minimize(problem, algorithm, termination, seed=1, pf=problem.pareto_front(use_cache=False), save_history=True, verbose=True) # create the performance indicator object with reference point (4,4) metric = Hypervolume(ref_point=np.array([1.0, 1.0])) # collect the population in each generation pop_each_gen = [a.pop for a in res.history] with open("./experiments/ga_{}_lastpop.json".format(countryname), 'w') as f: json.dump( {"df":[e.to_dict() for e in problem.last[0]],"x":problem.last[1].tolist()}, f) with open("./experiments/ga_{}_lastobj.json".format(countryname), 'w') as f: json.dump( {"deaths": problem.last_objectives[0].tolist(), "activity":problem.last_objectives[1].tolist()} , f) # Objective Space fig = plt.figure() plot = Scatter(title = "Objective Space") plot.add(res.F) plt.savefig("./experiments/ga_{}_objective.png".format(countryname)) # receive the population in each generation obj_and_feasible_each_gen = [pop[pop.get("feasible")[:,0]].get("F") for pop in pop_each_gen] # calculate for each generation the HV metric hv = [metric.calc(f) for f in obj_and_feasible_each_gen] # function evaluations at each snapshot n_evals = np.array([a.evaluator.n_eval for a in res.history]) # visualize the convergence curve fig = plt.figure() plt.plot(n_evals, hv, '-o') plt.title("Convergence") plt.xlabel("Function Evaluations") plt.ylabel("Hypervolume") plt.savefig("./experiments/ga_{}_hypervolume.png".format(countryname)) plt.show()
def demo_hello_world(): problem = get_problem("zdt1") algorithm = NSGA2(pop_size=10) res = minimize(problem, algorithm, ('n_gen', 10), seed=1, verbose=False) plot = Scatter() plot.add(problem.pareto_front(), plot_type="line", color="black", alpha=0.7) plot.add(res.F, color="red") plot.show()
def log(self, res_eval): self.res.problem = None for h in self.res_run.history: h.problem = None h.initialization = None self.res_run.algorithm.problem = None self.res_run.algorithm.initialization.sampling = None with open(self.logdir + 'res_train.pk', 'wb') as f: pickle.dump(self.res_run, f) with open(self.logdir + 'res_eval.pk', 'wb') as f: pickle.dump(res_eval, f) print('Run has terminated successfully') plot = Scatter() plot.add(res_eval['F'], color="red") plot.show()
for problem, name in zip([SYMPART(), SYMPARTRotated()], ["SYM-PART", "SYM-PART rotated"]): ref_dirs = get_reference_directions("das-dennis", problem.n_obj, n_partitions=20) PS = problem.pareto_set(500) PF = problem.pareto_front(500) algorithm = NSGA3(ref_dirs=ref_dirs) res = minimize(problem, algorithm, ('n_gen', 500), seed=1, verbose=False) fig_name = f"{algorithm.__class__.__name__} on {name}" # visualize decision space plot = Scatter(title="Decision Space") plot.add(PS, s=10, color='r', label="PS") plot.add(res.X, s=30, color='b', label="Obtained solutions") plot.do() plt.legend() # visualize objective space plot = Scatter(title="Objective Space") plot.add(PF, s=10, color='r', label="PF") plot.add(res.F, s=30, color='b', label="Obtained solutions") plot.do() plt.legend() plt.show()
def _do(self, problem, pop, n_survive, out=None, algorithm=None, **kwargs): X, F = pop.get("X", "F") if F.shape[1] != 1: raise ValueError("FitnessSurvival can only used for single objective single!") # calculate the normalized distance D = vectorized_cdist(X, X) np.fill_diagonal(D, np.inf) norm = np.linalg.norm(problem.xu - problem.xl) D /= norm # find the best solution in the population S = np.argmin(F[:, 0]) pop[S].set("rank", 0) # initialize utility data structures survivors = [S] remaining = [k for k in range(len(pop)) if k != S] n_neighbors = 10 cnt = 1 while len(survivors) < n_survive: closest = D[survivors, :][:, remaining].argmin(axis=0) delta_f = F[remaining, 0] - F[np.argmin(F[:, 0]), 0] delta_x = D[closest, remaining] fitness = delta_f / delta_x S = remaining[np.argmin(fitness)] if algorithm.n_gen == 20: sc = Scatter(title=algorithm.n_gen) sc.add(curve(problem), plot_type="line", color="black") sc.add(np.column_stack([pop.get("X"), F[:, 0]]), color="purple") sc.add(np.column_stack([pop[survivors].get("X"), pop[survivors].get("F")]), color="red", s=40, marker="x") sc.do() plt.ylim(0, 2) plt.show() plt.close() # update the survivors and remaining individuals individual = pop[S] neighbors = pop[D[S].argsort()[:n_neighbors]] # if individual has had neighbors before update them N = individual.get("neighbors") if N is not None: neighbors = Population.merge(neighbors, N) neighbors = neighbors[neighbors.get("F")[:, 0].argsort()[:n_neighbors]] individual.set("neighbors", neighbors) individual.set("rank", cnt) survivors.append(S) remaining = [k for k in remaining if k != S] cnt += 1 return pop[survivors]
def _do(self, problem, pop, n_survive, out=None, algorithm=None, **kwargs): X, F = pop.get("X", "F") if F.shape[1] != 1: raise ValueError( "FitnessSurvival can only used for single objective single!") # calculate the normalized distance D = vectorized_cdist(X, X) # np.fill_diagonal(D, np.inf) norm = np.linalg.norm(problem.xu - problem.xl) D /= norm # find the best solution in the population S = np.argmin(F[:, 0]) # create the data structure to work with in order to flag survivors survivors = [S] remaining = [k for k in range(len(pop)) if k != S] # assign all solutions to the minimum first assigned_to = np.full(len(pop), S) dist = D[S, :] # never select more than actually should survive while len(survivors) < n_survive: rem = np.array(remaining) vals = np.full(len(pop), np.inf) for S in survivors: I = rem[assigned_to[rem] == S] if len(I) > 0: vals[I] = calc_metric(dist[I], F[I], p=2) select = vals.argmin() reassign = np.logical_and(D[select] < dist, F[:, 0] >= F[select, 0]) assigned_to[reassign] = select survivors.append(select) remaining = [k for k in remaining if k != select] plt.scatter(X, F) plt.scatter(X[survivors], F[survivors], color="red", marker='x') _curve = curve(problem) plt.plot(_curve[:, 0], _curve[:, 1], color="black") plt.xlabel("X") plt.ylabel("F") plt.show() print(survivors) # set the neighborhood for the local search for each survivor # for k in survivors: # # individual = pop[k] # # if individual has had neighbors before update them # N = individual.get("neighbors") # if N is not None: # neighbors = Population.merge(neighbors, N) # neighbors = neighbors[neighbors.get("F")[:, 0].argsort()[:10]] # # individual.set("neighbors", neighbors) return pop[survivors] # do the non-dominated sorting val = np.column_stack([-D[S, :], F[:, 0]]) fronts = NonDominatedSorting().do(val) # for each of the fronts regarding the dummy objectives for k, front in enumerate(fronts): if len(survivors) + len(front) <= n_survive: survivors.extend(front) # if we have found the splitting front else: S = F[front, 0].argmin() survivors.append(front[S]) # the extreme point for decision making _D = D[front, :][:, front] farthest = _D[S].argmax() # sort by distance to best delta_x = _D[S, :] / _D[S, farthest] delta_f = (F[front, 0] - F[S, 0]) / (F[front[farthest], 0] - F[S, 0]) f = np.column_stack([-delta_x, delta_f]) z = np.array([-1, 0]) p = 2 val = ((f - z)**p).sum(axis=1)**(1 / p) I = val.argsort()[:n_survive] pop[front[I]].set("v", val[I]) survivors.extend(front[I]) plt.scatter(X, F) plt.scatter(X[survivors], F[survivors], color="red", marker='x') _curve = curve(problem) plt.plot(_curve[:, 0], _curve[:, 1], color="black") plt.xlabel("X") plt.ylabel("F") plt.show() return pop[fronts[0]] X, F = pop.get("X", "F") if F.shape[1] != 1: raise ValueError( "FitnessSurvival can only used for single objective single!") # the final indices of surviving individuals survivors = [] # calculate the normalized distance D = vectorized_cdist(X, X) # np.fill_diagonal(D, np.inf) norm = np.linalg.norm(problem.xu - problem.xl) D /= norm # find the best solution in the population S = np.argmin(F[:, 0]) # create the data structure to work with in order to flag survivors survivors = [] remaining = [k for k in range(len(pop)) if k != S] while len(survivors) < n_survive: plt.figure(figsize=(5, 5)) plt.scatter(X, F, color="black", alpha=0.8, s=20, label='pop') plt.scatter(X[survivors], F[survivors], color="red", label="survivors") v = np.round(pop[survivors].get("v"), 3) for i in range(len(survivors)): x = X[survivors][i] y = F[survivors][i] plt.text(x, y, v[i], fontsize=9) plt.scatter(X[farthest], F[farthest], color="green", label="survivors") _curve = curve(problem) plt.plot(_curve[:, 0], _curve[:, 1], color="black") plt.xlabel("X") plt.ylabel("F") plt.legend() plt.show() return pop[survivors] survivors.append(remaining[val.argmin()]) remaining = [k for k in range(len(pop)) if k != S] plt.scatter(X, F) plt.scatter(X[survivors], F[survivors], color="red", marker='x') _curve = curve(problem) plt.plot(_curve[:, 0], _curve[:, 1], color="black") plt.xlabel("X") plt.ylabel("F") plt.show() return pop[fronts[0]] plt.scatter(delta_x, delta_f) plt.scatter(delta_x[nds], delta_f[nds], color="red") plt.xlabel("D") plt.ylabel("F") plt.show() pop[S].set("rank", 0) # initialize utility data structures survivors = [S] remaining = [k for k in range(len(pop)) if k != S] n_neighbors = 10 cnt = 1 while len(survivors) < n_survive: closest = D[survivors, :][:, remaining].argmin(axis=0) delta_f = F[remaining, 0] - F[np.argmin(F[:, 0]), 0] delta_x = D[closest, remaining] fitness = delta_f / delta_x S = remaining[np.argmin(fitness)] if algorithm.n_gen == 20: sc = Scatter(title=algorithm.n_gen) sc.add(curve(problem), plot_type="line", color="black") sc.add(np.column_stack([pop.get("X"), F[:, 0]]), color="purple") sc.add(np.column_stack( [pop[survivors].get("X"), pop[survivors].get("F")]), color="red", s=40, marker="x") sc.do() plt.ylim(0, 2) plt.show() plt.close() # update the survivors and remaining individuals individual = pop[S] neighbors = pop[D[S].argsort()[:n_neighbors]] # if individual has had neighbors before update them N = individual.get("neighbors") if N is not None: neighbors = Population.merge(neighbors, N) neighbors = neighbors[neighbors.get("F")[:, 0].argsort() [:n_neighbors]] individual.set("neighbors", neighbors) individual.set("rank", cnt) survivors.append(S) remaining = [k for k in remaining if k != S] cnt += 1 return pop[survivors]
import os import numpy as np from pymoo.configuration import get_pymoo from pymoo.decision_making.high_tradeoff import HighTradeoffPoints from pymoo.decision_making.high_tradeoff_inverted import HighTradeoffPointsInverted from pymoo.visualization.scatter import Scatter pf = np.loadtxt( os.path.join(get_pymoo(), "pymoo", "usage", "decision_making", "knee-2d.out")) dm = HighTradeoffPoints() I = dm.do(pf) plot = Scatter(angle=(0, 0)) plot.add(pf, alpha=0.2) plot.add(pf[I], color="red", s=100) plot.show()
from pymoo.algorithms.nsga3 import NSGA3 from pymoo.factory import get_problem, get_reference_directions from pymoo.optimize import minimize from pymoo.visualization.scatter import Scatter problem = get_problem("mw12") ref_dirs = get_reference_directions("das-dennis", problem.n_obj, n_points=91) algorithm = NSGA3(ref_dirs) res = minimize(problem, algorithm, ("n_gen", 600), verbose=True) plot = Scatter() plot.add(problem.pareto_front(), color="black", alpha=0.5, s=40) plot.add(res.F, color="red", marker="x") plot.show()
def search(self): if self.resume: archive = self._resume_from_dir() else: # the following lines corresponding to Algo 1 line 1-7 in the paper archive = [ ] # initialize an empty archive to store all trained CNNs # Design Of Experiment if self.iterations < 1: arch_doe = self.search_space.sample(self.n_doe) else: arch_doe = self.search_space.initialize(self.n_doe) # parallel evaluation of arch_doe top1_err, complexity = self._evaluate(arch_doe, it=0) # store evaluated / trained architectures for member in zip(arch_doe, top1_err, complexity): archive.append(member) # reference point (nadir point) for calculating hypervolume ref_pt = np.array( [np.max([x[1] for x in archive]), np.max([x[2] for x in archive])]) # main loop of the search for it in range(1, self.iterations + 1): # construct accuracy predictor surrogate model from archive # Algo 1 line 9 / Fig. 3(a) in the paper acc_predictor, a_top1_err_pred = self._fit_acc_predictor(archive) # search for the next set of candidates for high-fidelity evaluation (lower level) # Algo 1 line 10-11 / Fig. 3(b)-(d) in the paper candidates, c_top1_err_pred = self._next(archive, acc_predictor, self.n_iter) # high-fidelity evaluation (lower level) # Algo 1 line 13-14 / Fig. 3(e) in the paper c_top1_err, complexity = self._evaluate(candidates, it=it) # check for accuracy predictor's performance rmse, rho, tau = get_correlation( np.vstack((a_top1_err_pred, c_top1_err_pred)), np.array([x[1] for x in archive] + c_top1_err)) # add to archive # Algo 1 line 15 / Fig. 3(e) in the paper for member in zip(candidates, c_top1_err, complexity): archive.append(member) # calculate hypervolume hv = self._calc_hv( ref_pt, np.column_stack( ([x[1] for x in archive], [x[2] for x in archive]))) # print iteration-wise statistics print("Iter {}: hv = {:.2f}".format(it, hv)) print( "fitting {}: RMSE = {:.4f}, Spearman's Rho = {:.4f}, Kendall’s Tau = {:.4f}" .format(self.predictor, rmse, rho, tau)) # dump the statistics with open(os.path.join(self.save_path, "iter_{}.stats".format(it)), "w") as handle: json.dump( { 'archive': archive, 'candidates': archive[-self.n_iter:], 'hv': hv, 'surrogate': { 'model': self.predictor, 'name': acc_predictor.name, 'winner': acc_predictor.winner if self.predictor == 'as' else acc_predictor.name, 'rmse': rmse, 'rho': rho, 'tau': tau } }, handle) if _DEBUG: # plot plot = Scatter(legend={'loc': 'lower right'}) F = np.full((len(archive), 2), np.nan) F[:, 0] = np.array([x[2] for x in archive]) # second obj. (complexity) F[:, 1] = 100 - np.array([x[1] for x in archive]) # top-1 accuracy plot.add(F, s=15, facecolors='none', edgecolors='b', label='archive') F = np.full((len(candidates), 2), np.nan) F[:, 0] = np.array(complexity) F[:, 1] = 100 - np.array(c_top1_err) plot.add(F, s=30, color='r', label='candidates evaluated') F = np.full((len(candidates), 2), np.nan) F[:, 0] = np.array(complexity) F[:, 1] = 100 - c_top1_err_pred[:, 0] plot.add(F, s=20, facecolors='none', edgecolors='g', label='candidates predicted') plot.save( os.path.join(self.save_path, 'iter_{}.png'.format(it))) return
from pymoo.algorithms.nsga2 import NSGA2 from pymoo.optimize import minimize from pymoo.problems.multi import CTP3 from pymoo.visualization.scatter import Scatter problem = CTP3() algorithm = NSGA2() res = minimize(problem, algorithm, seed=1, verbose=True) plot = Scatter() plot.add(problem.pareto_front(), plot_type="line", color="black", alpha=0.7) plot.add(res.F, color="red") plot.show()
import numpy as np from pymoo.interface import crossover from pymoo.operators.crossover.parent_centric_crossover import PCX from pymoo.visualization.scatter import Scatter X = np.eye(3) X[1] = [0.9, 0.1, 0.1] n_points = 1000 a = X[[0]].repeat(n_points, axis=0) b = X[[1]].repeat(n_points, axis=0) c = X[[2]].repeat(n_points, axis=0) obj = PCX(eta=0.1, zeta=0.1, impl="elementwise") _X = crossover(obj, a, c, b, xl=-1, xu=1) sc = Scatter() sc.add(_X, facecolor=None, edgecolor="blue", alpha=0.7) sc.add(X, s=100, color="red") sc.add(X.mean(axis=0), color="green", label="Centroid") sc.show()
hv = [metric.calc(f) for f in obj_and_feasible_each_gen] # visualze the convergence curve plt.plot(np.arange(len(hv)), hv, '-o') plt.title("Convergence") plt.xlabel("Generation") plt.ylabel("Hypervolume") plt.show() ps = problem.pareto_set(use_cache=False, flatten=False) pf = problem.pareto_front(use_cache=False, flatten=False) # Design Space #plot = Scatter(title = "Design Space", axis_labels="x") #plot.add(res.X, s=30, facecolors='none', edgecolors='r') #res.X design space values #plot.add(ps, plot_type="line", color="black", alpha=0.7) #plot.do() #plot.do() #plot.apply(lambda ax: ax.set_xlim(0, 300)) #plot.apply(lambda ax: ax.set_ylim(10000, 35000)) #plot.show() # Objective Space plot = Scatter(title = "Objective Space", legend=True) plot.add(res.F, label="ND Solutions") # res.F are objective space vlaues plot.add(res.history[0].pop.get("F"), label="DOE") plot.add(pf, plot_type="line", color="black", alpha=0.7) plot.show()
if rank == 1: nds = pop[front] self.nadir = nds[nds.get("closest")].get("F").max(axis=0) I = np.lexsort([pop.get("apd"), pop.get("rank")]) self.niches = niches_to_ind return pop[I][:n_survive] if __name__ == "__main__": problem = get_problem("zdt1") ref_dirs = get_reference_directions("das-dennis", 2, n_partitions=99) algorithm = ModifiedRVEA(ref_dirs, adapt_freq=2.0) pf = problem.pareto_front() res = minimize(problem, algorithm, ('n_gen', 200), seed=1, pf=pf, verbose=True) plot = Scatter() plot.add(pf, plot_type="line", color="black", alpha=0.7) plot.add(res.F, color="red") plot.show()
if RESULT is None: RESULT = res else: np.testing.assert_allclose(RESULT.F, res.F) print("YES") from pymoo.visualization.scatter import Scatter # get the pareto-set and pareto-front for plotting ps = problem.pareto_set(use_cache=False, flatten=False) pf = problem.pareto_front(use_cache=False, flatten=False) # Design Space plot = Scatter(title="Design Space", axis_labels="x") plot.add(res.X, s=30, facecolors='none', edgecolors='r') if ps is not None: plot.add(ps, plot_type="line", color="black", alpha=0.7) plot.do() plot.apply(lambda ax: ax.set_xlim(-0.5, 1.5)) plot.apply(lambda ax: ax.set_ylim(-2, 2)) plot.show() # Objective Space plot = Scatter(title="Objective Space") plot.add(res.F) if pf is not None: plot.add(pf, plot_type="line", color="black", alpha=0.7) plot.show() n_evals = [] # corresponding number of function evaluations\
from pymoo.algorithms.nsga3 import NSGA3 from pymoo.factory import get_problem, get_reference_directions, get_termination from pymoo.optimize import minimize from pymoo.visualization.scatter import Scatter problem = get_problem("dtlz3", None, 3, k=10) # create the reference directions to be used for the optimization ref_dirs = get_reference_directions("das-dennis", 3, n_partitions=12) # create the algorithm object algorithm = NSGA3(pop_size=92, ref_dirs=ref_dirs) res = minimize(problem, algorithm, get_termination("default", n_last=25), pf=True, seed=2, verbose=True) print(res.algorithm.n_gen) plot = Scatter(title="DTLZ3") plot.add(res.F, color="red", alpha=0.8, s=20) plot.show()
# Termination from pymoo.factory import get_termination termination = get_termination("n_gen", 30) # Optimize (works OK without verbose) from pymoo.optimize import minimize res = minimize(problem, algorithm, termination, seed=1, save_history=True, verbose=True) # Plot import matplotlib.pyplot as plt from pymoo.visualization.scatter import Scatter plot = Scatter(title="Design Space", axis_labels="x") plot.add(res.X, s=30, facecolors='none', edgecolors='r') plot.do() plot.apply(lambda ax: ax.set_xlim(-0.5, 1.5)) plot.apply(lambda ax: ax.set_ylim(-2, 2)) plt.show() plot = Scatter(title="Objective Space") plot.add(res.F) plot.do() plt.show()
algorithm = RNSGA3( ref_points=ref_points, pop_per_ref_point=50, mu=0.1) res = minimize(problem, algorithm=algorithm, termination=('n_gen', 300), pf=pf, seed=1, verbose=False) reference_directions = res.algorithm.survival.ref_dirs plot = Scatter() plot.add(pf, label="pf") plot.add(res.F, label="F") plot.add(ref_points, label="ref_points") plot.add(reference_directions, label="ref_dirs") plot.show() # END rnsga3 # START rnsga3_3d from pymoo.util.reference_direction import UniformReferenceDirectionFactory # Get problem problem = get_problem("dtlz4", n_var=12, n_obj=3) # Define reference points and reference directions ref_points = np.array([[1.0, 0.5, 0.2], [0.3, 0.2, 0.6]])
continue fname = f"{label}.pf" # # archive = Population() # # for i in range(10): # algorithm = NSGA2(pop_size=200) # # res = minimize(problem, # algorithm, # ('n_gen', 3000), # seed=1, # verbose=False) # # archive = Population.merge(archive, res.opt) # # opt = RankAndCrowdingSurvival().do(problem, archive, n_survive=1000) # # pf = opt.get("F") # # np.savetxt(fname, pf) # print(label) pf = np.loadtxt(f"../../data/pf/CTP/{fname}") plot = Scatter(title=label) plot.add(pf, color="red") plot.show()
def _do(self, problem, pop, n_survive, out=None, algorithm=None, **kwargs): X, F = pop.get("X", "F") if F.shape[1] != 1: raise ValueError("FitnessSurvival can only used for single objective single!") # the final indices of surviving individuals survivors = [] # calculate the normalized distance D = vectorized_cdist(X, X) # np.fill_diagonal(D, np.inf) norm = np.linalg.norm(problem.xu - problem.xl) D /= norm # find the best solution in the population S = np.argmin(F[:, 0]) # create the data structure to work with in order to flag survivors survivors = [] remaining = [k for k in range(len(pop)) if k != S] while len(survivors) < n_survive: # the extreme point for decision making farthest = D[S, :].argmax() # sort by distance to best delta_x = D[S, :] / D[S, farthest] delta_f = (F[:, 0] - F[S, 0]) / (F[farthest, 0] - F[S, 0]) f = np.column_stack([-delta_x, delta_f]) z = np.array([-1, 0]) p = 2 val = ((f - z) ** p).sum(axis=1) ** (1 / p) survivors = val.argsort()[:n_survive] pop[survivors].set("v", val[survivors]) plt.figure(figsize=(5, 5)) plt.scatter(X, F, color="black", alpha=0.8, s=20, label='pop') plt.scatter(X[survivors], F[survivors], color="red", label="survivors") v = np.round(pop[survivors].get("v"), 3) for i in range(len(survivors)): x = X[survivors][i] y = F[survivors][i] plt.text(x, y, v[i], fontsize=9) plt.scatter(X[farthest], F[farthest], color="green", label="survivors") _curve = curve(problem) plt.plot(_curve[:, 0], _curve[:, 1], color="black") plt.xlabel("X") plt.ylabel("F") plt.legend() plt.show() return pop[survivors] survivors.append(remaining[val.argmin()]) remaining = [k for k in range(len(pop)) if k != S] plt.scatter(X, F) plt.scatter(X[survivors], F[survivors], color="red", marker='x') _curve = curve(problem) plt.plot(_curve[:, 0], _curve[:, 1], color="black") plt.xlabel("X") plt.ylabel("F") plt.show() return pop[fronts[0]] plt.scatter(delta_x, delta_f) plt.scatter(delta_x[nds], delta_f[nds], color="red") plt.xlabel("D") plt.ylabel("F") plt.show() pop[S].set("rank", 0) # initialize utility data structures survivors = [S] remaining = [k for k in range(len(pop)) if k != S] n_neighbors = 10 cnt = 1 while len(survivors) < n_survive: closest = D[survivors, :][:, remaining].argmin(axis=0) delta_f = F[remaining, 0] - F[np.argmin(F[:, 0]), 0] delta_x = D[closest, remaining] fitness = delta_f / delta_x S = remaining[np.argmin(fitness)] if algorithm.n_gen == 20: sc = Scatter(title=algorithm.n_gen) sc.add(curve(problem), plot_type="line", color="black") sc.add(np.column_stack([pop.get("X"), F[:, 0]]), color="purple") sc.add(np.column_stack([pop[survivors].get("X"), pop[survivors].get("F")]), color="red", s=40, marker="x") sc.do() plt.ylim(0, 2) plt.show() plt.close() # update the survivors and remaining individuals individual = pop[S] neighbors = pop[D[S].argsort()[:n_neighbors]] # if individual has had neighbors before update them N = individual.get("neighbors") if N is not None: neighbors = Population.merge(neighbors, N) neighbors = neighbors[neighbors.get("F")[:, 0].argsort()[:n_neighbors]] individual.set("neighbors", neighbors) individual.set("rank", cnt) survivors.append(S) remaining = [k for k in remaining if k != S] cnt += 1 return pop[survivors]
termination=('n_gen', 50), seed=1, save_history=True, verbose=False) print(ret.F) with Video(GIF("animation.gif")) as vid: for algorithm in ret.history: if algorithm.n_gen % 1 == 0: X, F = algorithm.pop.get("X", "F") nds = NonDominatedSorting().do(F, only_non_dominated_front=True) other = [k for k in range(len(F)) if k not in nds] fig, (ax1, ax2) = plt.subplots(2, figsize=(8, 6)) fig.suptitle("%s - %s - Gen %s" % ("ZDT1", "NSGA2", algorithm.n_gen), fontsize=16) pcp = PCP(ax=ax1, bounds=(problem.xl, problem.xu)) pcp.add(X[other], color="blue", linewidth=0.5) pcp.add(X[nds], color="red", linewidth=2) pcp.do() sc = Scatter(ax=ax2) sc.add(F[other], color="blue") sc.add(F[nds], color="red") sc.add(problem.pareto_front(), plot_type="line") sc.do() vid.record()
termination = get_termination("f_tol", tol=0.001, n_last=20, n_max_gen=1000, nth_gen=10) termination = ("n_gen", 1000) from pymoo.factory import get_crossover, get_mutation, get_sampling from pymoo.optimize import minimize problem = MyProblem() res = minimize(MyProblem(), algorithm, termination, seed=1, pf=problem.pareto_front(use_cache=False), save_history=True, verbose=True) # res = minimize(objective,x0,algorithm,bounds=bnds,constraints=cons('n_gen', 200)) # minimize(objective,x0,method='COBYLA',bounds=bnds,constraints=cons) print("Best solution found: %s" % res.X) print("Function value: %s" % res.F) print("Constraint violation: %s" % res.CV) plot = Scatter() plot.add(res.F, color="red") plot.show()
def plot(algorithm): pop = algorithm.pop sc = Scatter(title=algorithm.n_gen) sc.add(curve(algorithm.problem), plot_type="line", color="black") sc.add(np.column_stack([pop.get("X"), pop.get("F")]), color="red") sc.do()
for i in range(100): if i != 23: continue res = minimize( problem, get_algorithm("nelder-mead", n_max_restarts=10, adaptive=True), #scipy_minimize("Nelder-Mead"), #termination=("n_eval", 30000), seed=i, verbose=False) #print(res.X) F = ModifiedZDT1(n_var=n_var).evaluate(res.X, return_values_of="F") print(i, F) opt = decomp.do(pf, weights).argmin() print(pf[opt]) print(decomp.do(pf, weights).min()) plot = Scatter() plot.add(pf) plot.add(F) plot.add(np.row_stack([np.zeros(2), weights]), plot_type="line") plot.add(pf[opt]) plot.show()
# START scatter2d from pymoo.visualization.scatter import Scatter from pymoo.factory import get_problem, get_reference_directions F = get_problem("zdt3").pareto_front() Scatter().add(F).show() # END scatter2d # START scatter2d_custom F = get_problem("zdt3").pareto_front(use_cache=False, flatten=False) plot = Scatter() plot.add(F, s=30, facecolors='none', edgecolors='r') plot.add(F, plot_type="line", color="black", linewidth=2) plot.show() # END scatter2d_custom # START scatter3d ref_dirs = get_reference_directions("uniform", 3, n_partitions=12) F = get_problem("dtlz1").pareto_front(ref_dirs) plot = Scatter() plot.add(F) plot.show() # END scatter3d # START scatter4d import numpy as np F = np.random.random((30, 4)) plot = Scatter(tight_layout=True) plot.add(F, s=10)
f1 = T f2 = L f3 = M g1 = (np.zeros((x.shape, 1)))**2 - 1e-5 #%%problem from pymoo.model.problem import FunctionalProblem from pymoo.model.problem import ConstraintsAsPenaltyProblem problem = FunctionalProblem(2, objs, xl=-2, xu=2, constr_ieq=constr_ieq, constr_eq=constr_eq) problem = ConstraintsAsPenaltyProblem(problem, penalty=1e6) #%%optimize from pymoo.algorithms.nsga2 import NSGA2 from pymoo.optimize import minimize algorithm = NSGA2(pop_size=40) res = minimize(problem, algorithm, seed=1) #%%visualize from pymoo.visualization.scatter import Scatter plot = Scatter(title="Objective Space") plot.add(res.F) plot.show()
from pymoo.algorithms.nsga2 import NSGA2 from pymoo.factory import get_problem, get_termination from pymoo.optimize import minimize from pymoo.visualization.scatter import Scatter problem = get_problem("zdt3") algorithm = NSGA2(pop_size=100) termination = get_termination("f_tol") res = minimize(problem, algorithm, termination, pf=True, seed=1, verbose=True) print(res.algorithm.n_gen) plot = Scatter(title="ZDT3") plot.add(problem.pareto_front(use_cache=False, flatten=False), plot_type="line", color="black") plot.add(res.F, color="red", alpha=0.8, s=20) plot.show()
from pymoo.factory import get_problem from pymoo.optimize import minimize problem = get_problem("zdt1") algorithm = NSGA2(pop_size=100, eliminate_duplicates=True) ret = minimize(problem, algorithm, termination=('n_gen', 100), seed=1, save_history=True, verbose=False) # use the video writer as a resource with Video(File("ga.mp4")) as vid: # for each algorithm object in the history for entry in ret.history: sc = Scatter(title=("Gen %s" % entry.n_gen)) sc.add(entry.pop.get("F")) sc.add(entry.problem.pareto_front(), plot_type="line", color="black", alpha=0.7) sc.do() # finally record the current visualization to the video vid.record() # END ga