Exemplo n.º 1
0
class MyCallback(Callback):
    def __init__(self) -> None:
        super().__init__()
        self.video = Video(Streamer())

    def notify(self, algorithm):
        problem = algorithm.problem

        pcp = PCP(title=("Gen %s" % algorithm.n_gen, {
            'pad': 30
        }),
                  bounds=(problem.xl, problem.xu),
                  labels=["$x_%s$" % k for k in range(problem.n_var)])
        pcp.set_axis_style(color="grey", alpha=0.5)

        pcp.add(algorithm.pop.get("X"),
                color="black",
                alpha=0.8,
                linewidth=1.5)
        if algorithm.off is not None:
            pcp.add(algorithm.off.get("X"),
                    color="blue",
                    alpha=0.8,
                    linewidth=0.5)

        pcp.add(algorithm.opt.get("X"), color="red", linewidth=4)
        pcp.do()

        self.video.record()
Exemplo n.º 2
0
    def __init__(self,
                 do_show=False,
                 do_close=False,
                 nth_gen=1,
                 fname=None,
                 dpi=None,
                 video=None,
                 exception_if_not_applicable=True):

        super().__init__()
        self.nth_gen = nth_gen
        self.do_show = do_show
        self.do_close = do_close
        self.exception_if_not_applicable = exception_if_not_applicable

        self.video = video
        if self.video is None and fname is not None:
            try:
                from pyrecorder.recorders.file import File
                from pyrecorder.video import Video
                from pyrecorder.converters.matplotlib import Matplotlib
            except:
                print(
                    "Please install pyrecorder for animation support: pip install pyrecorder"
                )
            self.video = Video(File(fname), converter=Matplotlib(dpi=dpi))
Exemplo n.º 3
0
class PathVisualization(Callback):
    def __init__(self):
        super().__init__()
        self.vid = Video(File("tsp.mp4"))

    def notify(self, algorithm):
        if algorithm.n_gen % 10 == 0:
            x = algorithm.opt[0].get("X")
            visualize(problem, x, show=False)
            plt.title(f"Generation: {algorithm.n_gen}")
            self.vid.record()
Exemplo n.º 4
0
class AnimationCallback(Callback):
    def __init__(self,
                 do_show=False,
                 do_close=False,
                 nth_gen=1,
                 fname=None,
                 dpi=None,
                 video=None,
                 exception_if_not_applicable=True):

        super().__init__()
        self.nth_gen = nth_gen
        self.do_show = do_show
        self.do_close = do_close
        self.exception_if_not_applicable = exception_if_not_applicable

        self.video = video
        if self.video is None and fname is not None:
            try:
                from pyrecorder.recorders.file import File
                from pyrecorder.video import Video
                from pyrecorder.converters.matplotlib import Matplotlib
            except:
                print(
                    "Please install pyrecorder for animation support: pip install pyrecorder"
                )
            self.video = Video(File(fname), converter=Matplotlib(dpi=dpi))

    def notify(self, algorithm, **kwargs):
        if algorithm.n_gen == 1 or algorithm.n_gen % self.nth_gen == 0:
            try:

                ret = self.do(algorithm.problem, algorithm, **kwargs)

                if self.do_show:
                    plt.show()

                if self.video is not None:
                    self.video.record()

                if self.do_close:
                    plt.close()

                return ret

            except Exception as ex:
                if self.exception_if_not_applicable:
                    raise ex
Exemplo n.º 5
0
    def test_stream_simple(self):
        vid = Video(Streamer(mode="stream", delay=1000, close_window=True))

        for k in range(3):
            X = np.random.random((100, 2))
            plt.scatter(X[:, 0], X[:, 1])
            vid.record()

        vid.close()
