def _init(self, factory): options = ScenarioTreeManagerClientSerial.register_options() sp = ScenarioTreeManagerClientSerial( options, factory=factory) sp.initialize() return sp
def compile_scenario_tree_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_unique_option( options, "output_directory", PySPConfigValue( ".", domain=_domain_must_be_str, description=("The directory in which to store all output files. " "Default is '.'."), doc=None, visibility=0)) safe_register_unique_option( options, "compiled_reference_model_filename", PySPConfigValue( "PySP_CompiledReferenceModel.py", domain=_domain_must_be_str, description=( "The filename to use for the new reference model that uses " "the compiled scenarios. This will be prefixed by the " "output directory name where compiled scenarios are stored. " "Default is 'PySP_CompiledReferenceModel.py'."), doc=None, visibility=0)) safe_register_common_option(options, "scenario_tree_manager") ScenarioTreeManagerClientSerial.register_options(options) ScenarioTreeManagerClientPyro.register_options(options) return options
def convertschuripopt_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "verbose") safe_register_common_option(options, "symbolic_solver_labels") safe_register_unique_option( options, "output_directory", PySPConfigValue( ".", domain=_domain_must_be_str, description=("The directory in which all SchurIpopt files " "will be stored. Default is '.'."), doc=None, visibility=0)) safe_register_unique_option( options, "ignore_bundles", PySPConfigValue( False, domain=bool, description=("Ignore bundles when converting the SP to " "SchurIpopt input files."), doc=None, visibility=0)) safe_register_common_option(options, "scenario_tree_manager") ScenarioTreeManagerClientSerial.register_options(options) ScenarioTreeManagerClientPyro.register_options(options) return options
def _init(self, factory): options = ScenarioTreeManagerClientSerial.register_options() sp = ScenarioTreeManagerClientSerial( options, factory=factory) sp.initialize() return sp
def run_generate_distributed_NL_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "scenario_tree_manager") safe_register_common_option(options, "symbolic_solver_labels") safe_register_unique_option( options, "output_directory", PySPConfigValue( ".", domain=_domain_must_be_str, description=( "The directory in which to store all output files. " "Default is '.'." ), doc=None, visibility=0)) safe_register_unique_option( options, "linking_suffix_name", PySPConfigValue( "variable_id", domain=_domain_must_be_str, description=( "The suffix name used to identify common variables " "across NL files. Default is 'ipopt_blend_id'." ), doc=None, visibility=0)) safe_register_unique_option( options, "objective_suffix_name", PySPConfigValue( "objective_weight", domain=_domain_must_be_str, description=( "The suffix name used to identify the relative " "objective weight for each NL-file subproblem." "Default is 'ipopt_blend_weight'." ), doc=None, visibility=0)) ScenarioTreeManagerClientSerial.register_options(options) ScenarioTreeManagerClientPyro.register_options(options) return options
def generate_sample_sp(self, size, **kwds): assert size > 0 def model_callback(scenario_name, node_list): m = self.sample(return_copy=True) return m scenario_tree_model = self._create_scenario_tree_model(size) factory = ScenarioTreeInstanceFactory( model=model_callback, scenario_tree=scenario_tree_model) options = \ ScenarioTreeManagerClientSerial.register_options() for key in kwds: options[key] = kwds[key] manager = ScenarioTreeManagerClientSerial(options, factory=factory) manager.initialize() manager.reference_model = self.reference_model.clone() return manager
def compile_scenario_tree_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_unique_option( options, "output_directory", PySPConfigValue( ".", domain=_domain_must_be_str, description=( "The directory in which to store all output files. " "Default is '.'." ), doc=None, visibility=0)) safe_register_unique_option( options, "compiled_reference_model_filename", PySPConfigValue( "PySP_CompiledReferenceModel.py", domain=_domain_must_be_str, description=( "The filename to use for the new reference model that uses " "the compiled scenarios. This will be prefixed by the " "output directory name where compiled scenarios are stored. " "Default is 'PySP_CompiledReferenceModel.py'." ), doc=None, visibility=0)) safe_register_common_option(options, "scenario_tree_manager") ScenarioTreeManagerClientSerial.register_options(options) ScenarioTreeManagerClientPyro.register_options(options) return options
def convertschuripopt_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "verbose") safe_register_common_option(options, "symbolic_solver_labels") safe_register_unique_option( options, "output_directory", PySPConfigValue( ".", domain=_domain_must_be_str, description=( "The directory in which all SchurIpopt files " "will be stored. Default is '.'." ), doc=None, visibility=0)) safe_register_unique_option( options, "ignore_bundles", PySPConfigValue( False, domain=bool, description=( "Ignore bundles when converting the SP to " "SchurIpopt input files." ), doc=None, visibility=0)) safe_register_common_option(options, "scenario_tree_manager") ScenarioTreeManagerClientSerial.register_options(options) ScenarioTreeManagerClientPyro.register_options(options) return options
def generate_sample_sp(self, size, **kwds): assert size > 0 def model_callback(scenario_name, node_list): m = self.sample(return_copy=True) return m scenario_tree_model = self._create_scenario_tree_model(size) factory = ScenarioTreeInstanceFactory( model=model_callback, scenario_tree=scenario_tree_model) options = \ ScenarioTreeManagerClientSerial.register_options() for key in kwds: options[key] = kwds[key] manager = ScenarioTreeManagerClientSerial(options, factory=factory) manager.initialize() manager.reference_model = self.reference_model.clone() return manager
def pysp2smps_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "verbose") safe_register_common_option(options, "symbolic_solver_labels") safe_register_common_option(options, "file_determinism") safe_register_unique_option( options, "explicit", PySPConfigValue( False, domain=bool, description=( "Generate SMPS files using explicit scenarios " "(or bundles). ** This option is deprecated. It is " "the default behavior. ** " ), doc=None, visibility=0)) safe_register_unique_option( options, "core_format", PySPConfigValue( "mps", domain=_domain_must_be_str, description=( "The format used to generate the core SMPS problem file. " "Choices are: [mps, lp]. The default format is MPS." ), doc=None, visibility=0), ap_kwds={'choices': ['mps','lp']}) safe_register_unique_option( options, "output_directory", PySPConfigValue( ".", domain=_domain_must_be_str, description=( "The directory in which all SMPS related output files " "will be stored. Default is '.'." ), doc=None, visibility=0)) safe_register_unique_option( options, "basename", PySPConfigValue( None, domain=_domain_must_be_str, description=( "The basename to use for all SMPS related output " "files. ** Required **" ), doc=None, visibility=0)) safe_register_unique_option( options, "disable_consistency_checks", PySPConfigValue( False, domain=bool, description=( "Disables consistency checks that attempt to find issues " "with the SMPS conversion. By default, these checks are run " "after conversion takes place and leave behind a temporary " "directory with per-scenario output files if the checks fail. " "This option is not recommended, but can be used if the " "consistency checks are prohibitively slow." ), doc=None, visibility=0)) safe_register_unique_option( options, "keep_scenario_files", PySPConfigValue( False, domain=bool, description=( "Keeps around the per-scenario SMPS files created for testing " "whether a conversion is valid (whether or not the validation " "checks are performed). These files can be useful for " "debugging purposes." ), doc=None, visibility=0)) safe_register_unique_option( options, "keep_auxiliary_files", PySPConfigValue( False, domain=bool, description=( "Keep auxiliary files for the template scenario that are normally " "used for testing the validity of the SMPS conversion. " "These include the .row, .col, .sto.struct, and .[mps,lp].det files." ), doc=None, visibility=0)) safe_register_common_option(options, "scenario_tree_manager") ScenarioTreeManagerClientSerial.register_options(options) ScenarioTreeManagerClientPyro.register_options(options) return options
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 setUp(self): self.options = PySPConfigBlock() self.async = False self.oneway = False self.delay = False ScenarioTreeManagerClientSerial.register_options(self.options)
# (3) Solves the extensive form instance and reports the dual values # associated with the non-anticipativity constraints on the # first-stage variables. import os import sys from pyomo.environ import * from pyomo.pysp.scenariotree.manager import \ ScenarioTreeManagerClientSerial from pyomo.pysp.ef import create_ef_instance from pyomo.opt import SolverFactory 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)
def convertddsip_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "verbose") safe_register_common_option(options, "symbolic_solver_labels") safe_register_unique_option( options, "output_directory", PySPConfigValue(".", domain=_domain_must_be_str, description=("The directory in which all DDSIP files " "will be stored. Default is '.'."), doc=None, visibility=0)) safe_register_unique_option( options, "first_stage_suffix", PySPConfigValue( "__DDSIP_FIRSTSTAGE", domain=_domain_must_be_str, description=("The suffix used to identify first-stage variables. " "Default: '__DDSIP_FIRSTSTAGE'"), doc=None, visibility=0)) safe_register_unique_option( options, "enforce_derived_nonanticipativity", PySPConfigValue( False, domain=bool, description=( "Adds nonanticipativity constraints for variables flagged " "as derived within their respective time stage (except for " "the final time stage). The default behavior behavior is " "to treat derived variables as belonging to the final " "time stage."), doc=None, visibility=0)) safe_register_unique_option( options, "disable_consistency_checks", PySPConfigValue( False, domain=bool, description=( "Disables consistency checks that attempt to find issues " "with the DDSIP conversion. By default, these checks are run " "after conversion takes place and leave behind a temporary " "directory with per-scenario output files if the checks fail. " "This option is not recommended, but can be used if the " "consistency checks are prohibitively slow."), doc=None, visibility=0)) safe_register_unique_option( options, "keep_scenario_files", PySPConfigValue( False, domain=bool, description=( "Keeps around the per-scenario DDSIP files created for testing " "whether a conversion is valid (whether or not the validation " "checks are performed). These files can be useful for " "debugging purposes."), doc=None, visibility=0)) safe_register_common_option(options, "scenario_tree_manager") ScenarioTreeManagerClientSerial.register_options(options) ScenarioTreeManagerClientPyro.register_options(options) return options
# (3) Solves the extensive form instance and reports the dual values # associated with the non-anticipativity constraints on the # first-stage variables. import os import sys from pyomo.environ import * from pyomo.pysp.scenariotree.manager import \ ScenarioTreeManagerClientSerial from pyomo.pysp.ef import create_ef_instance from pyomo.opt import SolverFactory thisdir = os.path.dirname(os.path.abspath(__file__)) farmer_example_dir = os.path.join(os.path.dirname(thisdir), 'farmer') options = ScenarioTreeManagerClientSerial.register_options() # To see detailed information about options #for name in options.keys(): # 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
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
def convertddsip_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "verbose") safe_register_common_option(options, "symbolic_solver_labels") safe_register_unique_option( options, "output_directory", PySPConfigValue( ".", domain=_domain_must_be_str, description=( "The directory in which all DDSIP files " "will be stored. Default is '.'." ), doc=None, visibility=0)) safe_register_unique_option( options, "first_stage_suffix", PySPConfigValue( "__DDSIP_FIRSTSTAGE", domain=_domain_must_be_str, description=( "The suffix used to identify first-stage variables. " "Default: '__DDSIP_FIRSTSTAGE'" ), doc=None, visibility=0)) safe_register_unique_option( options, "enforce_derived_nonanticipativity", PySPConfigValue( False, domain=bool, description=( "Adds nonanticipativity constraints for variables flagged " "as derived within their respective time stage (except for " "the final time stage). The default behavior behavior is " "to treat derived variables as belonging to the final " "time stage." ), doc=None, visibility=0)) safe_register_unique_option( options, "disable_consistency_checks", PySPConfigValue( False, domain=bool, description=( "Disables consistency checks that attempt to find issues " "with the DDSIP conversion. By default, these checks are run " "after conversion takes place and leave behind a temporary " "directory with per-scenario output files if the checks fail. " "This option is not recommended, but can be used if the " "consistency checks are prohibitively slow." ), doc=None, visibility=0)) safe_register_unique_option( options, "keep_scenario_files", PySPConfigValue( False, domain=bool, description=( "Keeps around the per-scenario DDSIP files created for testing " "whether a conversion is valid (whether or not the validation " "checks are performed). These files can be useful for " "debugging purposes." ), doc=None, visibility=0)) safe_register_common_option(options, "scenario_tree_manager") ScenarioTreeManagerClientSerial.register_options(options) ScenarioTreeManagerClientPyro.register_options(options) return options