コード例 #1
0
def main(argv):
    parser = MdpArgs(
        description=
        "plot CO2 related metrics of following MDP instance optimal policy")
    parser.add_model_version()
    parser.add_paramsfile_multiple()
    parser.add_time_range()
    parser.add_iterations()
    parser.add_confidence_interval()
    parser.add_use_data()
    parser.add_save()
    args = parser.get_args()

    if not parser.check_version(
            4, 4, "visualize_co2 only supported for MDP V4 or higher."):
        sys.exit(1)

    if not parser.check_paramfile_multiple():
        sys.exit(2)

    params_all = cl.get_params_multiple(args.version, args.paramsfiles)
    mdp_model = cl.get_mdp_model(args.version, params_all)

    t_range = cl.get_time_range(args, params_all[0])
    if not t_range:
        sys.exit(3)

    mdp_data = MdpDataGatherer(mdp_model, args.iterations, t_range)
    if args.confidenceinterval:
        mdp_data.set_ci(ci_type=args.confidenceinterval)

    x = mdp_data.get_time_range(t_range)

    y_l, y_price, y_tax, y_emit = ([] for i in range(4))
    if args.usedata:
        for pf in args.paramsfiles:
            data = cl.get_mdp_data(args.version, pf)
            y_l.append(
                mdp_data.get_data_component(data, 'tax_level', mean_only=True))
            y_price.append(mdp_data.get_data_component(data, 'co2_price'))
            y_tax.append(mdp_data.get_data_component(data, 'co2_tax'))
            y_emit.append(mdp_data.get_data_component(data, 'co2_emissions'))
    else:
        mdp_fh_all = cl.get_mdp_instance_multiple(mdp_model, params_all)
        for mdp_fh in mdp_fh_all:
            y_l.append(
                mdp_data.calc_data_bounds(
                    mdp_data.get_state_variable(mdp_fh, 'l'))['middle'])
            y_price.append(
                mdp_data.calc_data_bounds(mdp_data.co2_current_price(mdp_fh)))
            y_tax.append(
                mdp_data.calc_data_bounds(mdp_data.co2_tax_collected(mdp_fh)))
            y_emit.append(
                mdp_data.calc_data_bounds(mdp_data.co2_emissions(mdp_fh)))

    params_names = args.paramsfiles
    colors = None

    figs_co2 = []
    mdp_plot = MdpPlotter()
    # Tax level
    mdp_plot.initialize("State Variable: Tax Level", "Time (years)",
                        "Tax Delta (USD)")
    mdp_plot.plot_scatter(x, y_l, params_names, colors=colors)
    fig = mdp_plot.finalize()
    figs_co2.append(fig)
    # Current CO2 price
    mdp_plot.initialize("Current CO2 Price", "Time (years)", "Cost (USD/ton)")
    mdp_plot.plot_lines(x,
                        y_price,
                        params_names,
                        CI=args.confidenceinterval,
                        colors=colors)
    fig = mdp_plot.finalize()
    figs_co2.append(fig)
    # CO2 tax collected
    mdp_plot.initialize("Annual CO2 Tax Collected", "Time (years)",
                        "Cost (USD/yr)")
    mdp_plot.plot_lines(x,
                        y_tax,
                        params_names,
                        CI=args.confidenceinterval,
                        colors=colors)
    fig = mdp_plot.finalize()
    figs_co2.append(fig)
    # CO2 emissions
    mdp_plot.initialize("Annual CO2 Emissions", "Time (years)",
                        "CO2 Emissions (ton/yr)")
    mdp_plot.plot_lines(x,
                        y_emit,
                        params_names,
                        CI=args.confidenceinterval,
                        colors=colors)
    fig = mdp_plot.finalize()
    figs_co2.append(fig)

    names = ["level", "price", "tax", "emit"]
    visuals_dir = Path("visuals/v{}/plots".format(args.version))
    if args.save:
        for fig, name in zip(figs_co2, names):
            fig.savefig(visuals_dir / "g_v{}_co2_{}_{}.png".format(
                args.version, name, args.paramsfile))
    plt.show()
