Optimal power flow algorithm