Spot vs On-Demand Expected-Cost Decision
Problem Statement
Decide whether to run a batch job on cheaper but interruptible spot instances or reliable on-demand, based on expected cost including restart overhead from interruptions.
Background
On-demand cost is on_demand_rate * hours. Spot is cheaper per hour (spot_rate) but with interruption probability p per hour, each interruption wastes restart_hours of recompute. Expected spot cost is spot_rate * (hours + expected_interruptions * restart_hours), where expected_interruptions = p * hours. Recommend "spot" if its expected cost is strictly less than on-demand, else "on_demand".
Your Task
def choose_instance(hours, on_demand_rate, spot_rate, p, restart_hours):
Return "spot" or "on_demand".
Input Format
- hours (float), on_demand_rate, spot_rate (float), p (float in [0,1]), restart_hours (float).
Output Format
- A string.
Sample
print(choose_instance(10, 1.0, 0.3, 0.1, 2.0))
Output:
spot
Example
print(choose_instance(10, 1.0, 0.3, 0.1, 2.0))
spot
- Calculate the expected wasted time due to interruptions by multiplying the interruption probability, total hours, and restart duration: hours.
- Determine the total effective time for the spot instance by adding the wasted time to the original job duration: hours.
- Compute the total expected cost for the spot instance by multiplying the effective time by the spot rate: .
- Compute the total cost for the on-demand instance by multiplying the original job duration by the on-demand rate: .
- Compare the two costs to make the decision; since the spot cost () is strictly less than the on-demand cost (), the spot instance is recommended.
- The final output is
spot
Constraints
- expected_interruptions = p * hours; wasted = expected_interruptions * restart_hours.
- spot_cost = spot_rate * (hours + wasted); on_demand_cost = on_demand_rate * hours.
- Choose spot iff spot_cost < on_demand_cost.