コード例 #2
0
def main(argv):
    parser = MdpArgs(
        description="plot costs of following MDP instance optimal policy")
    parser.add_model_version()
    parser.add_paramsfile_multiple()
    parser.add_time_range()
    parser.add_iterations()
    parser.add_confidence_interval()
    parser.add_use_data()
    parser.add_granular()
    parser.add_save()
    args = parser.get_args()

    if not parser.check_version(4, 4,
                                "visualize_costs supported for MDP V4 only."):
        sys.exit(1)

    if not parser.check_paramfile_multiple():
        sys.exit(2)

    params_all = cl.get_params_multiple(args.version, args.paramsfiles)
    mdp_model = cl.get_mdp_model(args.version, params_all)

    t_range = cl.get_time_range(args, params_all[0])
    if not t_range:
        sys.exit(3)

    if args.granular:
        components = cl.COMPONENTS_GRANULAR
    else:
        components = cl.COMPONENTS

    mdp_data = MdpDataGatherer(mdp_model, args.iterations, t_range)
    if args.confidenceinterval:
        mdp_data.set_ci(ci_type=args.confidenceinterval)

    x = mdp_data.get_time_range(t_range)

    y_total, y_cum, y_breakdown, y_percents = ([] for i in range(4))
    if args.usedata:
        for pf in args.paramsfiles:
            data = cl.get_mdp_data(args.version, pf)
            y_total.append(mdp_data.get_data_component(data, 'cost_total'))
            y_cum.append(
                mdp_data.convert_to_cumulative(
                    mdp_data.get_data_component(data, 'cost_total')))
            y_breakdown.append(
                mdp_data.get_data_component(data, 'cost_breakdown'))
            y_percents.append(mdp_data.get_data_component(
                data, 'cost_percent'))
    else:
        mdp_fh_all = cl.get_mdp_instance_multiple(mdp_model, params_all)
        for mdp_fh in mdp_fh_all:
            y_total.append(
                mdp_data.calc_data_bounds(mdp_data.cost_total(mdp_fh)))
            y_cum.append(
                mdp_data.convert_to_cumulative(
                    mdp_data.calc_data_bounds(mdp_data.cost_total(mdp_fh))))
            y_breakdown.append(
                mdp_data.cost_breakdown_components(mdp_fh, components))
            y_percents.append(
                mdp_data.cost_breakdown_components(mdp_fh,
                                                   components,
                                                   is_percent=True))

    params_names = args.paramsfiles
    colors = None

    mdp_plot = MdpPlotter()
    # Total annual cost
    mdp_plot.initialize("Total Annual Cost", "Time (years)", "Cost (USD/yr)")
    mdp_plot.plot_lines(x,
                        y_total,
                        params_names,
                        colors=colors,
                        CI=args.confidenceinterval)
    fig_total = mdp_plot.finalize()
    # Total cumulative cost
    mdp_plot.initialize("Total Cumulative Cost", "Time (years)",
                        "Cost (USD/yr)")
    mdp_plot.plot_lines(x,
                        y_cum,
                        params_names,
                        colors=colors,
                        CI=args.confidenceinterval)
    fig_total = mdp_plot.finalize()
    # Absolute cost breakdown
    mdp_plot.initialize("Absolute Annual Cost Breakdown", "Time (years)",
                        "Cost (USD/yr)")
    mdp_plot.plot_stacked_bars(x, y_breakdown, components)
    fig_breakdown = mdp_plot.finalize()
    # Percentage cost breakdown
    mdp_plot.initialize("Percentage Annual Cost Breakdown", "Time (years)",
                        "Cost (%/yr)")
    mdp_plot.plot_stacked_bars(x, y_percents, components)
    fig_percents = mdp_plot.finalize()

    visuals_dir = Path("visuals/v{}/plots".format(args.version))
    if args.save:
        fig_total.savefig(visuals_dir / "g_v{}_total.png".format(args.version))
        fig_breakdown.savefig(visuals_dir /
                              "g_v{}_breakdown.png".format(args.version))
        fig_percents.savefig(visuals_dir /
                             "g_v{}_percents.png".format(args.version))
    plt.show()
