diff options
Diffstat (limited to 'opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy')
4 files changed, 184 insertions, 0 deletions
diff --git a/opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy/CartesianExperimentStrategy.kt b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy/CartesianExperimentStrategy.kt new file mode 100644 index 00000000..e5e08003 --- /dev/null +++ b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy/CartesianExperimentStrategy.kt @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.harness.engine.strategy + +import org.opendc.harness.api.ExperimentDefinition +import org.opendc.harness.api.Parameter +import org.opendc.harness.api.Scenario +import org.opendc.harness.internal.ScenarioImpl + +/** + * An [ExperimentStrategy] that takes the cartesian product of the parameters and evaluates every combination. + */ +public object CartesianExperimentStrategy : ExperimentStrategy { + /** + * Build the trials of an experiment. + */ + override fun generate(experiment: ExperimentDefinition): Sequence<Scenario> { + return experiment.parameters + .asSequence() + .map { param -> mapParameter(param).map { value -> listOf(param to value) } } + .reduce { acc, param -> + acc.flatMap { x -> param.map { y -> x + y } } + } + .mapIndexed { id, values -> ScenarioImpl(id, experiment, values.toMap()) } + } + + /** + * Instantiate a parameter and return a sequence of possible values. + */ + private fun <T> mapParameter(param: Parameter<T>): Sequence<T> { + return when (param) { + is Parameter.Generic<T> -> param.values.asSequence() + } + } +} diff --git a/opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy/CartesianExperimentStrategyProvider.kt b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy/CartesianExperimentStrategyProvider.kt new file mode 100644 index 00000000..f18795a3 --- /dev/null +++ b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy/CartesianExperimentStrategyProvider.kt @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.harness.engine.strategy + +/** + * An [ExperimentStrategyProvider] for constructing a [CartesianExperimentStrategy]. + */ +public class CartesianExperimentStrategyProvider : ExperimentStrategyProvider { + override val id: String = "cartesian" + + override fun create(): ExperimentStrategy = CartesianExperimentStrategy +} diff --git a/opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy/ExperimentStrategy.kt b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy/ExperimentStrategy.kt new file mode 100644 index 00000000..3a0148ad --- /dev/null +++ b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy/ExperimentStrategy.kt @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.harness.engine.strategy + +import org.opendc.harness.api.ExperimentDefinition +import org.opendc.harness.api.Scenario + +/** + * The [ExperimentStrategy] is responsible for traversing the design space of an [ExperimentDefinition] based on its + * parameters, generating concrete points in the space represented as [Scenario]s. + */ +public interface ExperimentStrategy { + /** + * Generate the points in the design space of the specified [experiment] to explore. + * + * @param experiment The experiment design space to explore. + * @return A sequence of [Scenario]s which may be explored by the [ExperimentEngine]. + */ + public fun generate(experiment: ExperimentDefinition): Sequence<Scenario> +} diff --git a/opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy/ExperimentStrategyProvider.kt b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy/ExperimentStrategyProvider.kt new file mode 100644 index 00000000..7fa05f34 --- /dev/null +++ b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/strategy/ExperimentStrategyProvider.kt @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.harness.engine.strategy + +import java.util.* + +/** + * A factory for constructing an [ExperimentStrategy]. + */ +public interface ExperimentStrategyProvider { + /** + * A unique identifier for this strategy implementation. + * + * Each experiment strategy must provide a unique ID, so that they can be selected by the user. + * When in doubt, you may use the fully qualified name of your custom [ExperimentStrategy] implementation class. + */ + public val id: String + + /** + * Factory method for creating a new [ExperimentStrategy] instance. + */ + public fun create(): ExperimentStrategy + + public companion object { + /** + * The available [ExperimentStrategyProvider]s. + */ + private val providers by lazy { ServiceLoader.load(ExperimentStrategyProvider::class.java) } + + /** + * Obtain the [ExperimentStrategy] with the specified [id] or return `null`. + */ + public fun findById(id: String): ExperimentStrategyProvider? { + return providers.find { it.id == id } + } + } +} |
