tree_structure.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:pyomo 作者: Pyomo 项目源码 文件源码
def create_random_bundles(self,
                              scenario_tree_instance,
                              num_bundles,
                              random_seed):

        random_state = random.getstate()
        random.seed(random_seed)
        try:
            num_scenarios = len(self._scenarios)

            sequence = list(range(num_scenarios))
            random.shuffle(sequence)

            scenario_tree_instance.Bundling[None] = True

            next_scenario_index = 0

            # this is a hack-ish way to re-initialize the Bundles set of a
            # scenario tree instance, which should already be there
            # (because it is defined in the abstract model).  however, we
            # don't have a "clear" method on a set, so...
            scenario_tree_instance.del_component("Bundles")
            scenario_tree_instance.add_component("Bundles", Set(ordered=True))
            for i in xrange(1, num_bundles+1):
                bundle_name = "Bundle"+str(i)
                scenario_tree_instance.Bundles.add(bundle_name)

            # ditto above comment regarding del_component/add_component
            scenario_tree_instance.del_component("BundleScenarios")
            scenario_tree_instance.add_component("BundleScenarios",
                                                 Set(scenario_tree_instance.Bundles,
                                                     ordered=True))

            bundles = []
            for i in xrange(num_bundles):
                bundle_name = "Bundle"+str(i+1)
                tmp = Set(ordered=True)
                tmp.construct()
                scenario_tree_instance.BundleScenarios[bundle_name] = tmp
                bundles.append(scenario_tree_instance.BundleScenarios[bundle_name])

            scenario_index = 0
            while (scenario_index < num_scenarios):
                for bundle_index in xrange(num_bundles):
                    if (scenario_index == num_scenarios):
                        break
                    bundles[bundle_index].add(
                        self._scenarios[sequence[scenario_index]]._name)
                    scenario_index += 1

            self._construct_scenario_bundles(scenario_tree_instance)
        finally:
            random.setstate(random_state)

    #
    # a utility function to pretty-print the static/non-cost
    # information associated with a scenario tree
    #
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号