Esempio n. 1
0
def main():

    args = _parse_args()

    num_scen = args.num_scens

    with_fwph = args.with_fwph
    with_xhatlooper = args.with_xhatlooper
    with_xhatshuffle = args.with_xhatshuffle
    with_lagrangian = args.with_lagrangian
    with_fixer = args.with_fixer
    fixer_tol = args.fixer_tol
    with_cross_scenario_cuts = args.with_cross_scenario_cuts

    scensavail = [3, 5, 10, 25, 50, 100]
    if num_scen not in scensavail:
        raise RuntimeError("num-scen was {}, but must be in {}".\
                           format(num_scen, scensavail))

    scenario_creator_kwargs = {
        "scenario_count": num_scen,
        "path": str(num_scen) + "scenarios_r1",
    }
    scenario_creator = uc.scenario_creator
    scenario_denouement = uc.scenario_denouement
    all_scenario_names = [f"Scenario{i+1}" for i in range(num_scen)]
    rho_setter = uc._rho_setter

    # Things needed for vanilla cylinders
    beans = (args, scenario_creator, scenario_denouement, all_scenario_names)

    ### start ph spoke ###
    # Start with Vanilla PH hub
    hub_dict = vanilla.ph_hub(*beans,
                              scenario_creator_kwargs=scenario_creator_kwargs,
                              ph_extensions=MultiExtension,
                              rho_setter=rho_setter)

    # Extend and/or correct the vanilla dictionary
    ext_classes = [Gapper]
    if with_fixer:
        ext_classes.append(Fixer)
    if with_cross_scenario_cuts:
        ext_classes.append(CrossScenarioExtension)
    if args.xhat_closest_tree:
        ext_classes.append(XhatClosest)

    hub_dict["opt_kwargs"]["extension_kwargs"] = {"ext_classes": ext_classes}
    if with_cross_scenario_cuts:
        hub_dict["opt_kwargs"]["options"]["cross_scen_options"]\
            = {"check_bound_improve_iterations" : args.cross_scenario_iter_cnt}

    if with_fixer:
        hub_dict["opt_kwargs"]["options"]["fixeroptions"] = {
            "verbose": args.with_verbose,
            "boundtol": fixer_tol,
            "id_fix_list_fct": uc.id_fix_list_fct,
        }
    if args.xhat_closest_tree:
        hub_dict["opt_kwargs"]["options"]["xhat_closest_options"] = {
            "xhat_solver_options": dict(),
            "keep_solution": True
        }

    if args.ph_mipgaps_json is not None:
        with open(args.ph_mipgaps_json) as fin:
            din = json.load(fin)
        mipgapdict = {int(i): din[i] for i in din}
    else:
        mipgapdict = None
    hub_dict["opt_kwargs"]["options"]["gapperoptions"] = {
        "verbose": args.with_verbose,
        "mipgapdict": mipgapdict
    }

    if args.default_rho is None:
        # since we are using a rho_setter anyway
        hub_dict.opt_kwargs.options["defaultPHrho"] = 1
    ### end ph spoke ###

    # FWPH spoke
    if with_fwph:
        fw_spoke = vanilla.fwph_spoke(
            *beans, scenario_creator_kwargs=scenario_creator_kwargs)

    # Standard Lagrangian bound spoke
    if with_lagrangian:
        lagrangian_spoke = vanilla.lagrangian_spoke(
            *beans,
            scenario_creator_kwargs=scenario_creator_kwargs,
            rho_setter=rho_setter)

    # xhat looper bound spoke
    if with_xhatlooper:
        xhatlooper_spoke = vanilla.xhatlooper_spoke(
            *beans, scenario_creator_kwargs=scenario_creator_kwargs)

    # xhat shuffle bound spoke
    if with_xhatshuffle:
        xhatshuffle_spoke = vanilla.xhatshuffle_spoke(
            *beans, scenario_creator_kwargs=scenario_creator_kwargs)

    # cross scenario cut spoke
    if with_cross_scenario_cuts:
        cross_scenario_cuts_spoke = vanilla.cross_scenario_cuts_spoke(
            *beans, scenario_creator_kwargs=scenario_creator_kwargs)

    list_of_spoke_dict = list()
    if with_fwph:
        list_of_spoke_dict.append(fw_spoke)
    if with_lagrangian:
        list_of_spoke_dict.append(lagrangian_spoke)
    if with_xhatlooper:
        list_of_spoke_dict.append(xhatlooper_spoke)
    if with_xhatshuffle:
        list_of_spoke_dict.append(xhatshuffle_spoke)
    if with_cross_scenario_cuts:
        list_of_spoke_dict.append(cross_scenario_cuts_spoke)

    spcomm, opt_dict = sputils.spin_the_wheel(hub_dict, list_of_spoke_dict)

    if args.solution_dir is not None:
        sputils.write_spin_the_wheel_tree_solution(
            spcomm, opt_dict, args.solution_dir,
            uc.scenario_tree_solution_writer)
