优化非线性复杂系统的参数
非线性项组合的系统
对于系统中的每一个复杂拟合,即每一个残差函数,都能表示为非线性方程的趋势,例如较为复杂的系统函数组,
from optimtool.base import sp, np
x = sp.symbols("x1:5")
res1 = 0.5*x[0] + 0.2*x[1] + 1.5*x[0]**3 - 2*x[2] # Damped oscillator with nonlinear term
res2 = 3*x[0] + x[1]**2 - x[2]**2 - 0.1*np.random.normal() # Coupled oscillator system
res3 = x[2]*(1 - x[3]) - x[0]*x[1] + 1.5*sp.sin(x[3]) # Predator-prey like interaction
res4 = x[3]*(x[0] - x[1]) + 0.5*sp.exp(-x[2]) - 2.0*x[1] # Delay differential equation component
要解上述方程组,需要用到线搜索、非线性最小二乘、系统初始状态,这些方法在optimtool的nonlinear_least_square模块中能找到。在给定系统初始状态为(1.0, 0.5, 0.2, 0.8),默认线搜索的前提下,优化的系统组是[res1, res2, res3, res4],使用如下指令调用levenberg_marquardt方法,
import optimtool.unconstrain as ou
ou.nonlinear_least_square.levenberg_marquardt([res1, res2, res3, res4], x, (1.0, 0.5, 0.2, 0.8), verbose=True, epsilon=1e-3)
系统内的迭代趋势如下,一个系统的解在优化过程里的局部最优解可以有多个,这里给出的是系统达到收敛阈值的平滑区域的解,
(1.0, 0.5, 0.2, 0.8) 6.457126524403201 0
[ 0.40429326 0.43564665 -0.17927255 1.02536062] 1.7366068984048804 1
[0.1438984 0.25405175 0.10550085 0.32590509] 0.2107042535909221 2
[0.04771543 0.25630044 0.0574562 0.0532376 ] 0.014097257076394237 3
[ 0.02155738 0.24964789 0.0327689 -0.00291764] 0.0005100292092591925 4
[ 0.01767332 0.2456185 0.02926977 -0.01465774] 1.103580671559125e-05 5
[ 0.01711515 0.24490507 0.02880226 -0.01647978] 1.2913438683552677e-07 6
可视化图例中迭代点快速收敛到局部最优的原因是wolfe线搜索方法和非线性系统的乘积组合funcs=(1/2)funcr.T*funcr,也是最小二乘法的线搜索基础。