# 简介

• minimize是更通用的优化算法接口，它不仅包含NM算法，也可以调用其他算法；而fmin就只能用NM算法
• fmin的一个好处：支持直接返回优化过程中的迭代参数，有助于可视化。

# 实例1：Himmelblau函数

## 优化流程

def cost_function(x):
return (x[0]**2+x[1]-11)**2+(x[0]+x[1]**2-7)**2

x_center = np.array([0,0])
step = 0.5
x0 = np.vstack((x_center, x_center+np.diag((step,step))))
xtol,ftol = 1e-3,1e-3
xopt,fopt,iter,funcalls,warnflags,allvecs = fmin(cost_function,x_center,initial_simplex=x0,xtol=xtol,ftol=ftol,disp=1,retall=1,full_output=1)
print(xopt,fopt)

n = 50
x = np.linspace(-6,6,n)
y = np.linspace(-6,6,n)
z = np.zeros((n,n))
for i,a in enumerate(x):
for j,b in enumerate(y):
z[i,j] = cost_function([a,b])

xx, yy = np.meshgrid(x,y)
fig, ax = plt.subplots()
c = ax.pcolormesh(xx,yy,z.T,cmap='jet')
fig.colorbar(c, ax=ax)

t = np.asarray(allvecs)
x_, y_ = t[:,0], t[:,1]
ax.plot(x_,y_,'r',x_[0],y_[0],'go',x_[-1],y_[-1],'y+',markersize=6)

fig2 = plt.figure()
ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(212)
ax1.plot(x_)
ax1.set_title('x')
ax2.plot(y_)
ax2.set_title('y')
ax3.plot(ys)


Optimization terminated successfully.
Current function value: 0.000002
Iterations: 35
Function evaluations: 68
[3.00021471 1.99974856] 1.7007332966814985e-06


## 对初值敏感

def cost_function(x):
return (x[0]**2+x[1]-11)**2+(x[0]+x[1]**2-7)**2

n = 50
x = np.linspace(-6,6,n)
y = np.linspace(-6,6,n)
z = np.zeros((n,n))
for i,a in enumerate(x):
for j,b in enumerate(y):
z[i,j] = cost_function([a,b])
xx, yy = np.meshgrid(x,y)
fig, axes = plt.subplots(2, 2, figsize=(12,8))

centers = [[0,0],[-1,0],[0,-1],[-1,-1]]
for i,center in enumerate(centers):
x_center = np.array(center)
step = 0.5
x0 = np.vstack((x_center, x_center+np.diag((step,step))))
xtol,ftol = 1e-3,1e-3
xopt,fopt,iter,funcalls,warnflags,allvecs = fmin(cost_function,x_center,initial_simplex=x0,xtol=xtol,ftol=ftol,disp=1,retall=1,full_output=1)
print(xopt,fopt)

ii,jj = i//2,i%2
ax = axes[ii][jj]
c = ax.pcolormesh(xx,yy,z.T,cmap='jet')
fig.colorbar(c, ax=ax)

t = np.asarray(allvecs)
x_, y_ = t[:,0], t[:,1]
ax.plot(x_,y_,'r',x_[0],y_[0],'go',x_[-1],y_[-1],'y+',markersize=6)


Optimization terminated successfully.
Current function value: 0.000002
Iterations: 35
Function evaluations: 68
[3.00021471 1.99974856] 1.7007332966814985e-06
Optimization terminated successfully.
Current function value: 0.000003
Iterations: 31
Function evaluations: 60
[-2.80534914  3.13148297] 2.851032148018009e-06
Optimization terminated successfully.
Current function value: 0.000001
Iterations: 40
Function evaluations: 74
[ 3.58440786 -1.8484002 ] 1.1592413943034185e-06
Optimization terminated successfully.
Current function value: 0.000002
Iterations: 37
Function evaluations: 69
[-3.77937271 -3.28299281] 2.2138273459910166e-06


04-09 5万+

06-14 7339

01-07 1064

04-13 608

08-09 5万+

04-15 612

10-31 952

09-08 1983

04-04 152

02-22 8335

09-08 1046

06-29 220

09-24 3542

05-17 368

09-08 3272

07-06 2054

01-14 5007

06-05 99

04-11 1万+

03-19 82万+

04-14 61万+

02-28 1万+

03-01 14万+

03-08 7万+

04-25 7万+

03-10 13万+

03-10 19万+

03-12 12万+

03-13 12万+

#### 我入职阿里后，才知道原来简历这么写

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客