コード例 #3
0
def main(argv):
    parser = MdpArgs(description="plot tax adjusted vs. non-adjusted targets of following optimal MDP policy")
    parser.add_model_version()
    parser.add_paramsfile_multiple()
    parser.add_time_range()
    parser.add_iterations()
    parser.add_confidence_interval()
    parser.add_use_data()
    parser.add_save()
    args = parser.get_args()

    if not parser.check_version(4, 4, "visualize_adjust only supported for MDP V3 with MDP V4 or higher."):
        sys.exit(1)

    if not parser.check_paramfile_multiple():
        sys.exit(2)

    paramsfiles3 = []
    paramsfiles4 = []
    for i in range(0, len(args.paramsfiles)):
        if i % 2:
            paramsfiles4.append(args.paramsfiles[i])
        else:
            paramsfiles3.append(args.paramsfiles[i])

    # MDP V3.
    params3_all = cl.get_params_multiple(COMP_VERSION, paramsfiles3)
    mdp_model3 = cl.get_mdp_model(COMP_VERSION, params3_all)
    mdp_fh3_all = cl.get_mdp_instance_multiple(mdp_model3, params3_all)
    # MDP V4.
    params4_all = cl.get_params_multiple(args.version, paramsfiles4)
    mdp_model4 = cl.get_mdp_model(args.version, params4_all)

    t_range = cl.get_time_range(args, params4_all[0])
    if not t_range:
        sys.exit(3)

    mdp_data = MdpDataGatherer(mdp_model4, args.iterations, t_range)
    if args.confidenceinterval:
        mdp_data.set_ci(ci_type=args.confidenceinterval)

    x = mdp_data.get_time_range(t_range)

    y_res3, y_emit3, y_tax3 = ([] for i in range(3))
    for mdp_fh3 in mdp_fh3_all:
        _, res, emit, tax = mv.avg_co2_probabilistic_v(mdp_fh3, t_range[0], t_range[1],
                                                       args.iterations, True, res_percent=True)
        y_res3.append(mdp_data.convert_to_percent(mdp_data.calc_data_bounds(res)))
        y_emit3.append(mdp_data.calc_data_bounds(emit))
        y_tax3.append(mdp_data.calc_data_bounds(tax))

    y_res4, y_emit4, y_tax4 = ([] for i in range(3))
    if args.usedata:
        for pf in paramsfiles4:
            data = cl.get_mdp_data(args.version, pf)
            y_res4.append(mdp_data.convert_to_percent(mdp_data.get_data_component(data, 'res_penetration')))
            y_emit4.append(mdp_data.get_data_component(data, 'co2_emissions'))
            y_tax4.append(mdp_data.get_data_component(data, 'co2_tax'))
    else:
        mdp_fh_all = cl.get_mdp_instance_multiple(mdp_model4, params4_all)
        for mdp_fh4 in mdp_fh_all:
            y_res4.append(mdp_data.convert_to_percent(mdp_data.calc_data_bounds(mdp_data.res_penetration(mdp_fh4))))
            y_emit4.append(mdp_data.calc_data_bounds(mdp_data.co2_emissions(mdp_fh4)))
            y_tax4.append(mdp_data.calc_data_bounds(mdp_data.co2_tax_collected(mdp_fh4)))

    params3_names = paramsfiles3 
    params4_names = paramsfiles4
    colors = None 

    figs_compare = []
    mdp_plot = MdpPlotter()
    # RES penetration
    mdp_plot.initialize("RES Penetration", "Time (years)", "RES Penetration (%)")
    mdp_plot.plot_lines(x, y_res3, params3_names, y_max=100, colors=colors, CI=args.confidenceinterval)
    mdp_plot.plot_lines(x, y_res4, params4_names, y_max=100, colors=colors, CI=args.confidenceinterval, linestyle='dashed')
    fig = mdp_plot.finalize()
    figs_compare.append(fig)
    # CO2 emissions
    mdp_plot.initialize("Annual CO2 Emissions", "Time (years)", "CO2 (million ton/yr)")
    mdp_plot.plot_lines(x, y_emit3, params3_names, scale=1e6, colors=colors, CI=args.confidenceinterval)
    mdp_plot.plot_lines(x, y_emit4, params4_names, scale=1e6, colors=colors, CI=args.confidenceinterval, linestyle='dashed')
    fig = mdp_plot.finalize()
    figs_compare.append(fig)
    # CO2 tax collected
    mdp_plot.initialize("Annual CO2 Tax Collected", "Time (years)", "Cost (million USD/yr)")
    mdp_plot.plot_lines(x, y_tax3, params3_names, scale=1e6, colors=colors, CI=args.confidenceinterval)
    mdp_plot.plot_lines(x, y_tax4, params4_names, scale=1e6, colors=colors, CI=args.confidenceinterval, linestyle='dashed')
    fig = mdp_plot.finalize()
    figs_compare.append(fig)

    names = ['res', 'emit', 'target']
    visuals_dir = Path("visuals/v{}/plots".format(args.version))
    if args.save:
        for fig, name in zip(figs_compare, names):
            fig.savefig(visuals_dir / "g_v{}_compare_{}.png".format(args.version, name))
    plt.show()
