diff --git a/android/app/src/test/java/tv/orbit/classic/WeightedRotationPrototypeTest.kt b/android/app/src/test/java/tv/orbit/classic/WeightedRotationPrototypeTest.kt new file mode 100644 index 00000000..e027fecc --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/WeightedRotationPrototypeTest.kt @@ -0,0 +1,111 @@ +package tv.orbit.classic + +import org.json.JSONObject +import org.junit.Assume.assumeTrue +import org.junit.Test +import java.io.File + +/** + * A prototype of editorially weighted rotation, for the Orbit Three proposal only: not used by the app. + * ORBIT_REPLAY_DIR= ./gradlew :app:testDebugUnitTest --tests '*WeightedRotationPrototypeTest*' + * Plays Orbit Three three weeks from a clean start both ways, today's equal turns with finish-together and the + * weighted rule, on the real pool, and writes weighted-rotation-three.txt with airtime by tier and title, longest + * waits, same-day repeats and episode order. + */ +class WeightedRotationPrototypeTest { + private val dir = System.getenv("ORBIT_REPLAY_DIR")?.let(::File) + private val zone = java.time.ZoneId.of("Europe/London") + private val day = 24 * 60 * 60_000L + + // Orbit Three's tiers, straight from docs/channels/orbit-three.md. + private val core = listOf("Still Game", "Limmy's Show!", "Burnistoun", "Chewin' the Fat", "Rab C. Nesbitt", "Gavin & Stacey", "The Mighty Boosh", "Little Britain", + "The Inbetweeners", "Two Pints of Lager and a Packet of Crisps", "Spaced", "Phoenix Nights").map(::seriesKey).toSet() + private val surprises = listOf("The League of Gentlemen", "Nighty Night", "Snuff Box", "Monkey Dust", "Psychoville", "Inside No. 9", "Garth Marenghi's Darkplace", + "Brass Eye", "The Day Today", "Nathan Barley", "Bo' Selecta!", "Stressed Eric", "Family Guy").map(::seriesKey).toSet() + private fun tier(title: String) = when (seriesKey(title)) { in core -> "core"; in surprises -> "surprise"; else -> "supporting" } + private val weights = mapOf("core" to 3.0, "supporting" to 2.0, "surprise" to 1.0) + + private fun pool(file: File): List { + val rows = JSONObject(file.readText()).getJSONArray("series") + return (0 until rows.length()).map { i -> + val o = rows.getJSONObject(i); val eps = o.getJSONArray("episodes") + PoolSeries(o.getString("id"), o.getString("title"), (0 until eps.length()).map { j -> + val e = eps.getJSONArray(j); PoolEpisode(e.getString(0), e.getString(1), EpisodeMark(e.getInt(2), e.getInt(3)), e.getString(4), e.getLong(5)) + }, o.optString("group")) + } + } + + /** + * The weighted rule. In each block, every show keeps a running tally of the minutes it has aired there divided by + * its tier's weight; the show with the lowest tally goes next. So airtime follows the tiers, not episode counts, + * and no show can wait for long: whoever has had least (for its tier) is always next. A show joining a block + * starts level with the lowest tally, so it neither floods in nor waits. Episodes follow each show's own cursor, + * in order or from its shuffled order, starting again when it finishes; nobody waits for anyone else to finish. + * The same file isn't repeated within 24 hours while another show could play. + */ + private fun weighted(pool: List, parts: List, mixed: (PoolSeries) -> Boolean, from: Long, until: Long): List { + val tallies = HashMap>() + val marks = HashMap() + val lastAired = HashMap() + val mixes = HashMap>() + val entries = mutableListOf() + var start = from + var last: String? = null + fun running(s: PoolSeries) = if (mixed(s)) mixes.getOrPut(s.id) { shuffledEpisodes(s) } else s.episodes + fun following(s: PoolSeries): PoolEpisode { val r = running(s); return r[(r.indexOfFirst { it.mark == marks[s.id] } + 1) % r.size] } + while (start < until) { + val (available, block, _) = dayPartFor(pool, parts, start, zone) + val tally = tallies.getOrPut(block) { HashMap() } + val floor = available.mapNotNull { tally[it.id] }.minOrNull() ?: 0.0 + available.forEach { tally.putIfAbsent(it.id, floor) } + fun ok(s: PoolSeries) = s.id != last && lastAired[following(s).id]?.let { start - it < day } != true + val choice = available.filter(::ok).ifEmpty { available.filter { it.id != last } }.ifEmpty { available }.minBy { tally[it.id]!! } + val episode = following(choice) + entries += ScheduleEntry(choice.id, episode.mark, ScheduledMedia(scheduledItem(choice.title, episode), episode.sourceId, episode.durationMs), start, "$block; weighted") + tally[choice.id] = tally[choice.id]!! + episode.durationMs / 60_000.0 / weights.getValue(tier(choice.title)) + marks[choice.id] = episode.mark; lastAired[episode.id] = start; last = choice.id + start += episode.durationMs + } + return entries + } + + private fun report(name: String, entries: List, pool: List, mixed: (PoolSeries) -> Boolean, out: StringBuilder) { + val titles = pool.associate { it.id to it.title } + val minutes = entries.groupBy { it.seriesId }.mapValues { (_, es) -> es.sumOf { it.media.durationMs } / 60_000 } + val total = minutes.values.sum().coerceAtLeast(1) + out.appendLine("== $name") + for (t in listOf("core", "supporting", "surprise")) { + val shows = pool.filter { tier(it.title) == t } + val m = shows.sumOf { minutes[it.id] ?: 0L } + out.appendLine(" $t: ${100 * m / total}% of airtime, ${shows.count { (minutes[it.id] ?: 0) > 0 }} of ${shows.size} shows aired") + } + // Longest gap between airings of each show (or since the start, for one that never airs). + val first = entries.first().startMs; val end = entries.last().endMs + val waits = pool.associate { s -> + val times = entries.filter { it.seriesId == s.id }.map { it.startMs } + s.title to ((listOf(first) + times + listOf(end)).zipWithNext().maxOf { (a, b) -> b - a } / day.toDouble()) + } + out.appendLine(" longest wait between airings: " + waits.entries.sortedByDescending { it.value }.take(6).joinToString { "${it.key} %.1f d".format(it.value) }) + val sameDay = entries.groupBy { it.media.item.id }.values.sumOf { es -> es.zipWithNext().count { (a, b) -> b.startMs - a.startMs < day } } + val backwards = entries.filter { e -> pool.firstOrNull { it.id == e.seriesId }?.let { !mixed(it) } == true }.groupBy { it.seriesId } + .values.sumOf { es -> es.zipWithNext().count { (a, b) -> b.mark < a.mark && b.mark != pool.first { it.id == a.seriesId }.episodes.first().mark } } + out.appendLine(" same file again within 24 h: $sameDay; in-order shows stepping backwards: $backwards") + out.appendLine(" by title: " + pool.sortedByDescending { minutes[it.id] ?: 0 }.joinToString { "${it.title} ${"%.1f".format(100.0 * (minutes[it.id] ?: 0) / total)}% [${tier(it.title)[0]}]" }) + } + + @Test fun orbitThree() { + assumeTrue(dir != null && File(dir, "episodes-103.json").exists()) + val programming = SmartChannels.first { it.number == 103 } + val pool = pool(File(dir, "episodes-103.json")) + val from = java.time.LocalDate.of(2026, 11, 10).atStartOfDay(zone).toInstant().toEpochMilli() + val until = from + 21 * day + val today = extendSchedule(pool, ScheduleCursor(), from, until, null, programming.finishTogether, programming.dayParts, zone, shuffle = programming.shuffleEpisodes, mixed = programming::mixes).entries + val proposed = weighted(pool, programming.dayParts, programming::mixes, from, until) + val out = StringBuilder("Orbit Three, three weeks from 10 November on the real pool, from a clean start (no history).\nWeights from the brief: core 3, supporting 2, surprises 1.\n\n") + report("Today: equal turns, finish together", today, pool, programming::mixes, out) + out.appendLine() + report("Proposed: weighted by tier, no waiting for others", proposed, pool, programming::mixes, out) + File(dir, "weighted-rotation-three.txt").writeText(out.toString()) + println(out) + } +} diff --git a/docs/replays/weighted-rotation-three-2026-11-04.txt b/docs/replays/weighted-rotation-three-2026-11-04.txt new file mode 100644 index 00000000..87b73765 --- /dev/null +++ b/docs/replays/weighted-rotation-three-2026-11-04.txt @@ -0,0 +1,18 @@ +Orbit Three, three weeks from 10 November on the real pool, from a clean start (no history). +Weights from the brief: core 3, supporting 2, surprises 1. + +== Today: equal turns, finish together + core: 46% of airtime, 12 of 12 shows aired + supporting: 39% of airtime, 20 of 20 shows aired + surprise: 13% of airtime, 13 of 13 shows aired + longest wait between airings: Lunch Monkeys 13.5 d, Snuff Box 12.1 d, Garth Marenghi's Darkplace 12.0 d, Brass Eye 12.0 d, The Day Today 12.0 d, Nathan Barley 11.9 d + same file again within 24 h: 0; in-order shows stepping backwards: 0 + by title: Two Pints of Lager and a Packet of Crisps 10.7% [c], Still Game 9.5% [c], Rab C. Nesbitt 5.5% [c], The Inbetweeners 4.3% [c], Scot Squad 4.0% [s], Gary: Tank Commander 3.4% [s], Limmy's Show! 3.1% [c], Black Books 2.9% [s], Bad Education 2.9% [s], Cuckoo 2.8% [s], Burnistoun 2.8% [c], Gavin & Stacey 2.7% [c], Coupling 2.7% [s], The Mighty Boosh 2.6% [c], Him & Her 2.3% [s], People Just Do Nothing 2.3% [s], Being Human 2.3% [s], Green Wing 2.1% [s], Chewin' the Fat 2.0% [c], Uncle 1.8% [s], Little Britain 1.8% [c], Man Like Mobeen 1.8% [s], Inside No. 9 1.7% [s], The League of Gentlemen 1.7% [s], Monkey Dust 1.7% [s], Fresh Meat 1.6% [s], Bo' Selecta! 1.4% [s], Psychoville 1.3% [s], Stressed Eric 1.1% [s], Nighty Night 1.1% [s], Lunch Monkeys 1.1% [s], The Office (UK) 1.1% [s], Extras 1.0% [s], This Country 1.0% [s], Spaced 1.0% [c], Phoenix Nights 1.0% [c], Peep Show 0.9% [s], The IT Crowd 0.9% [s], Toast of London 0.9% [s], Family Guy 0.8% [s], The Day Today 0.6% [s], Snuff Box 0.6% [s], Nathan Barley 0.5% [s], Garth Marenghi's Darkplace 0.5% [s], Brass Eye 0.5% [s] + +== Proposed: weighted by tier, no waiting for others + core: 50% of airtime, 12 of 12 shows aired + supporting: 37% of airtime, 20 of 20 shows aired + surprise: 11% of airtime, 13 of 13 shows aired + longest wait between airings: Being Human 4.9 d, Green Wing 3.9 d, Family Guy 3.9 d, The League of Gentlemen 3.0 d, Fresh Meat 3.0 d, Inside No. 9 3.0 d + same file again within 24 h: 0; in-order shows stepping backwards: 0 + by title: Burnistoun 6.3% [c], Limmy's Show! 6.1% [c], Still Game 6.1% [c], The Inbetweeners 5.7% [c], Two Pints of Lager and a Packet of Crisps 5.7% [c], Chewin' the Fat 4.1% [c], Rab C. Nesbitt 4.0% [c], Black Books 3.8% [s], Gavin & Stacey 3.7% [c], The Mighty Boosh 3.6% [c], Little Britain 2.8% [c], Gary: Tank Commander 2.7% [s], Scot Squad 2.7% [s], Uncle 2.4% [s], Bad Education 2.4% [s], People Just Do Nothing 2.4% [s], Man Like Mobeen 2.3% [s], Him & Her 2.3% [s], Coupling 2.3% [s], Lunch Monkeys 2.3% [s], Cuckoo 2.3% [s], Spaced 1.5% [c], Phoenix Nights 1.4% [c], Being Human 1.1% [s], Extras 1.0% [s], Fresh Meat 1.0% [s], The IT Crowd 1.0% [s], Green Wing 1.0% [s], Toast of London 1.0% [s], This Country 1.0% [s], Peep Show 1.0% [s], Bo' Selecta! 1.0% [s], The Office (UK) 1.0% [s], Psychoville 1.0% [s], Inside No. 9 1.0% [s], Stressed Eric 1.0% [s], The League of Gentlemen 1.0% [s], The Day Today 1.0% [s], Garth Marenghi's Darkplace 0.9% [s], Monkey Dust 0.9% [s], Nighty Night 0.9% [s], Brass Eye 0.9% [s], Snuff Box 0.9% [s], Nathan Barley 0.9% [s], Family Guy 0.5% [s]