Grammaticalization as Optimization