Car Wash

In this example, we'll learn how to wait for resources. The example is adopted from the SimPy example.

We simulate a carwash with a limited number of machines and a number of cars that arrive at the carwash to get cleaned. The carwash uses a resource to model the limited number of washing machines. It also defines a process for washing a car.

When a car arrives at the carwash, it requests a machine. Once it got one, it starts the carwash’s wash processes and waits for it to finish. It finally releases the machine and leaves.

The cars are generated by a setup process. After creating an initial amount of cars it creates new car processes after a random time interval as long as the simulation continues.

import org.kalasim.*

 *  A carwash has a limited number of washing machines and defines
 * a washing processes that takes some (random) time.
 * Car processes arrive at the carwash at a random time. If one washing
 * machine is available, they start the washing process and wait for it
 * to finish. If not, they wait until they an use one.
fun main() {

    val RANDOM_SEED = 42
    val NUM_MACHINES = 2  // Number of machines in the carwash
    val WASHTIME = 5      // Minutes it takes to clean a car
    val T_INTER = 7.0       // Create a car every ~7 minutes
    val SIM_TIME = 20     // Simulation time in minutes

    class Car : Component() {
        override fun process() = sequence {
            val carWash = get<Resource>()

    val env = declareDependencies {
        add { Resource("carwash", NUM_MACHINES) }
    }.createSimulation(true, randomSeed = RANDOM_SEED) {

        //Create 4 initial cars
        repeat(3) { Car() }
        // Create more cars while the simulation is running
        ComponentGenerator(iat = uniform(T_INTER - 2, T_INTER + 2)) { Car() }

    println("Check out while simulating ... ;-)")

    // Start the simulation = SIM_TIME.asTickTime())