def __init__(self, ref_dirs): self.ref_dirs = ref_dirs self.opt = None self.ideal_point = np.full(ref_dirs.shape[1], np.inf) self._decomposition = get_decomposition('asf') self._calc_perpendicular_distance = load_function( "calc_perpendicular_distance")
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 _solve(self, pop): # generate direction vectors by random sampling ref_dirs = np.random.random((self.batch_size, self.n_obj)) ref_dirs /= np.expand_dims(np.sum(ref_dirs, axis=1), 1) algo = MOEAD_algo(ref_dirs=ref_dirs, n_neighbors=len(ref_dirs), eliminate_duplicates=False) repair, crossover, mutation = algo.mating.repair, algo.mating.crossover, algo.mating.mutation if isinstance(algo.decomposition, str): decomp = algo.decomposition if decomp == 'auto': if self.n_obj <= 2: decomp = 'tchebi' else: decomp = 'pbi' decomposition = get_decomposition(decomp) else: decomposition = algo.decomposition ideal_point = np.min(pop.get('F'), axis=0) # find the optimal individual for each reference direction opt_pop = Population(0, individual=Individual()) for i in range(self.batch_size): N = algo.neighbors[i, :] FV = decomposition.do(pop.get("F"), weights=ref_dirs[i], ideal_point=ideal_point) opt_pop = Population.merge(opt_pop, pop[np.argmin(FV)]) all_off = Population(0, individual=Individual()) for i in np.random.permutation(self.batch_size): N = algo.neighbors[i, :] if self.batch_size > 1: if np.random.random() < algo.prob_neighbor_mating: parents = N[np.random.permutation(algo.n_neighbors)][:crossover.n_parents] else: parents = np.random.permutation(algo.pop_size)[:crossover.n_parents] # do recombination and create an offspring off = crossover.do(self.real_problem, opt_pop, parents[None, :]) else: off = opt_pop[N].copy() off = mutation.do(self.real_problem, off) off = off[np.random.randint(0, len(off))] # repair first in case it is necessary if repair: off = algo.repair.do(self.real_problem, off, algorithm=algo) all_off = Population.merge(all_off, off) return all_off
def _initialize(self): if isinstance(self.decomposition, str): # set a string decomp = self.decomposition # for one or two objectives use tchebi otherwise pbi if decomp == 'auto': if self.problem.n_obj <= 2: decomp = 'tchebi' else: decomp = 'pbi' # set the decomposition object self._decomposition = get_decomposition(decomp) else: self._decomposition = self.decomposition super()._initialize() self.ideal_point = np.min(self.pop.get("F"), axis=0)
hv = [metric.calc(f) for f in F] fig = plt.figure() # visualze the convergence curve plt.plot(n_evals, hv, '-o', markersize=4, linewidth=2) plt.title("Convergence") plt.xlabel("Function Evaluations") plt.ylabel("Hypervolume") # fig.savefig(LATEX_DIR + 'convergence.eps', format='eps') plt.show() from pymoo.factory import get_visualization, get_decomposition F = res.F weights = np.array([0.01, 0.99]) decomp = get_decomposition("asf") # We apply the decomposition and retrieve the best value (here minimum): I = get_decomposition("asf").do(F, weights).argmin() print("Best regarding decomposition: Point %s - %s" % (I, F[I])) plot = get_visualization("scatter") plot.add(F, color="blue", alpha=0.5, s=30) plot.add(F[I], color="red", s=40) plot.do() # plot.apply(lambda ax: ax.arrow(0, 0, F[I][0], F[I][1], color='black', # head_width=0.001, head_length=0.001, alpha=0.4)) plot.show() ## Parallel Coordinate Plots from pymoo.visualization.pcp import PCP
g = 1 + 9.0 / (self.n_var - 1) * np.sum((x[:, 1:])**2, axis=1) f2 = g * (1 - np.power((f1 / g), 0.5)) if out_of_bounds: f1 = np.full(x.shape[0], np.inf) f2 = np.full(x.shape[0], np.inf) out["F"] = np.column_stack([f1, f2]) n_var = 2 original_problem = ModifiedZDT1(n_var=n_var) weights = np.array([0.5, 0.5]) decomp = get_decomposition("asf", ideal_point=np.array([0.0, 0.0]), nadir_point=np.array([1.0, 1.0])) pf = original_problem.pareto_front() problem = decompose(original_problem, decomp, weights) 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"),
def __init__(self, ref_dirs): self.ref_dirs = ref_dirs self.opt = None self._decomposition = get_decomposition('tchebi') self._calc_perpendicular_distance = load_function( "calc_perpendicular_distance")
else: X = res.pop.get("X") ls = config.latent(config) ls.set_from_population(X) torch.save(ls.state_dict(), os.path.join(config.tmp_folder, "ls_result")) if config.problem_args["n_obj"] == 1: X = np.atleast_2d(res.X) else: try: result = get_decision_making("pseudo-weights", [0, 1]).do(res.F) except: print("Warning: cant use pseudo-weights") result = get_decomposition("asf").do(res.F, [0, 1]).argmin() X = res.X[result] X = np.atleast_2d(X) ls.set_from_population(X) with torch.no_grad(): generated = problem.generator.generate(ls) if config.task == "txt2img": ext = "jpg" elif config.task == "img2txt": ext = "txt" problem.generator.save(generated,