← All problemsFree · Python · 60 min

Algorithm design / guarantees under an adversary

The Forecast and the Lighting Rig

Rent or buy with a forecast that may be wrong. Break the rule that trusts it, find the price of trust, and write a rule that an adversary grades for consistency, robustness and graceful degradation.

0 / 4 graded parts passSaved in this browser · loading saved work…
Jump to your work ↓

A touring play needs a lighting rig. Renting costs 1 per night; buying costs B, after which every night is free. The run ends on a night nobody knows in advance. Each morning the run is still on, you rent again or buy.

0510152025151015202530nights the run lastsbuy on night 12: pays 23hindsight: min(run, 12) = 12cost
The example with B = 12: buying on night 12 against hindsight, for runs of 1 to 30 nights.

Before opening night the producer gives a forecast of how many nights the run will last. It may be right. It may be wildly wrong.

A rule maps the forecast to the night you buy: buy_night(forecast, B) returns a night 1, 2, 3, ..., or math.inf to never buy.

If the run lasts d nights, buying on night t costs d when d<t, and t−1+B otherwise. Someone who knew d in advance would pay min(d,B).

  • Consistency: the worst ratio of what you pay to what hindsight pays, over runs where the forecast was exactly right.
  • Robustness: the worst ratio over every forecast and every run.

Example

With B=12 and the rule "buy on night B whatever the forecast":

forecast run you pay hindsight ratio
20 20 11 + 12 = 23 12 1.92
3 40 23 12 1.92

Its consistency and its robustness are both 23/12≈1.92.

Grading

  • A hidden adversary plays B∈{5,12,37,100}: every run up to 20B with a correct forecast, and every forecast up to 20B plus two huge ones, each against its worst run.
  • No timing decides a grade. The whole check must finish within 5 seconds.
  • buy_night returns a whole number at least 1, or math.inf.

Background, if you want it: Online Algorithms, rent or buy without a forecast.

easy · Part A

Break "trust it fully"

At B=12, "trust it fully" buys on night 1 if the forecast is at least 12 and never otherwise. Its consistency is 1. Give a forecast and a run on which it pays at least 10 times what hindsight pays.

Explore in the sandbox

Experiment here, then copy a rule into the editor and test its guarantees.