Optimization Theory and Dynamical Systems: Invariant Sets and Invariance Preserving Discretization Methods