예제 #1
0
def exp4_plot():
    datadir = Path("data", "exp4")
    zlearner = plot.get_data(Path(datadir, "data00"))
    qlearner = plot.get_data(Path(datadir, "data01"))
    zlearner_na = plot.get_data(Path(datadir, "data02"))
    qlearner_na = plot.get_data(Path(datadir, "data03"))
    data = [zlearner, qlearner]
    data_na = [zlearner_na, qlearner_na]

    basestyle = dict(c="k", lw=0.7)
    refstyle = dict(basestyle, c="r", ls="--")
    zstyle = dict(basestyle, c="y", ls="-")
    qstyle = dict(basestyle, c="b", ls="-.")
    zlearner.style.update(zstyle)
    qlearner.style.update(qstyle)
    zlearner_na.style.update(zstyle)
    qlearner_na.style.update(qstyle)

    # Figure common setup
    cfg = linear.cfg
    linear.load_config()
    t_range = (0, cfg.final_time)

    # All in inches
    subsize = (4.05, 0.946)
    width = 4.94
    top = 0.2
    bottom = 0.671765
    left = 0.5487688
    hspace = 0.2716

    # =================
    # States and inputs
    # =================
    figsize, pos = plot.posing(5, subsize, width, top, bottom, left, hspace)
    plt.figure(figsize=figsize)

    ax = plot.subplot(pos, 0)
    [plot.vector_by_index(d, "x", 0)[0] for d in data]
    plt.ylabel(r"$x_1$")
    # plt.ylim(-2, 2)
    plt.legend()

    plot.subplot(pos, 1, sharex=ax)
    [plot.vector_by_index(d, "x", 1) for d in data]
    plt.ylabel(r"$x_2$")
    # plt.ylim(-2, 2)

    plot.subplot(pos, 2, sharex=ax)
    [plot.vector_by_index(d, "u", 0) for d in data]
    plt.ylabel(r'$u$')
    # plt.ylim(-80, 80)

    # ====================
    # Parameter estimation
    # ====================
    ax = plot.subplot(pos, 3)
    plot.all(qlearner, "K", style=dict(refstyle, label="True"))
    for d in data:
        plot.all(
            d, "K", is_agent=True,
            style=dict(marker="o", markersize=2)
        )
    plt.ylabel(r"$\hat{K}$")
    plt.legend()
    # plt.ylim(-70, 30)

    plot.subplot(pos, 4, sharex=ax)
    plot.all(qlearner, "P", style=dict(qlearner.style, c="r", ls="--"))
    for d in data:
        plot.all(
            d, "P", is_agent=True,
            style=dict(marker="o", markersize=2)
        )
    plt.ylabel(r"$\hat{P}$")
    # plt.ylim(-70, 30)

    plt.xlabel("Time, sec")
    plt.xlim(t_range)

    for ax in plt.gcf().get_axes():
        ax.label_outer()

    # ==================================
    # States and inputs (Non-Admissible)
    # ==================================
    figsize, pos = plot.posing(5, subsize, width, top, bottom, left, hspace)
    plt.figure(figsize=figsize)

    ax = plot.subplot(pos, 0)
    [plot.vector_by_index(d, "x", 0)[0] for d in data_na]
    plt.ylabel(r"$x_1$")
    # plt.ylim(-2, 2)
    plt.legend()

    plot.subplot(pos, 1, sharex=ax)
    [plot.vector_by_index(d, "x", 1) for d in data_na]
    plt.ylabel(r"$x_2$")
    # plt.ylim(-2, 2)

    plot.subplot(pos, 2, sharex=ax)
    [plot.vector_by_index(d, "u", 0) for d in data_na]
    plt.ylabel(r'$u$')
    # plt.ylim(-80, 80)

    # =====================================
    # Parameter estimation (Non-Admissible)
    # =====================================
    ax = plot.subplot(pos, 3)
    plot.all(qlearner_na, "K", style=dict(refstyle, label="True"))
    for d in data_na:
        plot.all(
            d, "K", is_agent=True,
            style=dict(marker="o", markersize=2)
        )
    plt.ylabel(r"$\hat{K}$")
    plt.legend()
    # plt.ylim(-70, 30)

    plot.subplot(pos, 4, sharex=ax)
    plot.all(qlearner_na, "P", style=refstyle)
    for d in data_na:
        plot.all(
            d, "P", is_agent=True,
            style=dict(marker="o", markersize=2)
        )
    plt.ylabel(r"$\hat{P}$")
    # plt.ylim(-70, 30)

    plt.xlabel("Time, sec")
    plt.xlim(t_range)

    for ax in plt.gcf().get_axes():
        ax.label_outer()

    imgdir = Path("img", datadir.relative_to("data"))
    imgdir.mkdir(exist_ok=True)

    plt.figure(1)
    plt.savefig(Path(imgdir, "figure_1.pdf"), bbox_inches="tight")

    plt.figure(2)
    plt.savefig(Path(imgdir, "figure_2.pdf"), bbox_inches="tight")

    plt.show()
