Пример #1
0
def main(argv):
    parser = argparse.ArgumentParser(description="run MDP instance")
    parser.add_argument("-m", "--version", help="MDP model version", type=int)
    parser.add_argument("-p",
                        "--paramsfile",
                        help="txt file with version specific params dict")
    args = parser.parse_args()

    params_dir = Path("results/v{}/params".format(args.version))
    pf = params_dir / "p_v{}_{}.txt".format(args.version, args.paramsfile)
    with open(pf, 'r') as paramsfile:
        params = eval(paramsfile.read())
    paramsfile.close()

    mdp_model = None
    if args.version == 0:
        mdp_model = MdpModelV0()
    elif args.version == 1:
        mdp_model = MdpModelV1()
    elif args.version == 2:
        mdp_model = MdpModelV2()
    elif args.version == 3:
        mdp_model = MdpModelV3()
    elif args.version == 4:
        mdp_model = MdpModelV4()
    assert (mdp_model is not None)
    assert (mdp_model.param_names == list(params.keys()))
    mdp_fh = mdp_model.run_fh(params)

    runs_dir = Path("results/v{}/runs".format(args.version))
    of = runs_dir / "r_v{}_{}.txt".format(args.version, args.paramsfile)
    outfile = open(of, 'w+')

    stdout_og = sys.stdout
    sys.stdout = outfile

    mdp_model.print_fh(mdp_fh)

    sys.stdout = stdout_og
    outfile.close()
Пример #2
0
def main(argv):
    parser = argparse.ArgumentParser(description="plot costs of following MDP instance optimal policy")
    parser.add_argument("-m", "--version", help="MDP model version", type=int)
    parser.add_argument("-p", "--paramsfile", help="txt file with version specific params dict")
    parser.add_argument("-c", "--component", help="see single cost component")
    parser.add_argument("-a", "--policy", help="txt file with policy as list", default=None)
    parser.add_argument("-t", "--timerange", help="see specific time range", nargs=2, type=int, default=None)
    parser.add_argument("-v", "--techstage", help="see single tech stage", type=int, default=None)
    parser.add_argument("--save", help="save plots as png files", action='store_true')
    args = parser.parse_args()

    if int(args.version) < 2:
        print("error: plot_cost_component only supported for MDP V2 or higher.")
        sys.exit(1)

    params_dir = Path("results/v{}/params".format(args.version))
    pf = params_dir / "p_v{}_{}.txt".format(args.version, args.paramsfile)
    with open(pf, 'r') as paramsfile:
        params = eval(paramsfile.read())
    paramsfile.close()

    mdp_model = None
    if int(args.version) == 2:
        mdp_model = MdpModelV2()
    elif int(args.version) == 3:
        mdp_model = MdpModelV3()
    elif int(args.version) == 4:
        mdp_model = MdpModelV4()

    assert(mdp_model is not None)
    assert(mdp_model.param_names == list(params.keys()))
    mdp_fh = mdp_model.run_fh(params)

    if args.techstage is not None:
        if args.techstage < 0 or args.techstage >= mdp_fh.n_tech_stages:
            print("error: tech stage {} out of range: {}".format(args.techstage, mdp_fh.n_tech_stages))
            sys.exit(2)

    if args.policy:
        policies_dir = Path("visuals/v{}/policies".format(args.version))
        af = policies_dir / "a_v{}_{}.txt".format(args.version, args.policy)
        with open(af, 'r') as policyfile:
            arb_policy = eval(policyfile.read())
        policyfile.close()
        assert(len(arb_policy) == mdp_fh.n_years)
        policy_type = args.policy
    else:
        policy_type = "optimal"

    if args.techstage is not None:
        if args.policy:
            policy = mv.get_arb_policy_trajectory(arb_policy, args.techstage)
        else:
            policy = mv.get_opt_policy_trajectory(mdp_fh, args.techstage)
        v_str = str(args.techstage)
    else:
        policy = []
        for v in np.arange(mdp_fh.n_tech_stages):
            if args.policy:
                policy.append(mv.get_arb_policy_trajectory(arb_policy, v))
            else:
                policy.append(mv.get_opt_policy_trajectory(mdp_fh, v))
        v_str = "all"

    if args.timerange:
        t0, tN = args.timerange
        t0 = max(0, t0-1)
        if tN - t0 > mdp_fh.n_years:
            print("error: time range {}-{} out of range: {}".format(t0, tN, mdp_fh.n_tech_stages))
            sys.exit(3)
    else:
        t0 = 0
        tN = mdp_fh.n_years

    np.set_printoptions(linewidth=300)
    visuals_dir = Path("visuals/v{}/plots".format(args.version))

    fig_component = mv.cost_by_component_wrapper(mdp_fh, policy, policy_type, args.component, [t0, tN], v=args.techstage)

    if args.save:
        fig_component.savefig(visuals_dir / "g_v{}_{}_{}_{}.png".format(args.version, policy_type, args.component, paramsfile, v_str))
    plt.show()
