Example #1
0
def exec_scenariotreeserver(options):

    modules_imported = {}
    for module_name in options.import_module:
        if module_name in sys.modules:
            modules_imported[module_name] = sys.modules[module_name]
        else:
            modules_imported[module_name] = \
                load_external_module(module_name, clear_cache=True)[0]

    try:
        # spawn the daemon
        TaskWorkerServer(ScenarioTreeServerPyro,
                         host=options.pyro_host,
                         port=options.pyro_port,
                         verbose=options.verbose,
                         modules_imported=modules_imported)
    except:
        # if an exception occurred, then we probably want to shut down
        # all Pyro components.  otherwise, the PH client may have
        # forever while waiting for results that will never
        # arrive. there are better ways to handle this at the PH
        # client level, but until those are implemented, this will
        # suffice for cleanup.
        #NOTE: this should perhaps be command-line driven, so it can
        #      be disabled if desired.
        print("ScenarioTreeServerPyro aborted. Sending shutdown request.")
        shutdown_pyro_components(host=options.pyro_host,
                                 port=options.pyro_port,
                                 num_retries=0)
        raise
Example #2
0
def _find_reference_model_or_callback(src):
    """Tries to find a single reference model or callback for
    generating scenario models."""
    module, _ = load_external_module(src, clear_cache=True)
    reference_model = None
    callback = None
    dir_module = dir(module)
    if "pysp_instance_creation_callback" in dir_module:
        callback = getattr(module, "pysp_instance_creation_callback")
        if not hasattr(callback, "__call__"):
            raise TypeError("'pysp_instance_creation_callback' "
                            "object found in source '%s' is not "
                            "callable" % (src))
    else:
        matching_names = []
        for attr_name in dir_module:
            obj = getattr(module, attr_name)
            if isinstance(obj, (_BlockData, Block)):
                reference_model = obj
                matching_names.append(attr_name)
        if len(matching_names) > 1:
            raise ValueError("Multiple objects found in source '%s' "
                             "that could be a reference model. Make "
                             "sure there is only one Pyomo model in "
                             "the source file. Object names: %s" %
                             (str(matching_names)))

    return module, reference_model, callback
Example #3
0
    def test_init18(self):
        self.assertTrue("reference_test_model" not in sys.modules)
        self.assertTrue("ScenarioStructure" not in sys.modules)
        nx_tree = load_external_module(
            os.path.join(testdatadir, "ScenarioStructure.py"))[0].G

        def scenario_model_callback(scenario_tree, scenario_name, node_list):
            self.assertIs(scenario_tree, nx_tree)
            instance = reference_test_model.create_instance()
            if scenario_name == "s1":
                instance.p = 1.0
            elif scenario_name == "s2":
                instance.p = 2.0
            else:
                assert scenario_name == "s3"
                instance.p = 3.0
            return instance

        with ScenarioTreeInstanceFactory(model=scenario_model_callback,
                                         scenario_tree=nx_tree) as factory:
            self.assertTrue(factory.model_directory() is None)
            self.assertTrue(factory.scenario_tree_directory() is None)
            self._check_factory(factory)
        self.assertEqual(factory._closed, True)
        self.assertEqual(len(factory._archives), 0)
Example #4
0
def _find_reference_model_or_callback(src):
    """Tries to find a single reference model or callback for
    generating scenario models."""
    module, _ = load_external_module(src, clear_cache=True)
    reference_model = None
    callback = None
    dir_module = dir(module)
    if "pysp_instance_creation_callback" in dir_module:
        callback = getattr(module, "pysp_instance_creation_callback")
        if not hasattr(callback,"__call__"):
            raise TypeError("'pysp_instance_creation_callback' "
                            "object found in source '%s' is not "
                            "callable" % (src))
    else:
        matching_names = []
        for attr_name in dir_module:
            obj = getattr(module, attr_name)
            if isinstance(obj, (_BlockData, Block)):
                reference_model = obj
                matching_names.append(attr_name)
        if len(matching_names) > 1:
            raise ValueError("Multiple objects found in source '%s' "
                             "that could be a reference model. Make "
                             "sure there is only one Pyomo model in "
                             "the source file. Object names: %s"
                             % (str(matching_names)))

    return module, reference_model, callback
