Esempio n. 1
0
    def pyro_sample_sp(self, size, **kwds):
        assert size > 0
        model = self.reference_model.clone()

        scenario_tree_model = \
            self._create_scenario_tree_model(size)
        factory = ScenarioTreeInstanceFactory(
            model=self.reference_model, scenario_tree=scenario_tree_model)
        options = \
            ScenarioTreeManagerClientPyro.register_options()
        for key in kwds:
            options[key] = kwds[key]
        manager = ScenarioTreeManagerClientPyro(options, factory=factory)
        try:
            init = manager.initialize(async_call=True)
            pcuids = ComponentMap()
            for param in self.stochastic_data:
                pcuids[param] = ComponentUID(param)
            init.complete()
            for scenario in manager.scenario_tree.scenarios:
                data = []
                for param, dist in self.stochastic_data.items():
                    data.append((pcuids[param], dist.sample()))
                manager.invoke_function(
                    "_update_data",
                    thisfile,
                    invocation_type=InvocationType.OnScenario(scenario.name),
                    function_args=(data, ),
                    oneway_call=True)
            manager.reference_model = model
        except:
            manager.close()
            raise
        return manager
 def _init(self, factory):
     _setUpPyro()
     [_poll(proc) for proc in _taskworker_processes]
     options = ScenarioTreeManagerClientPyro.register_options()
     options.pyro_port = _pyomo_ns_port
     options.pyro_required_scenariotreeservers = 3
     options.pyro_handshake_at_startup = True
     sp = ScenarioTreeManagerClientPyro(options, factory=factory)
     sp.initialize()
     return sp
    #
    # Pyro-specific options
    #
    options.pyro_host = 'localhost'
    # we allow this option to be overridden from the
    # command line for Pyomo testing purposes
    options.pyro_port = \
        None if (len(sys.argv) == 1) else int(sys.argv[1])
    # set this option to the number of scenario tree
    # servers currently running
    # Note: it can be fewer than the number of scenarios
    options.pyro_required_scenariotreeservers = 3
    # Shutdown all pyro-related components when the scenario
    # tree manager closes. Note that with Pyro4, the nameserver
    # must be shutdown manually.
    options.pyro_shutdown = True

    # using the 'with' block will automatically call
    # manager.close() and gracefully shutdown the
    # scenario tree servers
    with ScenarioTreeManagerClientPyro(options) as manager:
        manager.initialize()

        results = manager.invoke_function(
            "solve_model",  # function to execute
            thisfile,  # file (or module) containing the function
            invocation_type=InvocationType.PerScenario)

        for scenario_name in sorted(results):
            print(scenario_name + ": " + str(results[scenario_name]))