Adaptive evolution of phenotypic switching during environmental change