Ecological effects on the evolution of cooperative behaviors