Example #5
0
def exec_scenariotreeserver(options):

    modules_imported = {}
    for module_name in options.import_module:
        if module_name in sys.modules:
            modules_imported[module_name] = sys.modules[module_name]
        else:
            modules_imported[module_name] = \
                load_external_module(module_name, clear_cache=True)[0]

    try:
        # spawn the daemon
        TaskWorkerServer(ScenarioTreeServerPyro,
                         host=options.pyro_host,
                         port=options.pyro_port,
                         verbose=options.verbose,
                         modules_imported=modules_imported)
    except:
        # if an exception occurred, then we probably want to shut down
        # all Pyro components.  otherwise, the PH client may have
        # forever while waiting for results that will never
        # arrive. there are better ways to handle this at the PH
        # client level, but until those are implemented, this will
        # suffice for cleanup.
        #NOTE: this should perhaps be command-line driven, so it can
        #      be disabled if desired.
        print("ScenarioTreeServerPyro aborted. Sending shutdown request.")
        shutdown_pyro_components(host=options.pyro_host,
                                 port=options.pyro_port,
                                 num_retries=0)
        raise
    def _import_model_and_scenario_tree(self):

        model_import, module_name = \
            load_external_module(self._model_filename,
                                 clear_cache=True)
        self._model_module = model_import
        dir_model_import = dir(model_import)
        self._model_object = None
        self._model_callback = None
        if "pysp_instance_creation_callback" in dir_model_import:
            callback = model_import.pysp_instance_creation_callback
            if not hasattr(callback, "__call__"):
                raise TypeError("'pysp_instance_creation_callback' object is "
                                "not callable in model file: %s" %
                                (self._model_filename))
            self._model_callback = callback
        elif "model" in dir_model_import:
            model = model_import.model
            if not isinstance(model, (_BlockData, Block)):
                raise TypeError("'model' object has incorrect type "
                                "in model file: %s" % (self._model_filename))
            self._model_object = model
        else:
            raise AttributeError(
                "No 'model' or 'pysp_instance_creation_callback' "
                "object found in model file: %s" % (self._model_filename))

        if self._scenario_tree_filename is None:
            assert self._scenario_tree_location is None
            assert self._scenario_tree_directory is None
            if self._model_object is not None:
                self._data_directory = self._model_directory
            if "pysp_scenario_tree_model_callback" in dir_model_import:
                callback = model_import.pysp_scenario_tree_model_callback
                if not hasattr(callback, "__call__"):
                    raise TypeError(
                        "'pysp_scenario_tree_model_callback' object is "
                        "not callable in model file: %s" %
                        (self._model_filename))
                self._scenario_tree_instance = callback()
                if not isinstance(self._scenario_tree_instance,
                                  (_BlockData, Block)):
                    raise TypeError(
                        "'pysp_scenario_tree_model_callback' returned "
                        "an object that is not of the correct type for "
                        "a Pyomo model (e.g, _BockData, Block): %s" %
                        (type(self._scenario_tree_instance)))
            else:
                raise ValueError(
                    "No scenario tree file was given but no function "
                    "named 'pysp_scenario_tree_model_callback' was "
                    "found in the reference model file.")
        else:
            self._data_directory = self._scenario_tree_directory
            self._scenario_tree_instance = \
                CreateAbstractScenarioTreeModel().\
                create_instance(filename=self._scenario_tree_filename)
