【Python】【matLab】模拟退火算法求二元高次函数最小值

一、目标函数

求二元高次函数的最小值。目标函数选择:
在这里插入图片描述
用于测试算法的简单的目标函数:

在这里插入图片描述

二、Python代码实现

import numpy as np


# 目标函数(2变量)
def objective_function(x):
    return x[0] ** 2 + 2 * x[0] - 15 + 4 * 4 * 2 * x[1] + 4 * x[1] ** 2
    # 测试:return x[0] ** 2 + x[1] ** 2


# 模拟退火
def simulated_annealing(objective_func,  # 目标函数
                        initial_solution=np.array([0, 0]),  # 初始解
                        temperature=100,  # 初始温度
                        min_temperature=0.1,  # 最小温度值
                        cooling_rate=0.90,  # 冷却率(乘法系数)
                        iter_max=100,  # 最大迭代次数
                        seed=0  # 随机种子
                        ):
    np.random.seed(seed)
    current_solution = initial_solution
    best_solution = current_solution

    while temperature > min_temperature:
        for j in range(iter_max):
            # 生成新的解
            new_solution = current_solution + np.random.uniform(-1, 1, len(current_solution))

            # 计算新解与当前解之间的目标函数值差异
            current_cost = objective_func(current_solution)
            new_cost = objective_func(new_solution)
            cost_diff = new_cost - current_cost

            # 判断是否接受新解
            if cost_diff < 0 or np.exp(-cost_diff / temperature) > np.random.random():
                current_solution = new_solution

            # 更新最优解
            if objective_func(current_solution) < objective_func(best_solution):
                best_solution = current_solution

        # 降低温度
        temperature *= cooling_rate

    return best_solution


# 调用退火算法求解最小值
best_solution = simulated_annealing(objective_function)

print(f"函数最小值: {objective_function(best_solution)} 自变量取值:{best_solution}")

代码参考博客:利用Python 实现 模拟退火算法

三、程序输出

测试函数输出:
在这里插入图片描述

目标函数输出:

在这里插入图片描述

四、MatLab验证程序

参考博客:MATLAB求解二元(多元)函数极值

先定义目标函数(位于fun2_3.m中):

function f = fun2_3(x)
f = x(1) ^ 2 + 2 * x(1) - 15 + 32 * x(2) + 4 * x(2) ^ 2;

模拟退火算法求极值:

clc, clear
[x, y]=meshgrid(-10:0.3:10,-10:0.3:10);
z = x.^2 + 2 * x -15 + 32 * y + 4 * y.^2;
figure(1)
surf(x,y,z)
xlabel('X');
ylabel('Y');
zlabel('Z');

figure(2)
contour(x,y,z)
xlabel('X');
ylabel('Y');
grid on;

x0=[-3,-3];
% [x,fmin]=fminsearch(@fun2_3,x0)
[x,fmin] = fminunc(@fun2_3,x0)

程序输出:
在这里插入图片描述

在这里插入图片描述在这里插入图片描述可见,两种方法的求解结果基本相同。

相关推荐

  1. 简单函数_简单算术表达式

    2024-06-10 04:14:01       19 阅读
  2. prim算法生成树

    2024-06-10 04:14:01       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-10 04:14:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-10 04:14:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-10 04:14:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-10 04:14:01       20 阅读

热门阅读

  1. Jira的原理及应用详解(六)

    2024-06-10 04:14:01       9 阅读
  2. python 获取网页链接图片

    2024-06-10 04:14:01       10 阅读
  3. 《一心体系至善算法》“人文+AI”成果

    2024-06-10 04:14:01       12 阅读
  4. 七天进阶elasticsearch[Three]

    2024-06-10 04:14:01       10 阅读
  5. 常见汇编指令

    2024-06-10 04:14:01       11 阅读
  6. 6.9总结

    6.9总结

    2024-06-10 04:14:01      10 阅读
  7. 【MySQL】窗口函数原理,与where、group by关系

    2024-06-10 04:14:01       13 阅读
  8. Redis 数据拷贝

    2024-06-10 04:14:01       13 阅读
  9. Web前端的规划:深度解构与未来展望

    2024-06-10 04:14:01       10 阅读
  10. 论文写作神器:15大参考文献来源网站推荐

    2024-06-10 04:14:01       8 阅读