Пример #3
0
def main(argv):
    parser = argparse.ArgumentParser(description="plot costs of following MDP instance optimal policy")
    parser.add_argument("-m", "--version", help="MDP model version", type=int)
    parser.add_argument("-p", "--paramsfiles", help="list of txt files with version specific params as dict", nargs='*', action='store')
    parser.add_argument("-t", "--timerange", help="see specific time range", nargs=2, type=int, default=None)
    parser.add_argument("-i", "--iterations", help="number of simulations of tech stage transition", type=int, default=200)
    parser.add_argument("-e", "--CO2", help="limit on annual CO2 emissions", type=int, default=0)
    parser.add_argument("-r", "--RES", help="target RES penetration", type=int, default=0)
    parser.add_argument("--save", help="save plots as png files", action='store_true')
    args = parser.parse_args()

    if int(args.version) < 2:
        print("error: plot_compare_co2_res only supported for MDP V2 or higher.")
        sys.exit(1)

    if len(args.paramsfiles) == 0:
        print("error: plot_compare_co2_res requires at least one parameters file.")
        sys.exit(2)

    params_co2_res = []
    for paramsfile in args.paramsfiles:
        params_dir = Path("results/v{}/params".format(args.version))
        pf = params_dir / "p_v{}_{}.txt".format(args.version, paramsfile)
        with open(pf, 'r') as paramsfile:
            params = eval(paramsfile.read())
        paramsfile.close()
        params_co2_res.append(params)
    param_names = [mv.format_param_names(pf) for pf in args.paramsfiles]

    mdp_model = None
    if int(args.version) == 2:
        mdp_model = MdpModelV2()
        p_adv_vary = False
    elif int(args.version) == 3:
        mdp_model = MdpModelV3()
        p_adv_vary = True
    elif int(args.version) == 4:
        mdp_model = MdpModelV4()
        p_adv_vary = True

    assert(mdp_model is not None)
    assert(mdp_model.param_names == list(params.keys()))

    mdp_fh_co2_res = []
    for params in params_co2_res:
        mdp_fh = mdp_model.run_fh(params)
        mdp_fh_co2_res.append(mdp_fh)

    if args.timerange:
        t0, tN = args.timerange
        t0 = max(0, t0-1)
        if tN - t0 > mdp_fh.n_years:
            print("error: time range {}-{} out of range: {}".format(t0, tN, mdp_fh.n_tech_stages))
            sys.exit(3)
    else:
        t0 = 0
        tN = mdp_fh_co2_res[0].n_years

    np.set_printoptions(linewidth=300)
    visuals_dir = Path("visuals/v{}/plots".format(args.version))

    fig_co2_emit = mv.opt_policy_co2_emit(mdp_fh_co2_res, [t0, tN], args.iterations, param_names, CO2=args.CO2, p_adv_vary=p_adv_vary)
    fig_res = mv.opt_policy_res_percent(mdp_fh_co2_res, [t0, tN], args.iterations, param_names, RES=args.RES, p_adv_vary=p_adv_vary)

    if args.save:
        fig_co2_emit.savefig(visuals_dir / "g_v{}_compare_co2_emit_ann{}.png".format(args.version, paramsfile))
        fig_res.savefig(visuals_dir / "g_v{}_compare_res_ann{}.png".format(args.version, paramsfile))
    plt.show()
