The evolution of genetic and epigenetic diversity in changing environments