Esempio n. 1
0
def main():
    
    args = _parse_args()

    num_scen = args.num_scens
    CropsMult = 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 there must be --default-rho") 
    
    scenario_creator = farmer.scenario_creator
    scenario_denouement = farmer.scenario_denouement
    all_scenario_names = ['scen{}'.format(sn) for sn in range(num_scen)]
    cb_data={'use_integer': False, "CropsMult": CropsMult}
    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)
    
    # Vanilla PH hub
    hub_dict = vanilla.ph_hub(*beans,
                              cb_data=cb_data,
                              ph_extensions=None,
                              rho_setter = rho_setter)

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

    # Special Lagranger bound spoke
    if args.with_lagranger:
        lagranger_spoke = vanilla.lagranger_spoke(*beans,
                                              cb_data=cb_data,
                                              rho_setter = rho_setter)

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

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

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

    mpisppy.cylinders.SPOKE_SLEEP_TIME = 0.1

    spin_the_wheel(hub_dict, list_of_spoke_dict)
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)

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

    if "hub_class" in opt_dict:  # we are a hub rank
        if spcomm.opt.cylinder_rank == 0:  # we are the reporting hub rank
            print("BestInnerBound={} and BestOuterBound={}".\
                  format(spcomm.BestInnerBound, spcomm.BestOuterBound))
    
    if write_solution:
        sputils.write_spin_the_wheel_first_stage_solution(spcomm, opt_dict, 'hydro_first_stage.csv')
        sputils.write_spin_the_wheel_tree_solution(spcomm, opt_dict, 'hydro_full_solution')

    hydro.close()
