Beispiel #1
0
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()
Beispiel #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
def old_graph(s):
    """
    This code is horrific. Unfortunately started local NSGA2 without checking so will have to do for now
    :param s: seed
    :return: nothing
    """
    data = []
    for x in open("results/" + str(s) + "_results.txt"):
        if "    " in x:
            x = x.replace("    ", ",")
        if "   " in x:
            x = x.replace("   ", ",")
        if "  " in x:
            x = x.replace("  ", ",")
        if " " in x:
            x = x.replace(" ", ",")
        x = x.replace("[", "").replace("]", "").replace("\n", "")
        x = x.strip()
        x = x.split(",")
        # remove empty
        z = []
        for xx in x:
            if xx != '':
                z.append(xx)
        if z != []:
            data.append([float(z[0]), float(z[1])])
    Scatter().add(np.array(data)).show()
Beispiel #4
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()
Beispiel #5
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])))
Beispiel #6
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()
        crossover = get_crossover("real_sbx", prob = 0.9, eta = 15),
        mutation = get_mutation("real_pm", eta=20),
        eliminate_duplicates = True
        )


res = minimize(MyProb(), 
               algorithm, 
               ("n_gen", 500), 
               seed =1, 
               pf = problem.pareto_front(use_cache=False),
               save_history=True,
               verbose= True)


Scatter().add(res.F).show()

dspace = res.pop.get("X")
reynolds = dspace[:,0]
pitch= dspace[:,1]
depth = dspace[:,2]
#print(dspace)


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]

# receive the population in each generation
obj_and_feasible_each_gen = [pop[pop.get("feasible")[:,0]].get("F") for pop in pop_each_gen]
Beispiel #8
0
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()
Beispiel #9
0
 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()
Beispiel #10
0
               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()
Beispiel #11
0
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()
Beispiel #12
0
# 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)
Beispiel #13
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()
Beispiel #14
0
problems = [
    (get_problem("wfg1", 6, n_obj, 4), WFG1(n_obj, 6, 4)),
    (get_problem("wfg2", 6, n_obj, 4), WFG2(n_obj, 6, 4)),
    (get_problem("wfg3", 6, n_obj, 4), WFG3(n_obj, 6, 4)),
    (get_problem("wfg4", 6, n_obj, 4), WFG4(n_obj, 6, 4)),
    (get_problem("wfg5", 6, n_obj, 4), WFG5(n_obj, 6, 4)),
    (get_problem("wfg6", 6, n_obj, 4), WFG6(n_obj, 6, 4)),
    (get_problem("wfg7", 6, n_obj, 4), WFG7(n_obj, 6, 4)),
    (get_problem("wfg8", 6, n_obj, 4), WFG8(n_obj, 6, 4)),
    (get_problem("wfg9", 6, n_obj, 4), WFG9(n_obj, 6, 4)),
]

for my, other in problems:

    ps = other.get_optimal_solutions(2000)
    for e in ps:
        e.objective_values = other.objective_function(e.phenome)
    pf = np.array([e.objective_values for e in ps])
    ps = np.array([e.phenome for e in ps])

    _ps = my.pareto_set(n_pareto_points=3000)
    _pf = my.pareto_front(n_pareto_points=3000)

    name = my.__class__.__name__
    Scatter(title=name).add(pf, s=15, color="green", alpha=0.5).add(_pf, color="red", s=10).show()

    print(name, IGD(pf).calc(_pf))

    print()
Beispiel #15
0
                   verbose=True)

    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()
Beispiel #16
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])

        # 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]