Example #7
0
    def _import_model_and_scenario_tree(self):

        model_import, module_name = load_external_module(self._model_filename, clear_cache=True)
        self._model_module = model_import
        dir_model_import = dir(model_import)
        self._model_object = None
        self._model_callback = None
        if "pysp_instance_creation_callback" in dir_model_import:
            callback = model_import.pysp_instance_creation_callback
            if not hasattr(callback,"__call__"):
                raise TypeError(
                    "'pysp_instance_creation_callback' object is "
                    "not callable in model file: %s"
                    % (self._model_filename))
            self._model_callback = callback
        elif "model" in dir_model_import:
            model = model_import.model
            if not isinstance(model,(_BlockData, Block)):
                raise TypeError(
                    "'model' object has incorrect type "
                    "in model file: %s"
                    % (self._model_filename))
            self._model_object = model
        else:
            raise AttributeError(
                "No 'model' or 'pysp_instance_creation_callback' "
                "object found in model file: %s"
                % (self._model_filename))

        if self._scenario_tree_filename is None:
            assert self._scenario_tree_location is None
            if "pysp_scenario_tree_model_callback" in dir_model_import:
                callback = model_import.pysp_scenario_tree_model_callback
                if not hasattr(callback,"__call__"):
                    raise TypeError(
                        "'pysp_scenario_tree_model_callback' object is "
                        "not callable in model file: %s"
                        % (self._model_filename))
                self._scenario_tree_instance = callback()
                if not isinstance(self._scenario_tree_instance,
                                  (_BlockData, Block)):
                    raise TypeError(
                        "'pysp_scenario_tree_model_callback' returned "
                        "an object that is not of the correct type for "
                        "a Pyomo model (e.g, _BockData, Block): %s"
                        % (type(self._scenario_tree_instance)))
            else:
                raise ValueError(
                    "No scenario tree file was given but no function "
                    "named 'pysp_scenario_tree_model_callback' was "
                    "found in the model file.")
        else:
            self._scenario_tree_instance = \
                CreateAbstractScenarioTreeModel().\
                create_instance(filename=self._scenario_tree_filename)
Example #8
0
 def test_init16(self):
     self.assertTrue("reference_test_model" not in sys.modules)
     self.assertTrue("ScenarioStructure" not in sys.modules)
     nx_tree = load_external_module(
         os.path.join(testdatadir, "ScenarioStructure.py"))[0].G
     with ScenarioTreeInstanceFactory(model=join(testdatadir,
                                                 "reference_test_model.py"),
                                      scenario_tree=nx_tree) as factory:
         self.assertEqual(len(factory._archives), 0)
         self.assertTrue(factory.model_directory() is not None)
         self.assertTrue(factory.scenario_tree_directory() is None)
         self._check_factory(factory)
     self.assertTrue("reference_test_model" in sys.modules)
     self.assertTrue("ScenarioStructure" in sys.modules)
Example #9
0
 def test_init16(self):
     self.assertTrue("reference_test_model" not in sys.modules)
     self.assertTrue("ScenarioStructure" not in sys.modules)
     nx_tree = load_external_module(os.path.join(testdatadir,
                                                 "ScenarioStructure.py"))[0].G
     with ScenarioTreeInstanceFactory(
             model=join(testdatadir,
                        "reference_test_model.py"),
             scenario_tree=nx_tree) as factory:
         self.assertEqual(len(factory._archives), 0)
         self.assertTrue(factory.model_directory() is not None)
         self.assertTrue(factory.scenario_tree_directory() is None)
         self._check_factory(factory)
     self.assertTrue("reference_test_model" in sys.modules)
     self.assertTrue("ScenarioStructure" in sys.modules)
Example #10
0
def _find_scenariotree(src=None, module=None):
    """Tries to find a single reference model or callback for
    generating scenario models."""
    if module is None:
        assert src is not None
        module, _ = load_external_module(src, clear_cache=True)
    else:
        assert src is None
    scenario_tree_object = None
    scenario_tree_model = None
    callback = None
    dir_module = dir(module)
    if "pysp_scenario_tree_model_callback" in dir_module:
        callback = getattr(module, "pysp_scenario_tree_model_callback")
        if not hasattr(callback,"__call__"):
            raise TypeError("'pysp_scenario_tree_model_callback' "
                            "object found in source '%s' is not "
                            "callable" % (src))
        attrs = [(None, callback())]
    else:
        attrs = [(attr_name,getattr(module, attr_name))
                 for attr_name in dir_module]
    matching_names = []
    for attr_name, obj in attrs:
        if isinstance(obj, ScenarioTree):
            scenario_tree_object = obj
            matching_names.append(attr_name)
        elif isinstance(obj, (_BlockData, Block)):
            scenario_tree_model = obj
            matching_names.append(attr_name)
        elif has_networkx and \
             isinstance(obj, networkx.DiGraph):
            scenario_tree_model = obj
            matching_names.append(attr_name)
    if len(matching_names) > 1:
        raise ValueError("Multiple objects found in source '%s' "
                         "that could act as a scenario tree "
                         "specification. Make sure there is only "
                         "one Pyomo model, ScenarioTree, or "
                         "networkx.DiGraph object in the source "
                         "file. Object names: %s"
                         % (str(matching_names)))

    return module, scenario_tree_object, scenario_tree_model
