Search code examples
pythonscipyscipy-optimize-minimize

Minimize - eps parameter for each element of array


In scipy.optimize.minimize the eps parameter controls the step size between function evaluations and is applied to all the elements of the optimization vector. How can I apply a different eps to different values of the vector ?


Solution

  • eps, which is accepted by some methods of scipy.optimize.minimize as the step size for finite difference derivative approximations, can be an array (at least for some methods).

    import numpy as np
    from scipy.optimize import minimize
    
    eps = np.asarray([1e-2, 1e-3, 1e-4])  # desired steps
    x0 = np.asarray([100, 10, 1])  # guess
    x_opt = np.asarray([99, 9, 0.9])  # true solution
    
    def f(x):
        print(x - x0)
        return np.linalg.norm(x - x_opt)
    
    res = minimize(f, x0, options={'eps': eps})
    print(f"Solution: {res.x}")
    # [0. 0. 0.]
    # [0.01 0.   0.  ]
    # [0.    0.001 0.   ]
    # [0.e+00 0.e+00 1.e-04]
    # ...
    # Solution: [98.99387011  8.99667335  0.89964598]
    

    If it is not allowed to be an array for the method you are trying to use, you'd need to mention the method you want to use in the question. In that case, your options would be to re-scale the problem or approximate the derivatives manually.