Dynamic optimization for distributed energy production