Weighted rotation prototype for the Orbit Three proposal (test only, not used by the app)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -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=<copies of the box's files> ./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<PoolSeries> {
|
||||
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<PoolSeries>, parts: List<DayPart>, mixed: (PoolSeries) -> Boolean, from: Long, until: Long): List<ScheduleEntry> {
|
||||
val tallies = HashMap<String, MutableMap<String, Double>>()
|
||||
val marks = HashMap<String, EpisodeMark>()
|
||||
val lastAired = HashMap<String, Long>()
|
||||
val mixes = HashMap<String, List<PoolEpisode>>()
|
||||
val entries = mutableListOf<ScheduleEntry>()
|
||||
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<ScheduleEntry>, pool: List<PoolSeries>, 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user