python codes for optimizing sequence under constraints given as transition probabilites