最优化大作业(三):常用约束最优化方法
程序员文章站
2022-04-22 13:40:34
...
- 问题描述
对以下优化问题
取初始点,初始惩罚因子,缩小系数c=0.1,终止限。
(1)内点罚函数:
- 基本原理
(1)内点罚函数
图1 内点罚函数流程图
- 实验结果
(1)内点罚函数
迭代次数 |
1 |
2 |
当前迭代点 |
(3.00,4.00) |
(1.00,0.00) |
当前迭代点值 |
25.33 |
2.66 |
表1 内点罚函数迭代过程
图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()
上一篇: 夫妻笑料,他(她)们幽默极了
下一篇: 智能便签项目:连接现实与电脑世界