Optimal control of smart distributed power and energy systems