def __init__(self, manager, *args, **kwds): import pyomo.solvers.plugins.smanager.pyro super(ExtensiveFormAlgorithm, self).__init__(*args, **kwds) # TODO: after PH moves over to the new code #if not isinstance(manager, ScenarioTreeManager): # raise TypeError("ExtensiveFormAlgorithm requires an instance of the " # "ScenarioTreeManager interface as the " # "second argument") if not manager.initialized: raise ValueError("ExtensiveFormAlgorithm requires a scenario tree " "manager that has been fully initialized") self._manager = manager self.instance = None self._solver_manager = None self._solver = None # The following attributes will be modified by the # solve() method. For users that are scripting, these # can be accessed after the solve() method returns. # They will be reset each time solve() is called. ############################################ self.objective = None self.gap = None self.termination_condition = None self.termination_message = None self.solver_status = None self.solution_status = None self.solver_results = None self.time = None self.pyomo_time = None ############################################ # apparently the SolverFactory does not have sane # behavior when the solver name is None if self.get_option("solver") is None: raise ValueError("The 'solver' option can not be None") self._solver = SolverFactory(self.get_option("solver"), solver_io=self.get_option("solver_io")) if isinstance(self._solver, UnknownSolver): raise ValueError("Failed to create solver of type=" + self.get_option("solver") + " for use in extensive form solve") solver_manager_type = self.get_option("solver_manager") if solver_manager_type == "phpyro": print("*** WARNING ***: PHPyro is not a supported solver " "manager type for the extensive-form solver. " "Falling back to serial.") solver_manager_type = 'serial' self._solver_manager = SolverManagerFactory( solver_manager_type, host=self.get_option("solver_manager_pyro_host"), port=self.get_option("solver_manager_pyro_port")) if self._solver_manager is None: raise ValueError("Failed to create solver manager of type=" + self.get_option("solver") + " for use in extensive form solve")
def pyomo_solver(model, solver, config, neos=False): """Solve the continuous NLP by discretizing it using backward finite difference and pyomo solvers Arguments: model {pyomo model} -- a concrete model build using Pyomo solver {string} -- Solver used for the optimization problem. For e.g. ipopt config{yaml} -- configurations of the setting neos {bool} -- Use the neos solver if True """ # time variables if model.tf < 1.0: N = config['freq'] else: N = int(round(model.tf * config['freq'])) # finite difference discretization # pyo.TransformationFactory('dae.finite_difference').apply_to(model, wrt=model.t, nfe=N, scheme='BACKWARD') # direct collocation pyo.TransformationFactory('dae.collocation').apply_to(model, wrt=model.t, nfe=N, ncp=6) # if a particular variable has to be piecewise constant then uncomment the following and replace the var parameter in 'reduce_collocation_points' # pyo.TransformationFactory('dae.collocation').apply_to(model, wrt=model.t, nf=10, ncp=6).reduce_collocation_points(model, var=model.u, ncp=1, contset=model.t) if neos: print("Using NEOS with Pyomo") SolverManagerFactory('neos').solve(model, opt=solver).write() else: pyo.SolverFactory(solver).solve(model).write() return model
def __init__(self, *args, **kwds): if self.__class__ is _ScenarioTreeManagerSolverWorker: raise NotImplementedError( "%s is an abstract class for subclassing" % self.__class__) super(_ScenarioTreeManagerSolverWorker, self).__init__(*args, **kwds) assert self.manager is not None # solver related objects self._scenario_solvers = {} self._bundle_solvers = {} self._preprocessor = None self._solver_manager = None # there are situations in which it is valuable to snapshot / # store the solutions associated with the scenario # instances. for example, when one wants to use a warm-start # from a particular iteration solve, following a modification # and re-solve of the problem instances in a user-defined # callback. the following nested dictionary is intended to # serve that purpose. The nesting is dependent on whether # bundling and or phpyro is in use self._cached_solutions = {} self._cached_scenariotree_solutions = {} # # initialize the preprocessor # self._preprocessor = None if not self.get_option("disable_advanced_preprocessing"): self._preprocessor = ScenarioTreePreprocessor( self._options, options_prefix=self._options_prefix) assert self._manager.preprocessor is None self._manager.preprocessor = self._preprocessor # # initialize the solver manager # self._solver_manager = SolverManagerFactory( self.get_option("solver_manager"), host=self.get_option('solver_manager_pyro_host'), port=self.get_option('solver_manager_pyro_port')) for scenario in self.manager.scenario_tree._scenarios: assert scenario._instance is not None solver = self._scenario_solvers[scenario.name] = \ SolverFactory(self.get_option("solver"), solver_io=self.get_option("solver_io")) if self._preprocessor is not None: self._preprocessor.add_scenario(scenario, scenario._instance, solver) for bundle in self.manager.scenario_tree._scenario_bundles: solver = self._bundle_solvers[bundle.name] = \ SolverFactory(self.get_option("solver"), solver_io=self.get_option("solver_io")) bundle_instance = \ self.manager._bundle_binding_instance_map[bundle.name] if self._preprocessor is not None: self._preprocessor.add_bundle(bundle, bundle_instance, solver)
def __init__(self, params): self.numScen = params.numScen self.mstX = params.x self.mstSita = params.sita sb_mdl = import_file("sub.py").model sub_insts = [] global directory for s in range(self.numScen): sub_path = directory + "\\data\\ScenNode" + str(s + 1) + ".dat" sub_insts.append( sb_mdl.create_instance(name="sub"+str(s+1), \ filename=sub_path)) solver_manager = SolverManagerFactory("serial") self.model = sb_mdl self.instance = sub_insts self.solver_manager = solver_manager
def run_path(data): #call solver opt = SolverFactory('cplex') solver_manager = SolverManagerFactory('pyro') #report_timing = True to see time taken for each model component inst = pmod.model.create_instance(data, report_timing=True) #fix variable I to be 0 for (h, v, t) in inst.aux: inst.I[h, v, t].fix(0) #tee=True to trace the cplex process result = opt.solve(inst, tee=True, warmstart=True) inst.solutions.load_from(result) ''' for (s,e) in inst.OD: for p in inst.P: for j in inst.N: for t in inst.TS: if inst.q[s,e,p,j,t]() > 0: print 'queue', s,e,p,j,t,inst.q[s,e,p,j,t]() ''' for (s, e) in inst.OD: for p in inst.P: for j in inst.N: for t in inst.TS: if inst.f[s, e, p, j, t]() > 0: print >> f, 'outflow', s, e, p, j, t, inst.f[s, e, p, j, t]() ''' for (s,e) in inst.OD: for p in inst.P: for j in inst.N: for t in inst.TS: for v in inst.VS: if inst.b[s,e,p,v,t,j]() > 0: print 'board', s,e,p,v,t,j,inst.b[s,e,p,v,t,j]() ''' print >> f, 'objective value', inst.obj() #result.write() inst.display() exec_time = time.time() - start_time print "execution time", exec_time, 's'
def initialize(self, scenario_tree): import pyomo.environ import pyomo.solvers.plugins.smanager.phpyro init_start_time = time.time() print("Initializing Scenario Tree Manager") print("") if scenario_tree is None: raise ValueError("A scenario tree must be supplied to the " "ScenarioTreeManager initialize() method") self._scenario_tree = scenario_tree # construct the solver manager. if self._options.verbose: print("Constructing solver manager of type=" + self._options.solver_manager_type) self._solver_manager = \ SolverManagerFactory(self._options.solver_manager_type, host=self._options.pyro_manager_hostname) if self._solver_manager is None: raise ValueError("Failed to create solver manager of " "type=" + self._options.solver_manager_type) isPHPyro = isinstance(self._solver_manager, pyomo.solvers.plugins.\ smanager.phpyro.SolverManager_PHPyro) if isPHPyro: if self._scenario_tree.contains_bundles(): num_jobs = len(self._scenario_tree._scenario_bundles) if not _OLD_OUTPUT: print("Bundle solver jobs available: " + str(num_jobs)) else: num_jobs = len(self._scenario_tree._scenarios) if not _OLD_OUTPUT: print("Scenario solver jobs available: " + str(num_jobs)) workers_expected = self._options.phpyro_required_workers if (workers_expected is None): workers_expected = num_jobs timeout = self._options.phpyro_workers_timeout if \ (self._options.phpyro_required_workers is None) else \ None self._solver_manager.acquire_workers(workers_expected, timeout) initialization_action_handles = [] if isPHPyro: if self._options.verbose: print("Broadcasting requests to initialize " "distributed scenario tree workers") initialization_action_handles.extend( scenariotreeserverutils.\ initialize_scenariotree_workers(self)) if self._options.verbose: print("Distributed scenario tree initialization " "requests successfully transmitted") else: build_start_time = time.time() if not _OLD_OUTPUT: print("Constructing scenario tree instances") self._instances = \ self._scenario_tree._scenario_instance_factory.\ construct_instances_for_scenario_tree( scenario_tree, flatten_expressions=self._options.flatten_expressions, report_timing=self._options.output_times, preprocess=False) if self._options.verbose or self._options.output_times: print("Time to construct scenario instances=%.2f seconds" % (time.time() - build_start_time)) if not _OLD_OUTPUT: print("Linking instances into scenario tree") build_start_time = time.time() # with the scenario instances now available, link the # referenced objects directly into the scenario tree. self._scenario_tree.linkInInstances( self._instances, objective_sense=self._options.objective_sense, create_variable_ids=True) if self._options.verbose or self._options.output_times: print("Time link scenario tree with instances=%.2f seconds" % (time.time() - build_start_time)) if self._scenario_tree.contains_bundles(): build_start_time = time.time() if self._options.verbose: print("Forming binding instances for all scenario bundles") self._bundle_binding_instance_map.clear() self._bundle_scenario_instance_map.clear() if not self._scenario_tree.contains_bundles(): raise RuntimeError( "Failed to create binding instances for scenario " "bundles - no scenario bundles are defined!") for scenario_bundle in self._scenario_tree._scenario_bundles: if self._options.verbose: print( "Creating binding instance for scenario bundle=%s" % (scenario_bundle._name)) self._bundle_scenario_instance_map[ scenario_bundle._name] = {} for scenario_name in scenario_bundle._scenario_names: self._bundle_scenario_instance_map[scenario_bundle._name]\ [scenario_name] = self._instances[scenario_name] # IMPORTANT: The bundle variable IDs must be idential to # those in the parent scenario tree - this is # critical for storing results, which occurs at # the full-scale scenario tree. scenario_bundle._scenario_tree.linkInInstances( self._instances, create_variable_ids=False, master_scenario_tree=self._scenario_tree, initialize_solution_data=False) bundle_ef_instance = create_ef_instance( scenario_bundle._scenario_tree, ef_instance_name=scenario_bundle._name, verbose_output=self._verbose) self._bundle_binding_instance_map[scenario_bundle._name] = \ bundle_ef_instance if self._output_times: print("Scenario bundle construction time=%.2f seconds" % (time.time() - build_start_time)) # If specified, run the user script to collect aggregate # scenario data. This can slow down PH initialization as # syncronization across all phsolverservers is required if self._options.aggregate_cfgfile is not None: callback_name = "pysp_aggregategetter_callback" if isPHPyro: # Transmit invocation to phsolverservers print("Transmitting user aggregate callback invocations " "to phsolverservers") if self._scenario_tree.contains_bundles(): for scenario_bundle in self._scenario_tree._scenario_bundles: ah = scenariotreeserverutils.\ transmit_external_function_invocation_to_worker( self, scenario_bundle._name, self._callback_module_name[callback_name], callback_name, invocation_type=(scenariotreeserverutils.InvocationType.\ PerScenarioChainedInvocation), return_action_handle=True, function_args=(self._aggregate_user_data,)) while (1): action_handle = self._solver_manager.wait_any() if action_handle in initialization_action_handles: initialization_action_handles.remove( action_handle) self._solver_manager.get_results(action_handle) elif action_handle == ah: result = self._solver_manager.get_results( action_handle) break assert len(result) == 1 self._aggregate_user_data = result[0] else: for scenario in self._scenario_tree._scenarios: ah = scenariotreeserverutils.\ transmit_external_function_invocation_to_worker( self, scenario._name, self._callback_module_name[callback_name], callback_name, invocation_type=(scenariotreeserverutils.InvocationType.\ SingleInvocation), return_action_handle=True, function_args=(self._aggregate_user_data,)) while (1): action_handle = self._solver_manager.wait_any() if action_handle in initialization_action_handles: initialization_action_handles.remove( action_handle) self._solver_manager.get_results(action_handle) elif action_handle == ah: result = self._solver_manager.get_results( action_handle) break assert len(result) == 1 self._aggregate_user_data = result[0] # Transmit final aggregate state to phsolverservers print("Broadcasting final aggregate data to phsolverservers") initialization_action_handles.extend( scenariotreeserverutils.transmit_external_function_invocation( self, "pyomo.pysp.ph", "assign_aggregate_data", invocation_type=(scenariotreeserverutils.InvocationType.\ SingleInvocation), return_action_handles=True, function_args=(self._aggregate_user_data,))) else: print("Executing user aggregate getter callback function") for scenario in self._scenario_tree._scenarios: result = self._callback_function[callback_name]( self, self._scenario_tree, scenario, self._aggregate_user_data) assert len(result) == 1 self._aggregate_user_data = result[0] # if specified, run the user script to initialize variable # bounds at their whim. if self._options.bounds_cfgfile is not None: callback_name = "pysp_boundsetter_callback" if isPHPyro: # Transmit invocation to phsolverservers print("Transmitting user bound callback invocations to " "phsolverservers") if self._scenario_tree.contains_bundles(): for scenario_bundle in self._scenario_tree._scenario_bundles: initialization_action_handles.append( scenariotreeserverutils.\ transmit_external_function_invocation_to_worker( self, scenario_bundle._name, self._callback_module_name[callback_name], callback_name, invocation_type=(scenariotreeserverutils.InvocationType.\ PerScenarioInvocation), return_action_handle=True)) else: for scenario in self._scenario_tree._scenarios: initialization_action_handles.append( scenariotreeserverutils.\ transmit_external_function_invocation_to_worker( self, scenario._name, self._callback_module_name[callback_name], callback_name, invocation_type=(scenariotreeserverutils.InvocationType.\ SingleInvocation), return_action_handle=True)) else: print("Executing user bound setter callback function") for scenario in self._scenario_tree._scenarios: self._callback_function[callback_name](self, self._scenario_tree, scenario) # gather scenario tree data if not local if isPHPyro: if self._options.verbose: print("Broadcasting requests to collect scenario tree " "instance data from PH solver servers") scenariotreeserverutils.\ gather_scenario_tree_data(self, initialization_action_handles) assert len(initialization_action_handles) == 0 if self._options.verbose: print("Scenario tree instance data successfully collected") if self._options.verbose: print("Broadcasting scenario tree id mapping" "to PH solver servers") scenariotreeserverutils.transmit_scenario_tree_ids(self) if self._options.verbose: print("Scenario tree ids successfully sent") self._objective_sense = \ self._scenario_tree._scenarios[0]._objective_sense if self._options.verbose: print("Scenario tree manager is successfully initialized") if self._options.output_times: print("Overall initialization time=%.2f seconds" % (time.time() - init_start_time)) # gather and report memory statistics (for leak # detection purposes) if specified. if (guppy_available) and (self._options.profile_memory >= 1): print(hpy().heap()) # indicate that we're ready to run. self._initialized = True
def apply_optimizer(data, instance=None): """ Perform optimization with a concrete instance Required: instance: Problem instance. Returned: results: Optimization results. opt: Optimizer object. """ # if not data.options.runtime.logging == 'quiet': sys.stdout.write('[%8.2f] Applying solver\n' % (time.time() - start_time)) sys.stdout.flush() # # # Create Solver and Perform Optimization # solver = data.options.solvers[0].solver_name if solver is None: raise ValueError("Problem constructing solver: no solver specified") if len(data.options.solvers[0].suffixes) > 0: for suffix_name in data.options.solvers[0].suffixes: if suffix_name[0] in ['"', "'"]: suffix_name = suffix_name[1:-1] # Don't redeclare the suffix if it already exists suffix = getattr(instance, suffix_name, None) if suffix is None: setattr(instance, suffix_name, Suffix(direction=Suffix.IMPORT)) else: raise ValueError("Problem declaring solver suffix %s. A component "\ "with that name already exists on model %s." % (suffix_name, instance.name)) if getattr(data.options.solvers[0].options, 'timelimit', 0) == 0: data.options.solvers[0].options.timelimit = None # # Default results # results = None # # Figure out the type of solver manager # solver_mngr_name = None if data.options.solvers[0].manager is None: solver_mngr_name = 'serial' elif not data.options.solvers[0].manager in SolverManagerFactory: raise ValueError("Unknown solver manager %s" % data.options.solvers[0].manager) else: solver_mngr_name = data.options.solvers[0].manager # # Create the solver manager # solver_mngr_kwds = {} with SolverManagerFactory(solver_mngr_name, **solver_mngr_kwds) as solver_mngr: if solver_mngr is None: msg = "Problem constructing solver manager '%s'" raise ValueError(msg % str(data.options.solvers[0].manager)) # # Setup keywords for the solve # keywords = {} if (data.options.runtime.keep_files or \ data.options.postsolve.print_logfile): keywords['keepfiles'] = True if data.options.model.symbolic_solver_labels: keywords['symbolic_solver_labels'] = True if data.options.model.file_determinism != 1: keywords['file_determinism'] = data.options.model.file_determinism keywords['tee'] = data.options.runtime.stream_output keywords['timelimit'] = getattr(data.options.solvers[0].options, 'timelimit', 0) keywords['report_timing'] = data.options.runtime.report_timing # FIXME: solver_io and executable are not being used # in the case of a non-serial solver manager # # Call the solver # if solver_mngr_name == 'serial': # # If we're running locally, then we create the # optimizer and pass it into the solver manager. # sf_kwds = {} sf_kwds['solver_io'] = data.options.solvers[0].io_format if data.options.solvers[0].solver_executable is not None: sf_kwds['executable'] = data.options.solvers[ 0].solver_executable with SolverFactory(solver, **sf_kwds) as opt: if opt is None: raise ValueError("Problem constructing solver `%s`" % str(solver)) for name in registered_callback: opt.set_callback(name, registered_callback[name]) if len(data.options.solvers[0].options) > 0: opt.set_options(data.options.solvers[0].options) #opt.set_options(" ".join("%s=%s" % (key, value) # for key, value in data.options.solvers[0].options.iteritems() # if not key == 'timelimit')) if not data.options.solvers[0].options_string is None: opt.set_options(data.options.solvers[0].options_string) # # Use the solver manager to call the optimizer # results = solver_mngr.solve(instance, opt=opt, **keywords) else: # # Get the solver option arguments # if len( data.options.solvers[0].options ) > 0 and not data.options.solvers[0].options_string is None: # If both 'options' and 'options_string' were specified, then create a # single options string that is passed to the solver. ostring = " ".join("%s=%s" % (key, value) for key, value in data.options.solvers[0].options.iteritems() if not value is None) keywords['options'] = ostring + ' ' + data.options.solvers[ 0].options_string elif len(data.options.solvers[0].options) > 0: keywords['options'] = data.options.solvers[0].options else: keywords['options'] = data.options.solvers[0].options_string # # If we're running remotely, then we pass the optimizer name to the solver # manager. # results = solver_mngr.solve(instance, opt=solver, **keywords) if data.options.runtime.profile_memory >= 1 and pympler_available: global memory_data mem_used = pympler.muppy.get_size(pympler.muppy.get_objects()) if mem_used > data.local.max_memory: data.local.max_memory = mem_used print(" Total memory = %d bytes following optimization" % mem_used) return Bunch(results=results, opt=solver, local=data.local)
# parallel.py from __future__ import division from pyomo.environ import * from pyomo.opt import SolverFactory from pyomo.opt.parallel import SolverManagerFactory import sys action_handle_map = {} # maps action handles to instances # Create a solver optsolver = SolverFactory('cplex') # create a solver manager # 'pyro' could be replaced with 'serial' solver_manager = SolverManagerFactory('pyro') if solver_manager is None: print "Failed to create solver manager." sys.exit(1) # # A simple model with binary variables and # an empty constraint list. # model = AbstractModel() model.n = Param(default=4) model.x = Var(RangeSet(model.n), within=Binary) def o_rule(model): return sum_product(model.x) model.o = Objective(rule=o_rule) model.c = ConstraintList()
def __init__(self, manager, *args, **kwds): import pysp.pyro.smanager_pyro super(ExtensiveFormAlgorithm, self).__init__(*args, **kwds) # TODO: after PH moves over to the new code #if not isinstance(manager, ScenarioTreeManager): # raise TypeError("ExtensiveFormAlgorithm requires an instance of the " # "ScenarioTreeManager interface as the " # "second argument") if not manager.initialized: raise ValueError("ExtensiveFormAlgorithm requires a scenario tree " "manager that has been fully initialized") self._manager = manager self.instance = None self._solver_manager = None self._solver = None # The following attributes will be modified by the # solve() method. For users that are scripting, these # can be accessed after the solve() method returns. # They will be reset each time solve() is called. ############################################ self.objective = undefined self.gap = undefined self.termination_condition = undefined self.solver_status = undefined self.solution_status = undefined self.solver_results = undefined self.pyomo_solve_time = undefined self.solve_time = undefined ############################################ self._solver = SolverFactory(self.get_option("solver"), solver_io=self.get_option("solver_io")) if isinstance(self._solver, UnknownSolver): raise ValueError("Failed to create solver of type="+ self.get_option("solver")+ " for use in extensive form solve") if len(self.get_option("solver_options")) > 0: if self.get_option("verbose"): print("Initializing ef solver with options=" +str(list(self.get_option("solver_options")))) self._solver.set_options("".join(self.get_option("solver_options"))) if self.get_option("mipgap") is not None: if (self.get_option("mipgap") < 0.0) or \ (self.get_option("mipgap") > 1.0): raise ValueError("Value of the mipgap parameter for the EF " "solve must be on the unit interval; " "value specified="+str(self.get_option("mipgap"))) self._solver.options.mipgap = float(self.get_option("mipgap")) solver_manager_type = self.get_option("solver_manager") if solver_manager_type == "phpyro": print("*** WARNING ***: PHPyro is not a supported solver " "manager type for the extensive-form solver. " "Falling back to serial.") solver_manager_type = 'serial' self._solver_manager = SolverManagerFactory( solver_manager_type, host=self.get_option("pyro_host"), port=self.get_option("pyro_port")) if self._solver_manager is None: raise ValueError("Failed to create solver manager of type=" +self.get_option("solver")+ " for use in extensive form solve")
# import the master a # initialize the master instance. mstr_mdl = import_file("master.py").model mstr_inst = mstr_mdl.create_instance("master.dat") # initialize the sub-problem instances. sb_mdl = import_file("sub.py").model sub_insts = [] sub_insts.append( sb_mdl.create_instance(name="sub1", \ filename="sub1.dat")) sub_insts.append( sb_mdl.create_instance(name="sub2", \ filename="sub2.dat")) # initialize the solver manager. solver_manager = SolverManagerFactory("serial") # miscellaneous initialization. mstr_inst.Min_Stage2_cost = float("-Inf") gap = float("Inf") max_iterations = 50 # main benders loop. for i in range(1, max_iterations + 1): print("\nIteration=%d" % (i)) #solve the subproblem solve_all_instances(solver_manager, 'cplex', sub_insts) for instance in sub_insts: print("cost for scenario="+instance.name+" is"+\
def BenderAlgorithmBuilder(options, scenario_tree): import pyomo.environ import pyomo.solvers.plugins.smanager.phpyro import pyomo.solvers.plugins.smanager.pyro solution_writer_plugins = ExtensionPoint(ISolutionWriterExtension) for plugin in solution_writer_plugins: plugin.disable() solution_plugins = [] if len(options.solution_writer) > 0: for this_extension in options.solution_writer: if this_extension in sys.modules: print("User-defined PH solution writer module=" + this_extension + " already imported - skipping") else: print("Trying to import user-defined PH " "solution writer module=" + this_extension) # make sure "." is in the PATH. original_path = list(sys.path) sys.path.insert(0, '.') import_file(this_extension) print("Module successfully loaded") sys.path[:] = original_path # restore to what it was # now that we're sure the module is loaded, re-enable this # specific plugin. recall that all plugins are disabled # by default in phinit.py, for various reasons. if we want # them to be picked up, we need to enable them explicitly. import inspect module_to_find = this_extension if module_to_find.rfind(".py"): module_to_find = module_to_find.rstrip(".py") if module_to_find.find("/") != -1: module_to_find = module_to_find.split("/")[-1] for name, obj in inspect.getmembers(sys.modules[module_to_find], inspect.isclass): import pyomo.common # the second condition gets around goofyness related # to issubclass returning True when the obj is the # same as the test class. if issubclass(obj, pyomo.common.plugin.SingletonPlugin ) and name != "SingletonPlugin": for plugin in solution_writer_plugins(all=True): if isinstance(plugin, obj): plugin.enable() solution_plugins.append(plugin) # # if any of the ww extension configuration options are specified # without the ww extension itself being enabled, halt and warn the # user - this has led to confusion in the past, and will save user # support time. # if (len(options.ww_extension_cfgfile) > 0) and \ (options.enable_ww_extensions is False): raise ValueError("A configuration file was specified " "for the WW extension module, but the WW extensions " "are not enabled!") if (len(options.ww_extension_suffixfile) > 0) and \ (options.enable_ww_extensions is False): raise ValueError("A suffix file was specified for the WW " "extension module, but the WW extensions are not " "enabled!") if (len(options.ww_extension_annotationfile) > 0) and \ (options.enable_ww_extensions is False): raise ValueError("A annotation file was specified for the " "WW extension module, but the WW extensions are not " "enabled!") # # disable all plugins up-front. then, enable them on an as-needed # basis later in this function. the reason that plugins should be # disabled is that they may have been programmatically enabled in # a previous run of PH, and we want to start from a clean slate. # ph_extension_point = ExtensionPoint(IPHExtension) for plugin in ph_extension_point: plugin.disable() ph_plugins = [] # # deal with any plugins. ww extension comes first currently, # followed by an option user-defined plugin. order only matters # if both are specified. # if options.enable_ww_extensions: import pyomo.pysp.plugins.wwphextension # explicitly enable the WW extension plugin - it may have been # previously loaded and/or enabled. ph_extension_point = ExtensionPoint(IPHExtension) for plugin in ph_extension_point(all=True): if isinstance(plugin, pyomo.pysp.plugins.wwphextension.wwphextension): plugin.enable() ph_plugins.append(plugin) # there is no reset-style method for plugins in general, # or the ww ph extension in plugin in particular. if no # configuration or suffix filename is specified, set to # None so that remnants from the previous use of the # plugin aren't picked up. if len(options.ww_extension_cfgfile) > 0: plugin._configuration_filename = options.ww_extension_cfgfile else: plugin._configuration_filename = None if len(options.ww_extension_suffixfile) > 0: plugin._suffix_filename = options.ww_extension_suffixfile else: plugin._suffix_filename = None if len(options.ww_extension_annotationfile) > 0: plugin._annotation_filename = options.ww_extension_annotationfile else: plugin._annotation_filename = None if len(options.user_defined_extensions) > 0: for this_extension in options.user_defined_extensions: if this_extension in sys.modules: print("User-defined PH extension module=" + this_extension + " already imported - skipping") else: print("Trying to import user-defined PH extension module=" + this_extension) # make sure "." is in the PATH. original_path = list(sys.path) sys.path.insert(0, '.') import_file(this_extension) print("Module successfully loaded") # restore to what it was sys.path[:] = original_path # now that we're sure the module is loaded, re-enable this # specific plugin. recall that all plugins are disabled # by default in phinit.py, for various reasons. if we want # them to be picked up, we need to enable them explicitly. import inspect module_to_find = this_extension if module_to_find.rfind(".py"): module_to_find = module_to_find.rstrip(".py") if module_to_find.find("/") != -1: module_to_find = module_to_find.split("/")[-1] for name, obj in inspect.getmembers(sys.modules[module_to_find], inspect.isclass): import pyomo.common # the second condition gets around goofyness related # to issubclass returning True when the obj is the # same as the test class. if issubclass(obj, pyomo.common.plugin.SingletonPlugin ) and name != "SingletonPlugin": ph_extension_point = ExtensionPoint(IPHExtension) for plugin in ph_extension_point(all=True): if isinstance(plugin, obj): plugin.enable() ph_plugins.append(plugin) ph = None solver_manager = None try: # construct the solver manager. if options.verbose: print("Constructing solver manager of type=" + options.solver_manager_type) solver_manager = SolverManagerFactory(options.solver_manager_type, host=options.pyro_host, port=options.pyro_port) if solver_manager is None: raise ValueError("Failed to create solver manager of " "type=" + options.solver_manager_type + " specified in call to PH constructor") ph = ProgressiveHedging(options) if isinstance( solver_manager, pyomo.solvers.plugins.smanager.phpyro.SolverManager_PHPyro): if scenario_tree.contains_bundles(): num_jobs = len(scenario_tree._scenario_bundles) if not _OLD_OUTPUT: print("Bundle solver jobs available: " + str(num_jobs)) else: num_jobs = len(scenario_tree._scenarios) if not _OLD_OUTPUT: print("Scenario solver jobs available: " + str(num_jobs)) servers_expected = options.phpyro_required_workers if (servers_expected is None): servers_expected = num_jobs timeout = options.phpyro_workers_timeout if \ (options.phpyro_required_workers is None) else \ None solver_manager.acquire_servers(servers_expected, timeout) ph.initialize(scenario_tree=scenario_tree, solver_manager=solver_manager, ph_plugins=ph_plugins, solution_plugins=solution_plugins) except: if ph is not None: ph.release_components() if solver_manager is not None: if isinstance( solver_manager, pyomo.solvers.plugins.smanager.phpyro. SolverManager_PHPyro): solver_manager.release_servers( shutdown=ph._shutdown_pyro_workers) elif isinstance( solver_manager, pyomo.solvers.plugins.smanager.pyro.SolverManager_Pyro): if ph._shutdown_pyro_workers: solver_manager.shutdown_workers() print("Failed to initialize progressive hedging algorithm") raise return ph
def EFAlgorithmBuilder(options, scenario_tree): solution_writer_plugins = ExtensionPoint(ISolutionWriterExtension) for plugin in solution_writer_plugins: plugin.disable() solution_plugins = [] if len(options.solution_writer) > 0: for this_extension in options.solution_writer: if this_extension in sys.modules: print("User-defined EF solution writer module=" +this_extension+" already imported - skipping") else: print("Trying to import user-defined EF " "solution writer module="+this_extension) # make sure "." is in the PATH. original_path = list(sys.path) sys.path.insert(0,'.') pyutilib.misc.import_file(this_extension) print("Module successfully loaded") sys.path[:] = original_path # restore to what it was # now that we're sure the module is loaded, re-enable this # specific plugin. recall that all plugins are disabled # by default in phinit.py, for various reasons. if we want # them to be picked up, we need to enable them explicitly. import inspect module_to_find = this_extension if module_to_find.rfind(".py"): module_to_find = module_to_find.rstrip(".py") if module_to_find.find("/") != -1: module_to_find = module_to_find.split("/")[-1] for name, obj in inspect.getmembers(sys.modules[module_to_find], inspect.isclass): import pyomo.util # the second condition gets around goofyness related to issubclass returning # True when the obj is the same as the test class. if issubclass(obj, pyomo.util.plugin.SingletonPlugin) and name != "SingletonPlugin": for plugin in solution_writer_plugins(all=True): if isinstance(plugin, obj): plugin.enable() solution_plugins.append(plugin) ef_solver = SolverFactory(options.solver_type, solver_io=options.solver_io) if isinstance(ef_solver, UnknownSolver): raise ValueError("Failed to create solver of type="+ options.solver_type+ " for use in extensive form solve") if len(options.solver_options) > 0: print("Initializing ef solver with options=" +str(options.solver_options)) ef_solver.set_options("".join(options.solver_options)) if options.mipgap is not None: if (options.mipgap < 0.0) or (options.mipgap > 1.0): raise ValueError("Value of the mipgap parameter for the EF " "solve must be on the unit interval; " "value specified="+str(options.mipgap)) ef_solver.options.mipgap = float(options.mipgap) ef_solver_manager = SolverManagerFactory(options.solver_manager_type, host=options.pyro_host, port=options.pyro_port) if ef_solver_manager is None: raise ValueError("Failed to create solver manager of type=" +options.solver_type+ " for use in extensive form solve") binding_instance = CreateExtensiveFormInstance(options, scenario_tree) ef = ExtensiveFormAlgorithm(options, binding_instance, scenario_tree, ef_solver_manager, ef_solver, solution_plugins=solution_plugins) return ef