Exemplo n.º 6
0
    def test_html_embed_simple(self):
        fname = "example.mp4"
        vid = Video(HTML(File(fname), embedded=True))

        for k in range(10):
            X = np.random.random((100, 2))
            plt.scatter(X[:, 0], X[:, 1])
            vid.record()
        vid.close()

        self.assertTrue(os.path.exists("example.html"))
        os.remove("example.html")
Exemplo n.º 7
0
    def test_gif_simple(self):
        fname = "example.gif"
        vid = Video(GIF(fname))

        for k in range(10):
            X = np.random.random((100, 2))
            plt.scatter(X[:, 0], X[:, 1])
            vid.record()

        vid.close()

        self.assertTrue(os.path.exists(fname))
        os.remove(fname)
Exemplo n.º 8
0
    def test_html_wrap_simple(self):
        fname = "example.mp4"
        vid = Video(HTML(File(fname)))

        for k in range(10):
            X = np.random.random((100, 2))
            plt.scatter(X[:, 0], X[:, 1])
            vid.record()

        vid.close()

        self.assertTrue(os.path.exists(fname))

        html = fname.replace("mp4", "html")
        self.assertTrue(os.path.exists(html))
        os.remove(fname)
        os.remove(html)
Exemplo n.º 9
0
    def test_record_simple(self):
        fname = "example.mp4"
        vid = Video(File(fname), converter=Plotly())

        df = px.data.iris()

        for k in range(10):
            rnd = np.random.rand(len(df)) < 0.7
            fig = px.scatter(df.loc[rnd],
                             x="sepal_width",
                             y="sepal_length",
                             color="species",
                             size='petal_length')
            vid.record(fig=fig)

        vid.close()

        self.assertTrue(os.path.exists(fname))
Exemplo n.º 10
0

parse_doc_string(MMGA.__init__)

if __name__ == '__main__':
    problem = MultiModalSimple2()

    algorithm = MMGA(pop_size=20, eliminate_duplicates=True)

    ret = minimize(problem,
                   algorithm,
                   termination=('n_gen', 100),
                   seed=1,
                   save_history=True,
                   verbose=False)

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

    plot(ret.algorithm)
    plt.show()

    with Video(File("mm.mp4")) as vid:
        for entry in ret.history:
            plot(entry)
            vid.record()
Exemplo n.º 11
0
import matplotlib.pyplot as plt
import numpy as np

from pyrecorder.recorders.gif import GIF
from pyrecorder.video import Video

with Video(GIF("test.gif")) as vid:
    for t in range(2):
        X = np.random.random((100, 2))
        plt.scatter(X[:, 0], X[:, 1], color="yellow")
        vid.record()
Exemplo n.º 12
0
 def __init__(self):
     super().__init__()
     self.vid = Video(File("tsp.mp4"))
Exemplo n.º 13
0
from pyrecorder.video import Video

problem = ZDT1(n_var=6)

algorithm = NSGA2()

ret = minimize(problem,
               algorithm,
               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()
Exemplo n.º 14
0
import matplotlib.pyplot as plt
import numpy as np

from pyrecorder.recorders.file import File
from pyrecorder.video import Video

plt.style.use('dark_background')

with Video(File("coil.mp4", fps=15)) as vid:
    for t in range(500):
        a = np.arange(t) * 0.1
        plt.plot(a * np.sin(a), a * np.cos(a))
        plt.xlim(-50, 50)
        plt.ylim(-50, 50)
        plt.axis('off')
        vid.record()

Exemplo n.º 15
0
 def __init__(self) -> None:
     super().__init__()
     self.video = Video(Streamer())
Exemplo n.º 16
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,
Exemplo n.º 17
0
import numpy as np
from matplotlib import pyplot as plt

from pyrecorder.recorders.file import File
from pyrecorder.video import Video

with Video(File("wave.mp4", fps=15)) as vid:

    for t in range(200):
        x = np.linspace(0, 4, 1000)
        y = np.sin(2 * np.pi * (x - 0.01 * t))
        plt.plot(x, y)

        vid.record()