Ejemplo n.º 1
0
def main(argv):
    parser = MdpArgs(
        description=
        "find set of params with least MSE delta from target CO2 emissions")
    parser.add_paramsfile_multiple()
    parser.add_model_version()
    parser.add_targetsfile()
    parser.add_iterations()
    parser.add_use_data()
    args = parser.get_args()

    if not parser.check_version(
            4, 4, "get_mse_emissions 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 = [0, params_all[0]['n_years']]

    mdp_data = MdpDataGatherer(mdp_model, args.iterations, t_range)

    y_emit = []
    if args.usedata:
        for paramfile in args.paramsfiles:
            data = cl.get_mdp_data(args.version, paramfile)
            y_emit.append(
                mdp_data.get_data_component(data,
                                            'co2_emissions',
                                            mean_only=True))
    else:
        mdp_fh_all = cl.get_mdp_instance_multiple(mdp_model, params_all)
        for mdp_fh in mdp_fh_all:
            y_emit.append(
                cl.calc_data_bounds(mdp_data.co2_emissions(mdp_fh))['middle'])

    targets = cl.get_emissions_target(
        args.version,
        args.targetsfile.replace("3", "1").replace("5", "1"))

    y_mse = [mean_squared_error(targets['y'], y, squared=True) for y in y_emit]
    y_rmse = [
        mean_squared_error(targets['y'], y, squared=False) for y in y_emit
    ]

    print("\nCO2 emissions MSE and RMSE:\n")
    for mse, rmse, paramsfile in zip(y_mse, y_rmse, args.paramsfiles):
        print("{}: {:.3e} {:.3e}".format(paramsfile, mse, rmse))

    print("\n\nParams closest to target emissions:\n")
    idx = y_mse.index(min(y_mse))
    print("{}: {:.3e} {:.3e}".format(args.paramsfiles[idx], y_mse[idx],
                                     y_rmse[idx]))
Ejemplo n.º 2
0
def main(argv):
    parser = MdpArgs(description="debug")
    parser.add_model_version()
    parser.add_paramsfile_single()
    args = parser.get_args()

    params = cl.get_params_single(args.version, args.paramsfile)
    mdp_model = cl.get_mdp_model(args.version, [params])
    mdp_fh = cl.get_mdp_instance_single(mdp_model, params)

    # Iterations set to 1, timerange set to 0-20 yr.
    mdp_data = MdpDataGatherer(mdp_model, 1, [0, 20])
    # Variable pulled out of _adjust_co2_tax function in MdpCostCalculator.
    y_base = mdp_data.co2_base(mdp_fh)
    # Variable pulled from
    y_l = mdp_data.get_state_variable(mdp_fh, 'l')
    y_price = mdp_data.co2_current_price(mdp_fh)
    y_e = mdp_data.get_state_variable(mdp_fh, 'e')
    y_inc = mdp_data.co2_inc(mdp_fh)

    # # Use if changing iterations to > 1.
    # y_base = np.mean(mdp_data.co2_base(mdp_fh), axis=0)
    # y_l = np.mean(mdp_data.get_state_variable(mdp_fh, 'l'), axis=0)
    # y_price = np.mean(mdp_data.co2_current_price(mdp_fh), axis=0)
    # y_e = np.mean(mdp_data.get_state_variable(mdp_fh, 'e'), axis=0)
    # y_inc = np.mean(mdp_data.co2_inc(mdp_fh), axis=0)

    np.set_printoptions(linewidth=200)

    with open("debug_co2_output.txt", "a+") as debugfile:
        sys.stdout = debugfile
        print("### DEBUGGING CO2 ###\n\n")
        print(params, "\n")
        time = 0
        # for l, e, base, inc in zip(y_l, y_e, y_base, y_inc):
        #     print("TIME: ", time)
        #     print("level: ", l)
        #     print("act: ", e)
        #     print("base: ", base)
        #     print("inc: ", inc)
        #     time += 1
        print("inc: ", y_inc)
        print("base: ", y_base)
        print("level: ", y_l)
        print("price: ", y_price)
        print("act: ", y_e)
        print("\n\n")
        sys.stdout = og
    debugfile.close()
Ejemplo n.º 3
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()
Ejemplo n.º 4
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()
Ejemplo n.º 5
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()
Ejemplo n.º 6
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_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()
Ejemplo n.º 7
0
def main(argv):
    parser = MdpArgs(
        description=
        "plot state variables of following MDP instance optimal policy")
    parser.add_model_version()
    parser.add_paramsfile_multiple()
    parser.add_time_range()
    parser.add_iterations()
    parser.add_use_data()
    parser.add_save()
    args = parser.get_args()

    if not parser.check_version(
            4, 4, "visualize_state 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)

    x = mdp_data.get_time_range(t_range)

    y_v, y_r, y_l, y_e = ([] for i in range(4))
    if args.usedata:
        for pf in args.paramsfiles:
            data = cl.get_mdp_data(args.version, pf)
            y_v.append(
                mdp_data.get_data_component(data, 'tech_stage',
                                            mean_only=True))
            y_r.append(
                mdp_data.get_data_component(data, 'res_plants',
                                            mean_only=True))
            y_l.append(
                mdp_data.get_data_component(data, 'tax_level', mean_only=True))
            y_e.append(
                mdp_data.get_data_component(data,
                                            'tax_adjustment',
                                            mean_only=True))
    else:
        mdp_fh_all = cl.get_mdp_instance_multiple(mdp_model, params_all)
        for mdp_fh in mdp_fh_all:
            y_v.append(
                mdp_data.calc_data_bounds(
                    mdp_data.get_state_variable(mdp_fh, 'v'))['middle'])
            y_r.append(
                mdp_data.calc_data_bounds(
                    mdp_data.get_state_variable(mdp_fh, 'r'))['middle'])
            y_l.append(
                mdp_data.calc_data_bounds(
                    mdp_data.get_state_variable(mdp_fh, 'l'))['middle'])
            y_e.append(
                mdp_data.calc_data_bounds(
                    mdp_data.get_state_variable(mdp_fh, 'e'))['middle'])

    params_names = args.paramsfiles

    figs_state = []
    mdp_plot = MdpPlotter()
    # Tech stage
    mdp_plot.initialize("State Variable: Tech Stage", "Time (years)",
                        "Tech Stage")
    mdp_plot.plot_bars(x,
                       y_v,
                       params_names,
                       y_min=0,
                       y_max=params_all[0]['n_tech_stages'] - 1)
    fig = mdp_plot.finalize()
    figs_state.append(fig)
    # RES plants
    mdp_plot.initialize("State Variable: RES Plants", "Time (years)",
                        "RES Plants")
    mdp_plot.plot_bars(x,
                       y_r,
                       params_names,
                       y_min=0,
                       y_max=params_all[0]['n_plants'])
    fig = mdp_plot.finalize()
    figs_state.append(fig)
    # Tax level
    mdp_plot.initialize("State Variable: Tax Level", "Time (years)",
                        "Tax Delta (USD)")
    mdp_plot.plot_scatter(x, y_l, params_names)
    fig = mdp_plot.finalize()
    figs_state.append(fig)
    # Tax adjustment
    mdp_plot.initialize("State Variable: Tax Adjustment", "Time (years)",
                        "Tax Adjustment")
    mdp_plot.plot_scatter(x,
                          y_e,
                          params_names,
                          y_min=-2.5 * (params_all[0]['n_tax_levels'] // 2),
                          y_max=2.5 * (params_all[0]['n_tax_levels'] // 2))
    fig = mdp_plot.finalize()
    figs_state.append(fig)

    codes = ['v', 'r', 'l', 'e']
    visuals_dir = Path("visuals/v{}/plots".format(args.version))
    if args.save:
        for fig, code in zip(figs_state, codes):
            fig.savefig(visuals_dir /
                        "g_v{}_state_{}.png".format(args.version, code))
    plt.show()
Ejemplo n.º 8
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)

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

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

    params_names = args.paramsfiles

    mdp_plot = MdpPlotter()
    # RES penetration
    mdp_plot.initialize("RES Penetration", "Time (years)",
                        "RES Penetration (%)")
    mdp_plot.plot_lines(x, y_res, params_names, 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, params_names, CI=args.confidenceinterval)
    if targets:
        mdp_plot.add_scatter_points(targets['x'][t_range[0]:t_range[1]],
                                    targets['y'][t_range[0]:t_range[1]],
                                    "Emissions 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()