Carwash

Covers:

  • Waiting for other processes
  • Resources: Resource

The Carwash example is a simulation of 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 intial amount of cars it creates new car processes after a random time interval as long as the simulation continues.

using SimJulia
using Distributions

const RANDOM_SEED = 09011977
const NUM_MACHINES = 2  # Number of machines in the carwash
const WASHTIME = 5.0    # Minutes it takes to clean a car
const T_INTER = 5.0     # Create a car every ~5 minutes
const SIM_TIME = 30.0   # Simulation time in minutes

function wash(env::Environment)
  yield(Timeout(env, WASHTIME))
end

function car(env::Environment, cw::Resource)
  println("$(active_process(env)) arrives at the carwash at time $(round(now(env), 2)).")
  yield(Request(cw))
  println("$(active_process(env)) enters the carwash at time $(round(now(env), 2)).")
  yield(Process(env, wash))
  println("$(active_process(env)) leaves the carwash at time $(round(now(env), 2)).")
  yield(Release(cw))
end

function setup(env::Environment)
  cw = Resource(env, NUM_MACHINES)
  for i = 1:4
    Process(env, "Car $i", car, cw)
  end
  d = Uniform(T_INTER-2.0, T_INTER+2.0)
  i = 4
  while true
    yield(Timeout(env, rand(d)))
    Process(env, "Car $(i+=1)", car, cw)
  end
end

# Setup and start the simulation
println("Carwash")
srand(RANDOM_SEED)

# Create an environment and start the setup process
env = Environment()
Process(env, setup)

# Execute!
run(env, SIM_TIME)

The simulation’s output:

Carwash
Car 1 arrives at the carwash at time 0.0.
Car 2 arrives at the carwash at time 0.0.
Car 3 arrives at the carwash at time 0.0.
Car 4 arrives at the carwash at time 0.0.
Car 1 enters the carwash at time 0.0.
Car 2 enters the carwash at time 0.0.
Car 5 arrives at the carwash at time 3.89.
Car 1 leaves the carwash at time 5.0.
Car 2 leaves the carwash at time 5.0.
Car 3 enters the carwash at time 5.0.
Car 4 enters the carwash at time 5.0.
Car 6 arrives at the carwash at time 9.02.
Car 3 leaves the carwash at time 10.0.
Car 4 leaves the carwash at time 10.0.
Car 5 enters the carwash at time 10.0.
Car 6 enters the carwash at time 10.0.
Car 7 arrives at the carwash at time 12.56.
Car 5 leaves the carwash at time 15.0.
Car 6 leaves the carwash at time 15.0.
Car 7 enters the carwash at time 15.0.
Car 8 arrives at the carwash at time 17.14.
Car 8 enters the carwash at time 17.14.
Car 7 leaves the carwash at time 20.0.
Car 9 arrives at the carwash at time 21.49.
Car 9 enters the carwash at time 21.49.
Car 8 leaves the carwash at time 22.14.
Car 9 leaves the carwash at time 26.49.
Car 10 arrives at the carwash at time 26.51.
Car 10 enters the carwash at time 26.51.