Modelling and optimization of stochastic traffic flows