From Agents to Optimization: User Interface Understanding and Generation