Energy optimization of algebraic multigrid bases