Ejemplo n.º 1
0
    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])))
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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()
Ejemplo n.º 4
0
    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()
Ejemplo n.º 5
0
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()
Ejemplo n.º 6
0
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()
# --------------------------------------------------
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
plot2 = Scatter(title="Objective Space")
plot2.add(res.F)
if pf is not None:
    plot2.add(pf, plot_type="line", color="black", alpha=0.7)
plot2.show()
"""
res.X design space values are
res.F objective spaces values
res.G constraint values
res.CV aggregated constraint violation
res.algorithm algorithm object
res.pop final population object
res.history history of algorithm object. (only if save_history has been enabled during the algorithm initialization)
Ejemplo n.º 8
0
    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]

        # initialize the fitness vector
        delta_f = (F[:, 0] / F[S, 0])
        delta_x = D[S, :]
        fitness = delta_f / delta_x

        n_neighbors = 10
        cnt = 1

        while len(survivors) < n_survive:
            S = remaining[np.argmin(fitness[remaining])]

            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)
            sc.show()

            # 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]

            delta_f = (F[:, 0] / F[S, 0])
            delta_x = D[S, :]
            fitness = np.maximum(fitness, delta_f / delta_x)

            if algorithm.n_gen == 30:
                sc = Scatter(title=algorithm.n_gen)
                sc.add(curve(problem), plot_type="line", color="black")
                sc.add(np.column_stack([pop.get("X"), fitness]), color="purple")
                sc.add(np.column_stack([survivors.get("X"), survivors.get("F")]), color="red", s=40, marker="x")
                sc.do()
                plt.ylim(0, 2)
                sc.show()

            cnt += 1

        return survivors