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