All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.spoptchev.scientist.Experiment.kt Maven / Gradle / Ivy

The newest version!
package com.github.spoptchev.scientist

interface Experiment {
    fun conduct(contextProvider: ContextProvider): ExperimentState
    fun refresh(): Experiment
}

data class DefaultExperiment(
        val name: String,
        val control: Trial,
        val candidates: List>,
        val conductible: (ContextProvider) -> Boolean = { true }
) : Experiment {

    private val shuffledTrials: List> by lazy {
        (candidates + control).sorted()
    }

    override fun conduct(contextProvider: ContextProvider): ExperimentState {
        return if (conductible(contextProvider)) {
            val observations = shuffledTrials.map { it.run() }
            val controlObservation = observations.first { it.id == control.id }
            val candidateObservations = observations - controlObservation

            Conducted(name, observations, controlObservation, candidateObservations)
        } else {
            Skipped(control.run())
        }
    }

    override fun refresh(): Experiment = copy(
            control = control.refresh(),
            candidates = candidates.map { it.refresh() }
    )

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy