Algorithm design for optimal power flow, security-constrained unit commitment, and demand response in energy systems