def scenario_creator(scenario_name, scenario_count=None): """ The callback needs to create an instance and then attach the PySP nodes to it in a list _mpisppy_node_list ordered by stages. Optionally attach _PHrho. """ if scenario_count is None: raise ValueError( "Sizes scenario_creator requires a scenario_count kwarg") if scenario_count not in [3, 10]: raise ValueError("Sizes scenario count must equal either 3 or 10") sizes_dir = os.path.dirname(__file__) datadir = os.sep.join((sizes_dir, f"SIZES{scenario_count}")) try: fname = datadir + os.sep + scenario_name + ".dat" except: print("FAIL: datadir=", datadir, " scenario_name=", scenario_name) model = ref.model.create_instance(fname) # now attach the one and only tree node varlist = [model.NumProducedFirstStage, model.NumUnitsCutFirstStage] sputils.attach_root_node(model, model.FirstStageCost, varlist) return model
def pysp2_callback(scenario_name, node_names=None, cb_data=None): ''' The callback needs to create an instance and then attach the PySP nodes to it in a list _PySPnode_list ordered by stages. Optionally attach _PHrho. Standard (1.0) PySP signature for now... ''' instance = pysp_instance_creation_callback(scenario_name, node_names, cb_data) # now attach the one and only tree node (ROOT is a reserved word) # UnitOn[*,*] is the only set of nonant variables """ instance._PySPnode_list = [scenario_tree.ScenarioNode("ROOT", 1.0, 1, instance.StageCost["Stage_1"], #"Stage_1" hardcodes the commitments in all time periods None, [instance.UnitOn], instance, [instance.UnitStart, instance.UnitStop, instance.StartupIndicator], )] """ sputils.attach_root_node(instance, instance.StageCost["Stage_1"], [instance.UnitOn], nonant_ef_suppl_list=[ instance.UnitStart, instance.UnitStop, instance.StartupIndicator ]) return instance
def scenario_creator(scenario_name, node_names=None, cb_data=None): """ The callback needs to create an instance and then attach the PySP nodes to it in a list _PySPnode_list ordered by stages. Optionally attach _PHrho. Use cb_data for the scenario count (3 or 10) """ if cb_data not in [3, 10]: raise RuntimeError( "cb_data passed to scenario counter " "must equal either 3 or 10" ) sizes_dir = os.path.dirname(mpisppy.examples.sizes.sizes.__file__) datadir = os.sep.join((sizes_dir, f"SIZES{cb_data}")) try: fname = datadir + os.sep + scenario_name + ".dat" except: print("FAIL: datadir=", datadir, " scenario_name=", scenario_name) model = ref.model.create_instance(fname) # now attach the one and only tree node varlist = [model.NumProducedFirstStage, model.NumUnitsCutFirstStage] sputils.attach_root_node(model, model.FirstStageCost, varlist) return model
def scenario_creator(self, scenario_name): if self.demand_provider_kwargs: prov = self.demand_provider(**self.demand_provider_kwargs, seed=int(scenario_name)) else: prov = self.demand_provider(seed=int(scenario_name)) prov.reset() demand = { t: prov.generate_demand() for t in range(1, self.model_constructor_params["t_max"] + 1) } model = self.model_constructor( demand=demand, **self.model_constructor_params).build_model() # Telling it which decisions belong to first stage - for us this could be all our policy parameters # because we can't change them during a trajectory first_stage_params = self._get_first_stage_decision_params(model) sputils.attach_root_node(model, 0, first_stage_params) # If we don't specify, assume that all equally likely model._mpisppy_probability = 1.0 / self.n_scenarios return model
def scenario_creator(scenario_name, node_names=None, cb_data=None): if (cb_data is None): raise RuntimeError('Must provide the name of the .dat file ' 'containing the instance data via the ' 'cb_data argument to scenario_creator') scenario_ix = _get_scenario_ix(scenario_name) model = build_scenario_model(cb_data, scenario_ix) # now attach the one and only scenario tree node sputils.attach_root_node(model, model.FirstStageCost, [model.x]) return model
def scenario_creator(scenario_name, scenario_count=None): if scenario_count not in (3, 10): raise RuntimeError( "scenario_count passed to scenario counter must equal either 3 or 10" ) sizes_dir = os.path.dirname(__file__) datadir = os.sep.join((sizes_dir, f"SIZES{scenario_count}")) try: fname = datadir + os.sep + scenario_name + ".dat" except: print("FAIL: datadir=", datadir, " scenario_name=", scenario_name) model = ref.model.create_instance(fname) # now attach the one and only tree node varlist = [model.NumProducedFirstStage, model.NumUnitsCutFirstStage] sputils.attach_root_node(model, model.FirstStageCost, varlist) return model
def pysp2_callback(scenario_name, scenario_count=None, path=None, num_scens=None, seedoffset=0): ''' The callback needs to create an instance and then attach the nodes to it in a list _mpisppy_node_list ordered by stages. Optionally attach _PHrho. ''' instance = pysp_instance_creation_callback( scenario_name, scenario_count=scenario_count, path=path, ) #Add the probability of the scenario if num_scens is not None: instance._mpisppy_probability = 1 / num_scens # now attach the one and only tree node (ROOT is a reserved word) # UnitOn[*,*] is the only set of nonant variables """ instance._mpisppy_node_list = [scenario_tree.ScenarioNode("ROOT", 1.0, 1, instance.StageCost["Stage_1"], #"Stage_1" hardcodes the commitments in all time periods None, [instance.UnitOn], instance, [instance.UnitStart, instance.UnitStop, instance.StartupIndicator], )] """ sputils.attach_root_node(instance, instance.StageCost["Stage_1"], [instance.UnitOn], nonant_ef_suppl_list=[ instance.UnitStart, instance.UnitStop, instance.StartupIndicator ]) return instance