23/42
Part 6 of 8 · The best setLearn and do
Pick the best set of machines that fits a need
Now a different question about the same kind of resources: which set of them fits a need, at the best price?
On these machines, two greedy rules pay more than the cheapest set
A job needs 8 GPUs, and the lab has six machines, each with its GPUs and its cost per hour:
MACHINES = {
"atlas": {"gpus": 8, "cost": 20},
"birch": {"gpus": 4, "cost": 9},
"cedar": {"gpus": 4, "cost": 8},
"delta": {"gpus": 2, "cost": 3},
"ember": {"gpus": 2, "cost": 5},
"fjord": {"gpus": 6, "cost": 15},
}
def gpus(group):
return sum(MACHINES[name]["gpus"] for name in group)
def cost(group):
return sum(MACHINES[name]["cost"] for name in group)
for group in [("atlas",), ("delta", "cedar", "birch"), ("cedar", "delta", "ember")]:
print(group, gpus(group), cost(group))
('atlas',) 8 20
('delta', 'cedar', 'birch') 10 20
('cedar', 'delta', 'ember') 8 16
Two greedy rules come to mind, and both pay too much here:
- Biggest machine first takes atlas alone, for 20.
- Most GPUs per unit of cost first takes delta, cedar and birch: 10 GPUs for 20.
The cheapest set that fits is cedar, delta and ember: exactly 8 GPUs for 16. A rule that commits to one machine at a time can be fooled like this. So with a handful of machines, try every set.
In the editor, MACHINES is the lab above. Write by_ratio(machines, need): the most-GPUs-per-cost rule, which takes machines in that order until their GPUs reach need.