Scipy最小化返回的值高于最小值

2024-10-04 11:33:20 发布

您现在位置:Python中文网/ 问答频道 /正文

作为多启动优化的一部分,我正在运行差分进化(DE),我将其输出作为初始值输入到带有SLSQP的scipy最小化(我需要约束)

我正在测试Ackley函数的程序。即使在DE返回最佳值(零)的情况下,scipy最小化也会偏离最佳初始值,并返回高于最佳值的值

您知道如何使scipy最小化返回最佳值吗?我注意到它有助于指定scipy最小化的容差,但它确实完全解决了这个问题。缩放目标函数会让事情变得更糟。COBYLA解算器不存在此问题

以下是优化步骤:

# Set up
x0min = -20
x0max = 20
xdim = 4
fun = ackley
bounds = [(x0min,x0max)] * xdim
tol = 1e-12


# Get a DE solution
result = differential_evolution(fun, bounds, 
                                maxiter=10000,
                                tol=tol,
                                workers = 1, 
                                init='latinhypercube')

# Initialize at DE output
x0 = result.x

# Estimate the model
r = minimize(fun, x0, method='SLSQP', tol=1e-18)

在我的例子中,这会产生

result.fun = -4.440892098500626e-16
r.fun = 1.0008238682246429e-09

result.x = array([0., 0., 0., 0.])
r.x = array([-1.77227927e-10, -1.77062108e-10,  4.33179228e-10, -2.73031830e-12])

以下是Ackley函数的实现:

def ackley(x):
    # Computes the value of Ackley benchmark function.
    # ACKLEY accepts a matrix of size (dim,N) and returns a vetor
    # FVALS of size (N,)

    # Parameters
    # ----------
    # x : 1-D array size (dim,) or a 2-D array size (dim,N)
    #     Each row of the matrix represents one dimension. 
    #     Columns have therefore the interpretation of different points at which
    #     the function is evaluated.  N is number of points to be evaluated.
 
    # Returns
    # -------
    # fvals : a scalar if x is a 1-D array or 
    #         a 1-D array size (N,) if x is a 2-D array size (dim,N)
    #         in which each row contains the function value for each column of X.

    n = x.shape[0]
    ninverse = 1 / n
    sum1 = np.sum(x**2, axis=0)
    sum2 = np.sum(np.cos(2 * np.pi * x), axis=0)
    
    fvals = (20 + np.exp(1) - (20 * np.exp(-0.2 * np.sqrt( ninverse * sum1))) 
             - np.exp( ninverse * sum2))
    return fvals

Tags: ofthe函数sizeisnpdefunction