コード例 #4
0
def main(argv):
    parser = MdpArgs(
        description=
        "plot CO2 and RES actual vs. target of following MDP instance optimal policy"
    )
    parser.add_model_version()
    parser.add_paramsfile_multiple()
    parser.add_targetsfile()
    parser.add_time_range()
    parser.add_iterations()
    parser.add_confidence_interval()
    parser.add_use_data()
    parser.add_save()
    args = parser.get_args()

    if not parser.check_version(
            4, 4, "error: visualize_co2 only supported for MDP V4 or higher."):
        sys.exit(1)

    if not parser.check_paramfile_multiple():
        sys.exit(2)

    if not args.targetsfile:
        print("error: must pass in targetsfile.")
        sys.exit(4)

    params_all = cl.get_params_multiple(args.version, args.paramsfiles)
    mdp_model = cl.get_mdp_model(args.version, params_all)

    t_range = cl.get_time_range(args, params_all[0])
    if not t_range:
        sys.exit(3)

    mdp_data = MdpDataGatherer(mdp_model, args.iterations, t_range)
    if args.confidenceinterval:
        mdp_data.set_ci(ci_type=args.confidenceinterval)

    x = mdp_data.get_time_range(t_range)

    y_res, y_emit = ([] for i in range(2))
    if args.usedata:
        for pf in args.paramsfiles:
            data = cl.get_mdp_data(args.version, pf)
            y_res.append(
                mdp_data.convert_to_percent(
                    mdp_data.get_data_component(data, 'res_penetration')))
            y_emit.append(mdp_data.get_data_component(data, 'co2_emissions'))
    else:
        mdp_fh_all = cl.get_mdp_instance_multiple(mdp_model, params_all)
        for mdp_fh in mdp_fh_all:
            y_res.append(
                mdp_data.convert_to_percent(
                    mdp_data.calc_data_bounds(
                        mdp_data.res_penetration(mdp_fh))))
            y_emit.append(
                mdp_data.calc_data_bounds(mdp_data.co2_emissions(mdp_fh)))

    targets = cl.get_emissions_target(args.version, args.targetsfile)
    targets['x'] = [x + START_YEAR for x in targets['x']]

    mdp_plot = MdpPlotter()
    # RES penetration
    mdp_plot.initialize("RES Penetration", "Time (years)",
                        "RES Penetration (%)")
    mdp_plot.plot_lines(x, y_res, args.paramsfiles, CI=args.confidenceinterval)
    fig_res = mdp_plot.finalize()
    # CO2 emissions (actual vs. target)
    mdp_plot.initialize("Annual CO2 Emissions", "Time (years)",
                        "CO2 Emissions (ton/yr)")
    mdp_plot.plot_lines(x,
                        y_emit,
                        args.paramsfiles,
                        CI=args.confidenceinterval)
    mdp_plot.add_scatter_points(targets['x'], targets['y'], "Target")
    fig_emit = mdp_plot.finalize()

    visuals_dir = Path("visuals/v{}/plots".format(args.version))
    if args.save:
        fig_res.savefig(visuals_dir / "g_v{}_res_{}.png".format(args.version))
        fig_emit.savefig(visuals_dir /
                         "g_v{}_target_{}.png".format(args.version))
    plt.show()
