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 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 runef(options, solution_loaders=(), solution_savers=(), solution_writers=()): """ Construct a senario tree manager and solve it with the Extensive Form solver. """ start_time = time.time() solution_loaders = sort_extensions_by_precedence(solution_loaders) solution_savers = sort_extensions_by_precedence(solution_savers) solution_writers = sort_extensions_by_precedence(solution_writers) with ScenarioTreeManagerClientSerial(options) as sp: sp.initialize() for plugin in solution_loaders: ret = plugin.load(sp) if not ret: logger.warning( "Loader extension %s call did not return " "True. This might indicate failure to load data." % (plugin)) if options.output_file is not None: with ExtensiveFormAlgorithm(sp, options) as ef: ef.build_ef() ef.write(filename) else: print("") print("Running the EF solver for " "stochastic programming problems.") ef = EFSolver() ef_options = ef.extract_user_options_to_dict(options, sparse=True) results = ef.solve( sp, options=ef_options, output_solver_log=options.output_solver_log, keep_solver_files=options.keep_solver_files, symbolic_solver_labels=options.symbolic_solver_labels) xhat = results.xhat del results.xhat print("") print(results) results.xhat = xhat if options.output_scenario_tree_solution: print("") sp.scenario_tree.snapshotSolutionFromScenarios() sp.scenario_tree.pprintSolution() sp.scenario_tree.pprintCosts() if options.output_scenario_costs is not None: if options.output_scenario_costs.endswith('.json'): import json result = {} for scenario in sp.scenario_tree.scenarios: result[str(scenario.name)] = scenario._cost with open(options.output_scenario_costs, 'w') as f: json.dump(result, f, indent=2, sort_keys=True) elif options.output_scenario_costs.endswith('.yaml'): import yaml result = {} for scenario in sp.scenario_tree.scenarios: result[str(scenario.name)] = scenario._cost with open(options.output_scenario_costs, 'w') as f: yaml.dump(result, f) else: if not options.output_scenario_costs.endswith('.csv'): print("Unrecognized file extension. Using CSV " "format to store scenario costs") with open(options.output_scenario_costs, 'w') as f: for scenario in sp.scenario_tree.scenarios: f.write("%s,%r\n" % (scenario.name, scenario._cost)) for plugin in solution_savers: if not plugin.save(sp): logger.warning("Saver extension %s call did not " "return True. This might indicate failure " "to save data." % (plugin)) for plugin in solution_writers: plugin.write(sp.scenario_tree, "ef") print("") print("Total EF execution time=%.2f seconds" % (time.time() - start_time)) print("") return 0
def runef(options, solution_loaders=(), solution_savers=(), solution_writers=()): import pyomo.environ start_time = time.time() solution_loaders = sort_extensions_by_precedence(solution_loaders) solution_savers = sort_extensions_by_precedence(solution_savers) solution_writers = sort_extensions_by_precedence(solution_writers) with ScenarioTreeManagerClientSerial(options) \ as manager: manager.initialize() loaded = False for plugin in solution_loaders: ret = plugin.load(manager) if not ret: print("WARNING: Loader extension %s call did not return True. " "This might indicate failure to load data." % (plugin)) else: loaded = True print("") print("Initializing extensive form algorithm for " "stochastic programming problems.") with ExtensiveFormAlgorithm(manager, options) as ef: ef.build_ef() # This is somewhat of a hack to get around the # weird semantics of this script (assumed by tests) if (not options.solve) or \ (options.get("output_file")._userSet): ef.write(options.output_file) if not options.solve: if options.output_scenario_costs is not None: print("WARNING: output_scenario_costs option " "will be ignored because the extensive form " "has not been solved.") if len(solution_savers): print("WARNING: Solution saver extensions will " "not be called because the extensive form " "has not been solved.") if len(solution_writers): print("WARNING: Solution writer extensions will " "not be called because the extensive form " "has not been solved.") else: ef.solve() print("EF solve completed and solution status is %s" % ef.solution_status) print("EF solve termination condition is %s" % ef.termination_condition) print("EF objective: %12.5f" % ef.objective) if ef.gap is not undefined: print("EF gap: %12.5f" % ef.gap) print("EF bound: %12.5f" % ef.bound) else: assert ef.bound is undefined print("EF gap: <unknown>") print("EF bound: <unknown>") # handle output of solution from the scenario tree. print("") print("Extensive form solution:") manager.scenario_tree.pprintSolution() print("") print("Extensive form costs:") manager.scenario_tree.pprintCosts() if options.output_scenario_tree_solution: print("Final solution (scenario tree format):") manager.scenario_tree.pprintSolution() if options.output_scenario_costs is not None: if options.output_scenario_costs.endswith('.json'): import json result = {} for scenario in manager.scenario_tree.scenarios: result[str(scenario.name)] = scenario._cost with open(options.output_scenario_costs, 'w') as f: json.dump(result, f, indent=2, sort_keys=True) elif options.output_scenario_costs.endswith('.yaml'): import yaml result = {} for scenario in manager.scenario_tree.scenarios: result[str(scenario.name)] = scenario._cost with open(options.output_scenario_costs, 'w') as f: yaml.dump(result, f) else: if not options.output_scenario_costs.endswith('.csv'): print( "Unrecognized file extension. Using CSV format " "to store scenario costs") with open(options.output_scenario_costs, 'w') as f: for scenario in manager.scenario_tree.scenarios: f.write("%s,%r\n" % (scenario.name, scenario._cost)) for plugin in solution_savers: if not plugin.save(manager): print("WARNING: Saver extension %s call did not " "return True. This might indicate failure " "to save data." % (plugin)) for plugin in solution_writers: plugin.write(manager.scenario_tree, "ef") print("") print("Total EF execution time=%.2f seconds" % (time.time() - start_time)) print("") return 0