Esempio n. 3
0
                "threads": 2,
            },
            "xhat_scenario_name": "Scenario3",
            "csvname": "specific.csv",
        },
    """

    ub_spoke = {
        'spoke_class': XhatLooperInnerBound,
        "spoke_kwargs": dict(),
        "opt_class": PHBase,
        'opt_kwargs': {
            'PHoptions': xhat_options,
            'all_scenario_names': all_scenario_names,
            'scenario_creator': scenario_creator,
            'scenario_denouement': scenario_denouement,
            "cb_data": cb_data,
        },
    }

    list_of_spoke_dict = (fw_spoke, lagrangian_spoke, ub_spoke)

    spcomm, opt_dict = spin_the_wheel(hub_dict, list_of_spoke_dict)
    # there are ways to get the answer sooner
    if "hub_class" in opt_dict:  # we are hub rank
        if spcomm.opt.rank == spcomm.opt.rank0:  # we are the reporting hub rank
            print("BestInnerBound={} and BestOuterBound={}".\
                  format(spcomm.BestInnerBound, spcomm.BestOuterBound))
    print("End time={} for global rank={}".\
          format(datetime.datetime.now(), rank_global))
Esempio n. 4
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,
            )

            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=False)
            else:
                results = solver.solve(
                    ef,
                    tee=False,
                    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)

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

            self.opt = spcomm.opt
            self.cylinder_rank = self.opt.cylinder_rank
            self.on_hub = ("hub_class" in opt_dict)

            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']:
                    sputils.write_spin_the_wheel_first_stage_solution(
                        spcomm, opt_dict,
                        self.options['write_solution']['first_stage_solution'])
                if 'tree_solution' in self.options['write_solution']:
                    sputils.write_spin_the_wheel_tree_solution(
                        spcomm, opt_dict,
                        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 = sputils.local_nonant_cache(spcomm)
Esempio n. 5
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")

    cb_data = 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)]

    # 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,
                              cb_data=cb_data,
                              ph_extensions=ph_ext,
                              rho_setter = None)

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

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

    # Standard Lagrangian bound spoke
    if with_lagrangian:
        lagrangian_spoke = vanilla.lagrangian_spoke(*beans,
                                              cb_data=cb_data,
                                              rho_setter = None)
        
    # xhat looper bound spoke
    if with_xhatlooper:
        xhatlooper_spoke = vanilla.xhatlooper_spoke(*beans, cb_data=cb_data)

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

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

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

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

    spin_the_wheel(hub_dict, list_of_spoke_dict)
Esempio n. 6
0
def main():
    args = _parse_args()
    inst = args.instance_name
    num_scen = int(inst.split("_")[-1])
    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 sslp)")

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

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

    cb_data = f"{sslp.__file__[:-8]}/data/{inst}/scenariodata"
    scenario_creator = sslp.scenario_creator
    scenario_denouement = sslp.scenario_denouement    
    all_scenario_names = [f"Scenario{i+1}" for i in range(num_scen)]

    if with_fixer:
        ph_ext = Fixer
    else:
        ph_ext = None

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

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

    if with_fixer:
        hub_dict["opt_kwargs"]["PHoptions"]["fixeroptions"] = {
            "verbose": False,
            "boundtol": fixer_tol,
            "id_fix_list_fct": sslp.id_fix_list_fct,
        }
        
    # FWPH spoke
    if with_fwph:
        fw_spoke = vanilla.fwph_spoke(*beans, cb_data=cb_data)

    # Standard Lagrangian bound spoke
    if with_lagrangian:
        lagrangian_spoke = vanilla.lagrangian_spoke(*beans,
                                              cb_data=cb_data,
                                              rho_setter = None)
        
    # xhat looper bound spoke
    if with_xhatlooper:
        xhatlooper_spoke = vanilla.xhatlooper_spoke(*beans, cb_data=cb_data)

    # xhat shuffle bound spoke
    if with_xhatshuffle:
        xhatshuffle_spoke = vanilla.xhatshuffle_spoke(*beans, cb_data=cb_data)
       
    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)

    spin_the_wheel(hub_dict, list_of_spoke_dict)
Esempio n. 7
0
        "opt_kwargs": {
            "PHoptions": ph_options,
            "all_scenario_names": scenario_names,
            "scenario_creator": scenario_creator,
            "scenario_creator_kwargs": scenario_creator_kwargs,
        }
    }

    # xhat looper bound spoke
    xhat_options = hub_ph_options.copy()
    xhat_options['bundles_per_rank'] = 0  #  no bundles for xhat
    xhat_options["xhat_looper_options"] =  {"xhat_solver_options":\
                                          ph_options["iterk_solver_options"],
                                          "scen_limit": 3,
                                          "dump_prefix": "delme",
                                          "csvname": "looper.csv"}
    xhatlooper_spoke = {
        "spoke_class": XhatLooperInnerBound,
        "spoke_kwargs": dict(),
        "opt_class": PHBase,
        "opt_kwargs": {
            "PHoptions": xhat_options,
            "all_scenario_names": scenario_names,
            "scenario_creator": scenario_creator,
            "scenario_creator_kwargs": scenario_creator_kwargs,
        }
    }
    list_of_spoke_dict = (fw_spoke, lagrangian_spoke, xhatlooper_spoke)

    spin_the_wheel(hub_dict, list_of_spoke_dict)
Esempio n. 8
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

    if num_scen not in (3, 10):
        raise RuntimeError(f"num_scen must the 3 or 10; was {num_scen}")

    scenario_creator_kwargs = {"scenario_count": num_scen}
    scenario_creator = sizes.scenario_creator
    scenario_denouement = sizes.scenario_denouement
    all_scenario_names = [f"Scenario{i+1}" for i in range(num_scen)]
    rho_setter = sizes._rho_setter
    variable_probability = sizes._variable_probability

    if with_fixer:
        ph_ext = Fixer
    else:
        ph_ext = 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=ph_ext,
                              rho_setter=rho_setter,
                              variable_probability=variable_probability)

    if with_fixer:
        hub_dict["opt_kwargs"]["PHoptions"]["fixeroptions"] = {
            "verbose": False,
            "boundtol": fixer_tol,
            "id_fix_list_fct": sizes.id_fix_list_fct,
        }
    if args.default_rho is None:
        # since we are using a rho_setter anyway
        hub_dict.opt_kwargs.PHoptions["defaultPHrho"] = 1

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

    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)

    spin_the_wheel(hub_dict, list_of_spoke_dict)
Esempio n. 9
0
def main():
    start_time = dt.datetime.now()
    # start options
    casename = "pglib-opf-master/pglib_opf_case14_ieee.m"
    # pglib_opf_case3_lmbd.m
    # pglib_opf_case118_ieee.m
    # pglib_opf_case300_ieee.m
    # pglib_opf_case2383wp_k.m
    # pglib_opf_case2000_tamu.m
    # do not use pglib_opf_case89_pegase
    egret_path_to_data = "/thirdparty/" + casename
    number_of_stages = 3
    stage_duration_minutes = [5, 15, 30]
    if len(sys.argv) != number_of_stages + 3:
        print(
            "Usage: python ccop_multistage bf1 bf2 iters scenperbun(!) solver")
        exit(1)
    branching_factors = [int(sys.argv[1]), int(sys.argv[2])]
    PHIterLimit = int(sys.argv[3])
    scenperbun = int(sys.argv[4])
    solvername = sys.argv[5]

    seed = 1134
    a_line_fails_prob = 0.1
    repair_fct = FixFast
    verbose = False
    # end options
    # create an arbitrary xhat for xhatspecific to use
    xhat_dict = {"ROOT": "Scenario_1"}
    for i in range(branching_factors[0]):
        xhat_dict["ROOT_" +
                  str(i)] = "Scenario_" + str(1 + i * branching_factors[1])

    cb_data = dict()
    cb_data["convex_relaxation"] = True
    cb_data["epath"] = egret_path_to_data
    if cb_data["convex_relaxation"]:
        # for initialization solve
        solver = pyo.SolverFactory(solvername)
        cb_data["solver"] = None
        ##if "gurobi" in solvername:
        ##solver.options["BarHomogeneous"] = 1
    else:
        solver = pyo.SolverFactory(solvername)
        if "gurobi" in solvername:
            solver.options["BarHomogeneous"] = 1
        cb_data["solver"] = solver
    md_dict = _md_dict(cb_data)

    if verbose:
        print("start data dump")
        print(list(md_dict.elements("generator")))
        for this_branch in md_dict.elements("branch"):
            print("TYPE=", type(this_branch))
            print("B=", this_branch)
            print("IN SERVICE=", this_branch[1]["in_service"])
        print("GENERATOR SET=", md_dict.attributes("generator"))
        print("end data dump")

    lines = list()
    for this_branch in md_dict.elements("branch"):
        lines.append(this_branch[0])

    acstream = np.random.RandomState()

    cb_data["etree"] = etree.ACTree(number_of_stages, branching_factors, seed,
                                    acstream, a_line_fails_prob,
                                    stage_duration_minutes, repair_fct, lines)
    cb_data["acstream"] = acstream

    all_scenario_names=["Scenario_"+str(i)\
                        for i in range(1,len(cb_data["etree"].\
                                             rootnode.ScenarioList)+1)]
    all_nodenames = cb_data["etree"].All_Nonleaf_Nodenames()

    PHoptions = dict()
    if cb_data["convex_relaxation"]:
        PHoptions["solvername"] = solvername
        if "gurobi" in PHoptions["solvername"]:
            PHoptions["iter0_solver_options"] = {"BarHomogeneous": 1}
            PHoptions["iterk_solver_options"] = {"BarHomogeneous": 1}
        else:
            PHoptions["iter0_solver_options"] = None
            PHoptions["iterk_solver_options"] = None
    else:
        PHoptions["solvername"] = solvername  # needs to be ipopt
        PHoptions["iter0_solver_options"] = None
        PHoptions["iterk_solver_options"] = None
    PHoptions["PHIterLimit"] = PHIterLimit
    PHoptions["defaultPHrho"] = 1
    PHoptions["convthresh"] = 0.001
    PHoptions["subsolvedirectives"] = None
    PHoptions["verbose"] = False
    PHoptions["display_timing"] = False
    PHoptions["display_progress"] = True
    PHoptions["iter0_solver_options"] = None
    PHoptions["iterk_solver_options"] = None
    PHoptions["branching_factors"] = branching_factors

    # try to do something interesting for bundles per rank
    if scenperbun > 0:
        nscen = branching_factors[0] * branching_factors[1]
        PHoptions["bundles_per_rank"] = int((nscen / n_proc) / scenperbun)
    if rank_global == 0:
        appfile = "acopf.app"
        if not os.path.isfile(appfile):
            with open(appfile, "w") as f:
                f.write("datetime, hostname, BF1, BF2, seed, solver, n_proc")
                f.write(", bunperank, PHIterLimit, convthresh")
                f.write(", PH_IB, PH_OB")
                f.write(", PH_lastiter, PH_wallclock, aph_frac_needed")
                f.write(", APH_IB, APH_OB")
                f.write(", APH_lastiter, APH_wallclock")
        if "bundles_per_rank" in PHoptions:
            nbunstr = str(PHoptions["bundles_per_rank"])
        else:
            nbunstr = "0"
        oline = "\n" + str(start_time) + "," + socket.gethostname()
        oline += "," + str(branching_factors[0]) + "," + str(
            branching_factors[1])
        oline += ", " + str(seed) + ", " + str(PHoptions["solvername"])
        oline += ", " + str(n_proc) + ", " + nbunstr
        oline += ", " + str(PHoptions["PHIterLimit"])
        oline += ", " + str(PHoptions["convthresh"])

        with open(appfile, "a") as f:
            f.write(oline)
        print(oline)

    # PH hub
    PHoptions["tee-rank0-solves"] = True
    hub_dict = {
        "hub_class": PHHub,
        "hub_kwargs": {
            "options": None
        },
        "opt_class": PH,
        "opt_kwargs": {
            "PHoptions": PHoptions,
            "all_scenario_names": all_scenario_names,
            "scenario_creator": pysp2_callback,
            'scenario_denouement': scenario_denouement,
            "cb_data": cb_data,
            "rho_setter": rho_setter.ph_rhosetter_callback,
            "PH_extensions": None,
            "all_nodenames": all_nodenames,
        }
    }

    xhat_options = PHoptions.copy()
    xhat_options['bundles_per_rank'] = 0  #  no bundles for xhat
    xhat_options["xhat_specific_options"] = {
        "xhat_solver_options": PHoptions["iterk_solver_options"],
        "xhat_scenario_dict": xhat_dict,
        "csvname": "specific.csv"
    }

    ub2 = {
        'spoke_class': XhatSpecificInnerBound,
        "spoke_kwargs": dict(),
        "opt_class": PHBase,
        'opt_kwargs': {
            'PHoptions': xhat_options,
            'all_scenario_names': all_scenario_names,
            'scenario_creator': pysp2_callback,
            'scenario_denouement': scenario_denouement,
            "cb_data": cb_data,
            'all_nodenames': all_nodenames
        },
    }

    list_of_spoke_dict = [ub2]
    spcomm, opt_dict = spin_the_wheel(hub_dict, list_of_spoke_dict)
    if "hub_class" in opt_dict:  # we are hub rank
        if spcomm.opt.rank == spcomm.opt.rank0:  # we are the reporting hub rank
            ph_end_time = dt.datetime.now()
            IB = spcomm.BestInnerBound
            OB = spcomm.BestOuterBound
            print("BestInnerBound={} and BestOuterBound={}".\
                  format(IB, OB))
            with open(appfile, "a") as f:
                f.write(", " + str(IB) + ", " + str(OB) + ", " +
                        str(spcomm.opt._PHIter))
                f.write(", " + str((ph_end_time - start_time).total_seconds()))
Esempio n. 10
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))
    
    cb_data = {"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,
                              cb_data=cb_data,
                              ph_extensions=MultiPHExtension,
                              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"]["PH_extension_kwargs"] = {"ext_classes" : ext_classes}
    if with_cross_scenario_cuts:
        hub_dict["opt_kwargs"]["PHoptions"]["cross_scen_options"]\
            = {"check_bound_improve_iterations" : args.cross_scenario_iter_cnt}

    if with_fixer:
        hub_dict["opt_kwargs"]["PHoptions"]["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"]["PHoptions"]["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"]["PHoptions"]["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.PHoptions["defaultPHrho"] = 1  
    ### end ph spoke ###
    
    # FWPH spoke
    if with_fwph:
        fw_spoke = vanilla.fwph_spoke(*beans, cb_data=cb_data)

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

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

    # xhat shuffle bound spoke
    if with_xhatshuffle:
        xhatshuffle_spoke = vanilla.xhatshuffle_spoke(*beans, cb_data=cb_data)
       
    # cross scenario cut spoke
    if with_cross_scenario_cuts:
        cross_scenario_cut_spoke = vanilla.cross_scenario_cut_spoke(*beans, cb_data=cb_data)

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

    spcomm, opt_dict = spin_the_wheel(hub_dict, list_of_spoke_dict)

    if args.solution_dir is not None:
        uc.write_solution(spcomm, opt_dict, args.solution_dir)
Esempio n. 11
0
def main():
    args = _parse_args()

    num_scen = args.num_scens
    crops_mult = args.crops_mult
    with_fwph = args.with_fwph
    with_xhatlshaped = args.with_xhatlshaped

    scenario_creator = farmer.scenario_creator
    scenario_denouement = farmer.scenario_denouement
    all_scenario_names = [f"scen{sn}" for sn in range(num_scen)]
    cb_data = {"use_integer": False, "CropsMult": crops_mult}

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

    # Options for the L-shaped method at the hub
    # Bounds only valid for 3 scenarios, I think? Need to ask Chris
    spo = None if args.max_solver_threads is None else {
        "threads": args.max_solver_threads
    }
    options = {
        "master_solver": args.solver_name,
        "sp_solver": args.solver_name,
        "sp_solver_options": spo,
        #"valid_eta_lb": {i: -432000 for i in all_scenario_names},
        "max_iter": args.max_iterations,
        "verbose": False,
        "master_scenarios": [all_scenario_names[len(all_scenario_names) // 2]]
    }

    # L-shaped hub
    hub_dict = {
        "hub_class": LShapedHub,
        "hub_kwargs": {
            "options": {
                "rel_gap": args.rel_gap,
                "abs_gap": args.abs_gap,
            },
        },
        "opt_class": LShapedMethod,
        "opt_kwargs": { # Args passed to LShapedMethod __init__
            "options": options,
            "all_scenario_names": all_scenario_names,
            "scenario_creator": scenario_creator,
            "cb_data": cb_data,
        },
    }

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

    # xhat looper bound spoke -- any scenario will do for
    # lshaped (they're all the same)
    xhat_scenario_dict = {"ROOT": all_scenario_names[0]}

    if with_xhatlshaped:
        xhatlshaped_spoke = vanilla.xhatlshaped_spoke(*beans, cb_data=cb_data)

    list_of_spoke_dict = list()
    if with_fwph:
        list_of_spoke_dict.append(fw_spoke)
    if with_xhatlshaped:
        list_of_spoke_dict.append(xhatlshaped_spoke)

    spin_the_wheel(hub_dict, list_of_spoke_dict)
Esempio n. 12
0
def main():
    args = _parse_args()

    num_scen = args.num_scens
    with_fwph = args.with_fwph
    with_xhatlshaped = args.with_xhatlshaped

    scenario_creator = uc.scenario_creator
    scenario_denouement = uc.scenario_denouement
    scenario_creator_kwargs = {"path": f"./{num_scen}scenarios_r1/"}
    all_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)

    # Options for the L-shaped method at the hub
    spo = None if args.max_solver_threads is None else {
        "threads": args.max_solver_threads
    }
    spo['mipgap'] = 0.005
    options = {
        "master_solver": args.solver_name,
        "sp_solver": args.solver_name,
        "sp_solver_options": spo,
        "master_solver_options": spo,
        #"valid_eta_lb": {n:0. for n in all_scenario_names},
        "max_iter": args.max_iterations,
        "verbose": False,
        "master_scenarios": [all_scenario_names[len(all_scenario_names) // 2]],
    }

    # L-shaped hub
    hub_dict = {
        "hub_class": LShapedHub,
        "hub_kwargs": {
            "options": {
                "rel_gap": args.rel_gap,
                "abs_gap": args.abs_gap,
            },
        },
        "opt_class": LShapedMethod,
        "opt_kwargs": { # Args passed to LShapedMethod __init__
            "options": options,
            "all_scenario_names": all_scenario_names,
            "scenario_creator": scenario_creator,
            "scenario_creator_kwargs": scenario_creator_kwargs,
        },
    }

    # FWPH spoke
    if with_fwph:
        fw_spoke = vanilla.fwph_spoke(
            *beans, scenario_creator_kwargs=scenario_creator_kwargs)
        fw_spoke["opt_kwargs"]["PH_options"]["abs_gap"] = 0.
        fw_spoke["opt_kwargs"]["PH_options"]["rel_gap"] = 1e-5
        fw_spoke["opt_kwargs"]["rho_setter"] = uc.scenario_rhos

    if with_xhatlshaped:
        xhatlshaped_spoke = vanilla.xhatlshaped_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_xhatlshaped:
        list_of_spoke_dict.append(xhatlshaped_spoke)

    spin_the_wheel(hub_dict, list_of_spoke_dict)
Esempio n. 13
0
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)
    scenario_creator_kwargs = {"BFs": BFs}
    all_scenario_names = [f"scen{i}"
                          for i in range(ScenCount)]  #Scens are 0-based
    # print(all_scenario_names)
    scenario_creator = aircond_submodels.scenario_creator
    scenario_denouement = aircond_submodels.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)

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

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

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

    if "hub_class" in opt_dict:  # we are a hub rank
        if spcomm.opt.cylinder_rank == 0:  # we are the reporting hub rank
            print("BestInnerBound={} and BestOuterBound={}".\
                  format(spcomm.BestInnerBound, spcomm.BestOuterBound))

    if write_solution:
        sputils.write_spin_the_wheel_first_stage_solution(
            spcomm, opt_dict, 'aircond_first_stage.csv')
        sputils.write_spin_the_wheel_tree_solution(spcomm, opt_dict,
                                                   'aircond_full_solution')
Esempio n. 14
0
def main():

    args = _parse_args()

    BFs = [int(bf) for bf in args.BFs.split(',')]
    if len(BFs) != 2:
        raise RuntimeError("Hydro is a three stage problem, so it needs 2 BFs")

    with_xhatspecific = args.with_xhatspecific
    with_lagrangian = args.with_lagrangian

    # 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.
    for b in range(BFs[0]):
        all_nodenames.append("ROOT_" + str(b))

    ScenCount = BFs[0] * BFs[1]
    cb_data = 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,
                              cb_data=cb_data,
                              ph_extensions=None,
                              rho_setter=rho_setter)
    hub_dict["opt_kwargs"]["all_nodenames"] = all_nodenames
    hub_dict["opt_kwargs"]["PHoptions"]["branching_factors"] = BFs

    # Standard Lagrangian bound spoke
    if with_lagrangian:
        lagrangian_spoke = vanilla.lagrangian_spoke(*beans,
                                                    cb_data=cb_data,
                                                    rho_setter=rho_setter)
        lagrangian_spoke["opt_kwargs"]["all_nodenames"] = all_nodenames
        lagrangian_spoke["opt_kwargs"]["PHoptions"]["branching_factors"] = BFs

    # xhat looper bound spoke
    xhat_scenario_dict = {
        "ROOT": "Scen1",
        "ROOT_0": "Scen1",
        "ROOT_1": "Scen4",
        "ROOT_2": "Scen7"
    }

    if with_xhatspecific:
        xhatspecific_spoke = vanilla.xhatspecific_spoke(*beans,
                                                        xhat_scenario_dict,
                                                        all_nodenames,
                                                        BFs,
                                                        cb_data=cb_data)

    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)

    spcomm, opt_dict = spin_the_wheel(hub_dict, list_of_spoke_dict)

    if "hub_class" in opt_dict:  # we are a hub rank
        if spcomm.opt.rank == spcomm.opt.rank0:  # we are the reporting hub rank
            print("BestInnerBound={} and BestOuterBound={}".\
                  format(spcomm.BestInnerBound, spcomm.BestOuterBound))
Esempio n. 15
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 there must be --default-rho")

    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)

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

    ## hack in adaptive rho
    if args.use_norm_rho_updater:
        hub_dict['opt_kwargs']['PH_extensions'] = NormRhoUpdater
        hub_dict['opt_kwargs']['PHoptions']['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)

    mpisppy.cylinders.SPOKE_SLEEP_TIME = 0.1

    spin_the_wheel(hub_dict, list_of_spoke_dict)
Esempio n. 16
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")

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

    mpisppy.cylinders.SPOKE_SLEEP_TIME = 0.1

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

    if write_solution:
        sputils.write_spin_the_wheel_first_stage_solution(
            spcomm, opt_dict, 'farmer_plant.csv')
        sputils.write_spin_the_wheel_first_stage_solution(spcomm,
                                                          opt_dict,
                                                          'farmer_cyl_nonants.spy',
                                                          first_stage_solution_writer=\
                                                          sputils.first_stage_nonant_npy_serializer)
        sputils.write_spin_the_wheel_tree_solution(spcomm, opt_dict,
                                                   'farmer_full_solution')