Example #11
0
def _find_scenariotree(src=None, module=None):
    """Tries to find a single reference model or callback for
    generating scenario models."""
    if module is None:
        assert src is not None
        module, _ = load_external_module(src, clear_cache=True)
    else:
        assert src is None
    scenario_tree_object = None
    scenario_tree_model = None
    callback = None
    dir_module = dir(module)
    if "pysp_scenario_tree_model_callback" in dir_module:
        callback = getattr(module, "pysp_scenario_tree_model_callback")
        if not hasattr(callback,"__call__"):
            raise TypeError("'pysp_scenario_tree_model_callback' "
                            "object found in source '%s' is not "
                            "callable" % (src))
        attrs = [(None, callback())]
    else:
        attrs = [(attr_name,getattr(module, attr_name))
                 for attr_name in dir_module]
    matching_names = []
    for attr_name, obj in attrs:
        if isinstance(obj, ScenarioTree):
            scenario_tree_object = obj
            matching_names.append(attr_name)
        elif isinstance(obj, (_BlockData, Block)):
            scenario_tree_model = obj
            matching_names.append(attr_name)
        elif has_networkx and \
             isinstance(obj, networkx.DiGraph):
            scenario_tree_model = obj
            matching_names.append(attr_name)
    if len(matching_names) > 1:
        raise ValueError("Multiple objects found in source '%s' "
                         "that could act as a scenario tree "
                         "specification. Make sure there is only "
                         "one Pyomo model, ScenarioTree, or "
                         "networkx.DiGraph object in the source "
                         "file. Object names: %s"
                         % (str(matching_names)))

    return module, scenario_tree_object, scenario_tree_model
Example #12
0
    def test_init17(self):
        self.assertTrue("reference_test_model" not in sys.modules)
        self.assertTrue("ScenarioStructure" not in sys.modules)
        nx_tree = load_external_module(os.path.join(testdatadir,
                                                    "ScenarioStructure.py"))[0].G
        with ScenarioTreeInstanceFactory(
                model=join(testdatadir,
                           "reference_test_model.py"),
                scenario_tree=nx_tree) as factory:
            self.assertEqual(len(factory._archives), 0)
            self.assertTrue(factory.model_directory() is not None)
            self.assertTrue(factory.scenario_tree_directory() is None)
            self._check_factory(factory)

            scenario_tree = factory.generate_scenario_tree()
            self.assertEqual(scenario_tree.contains_bundles(), False)
            # check that we can modify the networkx tree to redefine
            # bundles
            nx_tree.node["s1"]["bundle"] = 0
            nx_tree.node["s2"]["bundle"] = 0
            nx_tree.node["s3"]["bundle"] = 0
            scenario_tree = factory.generate_scenario_tree()
            self.assertEqual(scenario_tree.contains_bundles(), True)
            self.assertEqual(len(scenario_tree.bundles), 1)
            nx_tree.node["s1"]["bundle"] = 0
            nx_tree.node["s2"]["bundle"] = 1
            nx_tree.node["s3"]["bundle"] = 2
            scenario_tree = factory.generate_scenario_tree()
            self.assertEqual(scenario_tree.contains_bundles(), True)
            self.assertEqual(len(scenario_tree.bundles), 3)
            nx_tree.node["s1"]["bundle"] = None
            nx_tree.node["s2"]["bundle"] = None
            nx_tree.node["s3"]["bundle"] = None
            scenario_tree = factory.generate_scenario_tree()
            self.assertEqual(scenario_tree.contains_bundles(), False)

        self.assertTrue("reference_test_model" in sys.modules)
        self.assertTrue("ScenarioStructure" in sys.modules)
