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 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 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 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 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_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)) ScenarioTreeManagerFactory.register_options(options) ScenarioTreeManagerSolverFactory.register_options(options, options_prefix="subproblem_") return options
def runsd(options): """ Construct a senario tree manager and solve it with the SD solver. """ start_time = time.time() with ScenarioTreeManagerFactory(options) as sp: sp.initialize() print("") print("Running SD solver for stochastic " "programming problems") sd = SDSolver() sd_options = sd.extract_user_options_to_dict(options, sparse=True) results = sd.solve( sp, options=sd_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) if options.output_scenario_tree_solution: print("") sp.scenario_tree.snapshotSolutionFromScenarios() sp.scenario_tree.pprintSolution() sp.scenario_tree.pprintCosts() print("") print("Total execution time=%.2f seconds" % (time.time() - start_time)) return 0
def runadmm(options): """ Construct a senario tree manager and solve it with the ADMM solver. """ start_time = time.time() with ScenarioTreeManagerFactory(options) as sp: sp.initialize() print("") print("Running ADMM solver for stochastic " "programming problems") admm = ADMMSolver() admm_options = admm.extract_user_options_to_dict(options, sparse=True) results = admm.solve(sp, options=admm_options, rho=options.default_rho, output_solver_log=options.output_solver_log) xhat = results.xhat del results.xhat print("") print(results) if options.output_scenario_tree_solution: print("Final solution (scenario tree format):") sp.scenario_tree.snapshotSolutionFromScenarios() sp.scenario_tree.pprintSolution() sp.scenario_tree.pprintCosts() print("") print("Total execution time=%.2f seconds" % (time.time() - start_time)) return 0
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 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 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)) ScenarioTreeManagerFactory.register_options(options) ScenarioTreeManagerSolverFactory.register_options( options, options_prefix="subproblem_") return options
def runddsip(options): """ Construct a senario tree manager and solve it with the DDSIP solver. """ start_time = time.time() with ScenarioTreeManagerFactory(options) as sp: sp.initialize() print("") print("Running DDSIP solver for stochastic " "programming problems") ddsip = DDSIPSolver() # add the implicit ddsip options solver_options = options.solver_options if len(solver_options) > 0: if type(solver_options) is tuple: for name_val in solver_options: assert "=" in name_val name, val = name_val.split("=") ddsip.options[name.strip()] = val.strip() else: for key, val in solver_options.items(): ddsip.options[key] = val ddsip_options = ddsip.extract_user_options_to_dict(options, sparse=True) results = ddsip.solve( sp, options=ddsip_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) if options.output_scenario_tree_solution: print("") sp.scenario_tree.snapshotSolutionFromScenarios() sp.scenario_tree.pprintSolution() sp.scenario_tree.pprintCosts() print("") print("Total execution time=%.2f seconds" % (time.time() - start_time)) return 0
def runschuripopt(options): """ Construct a senario tree manager and solve it with the SD solver. """ start_time = time.time() with ScenarioTreeManagerFactory(options) as manager: manager.initialize() print("") print("Running SchurIpopt solver for stochastic " "programming problems") schuripopt = SchurIpoptSolver() # add the implicit schuripopt options solver_options = options.solver_options if len(solver_options) > 0: if type(solver_options) is tuple: for name_val in solver_options: assert "=" in name_val name, val = name_val.split("=") schuripopt.options[name.strip()] = val.strip() else: for key, val in solver_options.items(): schuripopt.options[key] = val results = schuripopt.solve( manager, 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) if options.output_scenario_tree_solution: print("Final solution (scenario tree format):") manager.scenario_tree.snapshotSolutionFromScenarios() manager.scenario_tree.pprintSolution() print("") print("Total execution time=%.2f seconds" % (time.time() - start_time)) return 0
import matplotlib.pylab as plt from pyomo.pysp.scenariotree.manager import \ ScenarioTreeManagerFactory from pyomo.pysp.scenariotree.instance_factory import \ 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))
ScenarioTreeManagerFactory from pyomo.pysp.solvers.benders import BendersAlgorithm # *** How to run this example using Pyro ***: # # To run this example using Pyro, launch the following # command in another terminal: # $ mpirun -np 1 pyomo_ns -r -n localhost : \ # -np 1 dispatch_srvr -n localhost : \ # -np 3 scenariotreeserver --pyro-host=localhost --traceback # # In this shell launch: # $ python benders_scripting.py # with sp_options.scenario_tree_manager = "pyro" sp_options = ScenarioTreeManagerFactory.register_options() # To see detailed information about options #for name in sp_options.keys(): # print(sp_options.about(name)) # To see a more compact display of options #sp_options.display() # # General options for the scenario tree manager # sp_options.scenario_tree_manager = "serial" # using absolute paths so we can automate testing # of this example examplesdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ScenarioTreeManagerFactory from pyomo.pysp.solvers.benders import BendersAlgorithm # *** How to run this example using Pyro ***: # # To run this example using Pyro, launch the following # command in another terminal: # $ mpirun -np 1 pyomo_ns -r -n localhost : \ # -np 1 dispatch_srvr -n localhost : \ # -np 3 scenariotreeserver --pyro-host=localhost --traceback # # In this shell launch: # $ python benders_scripting.py # with sp_options.scenario_tree_manager = "pyro" sp_options = ScenarioTreeManagerFactory.register_options() # To see detailed information about options #for name in sp_options.keys(): # print(sp_options.about(name)) # To see a more compact display of options #sp_options.display() # # General options for the scenario tree manager # sp_options.scenario_tree_manager = "serial" # using absolute paths so we can automate testing # of this example examplesdir = os.path.dirname(
def run_evaluate_xhat(options, solution_loaders=(), solution_savers=()): import pyomo.environ start_time = time.time() solution_loaders = sort_extensions_by_precedence(solution_loaders) solution_savers = sort_extensions_by_precedence(solution_savers) with ScenarioTreeManagerFactory(options) as sp: sp.initialize() loaded = False for plugin in solution_loaders: ret = plugin.load(sp) if not ret: print("WARNING: Loader extension %s call did not return True. " "This might indicate failure to load data." % (plugin)) else: loaded = True if (not loaded) and (not options.disable_solution_loader_check): raise RuntimeError( "Either no solution loader extensions were provided or " "all solution loader extensions reported a bad return value. " "To disable this check use the disable_solution_loader_check " "option flag.") with ScenarioTreeManagerSolverFactory( sp, options, options_prefix="subproblem_") as sp_solver: evaluate_current_node_solution(sp, sp_solver) objective = sum(scenario.probability * \ scenario.get_current_objective() for scenario in sp.scenario_tree.scenarios) sp.scenario_tree.snapshotSolutionFromScenarios() print("") print("***********************************************" "************************************************") print(">>>THE EXPECTED SUM OF THE STAGE COST VARIABLES=" +str(sp.scenario_tree.findRootNode().\ computeExpectedNodeCost())+"<<<") print("***********************************************" "************************************************") # handle output of solution from the scenario tree. print("") print("Extensive form solution:") sp.scenario_tree.pprintSolution() print("") print("Extensive form costs:") sp.scenario_tree.pprintCosts() if options.output_scenario_tree_solution: print("Final solution (scenario tree format):") sp.scenario_tree.pprintSolution() 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): print("WARNING: Saver extension %s call did not return True. " "This might indicate failure to save data." % (plugin)) print("") print("Total execution time=%.2f seconds" % (time.time() - start_time)) return 0
from pyomo.pysp.scenariotree.manager import \ ScenarioTreeManagerFactory from pyomo.pysp.scenariotree.instance_factory import \ 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))