Optimal Gait Switching: Using Max-plus Linear Systems