Esempio n. 2
0
def main():
    args = _parse_args()
    inst = args.instance_name
    num_scen = int(inst.split("-")[-3])
    if args.num_scens is not None and args.num_scens != num_scen:
        raise RuntimeError("Argument num-scens={} does not match the number "
                           "implied by instance name={} "
                           "\n(--num-scens is not needed for netdes)")

    with_fwph = args.with_fwph
    with_xhatlooper = args.with_xhatlooper
    with_xhatshuffle = args.with_xhatshuffle
    with_lagrangian = args.with_lagrangian
    with_slamup = args.with_slamup
    with_cross_scenario_cuts = args.with_cross_scenario_cuts

    if args.default_rho is None:
        raise RuntimeError("The --default-rho option must be specified")

    path = f"{netdes.__file__[:-10]}/data/{inst}.dat"
    scenario_creator = netdes.scenario_creator
    scenario_denouement = netdes.scenario_denouement
    all_scenario_names = [f"Scen{i}" for i in range(num_scen)]
    scenario_creator_kwargs = {"path": path}

    # Things needed for vanilla cylinders
    beans = (args, scenario_creator, scenario_denouement, all_scenario_names)

    if with_cross_scenario_cuts:
        ph_ext = CrossScenarioExtension
    else:
        ph_ext = None

    # Vanilla PH hub
    hub_dict = vanilla.ph_hub(*beans,
                              scenario_creator_kwargs=scenario_creator_kwargs,
                              ph_extensions=ph_ext,
                              rho_setter=None)

    if with_cross_scenario_cuts:
        hub_dict["opt_kwargs"]["options"]["cross_scen_options"]\
            = {"check_bound_improve_iterations" : args.cross_scenario_iter_cnt}

    # FWPH spoke
    if with_fwph:
        fw_spoke = vanilla.fwph_spoke(
            *beans, scenario_creator_kwargs=scenario_creator_kwargs)

    # Standard Lagrangian bound spoke
    if with_lagrangian:
        lagrangian_spoke = vanilla.lagrangian_spoke(
            *beans,
            scenario_creator_kwargs=scenario_creator_kwargs,
            rho_setter=None)

    # xhat looper bound spoke
    if with_xhatlooper:
        xhatlooper_spoke = vanilla.xhatlooper_spoke(
            *beans, scenario_creator_kwargs=scenario_creator_kwargs)

    # xhat shuffle bound spoke
    if with_xhatshuffle:
        xhatshuffle_spoke = vanilla.xhatshuffle_spoke(
            *beans, scenario_creator_kwargs=scenario_creator_kwargs)

    # slam up bound spoke
    if with_slamup:
        slamup_spoke = vanilla.slamup_spoke(
            *beans, scenario_creator_kwargs=scenario_creator_kwargs)

    # cross scenario cuts spoke
    if with_cross_scenario_cuts:
        cross_scenario_cuts_spoke = vanilla.cross_scenario_cuts_spoke(
            *beans, scenario_creator_kwargs=scenario_creator_kwargs)

    list_of_spoke_dict = list()
    if with_fwph:
        list_of_spoke_dict.append(fw_spoke)
    if with_lagrangian:
        list_of_spoke_dict.append(lagrangian_spoke)
    if with_xhatlooper:
        list_of_spoke_dict.append(xhatlooper_spoke)
    if with_xhatshuffle:
        list_of_spoke_dict.append(xhatshuffle_spoke)
    if with_slamup:
        list_of_spoke_dict.append(slamup_spoke)
    if with_cross_scenario_cuts:
        list_of_spoke_dict.append(cross_scenario_cuts_spoke)

    wheel = WheelSpinner(hub_dict, list_of_spoke_dict)
    wheel.spin()

    if write_solution:
        wheel.write_first_stage_solution('netdes_build.csv')
        wheel.write_tree_solution('netdes_full_solution')