最优化大作业(三):常用约束最优化方法

  • 问题描述

对以下优化问题

\small minf\left ( X \right )=\frac{1}{3}\left ( x_1+1 \right )^3+x_2

\small s.t.\left\{\begin{matrix} x_1-1\geq 0\\ x_2\geq 0 \end{matrix}\right.

 取初始点\small X_0=\left [ 3,4 \right ]^T,初始惩罚因子\small r_1=10,缩小系数c=0.1,终止限\small \varepsilon =10^{-2}

(1)内点罚函数:

 

  • 基本原理

(1)内点罚函数

最优化大作业(三):常用约束最优化方法_第1张图片

图1  内点罚函数流程图

 

  • 实验结果

(1)内点罚函数

迭代次数

1

2

当前迭代点

(3.00,4.00)

(1.00,0.00)

当前迭代点值

25.33

2.66

                                                                           表1  内点罚函数迭代过程

最优化大作业(三):常用约束最优化方法_第2张图片

图4  内点罚函数迭代过程图

 

  • 代码展示
import matplotlib.pyplot as plt
from sympy import *
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import math

# alpha_x: 惩罚函数或者障碍函数, max_iteration: 梯度下降最大迭代次数
class GD(object):
    def __init__(self, func, alpha_x, max_iteration=5):
        self.func = func
        self.alpha_x = alpha_x
        self.max_iteration = max_iteration

    # 梯度下降求解无约束优化
    def gd(self, X, factor):# factor: 惩罚因子(障碍因子)
        x1, x2 = symbols('x1 x2')
        target_func = self.func + factor * self.alpha_x
        for _ in range(self.max_iteration):
            t = symbols('t')
            derivative_x1 = diff(target_func, x1).evalf(subs={x1: X[0], x2: X[1]})
            derivative_x2 = diff(target_func, x2).evalf(subs={x1: X[0], x2: X[1]})
            target_func_t = target_func.subs({x1: (X[0] - t*derivative_x1), x2: (X[1] - t*derivative_x2)})
            derivative_t = diff(target_func_t, t)
            t = solve(derivative_t, t)[0]
            X[0] = X[0] - t * derivative_x1
            X[1] = X[1] - t * derivative_x2
            print(X)
        return X  # 最优值


def method(X0, func, alpha_x):
    r = 1
    C = 0.1
    X = X0
    x1, x2 = symbols('x1 x2')
    Gd = GD(func, alpha_x)
    penalty_value = r * alpha_x.evalf(subs={x1: X[0], x2: X[1]})
    while(penalty_value > 0.01):
        X = Gd.gd(X, r)
        r = C * r
        penalty_value = r * alpha_x.evalf(subs={x1: X[0], x2: X[1]})

        optimal_value = func.evalf(subs={x1: X[0], x2: X[1]})
        print("极值点为({:.2f},{:.2f}), 极小值为{:.4f}".format(X[0],  X[1], optimal_value))
    return True

def draw():
    plt.figure()
    ax = plt.axes(projection='3d')
    xx = np.arange(-20, 20, 0.1)
    yy = np.arange(-20, 20, 0.1)
    x1, x2 = np.meshgrid(xx, yy)
    Z = np.power(x1+1, 3)/3 + x2
    ax.plot_surface(x1, x2, Z, cmap='rainbow', alpha=0.5)
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    plt.show()

def main():
    x1, x2 = symbols('x1 x2')
    X0 = [3, 4]
    func1 = np.power(x1+1, 3)/3 + x2
    alpha_x1 = 1/(x1-1) + 1/x2
    method(X0, func1, alpha_x1)


if __name__ == '__main__':
    main()

 

你可能感兴趣的:(大作业)