• 【optimtool.unconstrain】无约束优化工具箱


    【optimtool.unconstrain】无约束优化工具箱

    # import packages
    %matplotlib inline
    import sympy as sp
    import matplotlib.pyplot as plt
    import optimtool as oo
    
    • 1
    • 2
    • 3
    • 4
    • 5
    def train(funcs, args, x_0):
        f_list = []
        title = ["gradient_descent_barzilar_borwein", "newton_CG", "newton_quasi_L_BFGS", "trust_region_steihaug_CG"]
        colorlist = ["maroon", "teal", "slateblue", "orange"]
        _, _, f = oo.unconstrain.gradient_descent.barzilar_borwein(funcs, args, x_0, False, True)
        f_list.append(f)
        _, _, f = oo.unconstrain.newton.CG(funcs, args, x_0, False, True)
        f_list.append(f)
        _, _, f = oo.unconstrain.newton_quasi.L_BFGS(funcs, args, x_0, False, True)
        f_list.append(f)
        _, _, f = oo.unconstrain.trust_region.steihaug_CG(funcs, args, x_0, False, True)
        f_list.append(f)
        return colorlist, f_list, title
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    # 可视化函数:传参接口(颜色列表,函数值列表,标题列表)
    def test(colorlist, f_list, title):
        handle = []
        for j, z in zip(colorlist, f_list):
            ln, = plt.plot([i for i in range(len(z))], z, c=j, marker='o', linestyle='dashed')
            handle.append(ln)
        plt.xlabel("$Iteration \ times \ (k)$")
        plt.ylabel("$Objective \ function \ value: \ f(x_k)$")
        plt.legend(handle, title)
        plt.title("Performance Comparison")
        return None
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Extended Freudenstein & Roth function

    f ( x ) = ∑ i = 1 n / 2 ( − 13 + x 2 i − 1 + ( ( 5 − x 2 i ) x 2 i − 2 ) x 2 i ) 2 + ( − 29 + x 2 i − 1 + ( ( x 2 i + 1 ) x 2 i − 14 ) x 2 i ) 2 , x 0 = [ 0.5 , − 2 , 0.5 , − 2 , . . . , 0.5 , − 2 ] . f(x)=\sum_{i=1}^{n/2}(-13+x_{2i-1}+((5-x_{2i})x_{2i}-2)x_{2i})^2+(-29+x_{2i-1}+((x_{2i}+1)x_{2i}-14)x_{2i})^2, x_0=[0.5, -2, 0.5, -2, ..., 0.5, -2]. f(x)=i=1n/2(13+x2i1+((5x2i)x2i2)x2i)2+(29+x2i1+((x2i+1)x2i14)x2i)2,x0=[0.5,2,0.5,2,...,0.5,2].

    # make data(4 dimension)
    x = sp.symbols("x1:5")
    f = (-13 + x[0] + ((5 - x[1])*x[1] - 2)*x[1])**2 + \
        (-29 + x[0] + ((x[1] + 1)*x[1] - 14)*x[1])**2 + \
        (-13 + x[2] + ((5 - x[3])*x[3] - 2)*x[3])**2 + \
        (-29 + x[2] + ((x[3] + 1)*x[3] - 14)*x[3])**2
    x_0 = (1, -1, 1, -1) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Extended Trigonometric function:

    f ( x ) = ∑ i = 1 n ( ( n − ∑ j = 1 n cos ⁡ x j ) + i ( 1 − cos ⁡ x i ) − sin ⁡ x i ) 2 , x 0 = [ 0.2 , 0.2 , . . . , 0.2 ] f(x)=\sum_{i=1}^{n}((n-\sum_{j=1}^{n}\cos x_j)+i(1-\cos x_i)-\sin x_i)^2, x_0=[0.2, 0.2, ...,0.2] f(x)=i=1n((nj=1ncosxj)+i(1cosxi)sinxi)2,x0=[0.2,0.2,...,0.2]

    # make data(2 dimension)
    x = sp.symbols("x1:3")
    f = (2 - (sp.cos(x[0]) + sp.cos(x[1])) + (1 - sp.cos(x[0])) - sp.sin(x[0]))**2 + \
        (2 - (sp.cos(x[0]) + sp.cos(x[1])) + 2 * (1 - sp.cos(x[1])) - sp.sin(x[1]))**2
    x_0 = (0.1, 0.1) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Extended Rosenbrock function

    f ( x ) = ∑ i = 1 n / 2 c ( x 2 i − x 2 i − 1 2 ) 2 + ( 1 − x 2 i − 1 ) 2 , x 0 = [ − 1.2 , 1 , . . . , − 1.2 , 1 ] . c = 100 f(x)=\sum_{i=1}^{n/2}c(x_{2i}-x_{2i-1}^2)^2+(1-x_{2i-1})^2, x_0=[-1.2, 1, ...,-1.2, 1]. c=100 f(x)=i=1n/2c(x2ix2i12)2+(1x2i1)2,x0=[1.2,1,...,1.2,1].c=100

    # make data(4 dimension)
    x = sp.symbols("x1:5")
    f = 100 * (x[1] - x[0]**2)**2 + \
        (1 - x[0])**2 + \
        100 * (x[3] - x[2]**2)**2 + \
        (1 - x[2])**2
    x_0 = (-2, 2, -2, 2) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Generalized Rosenbrock function

    f ( x ) = ∑ i = 1 n − 1 c ( x i + 1 − x i 2 ) 2 + ( 1 − x i ) 2 , x 0 = [ − 1.2 , 1 , . . . , − 1.2 , 1 ] , c = 100. f(x)=\sum_{i=1}^{n-1}c(x_{i+1}-x_i^2)^2+(1-x_i)^2, x_0=[-1.2, 1, ...,-1.2, 1], c=100. f(x)=i=1n1c(xi+1xi2)2+(1xi)2,x0=[1.2,1,...,1.2,1],c=100.

    # make data(2 dimension)
    x = sp.symbols("x1:3")
    f = 100 * (x[1] - x[0]**2)**2 + (1 - x[0])**2
    x_0 = (-1, 0.5) # Random given
    
    • 1
    • 2
    • 3
    • 4
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Extended White & Holst function

    f ( x ) = ∑ i = 1 n / 2 c ( x 2 i − x 2 i − 1 3 ) 2 + ( 1 − x 2 i − 1 ) 2 , x 0 = [ − 1.2 , 1 , . . . , − 1.2 , 1 ] . c = 100 f(x)=\sum_{i=1}^{n/2}c(x_{2i}-x_{2i-1}^3)^2+(1-x_{2i-1})^2, x_0=[-1.2, 1, ...,-1.2, 1]. c=100 f(x)=i=1n/2c(x2ix2i13)2+(1x2i1)2,x0=[1.2,1,...,1.2,1].c=100

    # make data(4 dimension)
    x = sp.symbols("x1:5")
    f = 100 * (x[1] - x[0]**3)**2 + \
        (1 - x[0])**2 + \
        100 * (x[3] - x[2]**3)**2 + \
        (1 - x[2])**2
    x_0 = (-1, 0.5, -1, 0.5) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Extended Penalty function

    f ( x ) = ∑ i = 1 n − 1 ( x i − 1 ) 2 + ( ∑ j = 1 n x j 2 − 0.25 ) 2 , x 0 = [ 1 , 2 , . . . , n ] . f(x)=\sum_{i=1}^{n-1} (x_i-1)^2+(\sum_{j=1}^{n}x_j^2-0.25)^2, x_0=[1,2,...,n]. f(x)=i=1n1(xi1)2+(j=1nxj20.25)2,x0=[1,2,...,n].

    # make data(4 dimension)
    x = sp.symbols("x1:5")
    f = (x[0] - 1)**2 + (x[1] - 1)**2 + (x[2] - 1)**2 + \
        ((x[0]**2 + x[1]**2 + x[2]**2 + x[3]**2) - 0.25)**2
    x_0 = (5, 5, 5, 5) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Perturbed Quadratic function

    f ( x ) = ∑ i = 1 n i x i 2 + 1 100 ( ∑ i = 1 n x i ) 2 , x 0 = [ 0.5 , 0.5 , . . . , 0.5 ] . f(x)=\sum_{i=1}^{n}ix_i^2+\frac{1}{100}(\sum_{i=1}^{n}x_i)^2, x_0=[0.5,0.5,...,0.5]. f(x)=i=1nixi2+1001(i=1nxi)2,x0=[0.5,0.5,...,0.5].

    # make data(4 dimension)
    x = sp.symbols("x1:5")
    f = x[0]**2 + 2*x[1]**2 + 3*x[2]**2 + 4*x[3]**2 + \
        0.01 * (x[0] + x[1] + x[2] + x[3])**2
    x_0 = (1, 1, 1, 1) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Raydan 1 function

    f ( x ) = ∑ i = 1 n i 10 ( exp ⁡ x i − x i ) , x 0 = [ 1 , 1 , . . . , 1 ] . f(x)=\sum_{i=1}^{n}\frac{i}{10}(\exp{x_i}-x_i), x_0=[1,1,...,1]. f(x)=i=1n10i(expxixi),x0=[1,1,...,1].

    # make data(4 dimension)
    x = sp.symbols("x1:5")
    f = 0.1 * (sp.exp(x[0]) - x[0]) + \
        0.2 * (sp.exp(x[1]) - x[1]) + \
        0.3 * (sp.exp(x[2]) - x[2]) + \
        0.4 * (sp.exp(x[3]) - x[3])
    x_0 = (0.5, 0.5, 0.5, 0.5) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Raydan 2 function

    f ( x ) = ∑ i = 1 n ( exp ⁡ x i − x i ) , x 0 = [ 1 , 1 , . . . , 1 ] . f(x)=\sum_{i=1}^{n}(\exp{x_i}-x_i), x_0=[1,1,...,1]. f(x)=i=1n(expxixi),x0=[1,1,...,1].

    # make data(4 dimension)
    x = sp.symbols("x1:5")
    f = (sp.exp(x[0]) - x[0]) + \
        (sp.exp(x[1]) - x[1]) + \
        (sp.exp(x[2]) - x[2]) + \
        (sp.exp(x[3]) - x[3])
    x_0 = (2, 2, 2, 2) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Diagonal 1 function

    f ( x ) = ∑ i = 1 n ( exp ⁡ x i − i x i ) , x 0 = [ 1 / n , 1 / n , . . . , 1 / n ] . f(x)=\sum_{i=1}^{n}(\exp{x_i}-ix_i), x_0=[1/n,1/n,...,1/n]. f(x)=i=1n(expxiixi),x0=[1/n,1/n,...,1/n].

    # make data(4 dimension)
    x = sp.symbols("x1:5")
    f = (sp.exp(x[0]) - x[0]) + \
        (sp.exp(x[1]) - 2 * x[1]) + \
        (sp.exp(x[2]) - 3 * x[2]) + \
        (sp.exp(x[3]) - 4 * x[3])
    x_0 = (0.5, 0.5, 0.5, 0.5) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Diagonal 2 function

    f ( x ) = ∑ i = 1 n ( exp ⁡ x i − x i i ) , x 0 = [ 1 / 1 , 1 / 2 , . . . , 1 / n ] . f(x)=\sum_{i=1}^{n}(\exp{x_i}-\frac{x_i}{i}), x_0=[1/1,1/2,...,1/n]. f(x)=i=1n(expxiixi),x0=[1/1,1/2,...,1/n].

    # make data(4 dimension)
    x = sp.symbols("x1:5")
    f = (sp.exp(x[0]) - x[0]) + \
        (sp.exp(x[1]) - x[1] / 2) + \
        (sp.exp(x[2]) - x[2] / 3) + \
        (sp.exp(x[3]) - x[3] / 4)
    x_0 = (0.9, 0.6, 0.4, 0.3) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Diagonal 3 function

    f ( x ) = ∑ i = 1 n ( exp ⁡ x i − i sin ⁡ ( x i ) ) , x 0 = [ 1 , 1 , . . . , 1 ] . f(x)=\sum_{i=1}^{n}(\exp{x_i}-i\sin(x_i)), x_0=[1,1,...,1]. f(x)=i=1n(expxiisin(xi)),x0=[1,1,...,1].

    # make data(4 dimension)
    x = sp.symbols("x1:5")
    f = (sp.exp(x[0]) - sp.sin(x[0])) + \
        (sp.exp(x[1]) - 2 * sp.sin(x[1])) + \
        (sp.exp(x[2]) - 3 * sp.sin(x[2])) + \
        (sp.exp(x[3]) - 4 * sp.sin(x[3]))
    x_0 = (0.5, 0.5, 0.5, 0.5) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Hager function

    f ( x ) = ∑ i = 1 n ( exp ⁡ x i − i x i ) , x 0 = [ 1 , 1 , . . . , 1 ] . f(x)=\sum_{i=1}^{n}(\exp{x_i}-\sqrt{i}x_i), x_0=[1,1,...,1]. f(x)=i=1n(expxii xi),x0=[1,1,...,1].

    # make data(4 dimension)
    x = sp.symbols("x1:5")
    f = (sp.exp(x[0]) - x[0]) + \
        (sp.exp(x[1]) - sp.sqrt(2) * x[1]) + \
        (sp.exp(x[2]) - sp.sqrt(3) * x[2]) + \
        (sp.exp(x[3]) - sp.sqrt(4) * x[3])
    x_0 = (0.5, 0.5, 0.5, 0.5) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Generalized Tridiagonal 1 function

    f ( x ) = ∑ i = 1 n − 1 ( x i + x i + 1 − 3 ) 2 + ( x i − x i + 1 + 1 ) 4 , x 0 = [ 2 , 2 , . . . , 2 ] . f(x)=\sum_{i=1}^{n-1}(x_i+x_{i+1}-3)^2+(x_i-x_{i+1}+1)^4, x_0=[2,2,...,2]. f(x)=i=1n1(xi+xi+13)2+(xixi+1+1)4,x0=[2,2,...,2].

    # make data(3 dimension)
    x = sp.symbols("x1:4")
    f = (x[0] + x[1] - 3)**2 + (x[0] - x[1] + 1)**4 + \
        (x[1] + x[2] - 3)**2 + (x[1] - x[2] + 1)**4
    x_0 = (1, 1, 1) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Extended Tridiagonal 1 function:

    f ( x ) = ∑ i = 1 n / 2 ( x 2 i − 1 + x 2 i − 3 ) 2 + ( x 2 i − 1 − x 2 i + 1 ) 4 , x 0 = [ 2 , 2 , . . . , 2 ] . f(x)=\sum_{i=1}^{n/2}(x_{2i-1}+x_{2i}-3)^2+(x_{2i-1}-x_{2i}+1)^4, x_0=[2,2,...,2]. f(x)=i=1n/2(x2i1+x2i3)2+(x2i1x2i+1)4,x0=[2,2,...,2].

    # make data(2 dimension)
    x = sp.symbols("x1:3")
    f = (x[0] + x[1] - 3)**2 + (x[0] - x[1] + 1)**4
    x_0 = (1, 1) # Random given
    
    • 1
    • 2
    • 3
    • 4
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2

    Extended TET function : (Three exponential terms)

    f ( x ) = ∑ i = 1 n / 2 ( ( exp ⁡ x 2 i − 1 + 3 x 2 i − 0.1 ) + exp ⁡ ( x 2 i − 1 − 3 x 2 i − 0.1 ) + exp ⁡ ( − x 2 i − 1 − 0.1 ) ) , x 0 = [ 0.1 , 0.1 , . . . , 0.1 ] . f(x)=\sum_{i=1}^{n/2}((\exp x_{2i-1} + 3x_{2i} - 0.1) + \exp (x_{2i-1} - 3x_{2i} - 0.1) + \exp (-x_{2i-1}-0.1)), x_0=[0.1,0.1,...,0.1]. f(x)=i=1n/2((expx2i1+3x2i0.1)+exp(x2i13x2i0.1)+exp(x2i10.1)),x0=[0.1,0.1,...,0.1].

    # make data(4 dimension)
    x = sp.symbols("x1:5")
    f = sp.exp(x[0] + 3*x[1] - 0.1) + sp.exp(x[0] - 3*x[1] - 0.1) + sp.exp(-x[0] - 0.1) + \
        sp.exp(x[2] + 3*x[3] - 0.1) + sp.exp(x[2] - 3*x[3] - 0.1) + sp.exp(-x[2] - 0.1)
    x_0 = (0.2, 0.2, 0.2, 0.2) # Random given
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # train
    color, values, title = train(funcs=f, args=x, x_0=x_0)
    
    • 1
    • 2
    # test
    test(color, values, title)
    
    • 1
    • 2
  • 相关阅读:
    java毕业生设计员工婚恋交友平台计算机源码+系统+mysql+调试部署+lw
    C++ 学习(四)程序流程结构 - 顺序结构、选择结构、循环结构、跳转语句
    C++算法:全 O(1) 的数据结构
    华为交换机一端口3网段,跨网段通讯
    Terraform 初始化慢~配置本地离线源
    旋转矩阵与欧拉角的相互转换
    LVGL v8学习笔记 | 07 - 字体的使用方法
    操作系统实现-loader
    jenkins的安装配置并集成jdk、git
    jvm启动流程
  • 原文地址:https://blog.csdn.net/linjing_zyq/article/details/125572021