Optimal control of demand response in a smart grid