コード例 #5
0
ファイル: visualize_adjust.py プロジェクト: taigereye/cs91r
def main(argv):
    parser = MdpArgs(
        description=
        "plot tax adjusted vs. non-adjusted targets of following optimal MDP policy"
    )
    parser.add_model_version()
    parser.add_paramsfile_multiple()
    parser.add_targetsfile()
    parser.add_time_range()
    parser.add_iterations()
    parser.add_confidence_interval()
    parser.add_use_data()
    parser.add_save()
    args = parser.get_args()

    if not parser.check_version(
            4, 4,
            "visualize_adjust only supported for MDP V3 with MDP V4 or higher."
    ):
        sys.exit(1)

    if not parser.check_paramfile_multiple():
        sys.exit(2)

    # MDP V3.
    params3 = cl.get_params_single(COMP_VERSION, args.paramsfiles[0])
    mdp_model3 = cl.get_mdp_model(COMP_VERSION, [params3])
    mdp_fh3 = cl.get_mdp_instance_single(mdp_model3, params3)
    # MDP V4.
    params4_all = cl.get_params_multiple(args.version, args.paramsfiles[1:])
    mdp_model4 = cl.get_mdp_model(args.version, params4_all)

    t_range = cl.get_time_range(args, params4_all[0])
    if not t_range:
        sys.exit(3)

    mdp_data = MdpDataGatherer(mdp_model4, args.iterations, t_range)
    if args.confidenceinterval:
        mdp_data.set_ci(ci_type=args.confidenceinterval)

    x = mdp_data.get_time_range(t_range)

    _, y_res3, y_emit3, y_tax3 = mv.avg_co2_probabilistic_v(mdp_fh3,
                                                            t_range[0],
                                                            t_range[1],
                                                            args.iterations,
                                                            True,
                                                            res_percent=True)
    y_res3 = mdp_data.convert_to_percent(mdp_data.calc_data_bounds(y_res3))
    y_emit3 = mdp_data.calc_data_bounds(y_emit3)
    y_tax3 = mdp_data.calc_data_bounds(y_tax3)

    y_res4, y_emit4, y_tax4 = ([] for i in range(3))
    if args.usedata:
        for pf in args.paramsfiles[1:]:
            data = cl.get_mdp_data(args.version, pf)
            y_res4.append(
                mdp_data.convert_to_percent(
                    mdp_data.get_data_component(data, 'res_penetration')))
            y_emit4.append(mdp_data.get_data_component(data, 'co2_emissions'))
            y_tax4.append(mdp_data.get_data_component(data, 'co2_tax'))
    else:
        mdp_fh_all = cl.get_mdp_instance_multiple(mdp_model4, params4_all)
        for mdp_fh4 in mdp_fh_all:
            y_res4.append(
                mdp_data.convert_to_percent(
                    mdp_data.calc_data_bounds(
                        mdp_data.res_penetration(mdp_fh4))))
            y_emit4.append(
                mdp_data.calc_data_bounds(mdp_data.co2_emissions(mdp_fh4)))
            y_tax4.append(
                mdp_data.calc_data_bounds(mdp_data.co2_tax_collected(mdp_fh4)))

    y_res = [y_res3] + y_res4
    y_emit = [y_emit3] + y_emit4
    y_tax = [y_tax3] + y_tax4

    if args.targetsfile:
        targets = cl.get_emissions_target(args.version, args.targetsfile)
        targets['x'] = [x + START_YEAR for x in targets['x']]
        targets['x'] = targets['x'][0:7]
        targets['y'] = targets['y'][0:7]
    else:
        targets = None

    # params_names = args.paramsfiles
    params_names = ["{}: static".format(args.paramsfiles[0])] + [
        "{}: adjusted".format(pf) for pf in args.paramsfiles[1:]
    ]
    colors = None

    figs_compare = []
    mdp_plot = MdpPlotter()
    # RES penetration
    mdp_plot.initialize("RES Penetration", "Time (years)",
                        "RES Penetration (%)")
    mdp_plot.plot_lines(x,
                        y_res,
                        params_names,
                        y_max=100,
                        colors=colors,
                        CI=args.confidenceinterval)
    fig = mdp_plot.finalize()
    figs_compare.append(fig)
    # CO2 emissions
    mdp_plot.initialize("Annual CO2 Emissions", "Time (years)",
                        "CO2 (million ton/yr)")
    mdp_plot.plot_lines(x,
                        y_emit,
                        params_names,
                        scale=1e6,
                        colors=colors,
                        CI=args.confidenceinterval)
    if targets:
        mdp_plot.add_scatter_points(targets['x'],
                                    targets['y'],
                                    "Emissions target",
                                    scale=1e6,
                                    marker='o')
    fig = mdp_plot.finalize()
    figs_compare.append(fig)
    # CO2 tax collected
    mdp_plot.initialize("Annual CO2 Tax Collected", "Time (years)",
                        "Cost (billion USD/yr)")
    mdp_plot.plot_lines(x,
                        y_tax,
                        params_names,
                        scale=1e9,
                        colors=colors,
                        CI=args.confidenceinterval)
    fig = mdp_plot.finalize()
    figs_compare.append(fig)

    names = ['res', 'emit', 'target']
    visuals_dir = Path("visuals/v{}/plots".format(args.version))
    if args.save:
        for fig, name in zip(figs_compare, names):
            fig.savefig(visuals_dir /
                        "g_v{}_compare_{}.png".format(args.version, name))
    plt.show()