An optimization-based computational model for domain evolution in polycrystalline ferroelastics