예제 #2
0
    def comp(sqldir, klmdir):
        # Data 001 ~ Data 002
        sql = plot.get_data(Path(datadir, sqldir))
        klm = plot.get_data(Path(datadir, klmdir))
        data = [sql, klm]
        # data_na = []

        basestyle = dict(c="k", lw=0.7)
        refstyle = dict(basestyle, c="r", ls="--")
        klm_style = dict(basestyle, c="y", ls="-")
        sql_style = dict(basestyle, c="b", ls="-.")
        klm.style.update(klm_style)
        sql.style.update(sql_style)
        # zlearner_na.style.update(klm_style)
        # qlearner_na.style.update(sql_style)

        # Figure common setup
        t_range = (0, sql.info["cfg"].env_kwargs.max_t)

        # All in inches
        subsize = (4.05, 0.946)
        width = 4.94
        top = 0.2
        bottom = 0.671765
        left = 0.5487688
        hspace = 0.2716

        # =================
        # States and inputs
        # =================
        figsize, pos = plot.posing(3, subsize, width, top, bottom, left,
                                   hspace)
        plt.figure(figsize=figsize)

        ax = plot.subplot(pos, 0)
        [plot.vector_by_index(d, "x", 0)[0] for d in data]
        plt.ylabel(r"$x_1$")
        # plt.ylim(-2, 2)
        plt.legend()

        plot.subplot(pos, 1, sharex=ax)
        [plot.vector_by_index(d, "x", 1) for d in data]
        plt.ylabel(r"$x_2$")
        # plt.ylim(-2, 2)

        plot.subplot(pos, 2, sharex=ax)
        [plot.vector_by_index(d, "x", 2) for d in data]
        plt.ylabel(r'$x_3$')
        # plt.ylim(-80, 80)

        plt.xlabel("Time, sec")
        plt.xlim(t_range)

        for ax in plt.gcf().get_axes():
            ax.label_outer()

        # ====================
        # Parameter estimation
        # ====================
        figsize, pos = plot.posing(3, subsize, width, top, bottom, left,
                                   hspace)
        plt.figure(figsize=figsize)

        ax = plot.subplot(pos, 0)
        [plot.vector_by_index(d, "u", 0) for d in data]
        plt.ylabel(r'$\delta_t$')

        plot.subplot(pos, 1, sharex=ax)
        plot.all(sql, "K", style=dict(refstyle, label="True"))
        for d in data:
            plot.all(d,
                     "K",
                     is_agent=True,
                     style=dict(marker="o", markersize=2))
        plt.ylabel(r"$\hat{K}$")
        plt.legend()
        # plt.ylim(-70, 30)

        plot.subplot(pos, 2, sharex=ax)
        plot.all(sql, "P", style=dict(sql.style, c="r", ls="--"))
        for d in data:
            plot.all(d,
                     "P",
                     is_agent=True,
                     style=dict(marker="o", markersize=2))
        plt.ylabel(r"$\hat{P}$")
        # plt.ylim(-70, 30)

        plt.xlabel("Time, sec")
        plt.xlim(t_range)

        for ax in plt.gcf().get_axes():
            ax.label_outer()
