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

it.unibo.alchemist.model.actions.ToggleMoleculeRandomly.kt Maven / Gradle / Ivy

Go to download

Abstract, incarnation independent implementations of the Alchemist's interfaces. Provides support for those who want to write incarnations.

There is a newer version: 35.0.1
Show newest version
/*
 * Copyright (C) 2010-2023, Danilo Pianini and contributors
 * listed, for each module, in the respective subproject's build.gradle.kts file.
 *
 * This file is part of Alchemist, and is distributed under the terms of the
 * GNU General Public License, with a linking exception,
 * as described in the file LICENSE in the Alchemist distribution's top directory.
 */

package it.unibo.alchemist.model.actions

import it.unibo.alchemist.model.Molecule
import it.unibo.alchemist.model.Node
import it.unibo.alchemist.model.Reaction
import org.apache.commons.math3.random.RandomGenerator

/**
 * Treats [molecule] as a probabilistic switch:
 * * if it is present, then with probability [odds] it's removed from [node];
 * * otherwise, with probability [odds] it is inserted in [node] with the provided [concentration].
 *
 * @param  concentration type
 * @param node the node containing the molecule to toggle
 * @param randomGenerator random number generator to use
 * @param molecule the molecule to toggle
 * @param concentration the concentration to use for the "on" state
 * @param odds probability to toggle the molecule every time the action is triggered
 */
open class ToggleMoleculeRandomly(
    node: Node,
    private val randomGenerator: RandomGenerator,
    molecule: Molecule,
    concentration: T,
    private val odds: Double,
) : ToggleMolecule(node, molecule, concentration) {

    init {
        require(odds in 0.0..1.0) {
            "Probability of toggling should in [0, 1], provided: $odds"
        }
    }

    override fun cloneAction(node: Node, reaction: Reaction) =
        ToggleMoleculeRandomly(node, randomGenerator, molecule, concentration, odds)

    /**
     * Rolls the dice and toggles the molecule.
     */
    override fun execute() {
        if (shouldToggle()) {
            super.execute()
        }
    }

    /**
     * Returns true if it is time to toggle the molecule.
     */
    protected open fun shouldToggle() = randomGenerator.nextDouble() <= odds
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy