Exemple #1
0
def main():

    args = _parse_args()

    with_xhatshuffle = args.with_xhatshuffle
    with_lagrangian = args.with_lagrangian

    # This is multi-stage, so we need to supply node names
    hydro = PySPModel("./PySP/models/", "./PySP/nodedata/")
    rho_setter = None

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

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

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

    if with_xhatshuffle:
        xhatshuffle_spoke = vanilla.xhatshuffle_spoke(*beans,
                                                      hydro.all_nodenames)

    list_of_spoke_dict = list()
    if with_lagrangian:
        list_of_spoke_dict.append(lagrangian_spoke)
    if with_xhatshuffle:
        list_of_spoke_dict.append(xhatshuffle_spoke)

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

    if wheel.global_rank == 0:  # we are the reporting hub rank
        print(
            f"BestInnerBound={wheel.BestInnerBound} and BestOuterBound={wheel.BestOuterBound}"
        )

    if write_solution:
        wheel.write_first_stage_solution('hydro_first_stage.csv')
        wheel.write_tree_solution('hydro_full_solution')

    hydro.close()
Exemple #2
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 ###
    if args.run_aph:
        hub_dict = vanilla.aph_hub(
            *beans,
            scenario_creator_kwargs=scenario_creator_kwargs,
            ph_extensions=MultiExtension,
            rho_setter=rho_setter)
    else:
        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)

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

    if args.solution_dir is not None:
        wheel.write_tree_solution(args.solution_dir,
                                  uc.scenario_tree_solution_writer)

    wheel.write_first_stage_solution(
        'uc_cyl_nonants.npy',
        first_stage_solution_writer=sputils.first_stage_nonant_npy_serializer)
def main():

    args = _parse_args()

    BFs = args.branching_factors

    xhat_scenario_dict = make_node_scenario_dict_balanced(BFs)
    all_nodenames = list(xhat_scenario_dict.keys())

    with_xhatspecific = args.with_xhatspecific
    with_lagrangian = args.with_lagrangian
    with_xhatshuffle = args.with_xhatshuffle

    # This is multi-stage, so we need to supply node names
    #all_nodenames = ["ROOT"] # all trees must have this node
    # The rest is a naming convention invented for this problem.
    # Note that mpisppy does not have nodes at the leaves,
    # and node names must end in a serial number.

    ScenCount = np.prod(BFs)
    #ScenCount = _get_num_leaves(BFs)
    sc_options = {"args": args}
    scenario_creator_kwargs = aircond.kw_creator(sc_options)

    all_scenario_names = [f"scen{i}"
                          for i in range(ScenCount)]  #Scens are 0-based
    # print(all_scenario_names)
    scenario_creator = aircond.scenario_creator
    scenario_denouement = aircond.scenario_denouement
    primal_rho_setter = aircond.primal_rho_setter
    dual_rho_setter = aircond.dual_rho_setter

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

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

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

    # xhat specific bound spoke
    if with_xhatspecific:
        xhatspecific_spoke = vanilla.xhatspecific_spoke(
            *beans,
            xhat_scenario_dict,
            all_nodenames=all_nodenames,
            scenario_creator_kwargs=scenario_creator_kwargs)

    #xhat shuffle looper bound spoke

    if with_xhatshuffle:
        xhatshuffle_spoke = vanilla.xhatshuffle_spoke(
            *beans,
            all_nodenames=all_nodenames,
            scenario_creator_kwargs=scenario_creator_kwargs)
    list_of_spoke_dict = list()
    if with_lagrangian:
        list_of_spoke_dict.append(lagrangian_spoke)
    if with_xhatspecific:
        list_of_spoke_dict.append(xhatspecific_spoke)
    if with_xhatshuffle:
        list_of_spoke_dict.append(xhatshuffle_spoke)

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

    fname = 'aircond_cyl_nonants.npy'
    if wheel.global_rank == 0:
        print("BestInnerBound={} and BestOuterBound={}".\
              format(wheel.BestInnerBound, wheel.BestOuterBound))
        if write_solution:
            print(f"Writing first stage solution to {fname}")
    # all ranks need to participate because only the winner will write
    if write_solution:
        wheel.write_first_stage_solution('aircond_first_stage.csv')
        wheel.write_tree_solution('aircond_full_solution')
        wheel.write_first_stage_solution(
            fname,
            first_stage_solution_writer=first_stage_nonant_npy_serializer)
