The Emergence of Cooperative Playing Routines: Optimality and Learning