def runadmm_register_options(options=None): if options is None: options = PySPConfigBlock() ADMMSolver.register_options(options) ScenarioTreeManagerFactory.register_options(options) safe_register_common_option(options, "verbose") safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "output_solver_log") safe_register_common_option(options, "output_scenario_tree_solution") safe_register_unique_option( options, "default_rho", PySPConfigValue( 1.0, domain=_domain_positive, description=("The default rho value for all non-anticipative " "variables. Default is 1.0."), doc=None, visibility=0), ap_args=("-r", "--default-rho"), ap_group=_admm_group_label) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() # # solve and I/O related # safe_declare_common_option(options, "symbolic_solver_labels") safe_declare_common_option(options, "solver_options") safe_declare_common_option(options, "solver") safe_declare_common_option(options, "solver_io") safe_declare_common_option(options, "solver_manager") safe_declare_common_option(options, "disable_warmstart") safe_declare_common_option(options, "disable_advanced_preprocessing") safe_declare_common_option(options, "output_solver_log") safe_declare_common_option(options, "keep_solver_files") safe_declare_common_option(options, "comparison_tolerance_for_fixed_variables") return options
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 runddsip_register_options(options=None): if options is None: options = PySPConfigBlock() DDSIPSolver.register_options(options) ScenarioTreeManagerFactory.register_options(options) safe_register_common_option(options, "verbose") safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "output_scenario_tree_solution") safe_register_common_option(options, "keep_solver_files") safe_register_common_option(options, "output_solver_log") safe_register_common_option(options, "symbolic_solver_labels") # used to populate the implicit DDSIP options safe_register_unique_option( options, "solver_options", PySPConfigValue( (), domain=_domain_tuple_of_str_or_dict, description=("Unregistered solver options that will be passed " "to DDSIP via the config file (e.g., NODELIM=4, " "CPLEX_1067=1). This option can be used multiple " "times from the command line to specify more " "than one DDSIP option."), doc=None, visibility=0), ap_kwds={'action': 'append'}, ap_group=_ddsip_group_label) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_common_option(options, "extension_precedence") return options
def runschuripopt_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "verbose") safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "output_scenario_tree_solution") safe_register_common_option(options, "output_solver_log") safe_register_common_option(options, "keep_solver_files") safe_register_common_option(options, "symbolic_solver_labels") ScenarioTreeManagerFactory.register_options(options) SchurIpoptSolver.register_options(options) # used to populate the implicit SchurIpopt options safe_register_unique_option( options, "solver_options", PySPConfigValue((), domain=_domain_tuple_of_str_or_dict, description=("Solver options to pass to SchurIpopt " "(e.g., relax_integrality=yes). This " "option can be used multiple times " "from the command line to specify more " "than one SchurIpopt option."), doc=None, visibility=0), ap_kwds={'action': 'append'}, ap_group=_schuripopt_group_label) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_common_option(options, "verbose", ap_group=_rho_group_label) return options
def scenariotreeserver_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, "pyro_host") safe_register_common_option(options, "pyro_port") safe_register_unique_option( options, "mpi", PySPConfigValue(False, domain=bool, description=("Activate MPI based functionality. " "Requires the mpi4py module."), doc=None, visibility=0)) safe_register_unique_option( options, "import_module", PySPConfigValue( (), domain=_domain_tuple_of_str, description=( "The name of a user-defined python module to import that, " "e.g., registers a user-defined scenario tree worker class."), doc=None, visibility=0)) return options
class JSONSolutionSaverExtension(PySPConfiguredExtension, PySPConfiguredObject, SingletonPlugin): implements(IPySPSolutionSaverExtension) _declared_options = \ PySPConfigBlock("Options declared for the " "JSONSolutionSaverExtension class") safe_declare_common_option(_declared_options, "output_name") safe_declare_common_option(_declared_options, "save_stages") _default_prefix = "jsonsaver_" # # Note: Do not try to user super() or access the # class name inside the __init__ method when # a class derives from a SingletonPlugin. Due to # how Pyutilib implements its Singleton type, # the __class__ cell will be empty. # (See: https://stackoverflow.com/questions/ # 13126727/how-is-super-in-python-3-implemented) # def __init__(self): PySPConfiguredExtension.__init__(self) def save(self, manager): if self.get_option("output_name") is not None: stage_solutions = [] # Do NOT open file in 'binary' mode when dumping JSON # (produces an error in Python3) with open(self.get_option('output_name'), 'w') as f: cntr = 0 for stage in manager.scenario_tree.stages: if (self.get_option('save_stages') <= 0) or \ (cntr+1 <= self.get_option('save_stages')): cntr += 1 node_solutions = {} for tree_node in stage.nodes: _node_solution = extract_node_solution(tree_node) if _node_solution is None: print( "No solution appears to be stored in node with " "name %s. No solution will be saved." % (tree_node.name)) return False node_solutions[tree_node.name] = _node_solution stage_solutions.append(node_solutions) else: break json.dump(stage_solutions, f, indent=2, sort_keys=True) print("Saved scenario tree solution for %s time stages " "to file %s" % (cntr, self.get_option('output_name'))) return True print("No value was set for %s option 'output_name'. " "Nothing will be saved." % (type(self).__name__)) return False
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_common_option(options, "input_name") safe_declare_common_option(options, "load_stages") return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_common_option(options, "verbose", ap_group=_admm_group_label) ScenarioTreeManagerSolverFactory.register_options( options, options_prefix="subproblem_", setup_argparse=False) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_common_option(options, "preprocess_fixed_variables") safe_declare_common_option(options, "symbolic_solver_labels") safe_declare_common_option(options, "output_times") safe_declare_common_option(options, "verbose") return options
def runsd_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "verbose") safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") ScenarioTreeManagerFactory.register_options(options) SDSolver.register_options(options) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() # options for controlling the solver manager # (not the scenario tree manager) safe_declare_common_option(options, "solver_manager_pyro_host") safe_declare_common_option(options, "solver_manager_pyro_port") safe_declare_common_option(options, "solver_manager_pyro_shutdown") ScenarioTreePreprocessor._declare_options(options) return options
def main(args=None): # # Top-level command that executes everything # # # Import plugins # import pyomo.environ # # Parse command-line options. # options = PySPConfigBlock() convertddsip_register_options(options) # # Prevent the compile_scenario_instances option from # appearing on the command line. This script relies on # the original constraints being present on the model # argparse_val = options.get('compile_scenario_instances')._argparse options.get('compile_scenario_instances')._argparse = None try: ap = argparse.ArgumentParser(prog='pyomo.pysp.convert.ddsip') options.initialize_argparse(ap) # restore the option so the class validation does not # raise an exception options.get('compile_scenario_instances')._argparse = argparse_val options.import_argparse(ap.parse_args(args=args)) except SystemExit as _exc: # the parser throws a system exit if "-h" is specified # - catch it to exit gracefully. return _exc.code return launch_command(run_convertddsip, options, error_label="pyomo.pysp.convert.ddsip: ", disable_gc=options.disable_gc, profile_count=options.profile, traceback=options.traceback)
def runsd_register_options(options=None): if options is None: options = PySPConfigBlock() SDSolver.register_options(options) ScenarioTreeManagerFactory.register_options(options) safe_register_common_option(options, "verbose") safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "output_scenario_tree_solution") safe_register_common_option(options, "keep_solver_files") safe_register_common_option(options, "output_solver_log") safe_register_common_option(options, "symbolic_solver_labels") return options
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 _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_common_option(options, "max_iterations", ap_group=_admm_group_label) safe_declare_unique_option( options, "primal_residual_relative_tolerance", PySPConfigValue( 1.0e-4, domain=_domain_positive, description=( "Relative primal-residual tolerance. Default is 1e-4." ), doc=None, visibility=0), ap_group=_admm_group_label) safe_declare_unique_option( options, "dual_residual_relative_tolerance", PySPConfigValue( 1.0e-4, domain=_domain_positive, description=( "Relative dual-residual tolerance. Default is 1e-4." ), doc=None, visibility=0), ap_group=_admm_group_label) ADMMAlgorithm._declare_options(options) for rstype in RhoStrategyFactory.registered_types.values(): rstype._declare_options(options) assert 'adaptive' in RhoStrategyFactory.registered_types safe_declare_unique_option( options, "rho_strategy", PySPConfigValue( 'adaptive', domain=_domain_must_be_str, description=( "Rho update strategy. Choices are: %s. Default is 'adaptive'." % (str(sorted(RhoStrategyFactory.registered_types.keys()))) ), doc=None, visibility=0), ap_group=_admm_group_label) return options
def run_evaluate_xhat_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, "output_scenario_tree_solution") safe_register_common_option(options, "solution_saver_extension") safe_register_common_option(options, "solution_loader_extension") safe_register_unique_option( options, "disable_solution_loader_check", PySPConfigValue( False, domain=bool, description=( "Indicates that no solution loader extension is required to " "run this script, e.g., because the scenario tree manager " "is somehow pre-populated with a solution." ), doc=None, visibility=0), ap_group=_extension_options_group_title) safe_register_unique_option( options, "output_scenario_costs", PySPConfigValue( None, domain=_domain_must_be_str, description=( "A file name where individual scenario costs from the solution " "will be stored. The format is determined from the extension used " "in the filename. Recognized extensions: [.csv, .json, .yaml]" ), doc=None, visibility=0)) ScenarioTreeManagerSolverClientSerial.register_options(options) ScenarioTreeManagerSolverClientPyro.register_options(options) return options
def main(args=None): # # Top-level command that executes everything # # # Import plugins # import pyomo.environ # # Parse command-line options. # options = PySPConfigBlock() pysp2smps_register_options(options) # # Prevent the compile_scenario_instances option from # appearing on the command line. This script relies on # the original constraints being present on the model # argparse_val = options.get('compile_scenario_instances')._argparse options.get('compile_scenario_instances')._argparse = None try: ap = argparse.ArgumentParser(prog='pysp2smps') options.initialize_argparse(ap) # restore the option so the class validation does not # raise an exception options.get('compile_scenario_instances')._argparse = argparse_val options.import_argparse(ap.parse_args(args=args)) except SystemExit as _exc: # the parser throws a system exit if "-h" is specified # - catch it to exit gracefully. return _exc.code return launch_command(run_pysp2smps, options, error_label="pysp2smps: ", disable_gc=options.disable_gc, profile_count=options.profile, traceback=options.traceback)
def _register_scenario_tree_manager_solver_options(*args, **kwds): if len(args) == 0: options = PySPConfigBlock() else: if len(args) != 1: raise TypeError( "register_options(...) takes at most 1 argument (%s given)" % (len(args))) options = args[0] if not isinstance(options, PySPConfigBlock): raise TypeError( "register_options(...) argument must be of type PySPConfigBlock, " "not %s" % (type(options).__name__)) ScenarioTreeManagerSolverClientSerial.register_options(options, **kwds) ScenarioTreeManagerSolverClientPyro.register_options(options, **kwds) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() # # scenario instance construction # safe_declare_common_option(options, "objective_sense_stage_based") safe_declare_common_option(options, "output_instance_construction_time") safe_declare_common_option(options, "compile_scenario_instances") # # various # safe_declare_common_option(options, "verbose") safe_declare_common_option(options, "profile_memory") return options
def generate_scenario_tree_image_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_unique_option( options, "output_file", PySPConfigValue( "ScenarioStructure.pdf", domain=_domain_must_be_str, description=("The name of the file in which to store the scenario " "tree image. Default is ScenarioStructure.pdf."), doc=None, visibility=0)) safe_register_common_option(options, "verbose") safe_register_common_option(options, "model_location") safe_register_common_option(options, "scenario_tree_location") safe_register_common_option(options, "scenario_tree_random_seed") safe_register_common_option(options, "scenario_tree_downsample_fraction") safe_register_common_option(options, "scenario_bundle_specification") safe_register_common_option(options, "create_random_bundles") safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_unique_option( options, "firststage_suffix", PySPConfigValue( "__DDSIP_FIRSTSTAGE", domain=_domain_must_be_str, description=("The suffix used to identity first-stage " "variables to DDSIP. Default is " "'__DDSIP_FIRSTSTAGE'"), doc=None, visibility=0), ap_group=_ddsip_group_label) safe_declare_unique_option( options, "config_file", PySPConfigValue( None, domain=_domain_must_be_str, description=( "The name of a partial DDSIP configuration file " "that contains option specifications unrelated to " "the problem structure. If specified, the contents " "of this file will be appended to the " "configuration created by this solver interface. " "Default is None."), doc=None, visibility=0), ap_group=_ddsip_group_label) safe_declare_common_option(options, "verbose", ap_group=_ddsip_group_label) return options
def parse_command_line(args, register_options_callback, with_extensions=None, **kwds): import pyomo.pysp.plugins pyomo.pysp.plugins.load() from pyomo.pysp.util.config import _domain_tuple_of_str registered_extensions = {} if with_extensions is not None: for name in with_extensions: plugins = ExtensionPoint(with_extensions[name]) for plugin in plugins(all=True): registered_extensions.setdefault(name,[]).\ append(plugin.__class__.__module__) def _get_argument_parser(options): # if we modify this and don't copy it, # the this output will appear twice the second # time this function gets called _kwds = dict(kwds) if len(registered_extensions) > 0: assert with_extensions is not None epilog = _kwds.pop('epilog',"") if epilog != "": epilog += "\n\n" epilog += "Registered Extensions:\n" for name in registered_extensions: epilog += " - "+str(with_extensions[name].__name__)+": " epilog += str(registered_extensions[name])+"\n" _kwds['epilog'] = epilog ap = argparse.ArgumentParser( add_help=False, formatter_class=argparse.RawDescriptionHelpFormatter, **_kwds) options.initialize_argparse(ap) ap.add_argument("-h", "--help", dest="show_help", action="store_true", default=False, help="show this help message and exit") return ap # # Register options # options = PySPConfigBlock() register_options_callback(options) if with_extensions is not None: for name in with_extensions: configval = options.get(name, None) assert configval is not None assert configval._domain is _domain_tuple_of_str ap = _get_argument_parser(options) # First parse known args, then import any extension plugins # specified by the user, regenerate the options block and # reparse to pick up plugin specific registered options opts, _ = ap.parse_known_args(args=args) options.import_argparse(opts) extensions = {} if with_extensions is None: if opts.show_help: pass else: if all(len(options.get(name).value()) == 0 for name in with_extensions) and \ opts.show_help: ap.print_help() sys.exit(0) for name in with_extensions: extensions[name] = load_extensions( options.get(name).value(), with_extensions[name]) # regenerate the options options = PySPConfigBlock() register_options_callback(options) for name in extensions: for plugin in extensions[name]: if isinstance(plugin, PySPConfiguredObject): plugin.register_options(options) # do a dummy access to option to prevent # a warning about it not being used options.get(name).value() ap = _get_argument_parser(options) opts = ap.parse_args(args=args) options.import_argparse(opts) for name in extensions: for plugin in extensions[name]: if isinstance(plugin, PySPConfiguredObject): plugin.set_options(options) if opts.show_help: ap.print_help() sys.exit(0) if with_extensions: for name in extensions: extensions[name] = sort_extensions_by_precedence(extensions[name]) return options, extensions else: return options
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
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() return options
def test_declared(self): b = PySPConfigBlock() safe_register_common_option(b, "verbose") b.display() b.display() out = StringIO() b.display(ostream=out) self.assertEqual(out.getvalue(), "verbose: false\n") self.assertEqual(b.check_usage(), True) self.assertEqual(b.check_usage(error=False), True) b.verbose = True out = StringIO() b.display(ostream=out) self.assertEqual(out.getvalue(), "verbose: true\n") with self.assertRaises(ValueError): b.check_usage() with self.assertRaises(ValueError): b.check_usage() self.assertEqual(b.check_usage(error=False), False) b.verbose self.assertEqual(b.check_usage(), True) self.assertEqual(b.check_usage(error=False), True) verbose_about = \ """PySPConfigValue: verbose - type: <%s 'bool'> - default: False - doc: Generate verbose output for both initialization and execution.""" % ('class' if six.PY3 else 'type') self.assertEqual(b.about("verbose"), verbose_about)
def test_implicit(self): b = PySPConfigBlock() b._implicit_declaration = True b.name_a = 1 b.nameb = 2 b.display() out = StringIO() b.display(ostream=out) self.assertEqual(out.getvalue(), "name_a: 1\nnameb: 2\n") with self.assertRaises(ValueError): b.check_usage() with self.assertRaises(ValueError): b.check_usage() self.assertEqual(b.check_usage(error=False), False) b.name_a b.nameb self.assertEqual(b.check_usage(), True) self.assertEqual(b.check_usage(error=False), True) name_a_about = \ """ConfigValue: name_a - type: None - default: 1 - doc: None""" self.assertEqual(b.about("name_a"), name_a_about)
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 runef_register_options(options=None): if options is None: options = PySPConfigBlock() EFSolver.register_options(options) ScenarioTreeManagerClientSerial.register_options(options) safe_register_common_option(options, "verbose") safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "symbolic_solver_labels") safe_register_common_option(options, "output_solver_log") safe_register_common_option(options, "keep_solver_files") safe_register_common_option(options, "output_scenario_tree_solution") safe_register_common_option(options, "solution_saver_extension") safe_register_common_option(options, "solution_loader_extension") safe_register_unique_option( options, "solution_writer", PySPConfigValue( (), domain=_domain_tuple_of_str, description=( "The name of a python module specifying a user-defined " "plugin implementing the ISolutionWriterExtension " "interface. Invoked to save a scenario tree solution. Use " "this option when generating a template configuration file " "or invoking command-line help in order to include any " "plugin-specific options. This option can used multiple " "times from the command line to specify more than one " "plugin."), doc=None, visibility=0), ap_kwds={'action': 'append'}, ap_group=_extension_options_group_title) safe_register_unique_option( options, "output_file", PySPConfigValue( None, domain=_domain_must_be_str, description=("The name of the extensive form output file " "(currently LP, MPS, and NL file formats are " "supported). If the option value does not end " "in '.lp', '.mps', or '.nl', then the output format " "will be inferred from the settings for the chosen " "solver interface, and the appropriate suffix " "will be appended to the name. Use of this option " "will disable the solve."), doc=None, visibility=0), ap_group=_output_options_group_title) safe_register_unique_option( options, "output_scenario_costs", PySPConfigValue( None, domain=_domain_must_be_str, description=( "A file name where individual scenario costs from the " "solution will be stored. The format is determined " "from the extension used in the filename. Recognized " "extensions: [.csv, .json, .yaml]"), doc=None, visibility=0)) # # Deprecated # class _DeprecatedActivateJSONIOSolutionSaver( pyutilib.misc.config.argparse.Action): def __init__(self, option_strings, dest, nargs=None, **kwargs): if nargs is not None: raise ValueError("nargs not allowed") super(_DeprecatedActivateJSONIOSolutionSaver, self).\ __init__(option_strings, dest, nargs=0, **kwargs) def __call__(self, parser, namespace, values, option_string=None): logger.warning( "DEPRECATED: The '--activate-json-io-solution-saver " "command-line option has been deprecated and will " "be removed in the future. Please the following instead: " "'----solution-saver-extension=pyomo.pysp.plugins.jsonio'") val = getattr(namespace, 'CONFIGBLOCK.solution_saver_extension', []) setattr(namespace, 'CONFIGBLOCK.solution_saver_extension', val + ["pyomo.pysp.plugins.jsonio"]) def _warn_activate_jsonio_solution_saver(val): # don't use logger here since users might not import # the pyomo logger in a scripting interface sys.stderr.write( "\tWARNING: The 'activate_jsonio_solution_saver' " "config item will be ignored " "unless it is being used as a command-line option " "where it can be redirected to 'solution_saver_extension'. " "Please use 'solution_saver_extension=pyomo.pysp.plugins.jsonio' " "instead.\n") return _domain_tuple_of_str(val) safe_declare_unique_option( options, "activate_jsonio_solution_saver", PySPConfigValue( None, domain=_warn_activate_jsonio_solution_saver, description=( "Deprecated alias for " "--solution-saver-extension=pyomo.pysp.plugins.jsonio"), doc=None, visibility=1), ap_kwds={'action': _DeprecatedActivateJSONIOSolutionSaver}, ap_group=_deprecated_options_group_title, declare_for_argparse=True) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_unique_option( options, "cvar_weight", PySPConfigValue( 1.0, domain=_domain_nonnegative, description=("The weight associated with the CVaR term in " "the risk-weighted objective " "formulation. If the weight is 0, then " "*only* a non-weighted CVaR cost will appear " "in the EF objective - the expected cost " "component will be dropped. Default is 1.0."), doc=None, visibility=0), ap_group=_ef_group_label) safe_declare_unique_option( options, "generate_weighted_cvar", PySPConfigValue( False, domain=bool, description=("Add a weighted CVaR term to the " "primary objective. Default is False."), doc=None, visibility=0), ap_group=_ef_group_label) safe_declare_unique_option( options, "risk_alpha", PySPConfigValue( 0.95, domain=_domain_unit_interval, description=("The probability threshold associated with " "CVaR (or any future) risk-oriented " "performance metrics. Default is 0.95."), doc=None, visibility=0), ap_group=_ef_group_label) safe_declare_unique_option( options, "cc_alpha", PySPConfigValue( 0.0, domain=_domain_unit_interval, description=("The probability threshold associated with a " "chance constraint. The RHS will be one " "minus this value. Default is 0."), doc=None, visibility=0), ap_group=_ef_group_label) safe_declare_unique_option( options, "cc_indicator_var", PySPConfigValue( None, domain=_domain_must_be_str, description=("The name of the binary variable to be used " "to construct a chance constraint. Default " "is None, which indicates no chance " "constraint."), doc=None, visibility=0), ap_group=_ef_group_label) safe_declare_common_option(options, "solver") safe_declare_common_option(options, "solver_io") safe_declare_common_option(options, "solver_manager") safe_declare_common_option(options, "solver_options") safe_declare_common_option(options, "disable_warmstart") safe_declare_common_option(options, "solver_manager_pyro_host") safe_declare_common_option(options, "solver_manager_pyro_port") safe_declare_common_option(options, "solver_manager_pyro_shutdown") safe_declare_common_option(options, "verbose", ap_group=_ef_group_label) safe_declare_common_option(options, "output_times", ap_group=_ef_group_label) safe_declare_common_option(options, "output_solver_results", ap_group=_ef_group_label) return options
def register_options(cls, *args, **kwds): """Cls.register_options([options]) -> options. Fills an options block will all registered options for this class. The optional argument 'options' can be a previously existing options block, which would be both updated and returned by this function. The optional flag 'prefix' can be set to indicate that all class options should be registered with the given prefix prepended to their original name.""" prefix = kwds.pop('prefix',"") assert isinstance(prefix, six.string_types) if len(kwds) != 0: raise ValueError("Unexpected keywords: %s" % (str(kwds))) if len(args) == 0: options = PySPConfigBlock() else: if len(args) != 1: raise TypeError( "register_options(...) takes at most 1 argument (%s given)" % (len(args))) options = args[0] if not isinstance(options, PySPConfigBlock): raise TypeError( "register_options(...) argument must be of type PySPConfigBlock, " "not %s" % (type(options).__name__)) bases = inspect.getmro(cls) assert bases[-1] is object # # Register options in order of most derived class # first. This allows derived classes to update # option default values. The safe_declare_option # function will simply validate that an existing # option matches when a base class attempts to # register an option that is already registered (in # which cases we tell it to accept differences in # default values). # for base in bases: # # The check here is that PySPConfiguredObject needs to # appear as an immediate base in a class definition for us # to check the list of _registered options declared in # that class definitions immediate scope. This allows # _declared_options to be distributed across different # containers in the class hierarchy, while leaving the # ability for derived classes to NOT have to define an # empty _declared_options block if they don't have any # new options to add but are derived from some class which # does use PySPConfiguredObject as a base class. By not # declaring PySPConfiguredObject as an immediate base # class, we know not to check for a _declared_options # data member in this derived class's scope (because # otherwise we'd be getting some base class's definition) # if any(base is PySPConfiguredObject for base in base.__bases__): for name in base._declared_options: configval = base._declared_options.get(name) assert configval._parent is base._declared_options configval._parent = None declare_for_argparse = False if (configval._argparse is None) and \ (options.get(prefix+name, None) is None): declare_for_argparse = True safe_declare_option( options, prefix+name, configval, relax_default_check=True, declare_for_argparse=declare_for_argparse) configval._parent = base._declared_options return options