当前位置: 首页>>代码示例>>Python>>正文


Python Bag.has_enough_weight_for方法代码示例

本文整理汇总了Python中bag.Bag.has_enough_weight_for方法的典型用法代码示例。如果您正苦于以下问题:Python Bag.has_enough_weight_for方法的具体用法?Python Bag.has_enough_weight_for怎么用?Python Bag.has_enough_weight_for使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bag.Bag的用法示例。


在下文中一共展示了Bag.has_enough_weight_for方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: randomized_select

# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import has_enough_weight_for [as 别名]
    def randomized_select(self, select_range, priority_fn, lowestPriority=False):
        solution = list()
        total_profit = 0
        bag = Bag(self.P, self.M, self.constraints)
        options = list()
        for item in self.items:
            options.append((item, priority_fn(item)))
        options.sort(key=lambda x: -x[1])
        if lowestPriority:
            options = list(reversed(options))
        options = [x[0] for x in options]

        while len(options) > 0 and not bag.full():
            items = options[:min(select_range, len(options))]
            item = items[int(random() * len(items))]
            options.remove(item)
            print(" progress: {0:.2f} %".format(max(bag._weight / bag.P, bag._cost/bag.M) * 100), end="\r")
            if bag.has_enough_weight_for(item.weight) and bag.has_enough_money_for(item.cost):
                bag.take(item)
                solution.append(item)
                total_profit += item.profit
                new_options = list()
                for x in options:
                    if bag.can_take(x.classNumber):
                        new_options.append(x)
                options = new_options
        return (total_profit, solution)
开发者ID:Michael-Tu,项目名称:ClassWork,代码行数:29,代码来源:solver.py

示例2: greedy_on_fn

# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import has_enough_weight_for [as 别名]
 def greedy_on_fn(self, priority_fn, lowestPriority=False):
     solution = list()
     total_profit = 0
     bag = Bag(self.P, self.M, self.constraints)
     queue = get_priority_queue(self.items, priority_fn, lowestPriority)
     
     while not queue.isEmpty() and not bag.full():
         item = queue.pop()
         #print(" remaining items: {0:.2f}%".format(queue.count / self.N * 100), end="\r")
         print(" progress: {0:.2f} %".format(max(bag._weight / bag.P, bag._cost/bag.M) * 100), end="\r")
         if bag.has_enough_weight_for(item.weight) and bag.has_enough_money_for(item.cost):
             if bag.can_take(item.classNumber):
                 solution.append(item)
                 bag.take(item)
                 total_profit += item.profit
     return (total_profit, solution)
开发者ID:Michael-Tu,项目名称:ClassWork,代码行数:18,代码来源:solver.py


注:本文中的bag.Bag.has_enough_weight_for方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。