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=()): """ 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
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
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
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