Optimization with piecewise