Exemple #4
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')
Exemple #5
0
def main():

    args = _parse_args()

    num_scen = args.num_scens
    crops_multiplier = args.crops_mult

    rho_setter = farmer._rho_setter if hasattr(farmer, '_rho_setter') else None
    if args.default_rho is None and rho_setter is None:
        raise RuntimeError(
            "No rho_setter so a default must be specified via --default-rho")

    if args.use_norm_rho_converger:
        if not args.use_norm_rho_updater:
            raise RuntimeError(
                "--use-norm-rho-converger requires --use-norm-rho-updater")
        else:
            ph_converger = NormRhoConverger
    else:
        ph_converger = None

    scenario_creator = farmer.scenario_creator
    scenario_denouement = farmer.scenario_denouement
    all_scenario_names = ['scen{}'.format(sn) for sn in range(num_scen)]
    scenario_creator_kwargs = {
        'use_integer': False,
        "crops_multiplier": crops_multiplier,
    }
    scenario_names = [f"Scenario{i+1}" for i in range(num_scen)]

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

    if args.run_async:
        # Vanilla APH hub
        hub_dict = vanilla.aph_hub(
            *beans,
            scenario_creator_kwargs=scenario_creator_kwargs,
            ph_extensions=None,
            rho_setter=rho_setter)
    else:
        # Vanilla PH hub
        hub_dict = vanilla.ph_hub(
            *beans,
            scenario_creator_kwargs=scenario_creator_kwargs,
            ph_extensions=None,
            ph_converger=ph_converger,
            rho_setter=rho_setter)

    ## hack in adaptive rho
    if args.use_norm_rho_updater:
        hub_dict['opt_kwargs']['extensions'] = NormRhoUpdater
        hub_dict['opt_kwargs']['options']['norm_rho_options'] = {
            'verbose': True
        }

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

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

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

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

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

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

    if write_solution:
        wheel.write_first_stage_solution('farmer_plant.csv')
        wheel.write_first_stage_solution('farmer_cyl_nonants.npy',
                                         first_stage_solution_writer=sputils.
                                         first_stage_nonant_npy_serializer)
        wheel.write_tree_solution('farmer_full_solution')
