栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Scipy优化使用教程】三、全局优化

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Scipy优化使用教程】三、全局优化

参考官网:Scipy.

全局优化

eggholder函数使用全局优化全部代码
全局优化的目的是在可能存在许多局部最小值的情况下,在给定的范围内找到一个函数的全局最小值。通常情况下,全局优化器会有效地搜索参数空间,同时也会使用局部优化器(如最小化)。SciPy包含了许多优秀的全局优化器。在这里,我们将在同一目标函数上使用这些优化器,即 eggholder函数。

eggholder函数
def eggholder(x):
    return (-(x[1] + 47) * np.sin(np.sqrt(abs(x[0]/2 + (x[1]  + 47))))
            -x[0] * np.sin(np.sqrt(abs(x[0] - (x[1]  + 47)))))

bounds = [(-512, 512), (-512, 512)]

查看其形状:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.arange(-512, 513)
y = np.arange(-512, 513)
xgrid, ygrid = np.meshgrid(x, y)
xy = np.stack([xgrid, ygrid])

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.view_init(45, -45)
ax.plot_surface(xgrid, ygrid, eggholder(xy), cmap='terrain')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('eggholder(x, y)')
plt.show()

使用全局优化

我们现在使用全局优化器来获得最小值和最小值的函数值。我们将把结果存储在一个字典里,这样我们以后就可以比较不同的优化结果。

from scipy import optimize
results = dict()
results['shgo'] = optimize.shgo(eggholder, bounds)
print(results['shgo'])

其结果为:

     fun: -935.3379515604948
    funl: array([-935.33795156])
 message: 'Optimization terminated successfully.'
    nfev: 47
     nit: 2
   nlfev: 42
   nlhev: 0
   nljev: 10
 success: True
       x: array([439.48097554, 453.97741501])
      xl: array([[439.48097554, 453.97741501]])

另一种优化器:

results['DA'] = optimize.dual_annealing(eggholder, bounds)
print(results['DA'])

结果为:

fun: -959.6406627208503
 message: ['Maximum number of iteration reached']
    nfev: 4079
    nhev: 0
     nit: 1000
    njev: 26
  status: 0
 success: True
       x: array([512.        , 404.23180442])

所有的优化器都会返回一个OptimizeResult,除了解决方案之外,它还包含了函数评估的数量、优化是否成功等信息。
还有一种优化器为:

results['DE'] = optimize.differential_evolution(eggholder, bounds)
print(results['DE'])

其结果为:

     fun: -894.578900390448
     jac: array([1.13686748e-05, 1.13686748e-05])
 message: 'Optimization terminated successfully.'
    nfev: 588
     nit: 18
 success: True
       x: array([-465.69411618,  385.71669541])

另外shgo有第二个方法,它返回所有的局部最小值,而不是只返回它认为是全局最小值的值。

results['shgo_sobol'] = optimize.shgo(eggholder, bounds, n=200, iters=5,
                                      sampling_method='sobol')
全部代码
import numpy as np
def eggholder(x):
    return (-(x[1] + 47) * np.sin(np.sqrt(abs(x[0]/2 + (x[1]  + 47))))
            -x[0] * np.sin(np.sqrt(abs(x[0] - (x[1]  + 47)))))

bounds = [(-512, 512), (-512, 512)]

from scipy import optimize
results = dict()
#results['shgo'] = optimize.shgo(eggholder, bounds)
#print(results['shgo'])
#results['DA'] = optimize.dual_annealing(eggholder, bounds)
#print(results['DA'])
#results['DE'] = optimize.differential_evolution(eggholder, bounds)
#print(results['DE'])
results['shgo_sobol'] = optimize.shgo(eggholder, bounds, n=200, iters=5,
                                      sampling_method='sobol')
print(results['shgo_sobol'])
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/744179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号