本文整理汇总了Python中bag.Bag.can_take方法的典型用法代码示例。如果您正苦于以下问题:Python Bag.can_take方法的具体用法?Python Bag.can_take怎么用?Python Bag.can_take使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bag.Bag
的用法示例。
在下文中一共展示了Bag.can_take方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: randomized_select
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import can_take [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 can_take [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)
示例3: run_algorithm
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import can_take [as 别名]
def run_algorithm(self, num_solution_before_stop=100000, time_out=1000000):
"""
This is where we implement our logic and algorithms
Useful Parameters:
self.P -- max weight we can carry
self.M -- max purchasing power in dollars
self.N -- total number of avaliable items
self.C -- total number of constraints
self.items -- all items avaliable for choice
self.constraints -- a Constraint class with constraints
"""
# STEP: Create a hashmap from class number to its items
item_map = dict()
for item in self.items:
if item.classNumber not in item_map:
item_map[item.classNumber] = set()
item_map[item.classNumber].add(item)
# STEP: Calculate the total weight, cost, value, and profit of each class
def get_class_stats(items):
total_weight = 0
total_cost = 0
total_value = 0
total_profit = 0
for item in items:
total_weight += item.weight
total_cost += item.cost
total_value += item.value
total_profit += item.profit
return (total_weight, total_cost, total_value, total_profit)
class_stats = dict() # Format: key: class -> value: (weight, cost, value, profit)
for classNumber in item_map.keys():
class_stats[classNumber] = get_class_stats(item_map[classNumber])
# STEP: Create a BAG instance
bag = Bag(self.P, self.M, self.constraints)
# STEP: PriorityQueues of class's values
fn_extract_profit_per_weight_ratio = lambda x: x.profit_per_weight_ratio()
def fn_extractclass_ratio(x):
weight, _, _, profit = class_stats[x]
if weight == 0:
ratio = float("inf")
else:
ratio = profit / weight
return ratio
class_queue = PriorityQueue(lowest_priority=False) # based on class's item profit_per_weight_ratio
for classNumber in item_map.keys():
class_queue.push(classNumber, fn_extractclass_ratio(classNumber))
def add_to_queue(items, fn_extract_priority, queue):
for item in items:
priority_value = fn_extract_priority(item)
queue.push(item, -priority_value)
return queue
def get_queue_of_items(items, fn_extract_priority):
queue = PriorityQueue(lowest_priority=False)
return add_to_queue(items, fn_extract_priority, queue)
# STEP: pick from the bag with highest ratio
solutions_found = dict()
num_solution_found = 0
iteration = 0
class_not_used_due_to_conflict = Queue()
add_back_conflicts = True
while num_solution_found <= num_solution_before_stop and iteration <= time_out:
while not class_queue.isEmpty() and iteration <= time_out:
iteration += 1
if iteration % (time_out / 1000) == 0:
print("iteration {0} -- rate: {1:.2f} %".format(iteration, iteration / time_out * 100), end="\r")
if not class_not_used_due_to_conflict.isEmpty():
class_to_use = class_not_used_due_to_conflict.pop()
add_back_conflicts = not add_back_conflicts
else:
class_to_use = class_queue.pop()
add_back_conflicts = not add_back_conflicts
if bag.can_take(class_to_use):
items_queue = get_queue_of_items(item_map[class_to_use], \
fn_extract_profit_per_weight_ratio)
item = items_queue.pop()
while bag.take(item):
if not items_queue.isEmpty():
item = items_queue.pop()
else:
break
num_solution_found += 1
solutions_found[bag.score()] = bag.items()
print("solution {0} found".format(num_solution_found))
#.........这里部分代码省略.........