def _import_model_or_callback(src, callback_name):
    """ Try to import a Pyomo model or callback.

    Args:
        src: The python file, module name, or module to
            search. When the argument is a file or module
            name, it will force the import whether or not
            this overwrites an existing imported module.
        callback_name: The name of a callback to search for
            before looking for a model object.

    Returns:
        A tuple consisting of a reference to the imported
        module, the model object possibly found in this
        module, and the callback possibly found in this
        module. If the callback is found, the model object
        will be None. Otherwise, the callback will be None.
        If neither is found, they will both be None.
    """

    module, _ = load_external_module(src, clear_cache=True)
    model = None
    callback = None
    dir_module = dir(module)
    if callback_name in dir_module:
        callback = getattr(module, callback_name)
        if not hasattr(callback,"__call__"):
            raise TypeError(
                "'%s' object found in src '%s' is not callable"
                % (callback_name, src))
    elif "model" in dir_module:
        model = module.model
        if not isinstance(model, (_BlockData, Block)):
            raise TypeError(
                "'model' object found in src '%s' has "
                "incorrect type:" % (src))

    return module, model, callback
Example #14
0
    def test_init17(self):
        self.assertTrue("reference_test_model" not in sys.modules)
        self.assertTrue("ScenarioStructure" not in sys.modules)
        nx_tree = load_external_module(os.path.join(testdatadir,
                                                    "ScenarioStructure.py"))[0].G
        with ScenarioTreeInstanceFactory(
                model=join(testdatadir,
                           "reference_test_model.py"),
                scenario_tree=nx_tree) as factory:
            self.assertEqual(len(factory._archives), 0)
            self.assertTrue(factory.model_directory() is not None)
            self.assertTrue(factory.scenario_tree_directory() is None)
            self._check_factory(factory)

            scenario_tree = factory.generate_scenario_tree()
            self.assertEqual(scenario_tree.contains_bundles(), False)
            # check that we can modify the networkx tree to redefine
            # bundles
            nx_tree.nodes["s1"]["bundle"] = 0
            nx_tree.nodes["s2"]["bundle"] = 0
            nx_tree.nodes["s3"]["bundle"] = 0
            scenario_tree = factory.generate_scenario_tree()
            self.assertEqual(scenario_tree.contains_bundles(), True)
            self.assertEqual(len(scenario_tree.bundles), 1)
            nx_tree.nodes["s1"]["bundle"] = 0
            nx_tree.nodes["s2"]["bundle"] = 1
            nx_tree.nodes["s3"]["bundle"] = 2
            scenario_tree = factory.generate_scenario_tree()
            self.assertEqual(scenario_tree.contains_bundles(), True)
            self.assertEqual(len(scenario_tree.bundles), 3)
            nx_tree.nodes["s1"]["bundle"] = None
            nx_tree.nodes["s2"]["bundle"] = None
            nx_tree.nodes["s3"]["bundle"] = None
            scenario_tree = factory.generate_scenario_tree()
            self.assertEqual(scenario_tree.contains_bundles(), False)

        self.assertTrue("reference_test_model" in sys.modules)
        self.assertTrue("ScenarioStructure" in sys.modules)
Example #15
0
 def test_init18(self):
     self.assertTrue("reference_test_model" not in sys.modules)
     self.assertTrue("ScenarioStructure" not in sys.modules)
     nx_tree = load_external_module(os.path.join(testdatadir,
                                                 "ScenarioStructure.py"))[0].G
     def scenario_model_callback(scenario_tree, scenario_name, node_list):
         self.assertIs(scenario_tree, nx_tree)
         instance = reference_test_model.create_instance()
         if scenario_name == "s1":
             instance.p = 1.0
         elif scenario_name == "s2":
             instance.p = 2.0
         else:
             assert scenario_name == "s3"
             instance.p = 3.0
         return instance
     with ScenarioTreeInstanceFactory(
             model=scenario_model_callback,
             scenario_tree=nx_tree) as factory:
         self.assertTrue(factory.model_directory() is None)
         self.assertTrue(factory.scenario_tree_directory() is None)
         self._check_factory(factory)
     self.assertEqual(factory._closed, True)
     self.assertEqual(len(factory._archives), 0)
Example #16
0
def setUpModule():
    global reference_test_model
    reference_test_model = load_external_module(
        join(testdatadir, "reference_test_model.py"))[0].model
def setUpModule():
    global reference_test_model
    reference_test_model = load_external_module(
        join(testdatadir, "reference_test_model.py"))[0].model