Exemple #6
0
    def run(self):
        """ Top-level execution."""
        if self.is_EF:
            ef = sputils.create_EF(
                self.scenario_names,
                self.scenario_creator,
                scenario_creator_kwargs=self.kwargs,
                suppress_warnings=True,
            )

            tee_ef_solves = self.options.get('tee_ef_solves', False)

            solvername = self.solvername
            solver = pyo.SolverFactory(solvername)
            if hasattr(self, "solver_options") and (self.solver_options
                                                    is not None):
                for option_key, option_value in self.solver_options.items():
                    if option_value is not None:
                        solver.options[option_key] = option_value
            if self.verbose:
                global_toc("Starting EF solve")
            if 'persistent' in solvername:
                solver.set_instance(ef, symbolic_solver_labels=True)
                results = solver.solve(tee=tee_ef_solves)
            else:
                results = solver.solve(
                    ef,
                    tee=tee_ef_solves,
                    symbolic_solver_labels=True,
                )
            if self.verbose:
                global_toc("Completed EF solve")

            self.EF_Obj = pyo.value(ef.EF_Obj)

            objs = sputils.get_objs(ef)

            self.is_minimizing = objs[0].is_minimizing
            #TBD : Write a function doing this
            if self.is_minimizing:
                self.best_outer_bound = results.Problem[0]['Lower bound']
                self.best_inner_bound = results.Problem[0]['Upper bound']
            else:
                self.best_inner_bound = results.Problem[0]['Upper bound']
                self.best_outer_bound = results.Problem[0]['Lower bound']
            self.ef = ef

            if 'write_solution' in self.options:
                if 'first_stage_solution' in self.options['write_solution']:
                    sputils.write_ef_first_stage_solution(
                        self.ef,
                        self.options['write_solution']['first_stage_solution'])
                if 'tree_solution' in self.options['write_solution']:
                    sputils.write_ef_tree_solution(
                        self.ef,
                        self.options['write_solution']['tree_solution'])

            self.xhats = sputils.nonant_cache_from_ef(ef)
            self.local_xhats = self.xhats  #Every scenario is local for EF
            self.first_stage_solution = {"ROOT": self.xhats["ROOT"]}

        else:
            self.ef = None
            args = argparse.Namespace(**self.options)

            #Create a hub dict
            hub_name = find_hub(self.options['cylinders'], self.is_multi)
            hub_creator = getattr(vanilla, hub_name + '_hub')
            beans = {
                "args": args,
                "scenario_creator": self.scenario_creator,
                "scenario_denouement": self.scenario_denouement,
                "all_scenario_names": self.scenario_names,
                "scenario_creator_kwargs": self.kwargs
            }
            if self.is_multi:
                beans["all_nodenames"] = self.options["all_nodenames"]
            hub_dict = hub_creator(**beans)

            #Add extensions
            if 'extensions' in self.options:
                for extension in self.options['extensions']:
                    extension_creator = getattr(vanilla, 'add_' + extension)
                    hub_dict = extension_creator(hub_dict, args)

            #Create spoke dicts
            potential_spokes = find_spokes(self.options['cylinders'],
                                           self.is_multi)
            #We only use the spokes with an associated command line arg set to True
            spokes = [
                spoke for spoke in potential_spokes
                if self.options['with_' + spoke]
            ]
            list_of_spoke_dict = list()
            for spoke in spokes:
                spoke_creator = getattr(vanilla, spoke + '_spoke')
                spoke_beans = copy.deepcopy(beans)
                if spoke == "xhatspecific":
                    spoke_beans["scenario_dict"] = self.options[
                        "scenario_dict"]
                spoke_dict = spoke_creator(**spoke_beans)
                list_of_spoke_dict.append(spoke_dict)

            ws = WheelSpinner(hub_dict, list_of_spoke_dict)
            ws.run()

            spcomm = ws.spcomm

            self.opt = spcomm.opt
            self.on_hub = ws.on_hub()

            if self.on_hub:  # we are on a hub rank
                self.best_inner_bound = spcomm.BestInnerBound
                self.best_outer_bound = spcomm.BestOuterBound
                #NOTE: We do not get bounds on every rank, only on hub
                #      This should change if we want to use cylinders for MMW

            if 'write_solution' in self.options:
                if 'first_stage_solution' in self.options['write_solution']:
                    ws.write_first_stage_solution(
                        self.options['write_solution']['first_stage_solution'])
                if 'tree_solution' in self.options['write_solution']:
                    ws.write_tree_solution(
                        self.options['write_solution']['tree_solution'])

            if self.on_hub:  #we are on a hub rank
                a_sname = self.opt.local_scenario_names[0]
                root = self.opt.local_scenarios[a_sname]._mpisppy_node_list[0]
                self.first_stage_solution = {
                    "ROOT":
                    [pyo.value(var) for var in root.nonant_vardata_list]
                }
                self.local_xhats = ws.local_nonant_cache()
Exemple #7
0
def main():

    args = _parse_args()

    BFs = args.branching_factors
    if len(BFs) != 2:
        raise RuntimeError("Hydro is a three stage problem, so it needs 2 BFs")

    with_xhatshuffle = args.with_xhatshuffle
    with_lagrangian = args.with_lagrangian

    # This is multi-stage, so we need to supply node names
    all_nodenames = sputils.create_nodenames_from_branching_factors(BFs)

    ScenCount = BFs[0] * BFs[1]
    scenario_creator_kwargs = {"branching_factors": BFs}
    all_scenario_names = [f"Scen{i+1}" for i in range(ScenCount)]
    scenario_creator = hydro.scenario_creator
    scenario_denouement = hydro.scenario_denouement
    rho_setter = None

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

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

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

    # xhat looper bound spoke

    if with_xhatshuffle:
        xhatshuffle_spoke = vanilla.xhatshuffle_spoke(
            *beans,
            all_nodenames=all_nodenames,
            scenario_creator_kwargs=scenario_creator_kwargs,
            spoke_sleep_time=SPOKE_SLEEP_TIME)

    list_of_spoke_dict = list()
    if with_lagrangian:
        list_of_spoke_dict.append(lagrangian_spoke)
    if with_xhatshuffle:
        list_of_spoke_dict.append(xhatshuffle_spoke)

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

    if wheel.global_rank == 0:  # we are the reporting hub rank
        print(
            f"BestInnerBound={wheel.BestInnerBound} and BestOuterBound={wheel.BestOuterBound}"
        )

    if write_solution:
        wheel.write_first_stage_solution('hydro_first_stage.csv')
        wheel.write_tree_solution('hydro_full_solution')