【发布时间】:2020-08-15 16:43:30
【问题描述】:
我又花了几个小时在一个所谓的“简单”练习题上,并认为我可以提出一个更有力的问题。希望得到反馈。
问题依旧:
您需要安排一张办公桌,以便一个人始终在那里,连续 24 小时。你可以雇佣 6 个人来做这件事,但你想把成本降到最低。任何工作 8 小时的人每小时赚 20 美元。可以使用什么贪心算法来最小化成本? (即并确定是否雇用 2 人每人 12 小时、1 人 24 小时、3 人每人 8 小时等)?
所以我知道 Dijkstra 是一种“贪婪”算法,允许我们使用加权边缘。
我了解问题的症结所在 - 我可以使用 Dijkstra 找到能够最大限度降低 24 小时轮班成本的设备。 (例如从顶层节点,将子节点拆分为“1人班”、“2人班”、“3人班”、“4人班”、“5人班”、“6人班” “ ...然后在进一步的子节点中,列出每个组合。边缘的“权重”是成本。我想找到“最便宜”的路径但显然这很笨拙,因为我必须列出所有可能的分组(即 5!和 6!保安)及其成本。
In [4]:
shift = {}
shift["1_guard"] = {}
shift["2_guards"] = {}
shift["3_guards"] = {}
shift["4_guards"] = {}
shift["5_guards"] = {}
shift["6_guards"] = {}
#at each step we're doing a brute force attack to figure out ok ... if we have x guards, whats the breakdown of the total costs possible?
shift["1_guard"]["Shifts_Filled"] = (24 * 20)
shift["2_guards"]["1_hour_23_hours"] = ((15 * 1)+ (20 * 23))
shift["2_guards"]["2_hour_22_hours"] = ((15 * 2)+ (20 * 22))
shift["2_guards"]["3_hour_21_hours"] = ((15 * 3)+ (20 * 21))
shift["2_guards"]["4_hour_20_hours"] = ((15 * 4)+ (20 * 20))
shift["2_guards"]["5_hour_19_hours"] = ((15 * 5)+ (20 * 19))
shift["2_guards"]["6_hour_18_hours"] = ((15 * 6)+ (20 * 18))
shift["2_guards"]["7_hour_17_hours"] = ((15 * 7)+ (20 * 17))
shift["2_guards"]["8_hour_16_hours"] = ((15 * 8)+ (20 * 16))
shift["2_guards"]["9_hour_15_hours"] = ((15 * 9)+ (20 * 15))
shift["2_guards"]["10_hour_14_hours"] = ((15 * 10)+ (20 * 14))
shift["2_guards"]["11_hour_13_hours"] = ((15 * 11)+ (20 * 13))
shift["2_guards"]["12_hour_12_hours"] = ((15 * 12)+ (20 * 12))
#you dont need to do more than this because it will be one of these combos (it doesnt matter which guard works which shift)
shift["3_guards"]["1_hour_1_hour_22_hours"] = ((15 * 1) + (15 * 1) + (20*22)
#fill in for every combination of hours 3 guards could have
shift("4_guards")["1_hour_1_hour_1_hour_20_hours"] = ((15 * 1) + (15 * 1) (15 * 1) + (20*22)
#fill in for every combination of hours 3 guards could have
def find_lowest_cost_node(costs):
lowest_cost = float("inf")
lowest_cost_node = None
# Go through each node.
for node in costs:
cost = costs[node]
# If it's the lowest cost so far and hasn't been processed yet...
if cost < lowest_cost and node not in processed:
# ... set it as the new lowest-cost node.
lowest_cost = cost
lowest_cost_node = node
return lowest_cost_node
start_time = time.perf_counter()
# Find the lowest-cost node that you haven't processed yet.
node = find_lowest_cost_node(costs)
# If you've processed all the nodes, this while loop is done.
while node is not None:
cost = costs[node]
# Go through all the neighbors of this node.
neighbors = shift[node]
for n in neighbors.keys():
new_cost = cost + neighbors[n]
# If it's cheaper to get to this neighbor by going through this node...
if costs[n] > new_cost:
# ... update the cost for this node.
costs[n] = new_cost
# This node becomes the new parent for this neighbor.
parents[n] = node
# Mark the node as processed.
processed.append(node)
# Find the next node to process, and loop.
node = find_lowest_cost_node(costs)
print("Cost from the start to each node:")
print(costs)
print(time.perf_counter() - start_time, "seconds")
dijkstras_time = time.perf_counter() - start_time
# the costs table
infinity = float("inf")
costs = {}
costs["1_guard"] =
costs["2_guards"] =
costs["3_guards"] =
costs["4_guards"] = infinity
# the parents table
#need help here
parents = {}
parents[""] = "NYC"
parents[""] = "NYC"
parents[""] = "NYC"
#all the ones below a certain threshold should have None*
parents[""] = None
processed = []
我这样做的方式是否正确?对我的代码的反馈会非常有帮助。
【问题讨论】:
-
这似乎没什么意义。您需要 3 个人每人 8 小时(或更多人用更少时间,没关系)。您需要搜索什么样的图表才能找到它?
-
记得对过去回答的问题提供反馈。
标签: python algorithm optimization dijkstra