【发布时间】:2020-10-22 16:55:18
【问题描述】:
我正在构建一些弹簧网络,以便构建橡胶牵引力测试模型。我在我的网络的一些节点上施加位移,并锁定其他一些节点,然后对系统的能量运行最小化函数以获得稳定的系统。
首先,使用Nelder-Mead 方法时,我遇到了分歧问题,系统的能量随着迭代次数的增加而增长,所以我正在尝试鲍威尔的方法。方向向量应该有助于我的函数向正确的方向“移动”我的节点。
很遗憾,我仍然收到错误消息。
这是我的代码:
import scipy as sc
import scipy.optimize as opt
import numpy as np
### Scipy.optimize.minimize with Powell's method
# Algorithm' starting point
x0 = np.array([1., 1., 2., 1., 2., 3., 1., 2., 3., 1., 2., 3., 2., 3., 3., 0., 1.,
0., 2., 1., 0., 3., 2., 1., 4., 3., 2., 4., 3., 4.])
# Setting up the variables we will need
Bounds= opt.Bounds(lb=0, ub=10)
Direc = (np.array([[1, 1, 2, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 1, 2,-1, 0, 1,-2,-1, 0,-2,-1,-2]]))
# Run the optimization function
x_pow = opt.minimize(minimize_energy, x0, method='Powell', tol=1e-6, bounds = Bounds,
options={'maxiter': 1e4, 'disp': True, 'direc':Direc})
这是我得到的错误:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-23-f0bb1bfcaa17> in <module>
8 # Run the optimization function
9 x_pow = opt.minimize(minimize_energy, x0, method='Powell', tol=1e-6, bounds = Bounds,
---> 10 options={'maxiter': 1e4, 'disp': True, 'direc':Direc})
11
12 print('final x')
C:\Bib\envs\statmath3\lib\site-packages\scipy\optimize\_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
598 return _minimize_neldermead(fun, x0, args, callback, **options)
599 elif meth == 'powell':
--> 600 return _minimize_powell(fun, x0, args, callback, **options)
601 elif meth == 'cg':
602 return _minimize_cg(fun, x0, args, jac, callback, **options)
C:\Bib\envs\statmath3\lib\site-packages\scipy\optimize\optimize.py in _minimize_powell(func, x0, args, callback, xtol, ftol, maxiter, maxfev, disp, direc, return_all, **unknown_options)
2638 delta = 0.0
2639 for i in ilist:
-> 2640 direc1 = direc[i]
2641 fx2 = fval
2642 fval, x, direc1 = _linesearch_powell(func, x, direc1,
IndexError: index 2 is out of bounds for axis 0 with size 2
我不明白为什么我的方向向量与x0 的长度相同,而我的方向向量的长度与将被优化的参数向量相同。
感谢您的帮助!
【问题讨论】:
-
鲍威尔是否期望 N 个方向向量,其中 N 是 x0 的长度?我知道它在默认情况下使用的是什么,但我不确定它是否是严格要求的。
标签: python scipy scipy-optimize scipy-optimize-minimize