We propose a new optimization strategy based on inducing stop-and-go waves on the main road and controlling their wavelength. Using numerical simulations of a recent stochastic car-following model we show that this strategy yields optimization of traffic flow when implemented in systems with a localized periodic inhomogeneity, such as signalized intersections and entry ramps. The optimization process is explained by our finding of a generalized fundamental diagram (GFD) for traffic, namely a flux-density-wavelength relation. Projecting the GFD on the density-flux plane yields a two-dimensional region of stable states, qualitatively similar to that found empirically [Kerner, Phys. Rev. Lett. 81, 3797 (1998)] in synchronized traffic.