Distributed Multi-Agent Optimization And Control With Applications In Smart Grid