예제 #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]))
예제 #2
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()
예제 #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_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()
예제 #4
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()
예제 #5
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()