本文整理汇总了Python中bag.Bag.has_enough_money_for方法的典型用法代码示例。如果您正苦于以下问题:Python Bag.has_enough_money_for方法的具体用法?Python Bag.has_enough_money_for怎么用?Python Bag.has_enough_money_for使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bag.Bag
的用法示例。
在下文中一共展示了Bag.has_enough_money_for方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: randomized_select
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import has_enough_money_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)
示例2: greedy_on_fn
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import has_enough_money_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)