예제 #1
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
예제 #2
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)
예제 #3
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
예제 #4
0
def exec_scenariotreeserver(options):

    mpi = None
    if options.mpi:
        import mpi4py
        # This import calls MPI_Init
        import mpi4py.MPI
        mpi = (mpi4py.MPI, mpi4py.MPI.COMM_WORLD)

    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,
                                     verbose=True)[0]

    try:
        # spawn the daemon
        pyu_pyro.TaskWorkerServer(ScenarioTreeServerPyro,
                                  host=options.pyro_host,
                                  port=options.pyro_port,
                                  verbose=options.verbose,
                                  modules_imported=modules_imported,
                                  mpi=mpi)
    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.")
        pyu_pyro.shutdown_pyro_components(host=options.pyro_host,
                                          port=options.pyro_port,
                                          num_retries=0)
        raise
예제 #5
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)
예제 #6
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)
예제 #7
0
def setUpModule():
    global reference_test_model
    reference_test_model = load_external_module(
        join(testdatadir, "reference_test_model.py"))[0].model