예제 #3
0
def exp2_plot():
    def get_data(datadir):
        data = SN()
        env, info = fym.logging.load(list(datadir.glob("*env.h5"))[0],
                                     with_info=True)
        data.env = env
        data.info = info
        agentlist = list(datadir.glob("*agent.h5"))
        if agentlist != []:
            data.agent = fym.logging.load(agentlist[0])
        data.style = dict(label=info["cfg"].label)
        return data

    cfg = multirotor.cfg
    multirotor.load_config()

    plt.rc("font", family="Times New Roman")
    plt.rc("text", usetex=True)
    plt.rc("lines", linewidth=1)
    plt.rc("axes", grid=True)
    plt.rc("grid", linestyle="--", alpha=0.8)

    datadir = Path("data", "exp2")
    mrac = get_data(Path(datadir, "data00"))
    hmrac = get_data(Path(datadir, "data01"))
    data = [mrac, hmrac]

    basestyle = dict(c="k", lw=0.7)
    cmdstyle = dict(basestyle, c="r", ls="--", label="Command")
    refstyle = dict(basestyle, c="k", ls="-", label="Ref. Model")
    mrac.style.update(basestyle, c="g", ls="-")
    hmrac.style.update(basestyle, c="b", ls="-")

    # Figure common setup
    t_range = (0, cfg.final_time)
    # t_range = (0, 15)

    # All in inches
    subsize = (4.05, 0.946)
    width = 4.94
    top = 0.2
    bottom = 0.671765
    left = 0.5487688
    hspace = 0.2716
    r2d = np.rad2deg(1)

    # =================
    # States and inputs
    # =================
    figsize, pos = plot.posing(3, subsize, width, top, bottom, left, hspace)

    plt.figure(figsize=figsize)

    ax = plt.subplot(311, position=pos[0])
    lines = []
    # lines += plt.plot(mrac.env["t"], mrac.env["c"][:, 0], **cmdstyle)
    lines += plot.vector_by_index(mrac, "c", 0, mult=r2d, style=cmdstyle)
    lines += plot.vector_by_index(mrac, "xr", 0, mult=r2d, style=refstyle)
    lines += [plot.vector_by_index(d, "x", 0, r2d)[0] for d in data]
    plt.ylabel(r"$p$ [deg/s]")
    # plt.ylim(-40, 40)
    plt.figlegend(
        lines,
        [line.get_label() for line in lines],
        bbox_to_anchor=(0.99, 0.78)
    )

    plt.subplot(312, sharex=ax, position=pos[1])
    plot.vector_by_index(mrac, "c", 1, mult=r2d, style=cmdstyle)
    plot.vector_by_index(mrac, "xr", 1, mult=r2d, style=refstyle)
    [plot.vector_by_index(d, "x", 1, r2d) for d in data]
    plt.ylabel(r"$q$ [deg/s]")
    # plt.ylim(-40, 40)

    plt.subplot(313, sharex=ax, position=pos[2])
    plot.vector_by_index(mrac, "c", 2, mult=r2d, style=cmdstyle)
    plot.vector_by_index(mrac, "xr", 2, mult=r2d, style=refstyle)
    [plot.vector_by_index(d, "x", 2, r2d) for d in data]
    plt.ylabel(r"$r$ [deg/s]")
    # plt.ylim(-40, 40)

    # plt.subplot(414, sharex=ax, position=pos[3])
    # [plot.all(d, "u") for d in data]
    # plt.ylabel(r'$u$')
    # plt.ylim(1.07, 1.47)

    plt.xlabel("Time, sec")
    plt.xlim(t_range)

    for ax in plt.gcf().get_axes():
        ax.label_outer()

    # =======================================
    # Tracking error and parameter estimation
    # =======================================
    figsize, pos = plot.posing(3, subsize, width, top, bottom, left, hspace)
    plt.figure(figsize=figsize)

    ax = plt.subplot(311, position=pos[0])
    [plot.tracking_error(d) for d in data]
    plt.ylabel(r"$||e||$")
    # plt.ylim(0, 0.2)
    plt.legend(loc='best')

    plt.subplot(312, sharex=ax, position=pos[1])
    [plot.all(d, "W") for d in data]
    plt.ylabel(r"$W$")
    # plt.ylim(0, 85)

    # plt.subplot(313, sharex=ax, position=pos[2])
    # plot.all(hmrac, "What")
    # plt.ylabel(r"$\hat{W}$")
    # plt.ylim(0, 85)

    plt.subplot(313, sharex=ax, position=pos[2])
    [plot.all(d, "u") for d in data]
    plt.ylabel(r'$u$')
    # plt.ylim(1.07, 1.47)

    plt.xlabel("Time, sec")
    plt.xlim(t_range)

    plt.show()