Пример #4
0
def main(argv):
    parser = argparse.ArgumentParser(
        description="plot costs of following MDP instance optimal policy")
    parser.add_argument("-m", "--version", help="MDP model version", type=int)
    parser.add_argument("-p",
                        "--paramsfile",
                        help="txt file with version specific params as dict")
    parser.add_argument(
        "-a",
        "--policies",
        help="keyword opt and/or txt files with policy as list",
        nargs='*',
        action='store')
    parser.add_argument("-v",
                        "--techstage",
                        help="see single tech stage",
                        type=int,
                        default=None)
    parser.add_argument("-t",
                        "--timerange",
                        help="see specific time range",
                        nargs=2,
                        type=int,
                        default=None)
    parser.add_argument("--granular",
                        help="more granular cost component breakdown",
                        action='store_true')
    parser.add_argument("--save",
                        help="save plots as png files",
                        action='store_true')
    args = parser.parse_args()

    if int(args.version) < 2:
        print(
            "error: plot_double_policy_costs only supported for MDP V2 or higher."
        )
        sys.exit(1)

    if args.policies is not None and len(args.policies) not in (1, 2):
        print(
            "error: plot_double_policy_costs takes 2 arbitrary policies or 1 arbitrary and the optimal policy."
        )
        sys.exit(1)

    params_dir = Path("results/v{}/params".format(args.version))
    pf = params_dir / "p_v{}_{}.txt".format(args.version, args.paramsfile)
    with open(pf, 'r') as paramsfile:
        params = eval(paramsfile.read())
    paramsfile.close()

    mdp_model = None
    if int(args.version) == 2:
        mdp_model = MdpModelV2()
    elif int(args.version) == 3:
        mdp_model = MdpModelV3()
    elif int(args.version) == 4:
        mdp_model = MdpModelV4()

    assert (mdp_model is not None)
    assert (mdp_model.param_names == list(params.keys()))
    mdp_fh = mdp_model.run_fh(params)

    if args.techstage:
        if args.techstage < 0 or args.techstage >= mdp_fh.n_tech_stages:
            print("error: tech stage {} out of range: {}".format(
                args.techstage, mdp_fh.n_tech_stages))
            sys.exit(2)

    pol_strs = args.policies
    two_pols = []
    for pol_str in pol_strs:
        policies_dir = Path("visuals/v{}/policies".format(args.version))
        af = policies_dir / "a_v{}_{}.txt".format(args.version, pol_str)
        with open(af, 'r') as policyfile:
            arb_policy = eval(policyfile.read())
            two_pols.append(arb_policy)
        policyfile.close()
        assert (len(arb_policy) == mdp_fh.n_years)

    if args.techstage is not None:
        double_policy = [
            mv.get_arb_policy_trajectory(pol, args.techstage)
            for pol in two_pols
        ]
        if len(two_pols) == 1:
            double_policy.insert(
                0, mv.get_opt_policy_trajectory(mdp_fh, args.techstage))
        v_str = str(args.techstage)
    else:
        double_policy = []
        if len(two_pols) == 1:
            double_policy.append([
                mv.get_opt_policy_trajectory(mdp_fh, v)
                for v in np.arange(mdp_fh.n_tech_stages)
            ])
            double_policy.append([
                mv.get_arb_policy_trajectory(two_pols[0], v)
                for v in np.arange(mdp_fh.n_tech_stages)
            ])
        else:
            for i in [0, 1]:
                double_policy.append([
                    mv.get_arb_policy_trajectory(two_pols[i], v)
                    for v in np.arange(mdp_fh.n_tech_stages)
                ])
        v_str = "all"

    if len(pol_strs) == 1:
        pol_strs.insert(0, "optimal")
    policy_type = "{}_VS_{}".format(pol_strs[0], pol_strs[1])

    if args.timerange:
        t0, tN = args.timerange
        t0 = max(0, t0 - 1)
        if tN - t0 > mdp_fh.n_years:
            print("error: time range {}-{} out of range: {}".format(
                t0, tN, mdp_fh.n_tech_stages))
            sys.exit(3)
    else:
        t0 = 0
        tN = mdp_fh.n_years

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

    np.set_printoptions(linewidth=300)
    visuals_dir = Path("visuals/v{}/plots".format(args.version))

    fig_breakdown = mv.total_cost_wrapper(mdp_fh,
                                          double_policy,
                                          policy_type, [t0, tN],
                                          v=args.techstage)
    fig_percents = mv.cost_breakdown_wrapper(mdp_fh,
                                             double_policy,
                                             policy_type,
                                             components, [t0, tN],
                                             v=args.techstage)
    fig_total = mv.cost_breakdown_wrapper(mdp_fh,
                                          double_policy,
                                          policy_type,
                                          components, [t0, tN],
                                          v=args.techstage,
                                          percent=True)

    if args.save:
        fig_breakdown.savefig(
            visuals_dir / "g_v{}_{}_breakdown_{}_{}.png".format(
                args.version, policy_type, paramsfile, v_str))
        fig_percents.savefig(visuals_dir /
                             "g_v{}_{}_percents_{}_{}.png".format(
                                 args.version, policy_type, paramsfile, v_str))
        fig_total.savefig(visuals_dir / "g_v{}_{}_total_{}_{}.png".format(
            args.version, policy_type, paramsfile, v_str))
    plt.show()
