Nonlinear shape optimization of flexible mechanical metamaterials