예제 #4
0
def exp6_plot():
    def get_data(name, style=dict(), with_info=False):
        path = Path(datadir, name)
        style = datastyle | style

        dataset = SN()
        if with_info:
            data, info = fym.logging.load(path, with_info=with_info)
            dataset.info = info
            dataset.style = style | dict(label=info["cfg"].label)
        else:
            data = fym.logging.load(path)
            dataset.style = style
        dataset.data = data
        return dataset

    # ------ Exp Setup ------ #
    expdir = Path("data", "exp6")
    basestyle = dict(c="k", lw=0.7)
    refstyle = basestyle | dict(c="r", ls="--")
    sql_style = basestyle | dict(c="b", ls="-.")
    klm_style = basestyle | dict(c="g", ls="-.")
    test_style = basestyle | dict(c="k", ls="--")

    # ------ Data 001 ------ #
    datadir = Path(expdir, "data-001")
    datastyle = sql_style
    sql_env = get_data("env.h5", with_info=True)
    sql_agent = get_data("sql-agent.h5", style=sql_env.style)
    sql_test = get_data("test-learnt-env.h5", style=sql_env.style)

    datastyle = test_style
    lqr_test = get_data("test-lqr-env.h5", style=dict(label="LQR"))

    # ------ Data 002 ------ #
    datadir = Path(expdir, "data-002")
    datastyle = klm_style
    klm_env = get_data("env.h5", with_info=True)
    klm_agent = get_data("klm-agent.h5", style=klm_env.style)
    klm_test = get_data("test-learnt-env.h5", style=klm_env.style)

    data_train = [sql_env, klm_env]
    data_agent = [sql_agent, klm_agent]
    data_test = [sql_test, klm_test, lqr_test]

    # Figure common setup
    t_range = (0, sql_env.info["cfg"].env_kwargs.max_t)

    # All in inches
    subsize = (4.05, 0.946)
    width, top, bottom, left = (4.94, 0.2, 0.671765, 0.5487688)
    hspace = 0.2716

    # ============================
    # States and inputs (Training)
    # ============================
    figsize, pos = plot.posing(6, subsize, width, top, bottom, left, hspace)
    plt.figure(figsize=figsize)

    ax = plot.subplot(pos, 0)
    [plot.vector_by_index(d, "x", 0)[0] for d in data_train]
    plt.ylabel(r"$V_T$, m/s")
    # plt.ylim(19, 23)
    plt.legend()

    plot.subplot(pos, 1, sharex=ax)
    [plot.vector_by_index(d, "x", 1, mult=np.rad2deg(1)) for d in data_train]
    plt.ylabel(r"$\alpha$, deg")
    # plt.ylim(-5, 8)

    plot.subplot(pos, 2, sharex=ax)
    [plot.vector_by_index(d, "x", 2, mult=np.rad2deg(1)) for d in data_train]
    plt.ylabel(r"$q$, deg/s")
    # plt.ylim(-50, 50)

    plot.subplot(pos, 3, sharex=ax)
    [plot.vector_by_index(d, "x", 3, mult=np.rad2deg(1)) for d in data_train]
    plt.ylabel(r"$\gamma$, deg")
    # plt.ylim(-5, 23)

    plot.subplot(pos, 4, sharex=ax)
    [plot.vector_by_index(d, "u", 0) for d in data_train]
    plt.ylabel(r"$\delta_t$")
    # plt.ylim(0, 0.2)

    plot.subplot(pos, 5, sharex=ax)
    [plot.vector_by_index(d, "u", 1, mult=np.rad2deg(1)) for d in data_train]
    plt.ylabel(r'$\delta_e$, deg')
    # plt.ylim(-15, 5)

    plt.xlabel("Time, sec")
    plt.xlim(t_range)

    for ax in plt.gcf().get_axes():
        ax.label_outer()

    # ===============================
    # Parameter estimation (Training)
    # ===============================
    figsize, pos = plot.posing(2, subsize, width, top, bottom, left, hspace)
    plt.figure(figsize=figsize)

    plot.subplot(pos, 0, sharex=ax)
    plot.all(sql_env, "K", style=dict(refstyle, label="True"))
    for d in data_agent:
        plot.all(d, "K", is_agent=True, style=dict(marker="o", markersize=2))
    plt.ylabel(r"$\hat{K}$")
    plt.legend()
    plt.ylim(-15, 7)

    plot.subplot(pos, 1, sharex=ax)
    plot.all(sql_env, "P", style=dict(sql_env.style, c="r", ls="--"))
    for d in data_agent:
        plot.all(d, "P", is_agent=True, style=dict(marker="o", markersize=2))
    plt.ylabel(r"$\hat{P}$")
    plt.ylim(-12, 28)

    plt.xlabel("Time, sec")
    plt.xlim(t_range)

    for ax in plt.gcf().get_axes():
        ax.label_outer()

    # ========================
    # States and inputs (Test)
    # ========================
    figsize, pos = plot.posing(6, subsize, width, top, bottom, left, hspace)
    plt.figure(figsize=figsize)

    ax = plot.subplot(pos, 0)
    [plot.vector_by_index(d, "x", 0)[0] for d in data_test]
    plt.ylabel(r"$V_T$, m/s")
    plt.ylim(19, 23)
    plt.legend()

    plot.subplot(pos, 1, sharex=ax)
    [plot.vector_by_index(d, "x", 1, mult=np.rad2deg(1)) for d in data_test]
    plt.ylabel(r"$\alpha$, deg")
    plt.ylim(-5, 8)

    plot.subplot(pos, 2, sharex=ax)
    [plot.vector_by_index(d, "x", 2, mult=np.rad2deg(1)) for d in data_test]
    plt.ylabel(r"$q$, deg/s")
    plt.ylim(-50, 50)

    plot.subplot(pos, 3, sharex=ax)
    [plot.vector_by_index(d, "x", 3, mult=np.rad2deg(1)) for d in data_test]
    plt.ylabel(r"$\gamma$, deg")
    plt.ylim(-5, 23)

    plot.subplot(pos, 4, sharex=ax)
    [plot.vector_by_index(d, "u", 0) for d in data_test]
    plt.ylabel(r"$\delta_t$")
    plt.ylim(0, 0.2)

    plot.subplot(pos, 5, sharex=ax)
    [plot.vector_by_index(d, "u", 1, mult=np.rad2deg(1)) for d in data_test]
    plt.ylabel(r'$\delta_e$, deg')
    plt.ylim(-15, 5)

    plt.xlabel("Time, sec")
    plt.xlim(0, 5)

    for ax in plt.gcf().get_axes():
        ax.label_outer()

    # =================
    # Performance Index
    # =================
    figsize, pos = plot.posing(1, subsize, width, top, bottom, left, hspace)
    plt.figure(figsize=figsize)

    plot.subplot(pos, 0)
    [plot.vector_by_index(d, "PI", 0)[0] for d in data_test]
    plt.ylabel(r"Performance Index")
    plt.ylim(-1, 20)
    plt.legend()

    plt.xlabel("Time, sec")
    plt.xlim(0, 5)

    for ax in plt.gcf().get_axes():
        ax.label_outer()

    # # ==================================
    # # States and inputs (Non-Admissible)
    # # ==================================
    # figsize, pos = plot.posing(5, subsize, width, top, bottom, left, hspace)
    # plt.figure(figsize=figsize)

    # ax = plot.subplot(pos, 0)
    # [plot.vector_by_index(d, "x", 0)[0] for d in data_na]
    # plt.ylabel(r"$x_1$")
    # # plt.ylim(-2, 2)
    # plt.legend()

    # plot.subplot(pos, 1, sharex=ax)
    # [plot.vector_by_index(d, "x", 1) for d in data_na]
    # plt.ylabel(r"$x_2$")
    # # plt.ylim(-2, 2)

    # plot.subplot(pos, 2, sharex=ax)
    # [plot.vector_by_index(d, "u", 0) for d in data_na]
    # plt.ylabel(r'$u$')
    # # plt.ylim(-80, 80)

    # # =====================================
    # # Parameter estimation (Non-Admissible)
    # # =====================================
    # ax = plot.subplot(pos, 3)
    # plot.all(qlearner_na, "K", style=dict(refstyle, label="True"))
    # for d in data_na:
    #     plot.all(
    #         d, "K", is_agent=True,
    #         style=dict(marker="o", markersize=2)
    #     )
    # plt.ylabel(r"$\hat{K}$")
    # plt.legend()
    # # plt.ylim(-70, 30)

    # plot.subplot(pos, 4, sharex=ax)
    # plot.all(qlearner_na, "P", style=refstyle)
    # for d in data_na:
    #     plot.all(
    #         d, "P", is_agent=True,
    #         style=dict(marker="o", markersize=2)
    #     )
    # plt.ylabel(r"$\hat{P}$")
    # # plt.ylim(-70, 30)

    # plt.xlabel("Time, sec")
    # plt.xlim(t_range)

    # for ax in plt.gcf().get_axes():
    #     ax.label_outer()

    imgdir = Path("img", datadir.relative_to("data"))
    imgdir.mkdir(exist_ok=True)

    plt.figure(1)
    plt.savefig(Path(imgdir, "figure_1.pdf"), bbox_inches="tight")

    plt.figure(2)
    plt.savefig(Path(imgdir, "figure_2.pdf"), bbox_inches="tight")

    plt.figure(3)
    plt.savefig(Path(imgdir, "figure_3.pdf"), bbox_inches="tight")

    plt.show()