Пример #5
0
def main(argv):
    parser = argparse.ArgumentParser(
        description="plot costs of following MDP instance optimal policy")
    parser.add_argument("-m", "--version", help="MDP model version", type=int)
    parser.add_argument("-p",
                        "--paramsfile",
                        help="txt file with version specific params as dict")
    parser.add_argument("-t",
                        "--timerange",
                        help="see specific time range",
                        nargs=2,
                        type=int,
                        default=None)
    parser.add_argument("-i",
                        "--iterations",
                        help="number of simulations of tech stage transition",
                        type=int,
                        default=200)
    parser.add_argument("--save",
                        help="save plots as png files",
                        action='store_true')
    args = parser.parse_args()

    if int(args.version) < 2:
        print("error: plot_co2_impacts only supported for MDP V2 or higher.")
        sys.exit(1)

    params_dir = Path("results/v{}/params".format(args.version))
    pf = params_dir / "p_v{}_{}.txt".format(args.version, args.paramsfile)
    with open(pf, 'r') as paramsfile:
        params = eval(paramsfile.read())
    paramsfile.close()

    mdp_model = None
    if int(args.version) == 2:
        mdp_model = MdpModelV2()
        p_adv_vary = False
    elif int(args.version) == 3:
        mdp_model = MdpModelV3()
        p_adv_vary = True
    elif int(args.version) == 4:
        mdp_model = MdpModelV4()
        p_adv_vary = True

    assert (mdp_model is not None)
    assert (mdp_model.param_names == list(params.keys()))
    mdp_fh = mdp_model.run_fh(params)

    policy = [
        mv.get_opt_policy_trajectory(mdp_fh, v)
        for v in np.arange(mdp_fh.n_tech_stages)
    ]

    if args.timerange:
        t0, tN = args.timerange
        t0 = max(0, t0 - 1)
        if tN - t0 > mdp_fh.n_years:
            print("error: time range {}-{} out of range: {}".format(
                t0, tN, mdp_fh.n_tech_stages))
            sys.exit(3)
    else:
        t0 = 0
        tN = mdp_fh.n_years

    np.set_printoptions(linewidth=300)
    visuals_dir = Path("visuals/v{}/plots".format(args.version))

    fig_annual = mv.co2_emit_tax_wrapper(mdp_fh,
                                         policy, [t0, tN],
                                         args.iterations,
                                         is_annual=True,
                                         p_adv_vary=p_adv_vary)
    fig_cum = mv.co2_emit_tax_wrapper(mdp_fh,
                                      policy, [t0, tN],
                                      args.iterations,
                                      is_annual=False,
                                      p_adv_vary=p_adv_vary)

    if args.save:
        fig_annual.savefig(
            visuals_dir /
            "g_v{}_co2_emit_tax_ann_{}.png".format(args.version, paramsfile))
        fig_cum.savefig(
            visuals_dir /
            "g_v{}_co2_emit_tax_cum_{}.png".format(args.version, paramsfile))
    plt.show()
