A reinforcement learning algorithm for speed optimization and optimal energy management of advanced driver assistance systems and connected vehicles