66 KiB
66 KiB
In [8]:
@file:Repository("https://repo.kotlin.link") @file:DependsOn("space.kscience:plotlykt-jupyter:0.5.0")
In [9]:
Plotly.jupyter.notebook()
Plotly notebook integration switch into the legacy mode.
API¶
In [34]:
enum class Severity(val penalty: Double){ MINOR(1.0), MAJOR(2.0), CRITICAL(3.0) } enum class State{ OPEN, ASSIGNED, RESOLVED } data class Issue(val id: String, val dayCreated: Int, val severity: Severity, val complexity: Int, var state: State = State.OPEN, var dayAssigned: Int? = null, var dayResolved: Int? = null){ fun activate(day: Int){ state = State.ASSIGNED dayAssigned = day } fun resolve(day: Int){ state = State.RESOLVED dayResolved = day } internal fun tryResolve(day: Int){ if(state == State.ASSIGNED && day >= (dayAssigned ?: 0) + complexity ){ resolve(day) } } } class Worker(val name: String){ var currentIssue: Issue? = null private set fun isBusy(): Boolean = currentIssue != null fun update(day: Int){ currentIssue?.tryResolve(day) if(currentIssue?.state == State.RESOLVED){ currentIssue = null } } fun assign(day: Int, issue: Issue){ if(currentIssue != null) error("Can't assign work to a worker which is busy") issue.activate(day) currentIssue = issue } } interface IssueGenerator{ fun generate(day: Int): List<Issue> } interface Strategy{ fun selectIssue(day: Int, issues: List<Issue>): Issue? } class WorkResult(val issues: List<Issue>, val workers: Int, val days: Int) @OptIn(kotlin.ExperimentalStdlibApi::class) fun simulate(generator: IssueGenerator, strategy: Strategy, numWorkers: Int = 10, days: Int = 100): WorkResult{ val workers = (0 until numWorkers).map{Worker("worker $it")} val issues = buildList<Issue>{ for(day in 0 until days){ //update all workers workers.forEach { it.update(day) } //generate new issues val newIssues = generator.generate(day) addAll(newIssues) //Select all free workers workers.filter { !it.isBusy() }.forEach { worker-> val unasigned = filter { it.state == State.OPEN } val anIssue = strategy.selectIssue(day, unasigned) //select an issue to assign from all unassigned issues if(anIssue != null){ worker.assign(day, anIssue) } } } } return WorkResult(issues, numWorkers, days) } fun WorkResult.computeLoss(): Double = issues.sumOf { ((it.dayResolved ?: days) - it.dayCreated).toDouble()*it.severity.penalty } / days / workers / issues.size
Implementations¶
In [35]:
import kotlin.random.Random import kotlin.math.pow /** * Generate [issuesPerDay] random issues per day */ class RandomIssueGenerator(seed: Long, val issuesPerDay: Int = 4 ) : IssueGenerator{ private val random = Random(seed) override fun generate(day: Int): List<Issue>{ return List(issuesPerDay){ val severity = Severity.values()[random.nextInt(3)] val complexity = random.nextInt(15) Issue("${day}_${it}", day, severity, complexity) } } } object TakeOldest: Strategy{ override fun selectIssue(day: Int, issues: List<Issue>): Issue?{ return issues.minByOrNull { it.dayCreated } } } class TakeRandom(seed: Long): Strategy{ private val random = Random(seed) override fun selectIssue(day: Int, issues: List<Issue>): Issue?{ if(issues.isEmpty()) return null return issues.random(random) } } object TakeMostLoss: Strategy{ override fun selectIssue(day: Int, issues: List<Issue>): Issue?{ return issues.maxByOrNull { it.severity.penalty*(day - it.dayCreated) } } }
In [36]:
object TakeCritical: Strategy{ override fun selectIssue(day: Int, issues: List<Issue>): Issue?{ val maxSeverity = issues.maxByOrNull{it.severity}?.severity ?: return null return issues.filter{it.severity == maxSeverity}.random() } }
Simulate lossseverity¶
In [44]:
val seed = 892L val days = 100 val workers = 40
Take oldest¶
In [45]:
val result = simulate(RandomIssueGenerator(seed, workers),TakeOldest, days = days) //result.issues.forEach { println(it)} result.computeLoss()
0.09711974999999999
Take random¶
In [46]:
simulate(RandomIssueGenerator(seed, workers),TakeRandom(seed), days = days).computeLoss()
0.0966175
Take critical¶
In [47]:
simulate(RandomIssueGenerator(seed, workers), TakeCritical, days = days).computeLoss()
0.09535774999999999
In [48]:
val seeds = List(1000){Random.nextLong()} Plotly.plot{ trace{ x.numbers = seeds.map{ seed -> simulate(RandomIssueGenerator(seed, workers), TakeOldest, days = days).computeLoss()} name = "oldest" type = TraceType.histogram } trace{ x.numbers = seeds.map{ seed -> simulate(RandomIssueGenerator(seed, workers), TakeRandom(seed), days = days).computeLoss()} name = "random" type = TraceType.histogram } trace{ x.numbers = seeds.map{ seed -> simulate(RandomIssueGenerator(seed, workers), TakeCritical, days = days).computeLoss()} name = "critical" type = TraceType.histogram } trace{ x.numbers = seeds.map{ seed -> simulate(RandomIssueGenerator(seed, workers), TakeMostLoss, days = days).computeLoss()} name = "mostLoss" type = TraceType.histogram } layout{ title = "Loss distribtution" xaxis { title = "Loss" } } }
<html>
<head>
</head>
</html>
In [ ]:
In [ ]: