Optimization and model-based control for max-plus linear and continuous piecewise affine systems