Пример #6
0
def main(argv):
    parser = argparse.ArgumentParser(description="plot costs of following MDP instance optimal policy")
    parser.add_argument("-m", "--version", help="MDP model version", type=int)
    parser.add_argument("-p", "--paramsfile", help="txt file with version specific params as dict")
    parser.add_argument("-s", "--reductions", help="fractional reductions in storage capital costs", nargs='+', type=float)
    parser.add_argument("-t", "--timerange", help="see specific time range", nargs=2, type=int, default=None)
    parser.add_argument("-i", "--iterations", help="number of simulations of tech stage transition", type=int, default=200)
    parser.add_argument("-b", "--budget", help="annual budget for renewable plants", type=int, default=0)
    parser.add_argument("-r", "--RES", help="target renewable penetration", type=int, default=0)
    parser.add_argument("--save", help="save plots as png files", action='store_true')
    args = parser.parse_args()

    if int(args.version) < 2:
        print("error: plot_storage_sensitivity only supported for MDP V2 or higher.")
        sys.exit(1)

    params_dir = Path("results/v{}/params".format(args.version))
    pf = params_dir / "p_v{}_{}.txt".format(args.version, args.paramsfile)
    with open(pf, 'r') as paramsfile:
        params = eval(paramsfile.read())
    paramsfile.close()

    params_storage_reduced = []
    for frac in args.reductions:
        params_reduced = mv.reduce_storage_costs_params(params, frac)
        params_storage_reduced.append(params_reduced)

    mdp_model = None
    if int(args.version) == 2:
        mdp_model = MdpModelV2()
        p_adv_vary = False
    elif int(args.version) == 3:
        mdp_model = MdpModelV3()
        p_adv_vary = True
    elif int(args.version) == 4:
        mdp_model = MdpModelV4()
        p_adv_vary = True

    assert(mdp_model is not None)
    assert(mdp_model.param_names == list(params.keys()))

    mdp_fh_storage_reduced = []
    for params in params_storage_reduced:
        mdp_fh = mdp_model.run_fh(params)
        mdp_fh_storage_reduced.append(mdp_fh)

    if args.timerange:
        t0, tN = args.timerange
        t0 = max(0, t0-1)
        if tN - t0 > mdp_fh.n_years:
            print("error: time range {}-{} out of range: {}".format(t0, tN, mdp_fh.n_tech_stages))
            sys.exit(3)
    else:
        t0 = 0
        tN = mdp_fh.n_years

    np.set_printoptions(linewidth=300)
    visuals_dir = Path("visuals/v{}/plots".format(args.version))

    figs_budget, fig_res = mv.storage_reductions_wrapper(mdp_fh_storage_reduced, [t0, tN], args.iterations, args.reductions, budget=args.budget, RES=args.RES, p_adv_vary=p_adv_vary)

    if args.save:
        figs_budget[0].savefig(visuals_dir / "g_v{}_storage_reductions_budget_ann{}.png".format(args.version, paramsfile))
        figs_budget[1].savefig(visuals_dir / "g_v{}_storage_reductions_budget_cum{}.png".format(args.version, paramsfile))
        fig_res.savefig(visuals_dir / "g_v{}_storage_reductions_RESpenetration_{}.png".format(args.version, paramsfile))
    plt.show()