Optimization and Model-Based Control for Max-Plus Linear and Continuous Piecewise Affine Systems