Beispiel #17
0
def calc_pareto_front(problem, ref_dirs):
    n_pareto_points = 200
    np.random.seed(1)

    pf = problem.pareto_front(n_pareto_points=n_pareto_points, use_cache=False)
    # survival = ReferenceDirectionSurvival(ref_dirs)
    survival = RankAndCrowdingSurvival()

    for i in range(1000):
        _pf = problem.pareto_front(n_pareto_points=n_pareto_points,
                                   use_cache=False)
        F = np.row_stack([pf, _pf])

        pop = Population().new("F", F)
        pop = survival.do(problem, pop, n_pareto_points // 2)

        pf = pop.get("F")

    return pf


if __name__ == '__main__':

    ref_dirs = get_reference_directions("das-dennis", 3, n_points=91)
    F = calc_pareto_front(WFG3(6, 3), ref_dirs)

    Scatter().add(F).show()

    for problem in [WFG1, WFG2, WFG3, WFG4, WFG5, WFG6, WFG7, WFG8, WFG9]:
        print("")
Beispiel #18
0
# START example
import numpy as np
from pyrecorder.recorders.file import File
from pyrecorder.video import Video

from pymoo.algorithms.nsga2 import NSGA2
from pymoo.visualization.scatter import Scatter

vid = Video(File("example.mp4"))

for k in range(10):
    X = np.random.random((100, 2))
    Scatter(title=str(k)).add(X).do()
    vid.record()

vid.close()
# END example

# START ga
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,
Beispiel #19
0
        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()

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()





	
	

Beispiel #21
0
    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
Beispiel #22
0
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()
Beispiel #23
0
class MyMutation(Mutation):
    def __init__(self):
        super().__init__()

    def _do(self, problem, X, **kwargs):
        for i in range(len(X)):
            if np.random.random() < 0.5:
                X[i, 0] = "".join(
                    np.array([e for e in X[i, 0]
                              ])[np.random.permutation(problem.n_characters)])

        return X


class MyDuplicateElimination(ElementwiseDuplicateElimination):
    def is_equal(self, a, b):
        return a.X[0] == b.X[0]


algorithm = NSGA2(pop_size=20,
                  sampling=MySampling(),
                  crossover=MyCrossover(),
                  mutation=MyMutation(),
                  eliminate_duplicates=MyDuplicateElimination())

res = minimize(MyProblem(), algorithm, seed=1, verbose=True)

Scatter().add(res.F).show()
print(res.X[np.argsort(res.F[:, 0])])
Beispiel #24
0
# START example
import numpy as np

from pymoo.algorithms.nsga2 import NSGA2
from pymoo.visualization.scatter import Scatter
from pymoo.visualization.video import Video

vid = Video("example.mp4")

for k in range(10):
    X = np.random.random((100, 2))
    Scatter(title=str(k)).add(X).do()
    vid.snap(duration=1)

vid.close()
# END example


# START ga
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,
from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_problem, get_termination
from pymoo.operators.crossover.simulated_binary_crossover import SimulatedBinaryCrossover
from pymoo.operators.mutation.polynomial_mutation import PolynomialMutation
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter

problem = get_problem("welded_beam")
algorithm = NSGA2(
    pop_size=200,
    crossover=SimulatedBinaryCrossover(eta=20, prob=0.9),
    mutation=PolynomialMutation(prob=0.25, eta=40),
)
termination = get_termination("f_tol", n_last=60)

res = minimize(
    problem,
    algorithm,
    # ("n_gen", 800),
    pf=False,
    seed=10,
    verbose=True)

print(res.algorithm.n_gen)
Scatter().add(res.F, s=20).add(problem.pareto_front(),
                               plot_type="line",
                               color="black").show()
Beispiel #26
0
def my_plot(algorithm):
    Scatter(title=("Gen %s" % algorithm.n_gen)).add(algorithm.pop.get("F")).do()
Beispiel #27
0
# START griewank
from pymoo.factory import get_problem
from pymoo.util.plotting import plot_problem_surface

problem = get_problem("griewank", n_var=2)
plot_problem_surface(problem, 100, plot_type="wireframe+contour")
# END griewank

# START truss2d
from pymoo.factory import get_problem
from pymoo.visualization.scatter import Scatter

pf = get_problem("truss2d").pareto_front()

plot = Scatter(title="Pareto-front")
plot.add(pf, s=80, facecolors='none', edgecolors='r')
plot.add(pf, plot_type="line", color="black", linewidth=2)
plot.show()
# END truss2d

# START truss2d_log
plot.reset()
plot.do()
plot.apply(lambda ax: ax.set_yscale("log"))
plot.apply(lambda ax: ax.set_xscale("log"))
plot.show()
# END truss2d_log

# --------------------------------------------------------------------------------------------
Beispiel #28
0
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()
Beispiel #29
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()
Beispiel #30
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]

        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]