Best Python code snippet using localstack_python
generate_distributed_NL.py
Source:generate_distributed_NL.py
1# ___________________________________________________________________________2#3# Pyomo: Python Optimization Modeling Objects4# Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC5# Under the terms of Contract DE-NA0003525 with National Technology and 6# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain 7# rights in this software.8# This software is distributed under the 3-clause BSD License.9# ___________________________________________________________________________10import os11import sys12import time13import argparse14from pyomo.util import pyomo_command15from pyomo.pysp.util.config import (PySPConfigValue,16 PySPConfigBlock,17 safe_register_common_option,18 safe_register_unique_option,19 _domain_must_be_str)20from pyomo.pysp.util.misc import (parse_command_line,21 launch_command)22from pyomo.pysp.scenariotree.manager import (ScenarioTreeManagerClientSerial,23 ScenarioTreeManagerClientPyro,24 InvocationType)25from pyomo.core import Suffix, Block26# generate an absolute path to this file27thisfile = os.path.abspath(__file__)28def _write_bundle_NL(worker,29 bundle,30 output_directory,31 linking_suffix_name,32 objective_suffix_name,33 symbolic_solver_labels):34 assert os.path.exists(output_directory)35 bundle_instance = worker._bundle_binding_instance_map[bundle.name]36 assert not hasattr(bundle_instance, ".tmpblock")37 tmpblock = Block(concrete=True)38 bundle_instance.add_component(".tmpblock", tmpblock)39 #40 # linking variable suffix41 #42 tmpblock.add_component(linking_suffix_name,43 Suffix(direction=Suffix.EXPORT))44 linking_suffix = getattr(tmpblock, linking_suffix_name)45 # Loop over all nodes for the bundle except the leaf nodes,46 # which have no blended variables47 scenario_tree = worker.scenario_tree48 for stage in bundle.scenario_tree.stages[:-1]:49 for _node in stage.nodes:50 # get the node of off the real scenario tree51 # as this has the linked variable information52 node = scenario_tree.get_node(_node.name)53 master_variable = bundle_instance.find_component(54 "MASTER_BLEND_VAR_"+str(node.name))55 for variable_id in node._standard_variable_ids:56 linking_suffix[master_variable[variable_id]] = variable_id57 #58 # objective weight suffix59 #60 tmpblock.add_component(objective_suffix_name,61 Suffix(direction=Suffix.EXPORT))62 getattr(tmpblock, objective_suffix_name)[bundle_instance] = \63 bundle._probability64 output_filename = os.path.join(output_directory, str(bundle.name)+".nl")65 bundle_instance.write(66 output_filename,67 io_options={'symbolic_solver_labels': symbolic_solver_labels})68 bundle_instance.del_component(tmpblock)69def _write_scenario_NL(worker,70 scenario,71 output_directory,72 linking_suffix_name,73 objective_suffix_name,74 symbolic_solver_labels):75 assert os.path.exists(output_directory)76 instance = scenario._instance77 assert not hasattr(instance, ".tmpblock")78 tmpblock = Block(concrete=True)79 instance.add_component(".tmpblock", tmpblock)80 #81 # linking variable suffix82 #83 bySymbol = instance._ScenarioTreeSymbolMap.bySymbol84 tmpblock.add_component(linking_suffix_name,85 Suffix(direction=Suffix.EXPORT))86 linking_suffix = getattr(tmpblock, linking_suffix_name)87 # Loop over all nodes for the scenario except the leaf node,88 # which has no blended variables89 for node in scenario._node_list[:-1]:90 for variable_id in node._standard_variable_ids:91 linking_suffix[bySymbol[variable_id]] = variable_id92 #93 # objective weight suffix94 #95 tmpblock.add_component(objective_suffix_name,96 Suffix(direction=Suffix.EXPORT))97 getattr(tmpblock, objective_suffix_name)[instance] = \98 scenario._probability99 output_filename = os.path.join(output_directory,100 str(scenario.name)+".nl")101 instance.write(102 output_filename,103 io_options={'symbolic_solver_labels': symbolic_solver_labels})104 instance.del_component(tmpblock)105def write_distributed_NL_files(manager,106 output_directory,107 linking_suffix_name,108 objective_suffix_name,109 symbolic_solver_labels=False):110 if not os.path.exists(output_directory):111 os.makedirs(output_directory)112 scenario_tree = manager.scenario_tree113 #114 # Write list of subproblems to file115 #116 with open(os.path.join(output_directory,117 "PySP_Subproblems.txt"),'w') as f:118 if scenario_tree.contains_bundles():119 for bundle in scenario_tree.bundles:120 f.write(str(bundle.name)+".nl\n")121 else:122 for scenario in scenario_tree.scenarios:123 f.write(str(scenario.name)+".nl\n")124 if scenario_tree.contains_bundles():125 print("Executing bundle NL-file conversions")126 manager.invoke_function(127 "_write_bundle_NL",128 thisfile,129 invocation_type=InvocationType.PerBundle,130 function_args=(output_directory,131 linking_suffix_name,132 objective_suffix_name,133 symbolic_solver_labels))134 else:135 print("Executing scenario NL-file conversions")136 manager.invoke_function(137 "_write_scenario_NL",138 thisfile,139 invocation_type=InvocationType.PerScenario,140 function_args=(output_directory,141 linking_suffix_name,142 objective_suffix_name,143 symbolic_solver_labels))144def run_generate_distributed_NL_register_options(options=None):145 if options is None:146 options = PySPConfigBlock()147 safe_register_common_option(options, "disable_gc")148 safe_register_common_option(options, "profile")149 safe_register_common_option(options, "traceback")150 safe_register_common_option(options, "scenario_tree_manager")151 safe_register_common_option(options, "symbolic_solver_labels")152 safe_register_unique_option(153 options,154 "output_directory",155 PySPConfigValue(156 ".",157 domain=_domain_must_be_str,158 description=(159 "The directory in which to store all output files. "160 "Default is '.'."161 ),162 doc=None,163 visibility=0))164 safe_register_unique_option(165 options,166 "linking_suffix_name",167 PySPConfigValue(168 "variable_id",169 domain=_domain_must_be_str,170 description=(171 "The suffix name used to identify common variables "172 "across NL files. Default is 'ipopt_blend_id'."173 ),174 doc=None,175 visibility=0))176 safe_register_unique_option(177 options,178 "objective_suffix_name",179 PySPConfigValue(180 "objective_weight",181 domain=_domain_must_be_str,182 description=(183 "The suffix name used to identify the relative "184 "objective weight for each NL-file subproblem."185 "Default is 'ipopt_blend_weight'."186 ),187 doc=None,188 visibility=0))189 ScenarioTreeManagerClientSerial.register_options(options)190 ScenarioTreeManagerClientPyro.register_options(options)191 return options192#193# Convert a PySP scenario tree formulation to SMPS input files194#195def run_generate_distributed_NL(options):196 import pyomo.environ197 start_time = time.time()198 manager_class = None199 if options.scenario_tree_manager == 'serial':200 manager_class = ScenarioTreeManagerClientSerial201 elif options.scenario_tree_manager == 'pyro':202 manager_class = ScenarioTreeManagerClientPyro203 with manager_class(options) \204 as manager:205 manager.initialize()206 write_distributed_NL_files(207 manager,208 options.output_directory,209 options.linking_suffix_name,210 options.objective_suffix_name,211 symbolic_solver_labels=options.symbolic_solver_labels)212 print("")213 print("Total execution time=%.2f seconds"214 % (time.time() - start_time))215 return 0216#217# the main driver routine for the generate_distributed_NL script.218#219def main(args=None):220 #221 # Top-level command that executes everything222 #223 #224 # Import plugins225 #226 import pyomo.environ227 #228 # Parse command-line options.229 #230 try:231 options = parse_command_line(232 args,233 run_generate_distributed_NL_register_options,234 prog='generate_distributed_NL',235 description=(236"""Converts a scenario tree into multiple NL files with linking237information specified with suffixes."""238 ))239 except SystemExit as _exc:240 # the parser throws a system exit if "-h" is specified241 # - catch it to exit gracefully.242 return _exc.code243 return launch_command(run_generate_distributed_NL,244 options,245 error_label="generate_distributed_NL: ",246 disable_gc=options.disable_gc,247 profile_count=options.profile,248 traceback=options.traceback)249def generate_distributed_NL_main(args=None):250 return main(args=args)251if __name__ == "__main__":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!