本文整理匯總了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)