def make_ef(self, verbose=False, generate_weighted_cvar=False, cvar_weight=None, risk_alpha=None, cc_indicator_var_name=None, cc_alpha=0.0): """ Make an ef object (used by solve_ef); all Args are optional. Args: verbose (boolean): indicates verbosity to PySP for construction generate_weighted_cvar (boolean): indicates we want weighted CVar cvar_weight (float): weight for the cvar term risk_alpha (float): alpha value for cvar cc_indicator_var_name (string): name of the Var used for chance constraint cc_alpha (float): alpha for chance constraint Returns: ef_instance: the ef object """ return pyspef.create_ef_instance( self.scenario_tree, verbose_output=verbose, generate_weighted_cvar=generate_weighted_cvar, cvar_weight=cvar_weight, risk_alpha=risk_alpha, cc_indicator_var_name=cc_indicator_var_name, cc_alpha=cc_alpha)
def CreateExtensiveFormInstance(options, scenario_tree): start_time = time.time() print("Creating extensive form instance") # then validate the associated parameters. generate_weighted_cvar = False cvar_weight = None risk_alpha = None if options.generate_weighted_cvar is True: generate_weighted_cvar = True cvar_weight = options.cvar_weight risk_alpha = options.risk_alpha binding_instance = create_ef_instance(scenario_tree, verbose_output=options.verbose, generate_weighted_cvar=generate_weighted_cvar, cvar_weight=cvar_weight, risk_alpha=risk_alpha, cc_indicator_var_name=options.cc_indicator_var, cc_alpha=options.cc_alpha) if options.verbose or options.output_times: print("Time to construct extensive form instance=%.2f seconds" %(time.time() - start_time)) return binding_instance
def build_ef(self): self.destroy_ef() if self.get_option("verbose"): print("Creating extensive form instance") start_time = time.time() # then validate the associated parameters. generate_weighted_cvar = False cvar_weight = None risk_alpha = None if self.get_option("generate_weighted_cvar"): generate_weighted_cvar = True cvar_weight = self.get_option("cvar_weight") risk_alpha = self.get_option("risk_alpha") self.instance = create_ef_instance( self._manager.scenario_tree, verbose_output=self.get_option("verbose"), generate_weighted_cvar=generate_weighted_cvar, cvar_weight=cvar_weight, risk_alpha=risk_alpha, cc_indicator_var_name=self.get_option("cc_indicator_var"), cc_alpha=self.get_option("cc_alpha")) if self.get_option("verbose") or self.get_option("output_times"): print("Time to construct extensive form instance=%.2f seconds" %(time.time() - start_time))
def build_ef(self): self.destroy_ef() if self.get_option("verbose"): print("Creating extensive form instance") start_time = time.time() # then validate the associated parameters. generate_weighted_cvar = False cvar_weight = None risk_alpha = None if self.get_option("generate_weighted_cvar"): generate_weighted_cvar = True cvar_weight = self.get_option("cvar_weight") risk_alpha = self.get_option("risk_alpha") self.instance = create_ef_instance( self._manager.scenario_tree, verbose_output=self.get_option("verbose"), generate_weighted_cvar=generate_weighted_cvar, cvar_weight=cvar_weight, risk_alpha=risk_alpha, cc_indicator_var_name=self.get_option("cc_indicator_var"), cc_alpha=self.get_option("cc_alpha")) if self.get_option("verbose") or self.get_option("output_times"): print("Time to construct extensive form instance=%.2f seconds" % (time.time() - start_time))
def make_ef(self, verbose=False): """ Make an ef object (used by solve_ef) Args: verbose (boolean): indicates verbosity to PySP for construction Returns: ef_instance: the ef object """ return create_ef_instance(self.scenario_tree, verbose_output=verbose)
def make_ef(self, verbose=False): """ Make an ef object (used by solve_ef) Args: verbose (boolean): indicates verbosity to PySP for construction Returns: ef_instance: the ef object """ return pyspef.create_ef_instance(self.scenario_tree, verbose_output=verbose)
def solve_ef(self, subsolver, sopts=None): # Solve the stochastic program directly using the extensive form. # subsolver: the solver to call (e.g., 'ipopt') # sopts: dictionary of solver options # Returns the entire ef_instance populated with the solution. ef_instance = create_ef_instance(self.scenario_tree, verbose_output=True) solver = SolverFactory(subsolver) if sopts is not None: for key in sopts: solver.options[key] = sopts[key] solver.solve(ef_instance) # , tee = True) return ef_instance
def solve_ef(self, subsolver, sopts = None, tee = False): """ Solve the stochastic program directly using the extensive form. args: subsolver: the solver to call (e.g., 'ipopt') sopts: dictionary of solver options tee: the usual to indicate dynamic solver output to terminal. Update the scenario tree, populated with the solution. """ ef_instance = create_ef_instance(self.scenario_tree, verbose_output=True) solver = SolverFactory(subsolver) if sopts is not None: for key in sopts: solver.options[key] = sopts[key] solver.solve(ef_instance, tee = tee) self.scenario_tree.pullScenarioSolutionsFromInstances() self.scenario_tree.snapshotSolutionFromScenarios() # update nodes
def solve_ef(p_model, p_data, temoa_options=None): """ solve_ef(p_model, p_data) -> objective value of the extensive form Solves the model in stochastic mode. p_model -> string, the path to the model file (ReferenceModel.py). p_data -> string, the path to the directory of data for the stochastic mdoel, where ScenarioStructure.dat should resides. Returns a float point number of the value of objective function for the stochastic program model. """ options = ScenarioTreeManagerClientSerial.register_options() if os.path.basename(p_model) == 'ReferenceModel.py': options.model_location = os.path.dirname(p_model) else: sys.stderr.write( '\nModel file should be ReferenceModel.py. Exiting...\n') sys.exit(1) options.scenario_tree_location = p_data # using the 'with' block will automatically call # manager.close() and gracefully shutdown with ScenarioTreeManagerClientSerial(options) as manager: manager.initialize() ef_instance = create_ef_instance(manager.scenario_tree, verbose_output=options.verbose) ef_instance.dual = Suffix(direction=Suffix.IMPORT) with SolverFactory(temoa_options.solver) as opt: ef_result = opt.solve(ef_instance) # Write to database if hasattr(temoa_options, 'output'): sys.path.append(options.model_location) from pformat_results import pformat_results # from temoa_config import TemoaConfig # temoa_options = TemoaConfig() # temoa_options.config = temoa_options.config # temoa_options.keepPyomoLP = temoa_options.keepPyomoLP # temoa_options.saveTEXTFILE = temoa_options.saveTEXTFILE # temoa_options.path_to_db_io = temoa_options.path_to_db_io # temoa_options.saveEXCEL = temoa_options.saveEXCEL ef_result.solution.Status = 'feasible' # Assume it is feasible # Maybe there is a better solution using manager, but now it is a # kludge to use return_CP_and_path() function s2cd_dict, s2fp_dict = return_CP_and_path(p_data) stochastic_run = temoa_options.scenario # Name of stochastic run for s in manager.scenario_tree.scenarios: ins = s._instance temoa_options.scenario = '.'.join([stochastic_run, s.name]) temoa_options.dot_dat = list() for fname in s2fp_dict[s.name]: temoa_options.dot_dat.append( os.path.join(options.scenario_tree_location, fname)) # temoa_options.output = os.path.join( # options.scenario_tree_location, # stochastic_output # ) msg = '\nStoring results from scenario {} to database.\n'.format( s.name) sys.stderr.write(msg) formatted_results = pformat_results(ins, ef_result, temoa_options) ef_instance.solutions.store_to(ef_result) ef_obj = value(ef_instance.EF_EXPECTED_COST.values()[0]) return ef_obj
# print(options.about(name)) # To see a more compact display of options #options.display() options.model_location = \ os.path.join(farmer_example_dir, 'models') options.scenario_tree_location = \ os.path.join(farmer_example_dir, 'scenariodata') # using the 'with' block will automatically call # manager.close() and gracefully shutdown with ScenarioTreeManagerClientSerial(options) as manager: manager.initialize() ef_instance = create_ef_instance(manager.scenario_tree, verbose_output=options.verbose) ef_instance.dual = Suffix(direction=Suffix.IMPORT) with SolverFactory('cplex') as opt: opt.solve(ef_instance) # # Print duals of non-anticipaticity constraints # master_constraint_map = ef_instance.MASTER_CONSTRAINT_MAP print("%50s %20s" % ("Variable", "Dual")) for scenario in manager.scenario_tree.scenarios: instance = scenario._instance for i in instance.DevotedAcreage:
def solve_ef(p_model, p_data, dummy_temoa_options=None): """ solve_ef(p_model, p_data) -> objective value of the extensive form Solves the model in stochastic mode. p_model -> string, the path to the model file (ReferenceModel.py). p_data -> string, the path to the directory of data for the stochastic mdoel, where ScenarioStructure.dat should resides. Returns a float point number of the value of objective function for the stochastic program model. """ options = ScenarioTreeManagerClientSerial.register_options() if os.path.basename(p_model) == 'ReferenceModel.py': options.model_location = os.path.dirname(p_model) else: sys.stderr.write( '\nModel file should be ReferenceModel.py. Exiting...\n') sys.exit(1) options.scenario_tree_location = p_data # using the 'with' block will automatically call # manager.close() and gracefully shutdown with ScenarioTreeManagerClientSerial(options) as manager: manager.initialize() ef_instance = create_ef_instance(manager.scenario_tree, verbose_output=options.verbose) ef_instance.dual = Suffix(direction=Suffix.IMPORT) with SolverFactory('cplex') as opt: ef_result = opt.solve(ef_instance) # Write to database if dummy_temoa_options: sys.path.append(options.model_location) from pformat_results import pformat_results from temoa_config import TemoaConfig temoa_options = TemoaConfig() temoa_options.config = dummy_temoa_options.config temoa_options.keepPyomoLP = dummy_temoa_options.keepPyomoLP temoa_options.saveTEXTFILE = dummy_temoa_options.saveTEXTFILE temoa_options.path_to_db_io = dummy_temoa_options.path_to_db_io temoa_options.saveEXCEL = dummy_temoa_options.saveEXCEL ef_result.solution.Status = 'feasible' # Assume it is feasible for s in manager.scenario_tree.scenarios: ins = s._instance temoa_options.scenario = s.name temoa_options.dot_dat = [ os.path.join(options.scenario_tree_location, s.name + '.dat') ] temoa_options.output = os.path.join( options.scenario_tree_location, dummy_temoa_options.output) msg = '\nStoring results from scenario {} to database.\n'.format( s.name) sys.stderr.write(msg) formatted_results = pformat_results(ins, ef_result, temoa_options) ef_instance.solutions.store_to(ef_result) ef_obj = value(ef_instance.EF_EXPECTED_COST.values()[0]) return ef_obj
def create_problem_with_scenarios(power_system, times): logging.debug('constructing scenario tree') scenario_tree = ScenarioTree( scenarioinstance=power_system._model, scenariotreeinstance=power_system._scenario_tree_instance) if not scenario_tree.validate(): for s, scenario in enumerate(scenario_tree._scenarios): print s, scenario raise ValueError('not a valid scenario tree') gen = power_system.get_generator_with_scenarios() scenario_instances = {} # construct scenario instances logging.debug('constructing scenario instances') gc.disable() for s, scenario in enumerate(scenario_tree._scenarios): scenario_instance = power_system._model.clone() power = getattr(scenario_instance, 'power_{}'.format(str(gen))) # set the values of the parameter for this scenario logging.debug('setting scenario values for s%i' % s) scenario_vals = gen._get_scenario_values(times, s=s) for t, time in enumerate(times): power[time] = scenario_vals[t] # power.pprint() scenario_instance.preprocess() scenario_instances[scenario._name] = scenario_instance gc.enable() scenario_tree.defineVariableIndexSets(power_system._model) cvar_params = {} if user_config.cvar_weight > 0: cvar_params = dict( generate_weighted_cvar=True, cvar_weight=user_config.cvar_weight, risk_alpha=user_config.cvar_confidence_level, ) full_problem_instance = create_ef_instance(scenario_tree, scenario_instances, **cvar_params) # full_problem_instance.pprint() # relax the non-anticipatory constraints on the generator status variables # beyond the UC time horizon if len(times.post_horizon()) > 0: logging.debug( 'removing non-anticipatory constraints for post-commitment horizon' ) for time in times.post_horizon(): for scenario in scenario_instances.keys(): for gen in power_system.generators(): if not gen.is_controllable: continue u = gen.status().name delattr(full_problem_instance, '{s}_{u}_{t}'.format(s=scenario, u=u, t=str(time))) power_system.stochastic_formulation = True power_system._stochastic_instance = full_problem_instance power_system._scenario_tree = scenario_tree power_system._scenario_instances = scenario_instances return
thisdir = os.path.dirname(os.path.abspath(__file__)) farmer_example_dir = os.path.join(os.path.dirname(thisdir), 'farmer') options = ScenarioTreeManagerClientSerial.register_options() options.model_location = \ os.path.join(farmer_example_dir, 'models') options.scenario_tree_location = \ os.path.join(farmer_example_dir, 'scenariodata') # using the 'with' block will automatically call # manager.close() and gracefully shutdown with ScenarioTreeManagerClientSerial(options) as manager: manager.initialize() ef_instance = create_ef_instance(manager.scenario_tree, verbose_output=options.verbose) ef_instance.dual = Suffix(direction=Suffix.IMPORT) with SolverFactory('cplex') as opt: opt.solve(ef_instance) # # Print duals of non-anticipaticity constraints # master_constraint_map = ef_instance.MASTER_CONSTRAINT_MAP print("%50s %20s" % ("Variable", "Dual")) for scenario in manager.scenario_tree.scenarios: instance = scenario._instance for i in instance.DevotedAcreage:
ScenarioTreeInstanceFactory from gas_network_model import (pysp_instance_creation_callback, nx_scenario_tree) from pyomo.pysp.ef import create_ef_instance # define and initialize the SP instance_factory = ScenarioTreeInstanceFactory(pysp_instance_creation_callback, nx_scenario_tree) options = ScenarioTreeManagerFactory.register_options() options.scenario_tree_manager = 'serial' sp = ScenarioTreeManagerFactory(options, factory=instance_factory) sp.initialize() instance = create_ef_instance(sp.scenario_tree) #instance = create_model(1.0) print("\nHi this is PyNumero") nlp = PyomoNLP(instance) print("\n----------------------") print("Problem statistics:") print("----------------------") print("Number of variables: {:>25d}".format(nlp.nx)) print("Number of equality constraints: {:>14d}".format(nlp.nc)) print("Number of inequality constraints: {:>11d}".format(nlp.nd)) print("Total number of constraints: {:>17d}".format(nlp.ng)) print("Number of nnz in Jacobian: {:>20d}".format(nlp.nnz_jacobian_g)) print("Number of nnz in hessian of Lagrange: {:>8d}".format( nlp.nnz_hessian_lag))
def create_problem_with_scenarios(power_system, times): logging.debug('constructing scenario tree') scenario_tree = ScenarioTree( scenarioinstance=power_system._model, scenariotreeinstance=power_system._scenario_tree_instance) if not scenario_tree.validate(): for s, scenario in enumerate(scenario_tree._scenarios): print s, scenario raise ValueError('not a valid scenario tree') gen = power_system.get_generator_with_scenarios() scenario_instances = {} # construct scenario instances logging.debug('constructing scenario instances') gc.disable() for s, scenario in enumerate(scenario_tree._scenarios): scenario_instance = power_system._model.clone() power = getattr(scenario_instance, 'power_{}'.format(str(gen))) # set the values of the parameter for this scenario logging.debug('setting scenario values for s%i' % s) scenario_vals = gen._get_scenario_values(times, s=s) for t, time in enumerate(times): power[time] = scenario_vals[t] # power.pprint() scenario_instance.preprocess() scenario_instances[scenario._name] = scenario_instance gc.enable() scenario_tree.defineVariableIndexSets(power_system._model) cvar_params = {} if user_config.cvar_weight > 0: cvar_params = dict( generate_weighted_cvar=True, cvar_weight=user_config.cvar_weight, risk_alpha=user_config.cvar_confidence_level, ) full_problem_instance = create_ef_instance( scenario_tree, scenario_instances, **cvar_params) # full_problem_instance.pprint() # relax the non-anticipatory constraints on the generator status variables # beyond the UC time horizon if len(times.post_horizon()) > 0: logging.debug('removing non-anticipatory constraints for post-commitment horizon') for time in times.post_horizon(): for scenario in scenario_instances.keys(): for gen in power_system.generators(): if not gen.is_controllable: continue u = gen.status().name delattr(full_problem_instance, '{s}_{u}_{t}'.format(s=scenario, u=u, t=str(time))) power_system.stochastic_formulation = True power_system._stochastic_instance = full_problem_instance power_system._scenario_tree = scenario_tree power_system._scenario_instances = scenario_instances return
from gas_network_model import (pysp_instance_creation_callback, nx_scenario_tree) from pyomo.pysp.ef import create_ef_instance # define and initialize the SP instance_factory = ScenarioTreeInstanceFactory( pysp_instance_creation_callback, nx_scenario_tree) options = ScenarioTreeManagerFactory.register_options() options.scenario_tree_manager = 'serial' sp = ScenarioTreeManagerFactory(options, factory=instance_factory) sp.initialize() instance = create_ef_instance(sp.scenario_tree) #instance = create_model(1.0) print("\nHi this is PyNumero") nlp = PyomoNLP(instance) print("\n----------------------") print("Problem statistics:") print("----------------------") print("Number of variables: {:>25d}".format(nlp.nx)) print("Number of equality constraints: {:>14d}".format(nlp.nc)) print("Number of inequality constraints: {:>11d}".format(nlp.nd)) print("Total number of constraints: {:>17d}".format(nlp.ng)) print("Number of nnz in Jacobian: {:>20d}".format(nlp.nnz_jacobian_g)) print("Number of nnz in hessian of Lagrange: {:>8d}".format(nlp.nnz_hessian_lag)) x = nlp.x_init()