def dict_sampler2PySP(distrdict, \ TreeTemplateFileName, NumScen, OutDir, Seed = None, ScenTemplateFileName = None): """ Once you have a distrdict that has the usual d2p dictionary indexes and values that are inputs needed to create scipy distribution objects (see class IndepScipys) that are used to draw SumScen samples (independent across params) and uses the TreeTemplateFilame to create a ScenarioStructure.dat for PySP, which is written to OutDir. A random number seed and the scenario template is optional. """ dict_distr = IndepScipys(distrdict) treetemp = bc.PySP_Tree_Template() treetemp.read_AMPL_template(TreeTemplateFileName) if ScenTemplateFileName is not None: scentemp = bc.PySP_Scenario_Template() scentemp.read_JSON_template(ScenTemplateFileName) else: scentemp = None # we know we are using scipy distrubutions, so reseed accordingly if Seed is not None: np.random.seed(Seed) pyspscenlist = [] rawnodelist = [] # all nodes created ### we should copy distrdict so it doesn't get clobbered localdict = copy.deepcopy(distrdict) for s in range(NumScen): # distrdict has the correct indexes, of course. # So now it will get the sample values, but # they will be overwritten next time through the loop # so all processing needs to be done in the loop. # Note: dict_distr has the distributions. dict_distr.draw_sample_into(localdict) rawnode = bc.Raw_Node_Data(filespec=None, dictin=localdict, name="rs" + str(s + 1), parentname='ROOT', prob=1. / float(NumScen)) # it would not be hard to go multistage, given branching factors nodelistforscen = [] nodelistforscen.append(rawnode) # single for two-stage problem PySPScen = bc.PySP_Scenario(bc.Raw_Scenario_Data(nodelistforscen), \ scentemp) pyspscenlist.append(PySPScen) rawnodelist.append(rawnode) # needed for the tree # To use an standard (AMPL format) ScenarioStructure.dat the # name has to be the PySPScen name because the tree constructor # expects that. fname = os.path.join(OutDir, PySPScen.name + ".json") with open(fname, "w") as f: json.dump(rawnode.valdict, f) # we have everything we need tree = bc.PySP_Tree(treetemp, pyspscenlist, rawnodelist) tree.Write_ScenarioStructure_dat_file( os.path.join(OutDir, 'ScenarioStructure.dat'))
def dict_representative_points_scenarios(distrdict, TreeTemplateFileName, CutPointFileName, NumScen, OutDir, Seed=None, ScenTemplateFileName=None, Abstract=False): """ Once you have a distrdict that has the usual d2p dictionary indexes and values that are inputs needed to create scipy distribution objects (see class IndepScipys) that are used to draw NumScen samples (independent across params) and uses the TreeTemplateFilename to create a ScenarioStructure.dat for PySP, which is written to OutDir. A random number seed and the scenario template is optional. """ dict_distr = IndepScipys(distrdict) treetemp = bc.PySP_Tree_Template() treetemp.read_AMPL_template(TreeTemplateFileName) cutpoint_sets = cutpoint_set.parse_cutpoint_file(CutPointFileName) if ScenTemplateFileName is not None: scentemp = bc.PySP_Scenario_Template() scentemp.read_AMPL_template_with_tokens(ScenTemplateFileName) else: scentemp = None # we know we are using scipy distributions, so reseed accordingly if Seed is not None: np.random.seed(Seed) pyspscenlist = [] rawnodelist = [] # all nodes created ### we should copy distrdict so it doesn't get clobbered localdict = copy.deepcopy(distrdict) rectangledict = copy.deepcopy(distrdict) interval_count = sum(len(cpt_set.intervals) for cpt_set in cutpoint_sets) for cpt_set in cutpoint_sets: for name in cpt_set.cutpoint_names: # distrdict has the correct indexes, of course. # So now it will get the sample values, but # they will be overwritten next time through the loop # so all processing needs to be done in the loop. # Note: dict_distr has the distributions. interval = cpt_set.get(name) for pname in rectangledict: if isinstance(rectangledict[pname], dict): for pindex in rectangledict[pname]: rectangledict[pname][pindex] = interval else: rectangledict[pname] = interval dict_distr.pick_representative_points(localdict, rectangledict) rawnode = bc.Raw_Node_Data(filespec=None, dictin=localdict, name=name, parentname='ROOT', prob=1. / interval_count) # it would not be hard to go multistage, given branching factors nodelistforscen = [] nodelistforscen.append(rawnode) # single for two-stage problem PySPScen = bc.PySP_Scenario(bc.Raw_Scenario_Data(nodelistforscen), scentemp) pyspscenlist.append(PySPScen) rawnodelist.append(rawnode) # needed for the tree # To use an standard (AMPL format) ScenarioStructure.dat the # name has to be the PySPScen name because the tree constructor # expects that. if not Abstract: fname = os.path.join(OutDir, PySPScen.name + ".json") with open(fname, "w") as f: json.dump(rawnode.valdict, f) else: fname = os.path.join(OutDir, PySPScen.name + ".dat") with open(fname, "wt") as f: for line in PySPScen.scenariodata: f.write(line) # we have everything we need tree = bc.PySP_Tree(treetemp, pyspscenlist, rawnodelist) tree.Write_ScenarioStructure_dat_file(os.path.join(OutDir, 'ScenarioStructure.dat'))