The Extended Ritz Method in stochastic functional optimization: an example of dynamic routing in traffic networks