Learning optimal strategies in complex environments