A ticket optimizer looks like a black box: you type in a few prices and it hands back a combination of tickets. In reality its logic is compact and repeatable — it models every ticket as a cost over a span of days, then repeatedly buys whichever ticket is cheapest per remaining day until your whole stay is covered. This article follows that algorithm.
A transit ticket optimizer models single fares, short-term tickets and long-term tickets as options with a price and a validity length, then fills your stay one slice at a time. For each slice it divides the ticket price by the number of days the ticket can cover, picks the lowest cost per remaining day, subtracts that length from the days left, and repeats. The result is a mix of ticket types, not necessarily one type bought over and over.
How does it compute cost per remaining day?
Every candidate is scored with the same cost-per-day rule, taken directly from the tool's getCostPerDay function:
The first branch applies when a ticket fits inside the time remaining: its cost is spread over its full validity. The second branch applies to a ticket that outlasts your stay, such as a weekly pass on the last three days of a trip: the tool spreads its whole price over only the days you would actually use, which raises its cost per day and discourages buying it.
How does the optimizer model each ticket type?
The tool's own algorithm builds a list of candidate tickets before doing any arithmetic. Each ticket carries a name, a cost and a length measured in days:
| Ticket model | Cost input | Length (days covered) |
|---|---|---|
| Single fare tickets | Single fare ticket price | One trip: of a day |
| Short-term tickets | Short-term ticket price | The short-term validity period you enter |
| Long-term tickets | Long-term ticket price | The long-term validity period you enter |
Single fares are always in the list. Short-term and long-term tickets are added only when you select them in the Compare-with field. The length of a single fare is the inverse of your daily trip count — at four trips per day, one single fare covers 0.25 days.
Worked example — the Berlin ticket set: For the tool's 10-day example with four trips per day, the first scoring pass gives a single fare at €2.80 over 0.25 days, or €11.20 per day; a €7.00 daily ticket at €7.00 per day; and a €30.00 weekly pass over seven days, or about €4.29 per day. The weekly pass is cheapest, so it is chosen first.
Why does it pick tickets one slice at a time?
After choosing a ticket the optimizer subtracts that ticket's length from the days left and scores every candidate again. This loop is why the answer is a combination rather than a single repeated purchase. Continuing the Berlin example, the weekly pass leaves three days. Its own cost per remaining day is now €30 ÷ 3 = €10, while the daily ticket stays at €7.00, so three daily tickets are chosen. The final mix — one weekly pass and three daily tickets — is the cheapest option stated in the source.
What is the epsilon stopping threshold?
The loop runs while the remaining period is above days, a tiny tolerance that prevents floating-point rounding from adding a spurious extra ticket at the end. Once the remaining days fall below that threshold, the stay is treated as fully covered and the accumulated combination is returned.
Frequently Asked Questions
What happens when a ticket is valid longer than the days left? The tool divides its price by the days remaining instead of by its full validity (the second branch of the cost-per-day rule). That inflates its cost per day, so a long pass is unlikely to be chosen for a short final stretch of your stay.
Does the optimizer pick the same ticket for the whole trip? No. It rescoring all candidates after each purchase and can switch ticket types mid-stay. In the Berlin example it starts with a weekly pass and finishes with daily tickets.
Which values does the tool return? Its two defined functions return different things: one returns the index of the cheapest ticket in the current scoring pass, and the other returns the accumulated total cost of the chosen combination.
How is the chosen combination summarised? The algorithm groups the selected ticket names and counts how many of each were picked, which is what the summary display shows you at the end.
References
- Omni source content for the ticket optimizer, including the four step-by-step sections and the Berlin worked example.
- The tool's embedded algorithm (
getCostPerDay,findLowestValueIndex,countTicketsand theticket_function/cost_functiondefinitions). - The tool's dataset options for single, short-term, long-term and summary tickets.