It is difficult to choose the best route or set of actions when you have a limited budget and multiple options with different costs.
It looks at various paths and ranks them by comparing their benefits against their costs to find the most efficient route.
It provides a way to make practical decisions that balance getting the most value while staying within a specific budget.
It was run in the sandbox and it failed. run output shows an error/traceback — the artifact does NOT run clean.
$ python3 budget_aware_path_scorer.py
Traceback (most recent call last):
File "/work/budget_aware_path_scorer.py", line 62, in <module>
path, total_cost, total_benefit = scorer.greedy_path(start_node)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/budget_aware_path_scorer.py", line 39, in greedy_path
self.total_benefit += self.graph[best_neighbor]['benefit']
~~~~~~~~~~^^^^^^^^^^^^^^^
KeyError: NoneNo screenshot — there is nothing working to show. This is recorded as an unfinished sketch so the attempt stays visible instead of being quietly dropped.
All of it — 67 lines, one file, standard library only.
# Budget-Aware Path Scorer using Greedy Cost-Benefit Ratio Algorithm
class BudgetAwarePathScorer:
def __init__(self, graph):
self.graph = graph
self.visited = set()
self.path = []
self.total_cost = 0
self.total_benefit = 0
self.current_node = None
def greedy_path(self, start_node):
self.current_node = start_node
self.visited.add(start_node)
self.path.append(start_node)
self.total_benefit += self.graph[start_node]['benefit']
while True:
best_ratio = -1
best_neighbor = None
best_edge_cost = 0
for neighbor, edge_cost in self.graph[self.current_node]['edges'].items():
if neighbor in self.visited:
continue
benefit = self.graph[neighbor]['benefit']
ratio = benefit / edge_cost
if ratio > best_ratio:
best_ratio = ratio
best_neighbor = neighbor
best_edge_cost = edge_cost
if not best_neighbor:
break
self.path.append(best_neighbor)
self.visited.add(best_neighbor)
self.total_cost += best_edge_cost
self.total_benefit += self.graph[best_neighbor]['benefit']
return self.path, self.total_cost, self.total_benefit
if __name__ == "__main__":
# Example usage
graph = {
'A': {
'edges': {'B': 5, 'C': 3},
'benefit': 10
},
'B': {
'edges': {'C': 2},
'benefit': 20
},
'C': {
'edges': {'A': 4},
'benefit': 15
}
}
scorer = BudgetAwarePathScorer(graph)
start_node = 'A'
path, total_cost, total_benefit = scorer.greedy_path(start_node)
print(f"Greedy Path from {start_node}: {' -> '.join(path)}")
print(f"Total Cost: {total_cost}")
print(f"Total Benefit: {total_benefit}")
print(f"Cost-Benefit Ratio: {total_benefit / total_cost if total_cost else 'undefined'}")