added a generalized algorithm for parameter optimization